-
Notifications
You must be signed in to change notification settings - Fork 7
/
clarity-ad-blocker.php
144 lines (128 loc) · 4.41 KB
/
clarity-ad-blocker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* Plugin Name: Clarity - Ad blocker for WordPress
* Plugin URI: https://github.com/khromov/wp-clarity
* GitHub Plugin URI: khromov/clarity
* Description: Remove nags and upsells from popular WordPress plugins.
* Author: khromov
* Version: 1.3.231102
* Requires at least: 5.0
* Tested up to: 6.3
* Requires PHP: 7.0
* Text Domain: clarity-ad-blocker
* Domain Path: /languages/
* License: GPL v2 or later
*/
define('WP_CLARITY_PATH', trailingslashit(plugin_dir_path(__FILE__)));
define('CLARITY_AD_BLOCKER_ENABLED', true);
/**
* Class WP_Hush
*/
class WP_Clarity {
function __construct() {
add_action('admin_head', [$this, 'admin_head']);
add_action('plugins_loaded', [$this, 'plugins_loaded']);
add_action('after_setup_theme', [$this, 'themes_loaded']);
add_action('cli_init', [$this, 'cli_init']);
add_filter('plugin_action_links_clarity-ad-blocker/clarity-ad-blocker.php', [$this, 'filter_plugin_action_links']);
}
/**
* Generate definitions from definitions.txt
*
* @return string
*/
function getDefinitions($loadFromSource = false) {
if (!$loadFromSource && file_exists(WP_CLARITY_PATH . 'definitions.php')) {
do_action('qm/info', 'Loading definitions from precompiled PHP');
return include(WP_CLARITY_PATH . 'definitions.php');
}
do_action('qm/info', 'Loading definitions from text file');
$filterEmptyLines = function ($item) {
return !!$item;
};
$filterComments = function ($item) {
return trim(preg_replace('/(--.*)/', '', $item));
};
$rulesFile = explode("\n", file_get_contents(WP_CLARITY_PATH . 'definitions.txt'));
return implode(', ', apply_filters('wp_clarity_rules', array_filter(array_filter($rulesFile, $filterComments), $filterEmptyLines)));
}
/**
* Hides stuff via CSS in the admin header
*
* @return void
*/
function admin_head() {
$selectors = $this->getDefinitions();
if (strlen($selectors) === 0) return;
?>
<!-- Clarity - Ad blocker for WordPress -->
<style type="text/css">
<?php echo $selectors; ?> {
display: none !important;
}
</style>
<?php
}
/**
* Special handling for plugins that can't rely on CSS rules
*
* @return void
*/
function plugins_loaded() {
/* Google XML Sitemaps */
add_filter('option_sm_options', function ($option) {
$option['sm_i_hide_survey'] = true;
return $option;
});
/* wp-smtp */
add_filter('pre_option_postman_release_version', function ($option) {
return true;
});
/* Members review notice */
if (!defined('MEMBERS_DISABLE_REVIEW_PROMPT')) {
define('MEMBERS_DISABLE_REVIEW_PROMPT', true);
}
/* MetaSlider */
if (!defined('METASLIDER_DISABLE_SEASONAL_NOTICES')) {
define('METASLIDER_DISABLE_SEASONAL_NOTICES', true);
}
}
/**
* Special handling for themes that can't rely on CSS rules
*
* @return void
*/
function themes_loaded() {
/* VisualBusiness */
remove_action('admin_notices', 'visualbusiness_notice');
}
/**
* Registers WP CLI commands to build precompiled version
* of filters
*
* @return void
*/
function cli_init() {
WP_CLI::add_command('clarity-build', [$this, 'cli_build']);
}
/**
* Build WP Clarity definition file for production.
*
* @param [type] $args
* @param [type] $assoc_args
* @return void
*/
function cli_build($args, $assoc_args) {
$definitions = var_export($this->getDefinitions(true), true);
file_put_contents(WP_CLARITY_PATH . 'definitions.php', "<?php\n/* This file is automatically generated, do not update manually! Use 'wp clarity-build' to generate. */ \nreturn {$definitions};");
WP_CLI::success("Built definitions.php");
}
public function filter_plugin_action_links(array $actions) {
return array_merge(array(
'website' => '<a href="https://wp-clarity.dev/" target="_blank">' . esc_html__('Website', 'clarity-ad-blocker') . '</a>',
'faq' => '<a href="https://wordpress.org/plugins/clarity-ad-blocker/#faq" target="_blank">' . esc_html__('FAQ', 'clarity-ad-blocker') . '</a>',
'report-unwanted-banner' => '<a href="https://github.com/khromov/clarity/issues/new?assignees=khromov&labels=filter-request&template=1-report-notification.md&title=Plugin%2FTheme+name%3A+" target="_blank">' . esc_html__('Report unwanted banner', 'clarity-ad-blocker') . '</a>',
), $actions);
}
}
$wp_clarity = new WP_Clarity();