Plugin Directory

Changeset 3291045


Ignore:
Timestamp:
05/10/2025 11:20:00 PM (7 months ago)
Author:
khromov
Message:

Update to version 1.4 from GitHub

Location:
clarity-ad-blocker
Files:
6 added
2 deleted
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • clarity-ad-blocker/tags/1.4/clarity-ad-blocker.php

    r2988116 r3291045  
    66 * Description: Remove nags and upsells from popular WordPress plugins.
    77 * Author:      khromov
    8  * Version:     1.3.231102
     8 * Version:     1.4
    99 * Requires at least: 5.0
    10  * Tested up to: 6.0
     10 * Tested up to: 6.7
    1111 * Requires PHP: 7.0
    1212 * Text Domain: clarity-ad-blocker
     
    1515 */
    1616
    17 define('WP_CLARITY_PATH', trailingslashit(plugin_dir_path(__FILE__)));
    18 define('CLARITY_AD_BLOCKER_ENABLED', true);
     17
     18// For debugging purposes only
     19// define('CLARITY_DEBUG', true);
     20
     21define( 'WP_CLARITY_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
     22define( 'CLARITY_AD_BLOCKER_ENABLED', true );
    1923
    2024/**
    21  * Class WP_Hush
     25 * Class WP_Clarity
    2226 */
    2327class WP_Clarity {
    24   function __construct() {
    25     add_action('admin_head', [$this, 'admin_head']);
    26     add_action('plugins_loaded', [$this, 'plugins_loaded']);
    27     add_action('after_setup_theme', [$this, 'themes_loaded']);
    28     add_action('cli_init', [$this, 'cli_init']);
    29     add_filter('plugin_action_links_clarity-ad-blocker/clarity-ad-blocker.php', [$this, 'filter_plugin_action_links']);
    30   }
    31 
    32   /**
    33    * Generate definitions from definitions.txt
    34    *
    35    * @return string
    36    */
    37   function getDefinitions($loadFromSource = false) {
    38     if (!$loadFromSource && file_exists(WP_CLARITY_PATH . 'definitions.php')) {
    39       do_action('qm/info', 'Loading definitions from precompiled PHP');
    40       return include(WP_CLARITY_PATH . 'definitions.php');
    41     }
    42 
    43     do_action('qm/info', 'Loading definitions from text file');
    44     $filterEmptyLines = function ($item) {
    45       return !!$item;
    46     };
    47     $filterComments = function ($item) {
    48       return trim(preg_replace('/(--.*)/', '', $item));
    49     };
    50 
    51     $rulesFile = explode("\n", file_get_contents(WP_CLARITY_PATH . 'definitions.txt'));
    52 
    53     return implode(', ', apply_filters('wp_clarity_rules', array_filter(array_filter($rulesFile, $filterComments), $filterEmptyLines)));
    54   }
    55 
    56   /**
    57    *  Hides stuff via CSS in the admin header
    58    *
    59    * @return void
    60    */
    61   function admin_head() {
    62     $selectors = $this->getDefinitions();
    63     if (strlen($selectors) === 0) return;
    64 ?>
    65     <!-- Clarity - Ad blocker for WordPress -->
    66     <style type="text/css">
    67       <?php echo $selectors; ?> {
    68         display: none !important;
    69       }
    70     </style>
    71 <?php
    72   }
    73 
    74   /**
    75    * Special handling for plugins that can't rely on CSS rules
    76    *
    77    * @return void
    78    */
    79   function plugins_loaded() {
    80     /* Google XML Sitemaps */
    81     add_filter('option_sm_options', function ($option) {
    82       $option['sm_i_hide_survey'] = true;
    83       return $option;
    84     });
    85 
    86     /* wp-smtp */
    87     add_filter('pre_option_postman_release_version', function ($option) {
    88       return true;
    89     });
    90 
    91     /* Members review notice */
    92     if (!defined('MEMBERS_DISABLE_REVIEW_PROMPT')) {
    93       define('MEMBERS_DISABLE_REVIEW_PROMPT', true);
    94     }
    95 
    96     /* MetaSlider */
    97     if (!defined('METASLIDER_DISABLE_SEASONAL_NOTICES')) {
    98       define('METASLIDER_DISABLE_SEASONAL_NOTICES', true);
    99     }
    100   }
    101 
    102   /**
    103    * Special handling for themes that can't rely on CSS rules
    104    *
    105    * @return void
    106    */
    107   function themes_loaded() {
    108     /* VisualBusiness */
    109     remove_action('admin_notices', 'visualbusiness_notice');
    110   }
    111 
    112   /**
    113    * Registers WP CLI commands to build precompiled version
    114    * of filters
    115    *
    116    * @return void
    117    */
    118   function cli_init() {
    119     WP_CLI::add_command('clarity-build', [$this, 'cli_build']);
    120   }
    121 
    122   /**
    123    * Build WP Clarity definition file for production.
    124    *
    125    * @param [type] $args
    126    * @param [type] $assoc_args
    127    * @return void
    128    */
    129   function cli_build($args, $assoc_args) {
    130     $definitions = var_export($this->getDefinitions(true), true);
    131     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};");
    132     WP_CLI::success("Built definitions.php");
    133   }
    134 
    135   public function filter_plugin_action_links(array $actions) {
    136     return array_merge(array(
    137       'website' => '<a href="https://wp-clarity.dev/" target="_blank">' . esc_html__('Website', 'clarity-ad-blocker') . '</a>',
    138       'faq' => '<a href="https://wordpress.org/plugins/clarity-ad-blocker/#faq" target="_blank">' . esc_html__('FAQ', 'clarity-ad-blocker') . '</a>',
    139       '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>',
    140     ), $actions);
    141   }
     28    /**
     29     * Option name for storing the definitions
     30     */
     31    private $option_name = 'wp_clarity_definitions';
     32
     33    /**
     34     * CRON hook name
     35     */
     36    private $cron_hook = 'wp_clarity_update_definitions';
     37
     38    /**
     39     * URL to the remote definitions file
     40     */
     41    private $definitions_url = 'https://khromov.github.io/clarity/definitions.txt';
     42
     43    function __construct() {
     44        register_activation_hook( __FILE__, [ $this, 'activate_plugin' ] );
     45        register_deactivation_hook( __FILE__, [ $this, 'deactivate_plugin' ] );
     46
     47        add_action( 'admin_head', [ $this, 'admin_head' ] );
     48        add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
     49        add_action( 'after_setup_theme', [ $this, 'themes_loaded' ] );
     50        add_action( $this->cron_hook, [ $this, 'update_definitions_from_remote' ] );
     51        add_action( 'upgrader_process_complete', [ $this, 'handle_plugin_update' ], 10, 2 );
     52        add_filter( 'plugin_action_links_clarity-ad-blocker/clarity-ad-blocker.php', [ $this, 'filter_plugin_action_links' ] );
     53        add_action( 'cli_init', [ $this, 'cli_init' ] );
     54    }
     55
     56    /**
     57     * Handle plugin update
     58     */
     59    function handle_plugin_update( $upgrader_object, $options ) {
     60        if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' ) {
     61            return;
     62        }
     63
     64        if ( ! isset( $options['plugins'] ) || ! in_array( plugin_basename( __FILE__ ), $options['plugins'] ) ) {
     65            return;
     66        }
     67
     68        do_action( 'qm/info', 'Clarity plugin update detected' );
     69
     70        if ( ! wp_next_scheduled( $this->cron_hook ) ) {
     71            wp_schedule_event( time(), 'daily', $this->cron_hook );
     72            do_action( 'qm/info', 'Scheduled definitions update CRON job after plugin update' );
     73        }
     74    }
     75
     76    /**
     77     * Plugin activation hook
     78     */
     79    function activate_plugin() {
     80        if ( ! wp_next_scheduled( $this->cron_hook ) ) {
     81            wp_schedule_event( time(), 'daily', $this->cron_hook );
     82        }
     83
     84        $this->update_definitions_from_remote();
     85    }
     86
     87    /**
     88     * Plugin deactivation hook
     89     */
     90    function deactivate_plugin() {
     91        wp_clear_scheduled_hook( $this->cron_hook );
     92        delete_option( $this->option_name );
     93    }
     94
     95    /**
     96     * Process definitions text into CSS selectors
     97     */
     98    function process_definitions_text( $content ) {
     99        $filter_empty_lines = function ( $item ) {
     100            return (bool) $item;
     101        };
     102
     103        $filter_comments = function ( $item ) {
     104            return trim( preg_replace( '/(--.*)/', '', $item ) );
     105        };
     106
     107        $rules_file = explode( "\n", $content );
     108
     109        return implode(
     110            ', ',
     111            apply_filters(
     112                'wp_clarity_rules',
     113                array_filter( array_filter( $rules_file, $filter_comments ), $filter_empty_lines )
     114            )
     115        );
     116    }
     117
     118    /**
     119     * Get definitions from cache or local file
     120     */
     121    function getDefinitions( $force_refresh = false ) {
     122        // If debug mode is enabled, always use local definitions
     123        if ( defined( 'CLARITY_DEBUG' ) && CLARITY_DEBUG ) {
     124            do_action( 'qm/info', 'Debug mode enabled, using local definitions' );
     125            return $this->getLocalDefinitions();
     126        }
     127
     128        $cached = get_option( $this->option_name );
     129
     130        if ( $force_refresh || $cached === false ) {
     131            do_action( 'qm/info', 'No cached definitions found or refresh forced' );
     132            do_action( 'qm/info', 'Using local definitions as fallback' );
     133            return $this->getLocalDefinitions();
     134        }
     135
     136        do_action( 'qm/info', 'Using cached definitions from database' );
     137        return $cached;
     138    }
     139
     140    /**
     141     * Get definitions from local file
     142     */
     143    function getLocalDefinitions() {
     144        do_action( 'qm/info', 'Loading definitions from local text file' );
     145
     146        $content = file_get_contents( WP_CLARITY_PATH . 'definitions.txt' );
     147        return $this->process_definitions_text( $content );
     148    }
     149
     150    /**
     151     * Update definitions from remote source
     152     */
     153    function update_definitions_from_remote() {
     154        // Don't update from remote in debug mode
     155        if ( defined( 'CLARITY_DEBUG' ) && CLARITY_DEBUG ) {
     156            do_action( 'qm/info', 'Debug mode enabled, skipping remote definitions update' );
     157            return false;
     158        }
     159
     160        do_action( 'qm/info', 'Attempting to fetch remote definitions' );
     161
     162        $response = wp_remote_get( $this->definitions_url );
     163
     164        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
     165            do_action( 'qm/info', 'Failed to fetch remote definitions' );
     166            return false;
     167        }
     168
     169        $content = wp_remote_retrieve_body( $response );
     170
     171        if ( empty( $content ) ) {
     172            do_action( 'qm/info', 'Remote definitions were empty' );
     173            return false;
     174        }
     175
     176        $processed_definitions = $this->process_definitions_text( $content );
     177        update_option( $this->option_name, $processed_definitions, false );
     178
     179        do_action( 'qm/info', 'Updated remote definitions successfully' );
     180
     181        return true;
     182    }
     183
     184    /**
     185     * Special handling for plugins that can't rely on CSS rules
     186     */
     187    function plugins_loaded() {
     188        /* Google XML Sitemaps */
     189        add_filter(
     190            'option_sm_options',
     191            function ( $option ) {
     192                $option['sm_i_hide_survey'] = true;
     193                return $option;
     194            }
     195        );
     196
     197        /* wp-smtp */
     198        add_filter(
     199            'pre_option_postman_release_version',
     200            function ( $option ) {
     201                return true;
     202            }
     203        );
     204
     205        /* Members review notice */
     206        if ( ! defined( 'MEMBERS_DISABLE_REVIEW_PROMPT' ) ) {
     207            define( 'MEMBERS_DISABLE_REVIEW_PROMPT', true );
     208        }
     209
     210        /* MetaSlider */
     211        if ( ! defined( 'METASLIDER_DISABLE_SEASONAL_NOTICES' ) ) {
     212            define( 'METASLIDER_DISABLE_SEASONAL_NOTICES', true );
     213        }
     214    }
     215
     216    /**
     217     * Hides stuff via CSS in the admin header
     218     */
     219    function admin_head() {
     220        $selectors = $this->getDefinitions();
     221        if ( strlen( $selectors ) === 0 ) { return;
     222        }
     223        ?>
     224    <!-- Clarity - Ad blocker for WordPress -->
     225    <style type="text/css">
     226        <?php echo $selectors; ?> {
     227        display: none !important;
     228        }
     229    </style>
     230        <?php
     231    }
     232
     233    /**
     234     * Special handling for themes that can't rely on CSS rules
     235     */
     236    function themes_loaded() {
     237        /* VisualBusiness */
     238        remove_action( 'admin_notices', 'visualbusiness_notice' );
     239    }
     240
     241    /**
     242     * Registers WP CLI commands
     243     */
     244    function cli_init() {
     245        if ( ! class_exists( 'WP_CLI' ) ) {
     246            return;
     247        }
     248
     249        WP_CLI::add_command( 'clarity-update', [ $this, 'cli_update' ] );
     250    }
     251
     252    /**
     253     * Update definitions from remote source via CLI
     254     */
     255    function cli_update( $args, $assoc_args ) {
     256        $result = $this->update_definitions_from_remote();
     257
     258        if ( $result ) {
     259            WP_CLI::success( 'Remote definitions updated successfully' );
     260        } else {
     261            WP_CLI::warning( 'Failed to update remote definitions, using local definitions' );
     262        }
     263    }
     264
     265    /**
     266     * Filter plugin action links
     267     */
     268    public function filter_plugin_action_links( array $actions ) {
     269        return array_merge(
     270            array(
     271                'website'                => '<a href="https://wp-clarity.dev/" target="_blank">' . esc_html__( 'Website', 'clarity-ad-blocker' ) . '</a>',
     272                'faq'                    => '<a href="https://wordpress.org/plugins/clarity-ad-blocker/#faq" target="_blank">' . esc_html__( 'FAQ', 'clarity-ad-blocker' ) . '</a>',
     273                '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>',
     274            ),
     275            $actions
     276        );
     277    }
    142278}
    143279
  • clarity-ad-blocker/tags/1.4/definitions.txt

    r2988116 r3291045  
    44#wpseo-local-seo-upsell
    55.yoast-settings-section-upsell
     6#webinar-promo-notification
    67
    78-- Rank Math SEO
     
    9495div.notice[data-notice="webp-converter-for-media"]
    9596.webpLoader__popup.webpPopup
     97.webpcPopup
    9698
    9799-- Generic "Plugin Usage Tracker" class
  • clarity-ad-blocker/trunk/clarity-ad-blocker.php

    r2988116 r3291045  
    66 * Description: Remove nags and upsells from popular WordPress plugins.
    77 * Author:      khromov
    8  * Version:     1.3.231102
     8 * Version:     1.4
    99 * Requires at least: 5.0
    10  * Tested up to: 6.0
     10 * Tested up to: 6.7
    1111 * Requires PHP: 7.0
    1212 * Text Domain: clarity-ad-blocker
     
    1515 */
    1616
    17 define('WP_CLARITY_PATH', trailingslashit(plugin_dir_path(__FILE__)));
    18 define('CLARITY_AD_BLOCKER_ENABLED', true);
     17
     18// For debugging purposes only
     19// define('CLARITY_DEBUG', true);
     20
     21define( 'WP_CLARITY_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
     22define( 'CLARITY_AD_BLOCKER_ENABLED', true );
    1923
    2024/**
    21  * Class WP_Hush
     25 * Class WP_Clarity
    2226 */
    2327class WP_Clarity {
    24   function __construct() {
    25     add_action('admin_head', [$this, 'admin_head']);
    26     add_action('plugins_loaded', [$this, 'plugins_loaded']);
    27     add_action('after_setup_theme', [$this, 'themes_loaded']);
    28     add_action('cli_init', [$this, 'cli_init']);
    29     add_filter('plugin_action_links_clarity-ad-blocker/clarity-ad-blocker.php', [$this, 'filter_plugin_action_links']);
    30   }
    31 
    32   /**
    33    * Generate definitions from definitions.txt
    34    *
    35    * @return string
    36    */
    37   function getDefinitions($loadFromSource = false) {
    38     if (!$loadFromSource && file_exists(WP_CLARITY_PATH . 'definitions.php')) {
    39       do_action('qm/info', 'Loading definitions from precompiled PHP');
    40       return include(WP_CLARITY_PATH . 'definitions.php');
    41     }
    42 
    43     do_action('qm/info', 'Loading definitions from text file');
    44     $filterEmptyLines = function ($item) {
    45       return !!$item;
    46     };
    47     $filterComments = function ($item) {
    48       return trim(preg_replace('/(--.*)/', '', $item));
    49     };
    50 
    51     $rulesFile = explode("\n", file_get_contents(WP_CLARITY_PATH . 'definitions.txt'));
    52 
    53     return implode(', ', apply_filters('wp_clarity_rules', array_filter(array_filter($rulesFile, $filterComments), $filterEmptyLines)));
    54   }
    55 
    56   /**
    57    *  Hides stuff via CSS in the admin header
    58    *
    59    * @return void
    60    */
    61   function admin_head() {
    62     $selectors = $this->getDefinitions();
    63     if (strlen($selectors) === 0) return;
    64 ?>
    65     <!-- Clarity - Ad blocker for WordPress -->
    66     <style type="text/css">
    67       <?php echo $selectors; ?> {
    68         display: none !important;
    69       }
    70     </style>
    71 <?php
    72   }
    73 
    74   /**
    75    * Special handling for plugins that can't rely on CSS rules
    76    *
    77    * @return void
    78    */
    79   function plugins_loaded() {
    80     /* Google XML Sitemaps */
    81     add_filter('option_sm_options', function ($option) {
    82       $option['sm_i_hide_survey'] = true;
    83       return $option;
    84     });
    85 
    86     /* wp-smtp */
    87     add_filter('pre_option_postman_release_version', function ($option) {
    88       return true;
    89     });
    90 
    91     /* Members review notice */
    92     if (!defined('MEMBERS_DISABLE_REVIEW_PROMPT')) {
    93       define('MEMBERS_DISABLE_REVIEW_PROMPT', true);
    94     }
    95 
    96     /* MetaSlider */
    97     if (!defined('METASLIDER_DISABLE_SEASONAL_NOTICES')) {
    98       define('METASLIDER_DISABLE_SEASONAL_NOTICES', true);
    99     }
    100   }
    101 
    102   /**
    103    * Special handling for themes that can't rely on CSS rules
    104    *
    105    * @return void
    106    */
    107   function themes_loaded() {
    108     /* VisualBusiness */
    109     remove_action('admin_notices', 'visualbusiness_notice');
    110   }
    111 
    112   /**
    113    * Registers WP CLI commands to build precompiled version
    114    * of filters
    115    *
    116    * @return void
    117    */
    118   function cli_init() {
    119     WP_CLI::add_command('clarity-build', [$this, 'cli_build']);
    120   }
    121 
    122   /**
    123    * Build WP Clarity definition file for production.
    124    *
    125    * @param [type] $args
    126    * @param [type] $assoc_args
    127    * @return void
    128    */
    129   function cli_build($args, $assoc_args) {
    130     $definitions = var_export($this->getDefinitions(true), true);
    131     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};");
    132     WP_CLI::success("Built definitions.php");
    133   }
    134 
    135   public function filter_plugin_action_links(array $actions) {
    136     return array_merge(array(
    137       'website' => '<a href="https://wp-clarity.dev/" target="_blank">' . esc_html__('Website', 'clarity-ad-blocker') . '</a>',
    138       'faq' => '<a href="https://wordpress.org/plugins/clarity-ad-blocker/#faq" target="_blank">' . esc_html__('FAQ', 'clarity-ad-blocker') . '</a>',
    139       '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>',
    140     ), $actions);
    141   }
     28    /**
     29     * Option name for storing the definitions
     30     */
     31    private $option_name = 'wp_clarity_definitions';
     32
     33    /**
     34     * CRON hook name
     35     */
     36    private $cron_hook = 'wp_clarity_update_definitions';
     37
     38    /**
     39     * URL to the remote definitions file
     40     */
     41    private $definitions_url = 'https://khromov.github.io/clarity/definitions.txt';
     42
     43    function __construct() {
     44        register_activation_hook( __FILE__, [ $this, 'activate_plugin' ] );
     45        register_deactivation_hook( __FILE__, [ $this, 'deactivate_plugin' ] );
     46
     47        add_action( 'admin_head', [ $this, 'admin_head' ] );
     48        add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
     49        add_action( 'after_setup_theme', [ $this, 'themes_loaded' ] );
     50        add_action( $this->cron_hook, [ $this, 'update_definitions_from_remote' ] );
     51        add_action( 'upgrader_process_complete', [ $this, 'handle_plugin_update' ], 10, 2 );
     52        add_filter( 'plugin_action_links_clarity-ad-blocker/clarity-ad-blocker.php', [ $this, 'filter_plugin_action_links' ] );
     53        add_action( 'cli_init', [ $this, 'cli_init' ] );
     54    }
     55
     56    /**
     57     * Handle plugin update
     58     */
     59    function handle_plugin_update( $upgrader_object, $options ) {
     60        if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' ) {
     61            return;
     62        }
     63
     64        if ( ! isset( $options['plugins'] ) || ! in_array( plugin_basename( __FILE__ ), $options['plugins'] ) ) {
     65            return;
     66        }
     67
     68        do_action( 'qm/info', 'Clarity plugin update detected' );
     69
     70        if ( ! wp_next_scheduled( $this->cron_hook ) ) {
     71            wp_schedule_event( time(), 'daily', $this->cron_hook );
     72            do_action( 'qm/info', 'Scheduled definitions update CRON job after plugin update' );
     73        }
     74    }
     75
     76    /**
     77     * Plugin activation hook
     78     */
     79    function activate_plugin() {
     80        if ( ! wp_next_scheduled( $this->cron_hook ) ) {
     81            wp_schedule_event( time(), 'daily', $this->cron_hook );
     82        }
     83
     84        $this->update_definitions_from_remote();
     85    }
     86
     87    /**
     88     * Plugin deactivation hook
     89     */
     90    function deactivate_plugin() {
     91        wp_clear_scheduled_hook( $this->cron_hook );
     92        delete_option( $this->option_name );
     93    }
     94
     95    /**
     96     * Process definitions text into CSS selectors
     97     */
     98    function process_definitions_text( $content ) {
     99        $filter_empty_lines = function ( $item ) {
     100            return (bool) $item;
     101        };
     102
     103        $filter_comments = function ( $item ) {
     104            return trim( preg_replace( '/(--.*)/', '', $item ) );
     105        };
     106
     107        $rules_file = explode( "\n", $content );
     108
     109        return implode(
     110            ', ',
     111            apply_filters(
     112                'wp_clarity_rules',
     113                array_filter( array_filter( $rules_file, $filter_comments ), $filter_empty_lines )
     114            )
     115        );
     116    }
     117
     118    /**
     119     * Get definitions from cache or local file
     120     */
     121    function getDefinitions( $force_refresh = false ) {
     122        // If debug mode is enabled, always use local definitions
     123        if ( defined( 'CLARITY_DEBUG' ) && CLARITY_DEBUG ) {
     124            do_action( 'qm/info', 'Debug mode enabled, using local definitions' );
     125            return $this->getLocalDefinitions();
     126        }
     127
     128        $cached = get_option( $this->option_name );
     129
     130        if ( $force_refresh || $cached === false ) {
     131            do_action( 'qm/info', 'No cached definitions found or refresh forced' );
     132            do_action( 'qm/info', 'Using local definitions as fallback' );
     133            return $this->getLocalDefinitions();
     134        }
     135
     136        do_action( 'qm/info', 'Using cached definitions from database' );
     137        return $cached;
     138    }
     139
     140    /**
     141     * Get definitions from local file
     142     */
     143    function getLocalDefinitions() {
     144        do_action( 'qm/info', 'Loading definitions from local text file' );
     145
     146        $content = file_get_contents( WP_CLARITY_PATH . 'definitions.txt' );
     147        return $this->process_definitions_text( $content );
     148    }
     149
     150    /**
     151     * Update definitions from remote source
     152     */
     153    function update_definitions_from_remote() {
     154        // Don't update from remote in debug mode
     155        if ( defined( 'CLARITY_DEBUG' ) && CLARITY_DEBUG ) {
     156            do_action( 'qm/info', 'Debug mode enabled, skipping remote definitions update' );
     157            return false;
     158        }
     159
     160        do_action( 'qm/info', 'Attempting to fetch remote definitions' );
     161
     162        $response = wp_remote_get( $this->definitions_url );
     163
     164        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
     165            do_action( 'qm/info', 'Failed to fetch remote definitions' );
     166            return false;
     167        }
     168
     169        $content = wp_remote_retrieve_body( $response );
     170
     171        if ( empty( $content ) ) {
     172            do_action( 'qm/info', 'Remote definitions were empty' );
     173            return false;
     174        }
     175
     176        $processed_definitions = $this->process_definitions_text( $content );
     177        update_option( $this->option_name, $processed_definitions, false );
     178
     179        do_action( 'qm/info', 'Updated remote definitions successfully' );
     180
     181        return true;
     182    }
     183
     184    /**
     185     * Special handling for plugins that can't rely on CSS rules
     186     */
     187    function plugins_loaded() {
     188        /* Google XML Sitemaps */
     189        add_filter(
     190            'option_sm_options',
     191            function ( $option ) {
     192                $option['sm_i_hide_survey'] = true;
     193                return $option;
     194            }
     195        );
     196
     197        /* wp-smtp */
     198        add_filter(
     199            'pre_option_postman_release_version',
     200            function ( $option ) {
     201                return true;
     202            }
     203        );
     204
     205        /* Members review notice */
     206        if ( ! defined( 'MEMBERS_DISABLE_REVIEW_PROMPT' ) ) {
     207            define( 'MEMBERS_DISABLE_REVIEW_PROMPT', true );
     208        }
     209
     210        /* MetaSlider */
     211        if ( ! defined( 'METASLIDER_DISABLE_SEASONAL_NOTICES' ) ) {
     212            define( 'METASLIDER_DISABLE_SEASONAL_NOTICES', true );
     213        }
     214    }
     215
     216    /**
     217     * Hides stuff via CSS in the admin header
     218     */
     219    function admin_head() {
     220        $selectors = $this->getDefinitions();
     221        if ( strlen( $selectors ) === 0 ) { return;
     222        }
     223        ?>
     224    <!-- Clarity - Ad blocker for WordPress -->
     225    <style type="text/css">
     226        <?php echo $selectors; ?> {
     227        display: none !important;
     228        }
     229    </style>
     230        <?php
     231    }
     232
     233    /**
     234     * Special handling for themes that can't rely on CSS rules
     235     */
     236    function themes_loaded() {
     237        /* VisualBusiness */
     238        remove_action( 'admin_notices', 'visualbusiness_notice' );
     239    }
     240
     241    /**
     242     * Registers WP CLI commands
     243     */
     244    function cli_init() {
     245        if ( ! class_exists( 'WP_CLI' ) ) {
     246            return;
     247        }
     248
     249        WP_CLI::add_command( 'clarity-update', [ $this, 'cli_update' ] );
     250    }
     251
     252    /**
     253     * Update definitions from remote source via CLI
     254     */
     255    function cli_update( $args, $assoc_args ) {
     256        $result = $this->update_definitions_from_remote();
     257
     258        if ( $result ) {
     259            WP_CLI::success( 'Remote definitions updated successfully' );
     260        } else {
     261            WP_CLI::warning( 'Failed to update remote definitions, using local definitions' );
     262        }
     263    }
     264
     265    /**
     266     * Filter plugin action links
     267     */
     268    public function filter_plugin_action_links( array $actions ) {
     269        return array_merge(
     270            array(
     271                'website'                => '<a href="https://wp-clarity.dev/" target="_blank">' . esc_html__( 'Website', 'clarity-ad-blocker' ) . '</a>',
     272                'faq'                    => '<a href="https://wordpress.org/plugins/clarity-ad-blocker/#faq" target="_blank">' . esc_html__( 'FAQ', 'clarity-ad-blocker' ) . '</a>',
     273                '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>',
     274            ),
     275            $actions
     276        );
     277    }
    142278}
    143279
  • clarity-ad-blocker/trunk/definitions.txt

    r2988116 r3291045  
    44#wpseo-local-seo-upsell
    55.yoast-settings-section-upsell
     6#webinar-promo-notification
    67
    78-- Rank Math SEO
     
    9495div.notice[data-notice="webp-converter-for-media"]
    9596.webpLoader__popup.webpPopup
     97.webpcPopup
    9698
    9799-- Generic "Plugin Usage Tracker" class
Note: See TracChangeset for help on using the changeset viewer.