Plugin Directory


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • unusedcss/trunk/includes/modules/unused-css/UnusedCSS_Admin.php

    r2847136 r2877726  
    11<?php
    22
    33defined( 'ABSPATH' ) or die();
    44
    55/**
    66 * Class UnusedCSS
    77 */
    88abstract class UnusedCSS_Admin {
    99
    1010    use RapidLoad_Utils;
    1111
    1212    /**
    1313     * @var UnusedCSS_Autoptimize
    1414     */
    1515    public $uucss;
    1616
    1717    /**
    1818     * @var bool
    1919     */
    2020    public static $enabled = true;
    2121
    2222    /**
    2323     * Page related meta options
    2424     * @var array
    2525     */
    2626    public static $page_options = [
    2727        'safelist',
    2828        'exclude',
    2929        'blocklist'
    3030    ];
    3131
    3232    /**
    3333     * UnusedCSS constructor.
    3434     * @param UnusedCSS $uucss
    3535     */
    3636    public function __construct($uucss)
    3737    {
    3838
    3939        $this->uucss = $uucss;
    4040
    4141        if(is_admin()){
    4242
    4343            add_action( 'admin_menu', array( $this, 'add_uucss_option_page' ) );
    4444
    4545        }
    4646
    4747
    4848        if (!self::$enabled) {
    4949            return;
    5050        }
    5151
    5252        add_action( 'current_screen', function () {
    5353
    5454            if ( get_current_screen() && get_current_screen()->base == 'settings_page_uucss' ) {
    5555                add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] );
    5656            }
    5757        } );
    5858
    5959        $this->cache_trigger_hooks();
    6060
    6161        add_action( 'add_meta_boxes', [$this, 'add_meta_boxes'] );
    6262        add_action( 'save_post', [$this, 'save_meta_box_options'] , 10, 2);
    6363        add_action( "uucss_run_gpsi_test_for_all", [ $this, 'run_gpsi_test_for_all' ]);
    6464
    6565        add_filter( 'plugin_action_links_' . plugin_basename( UUCSS_PLUGIN_FILE ), [
    6666            $this,
    6767            'add_plugin_action_link'
    6868        ] );
    6969
    7070        if(is_admin()){
    7171
    7272            $this->deactivate();
    7373
    7474            add_action('current_screen', [$this, 'validate_domain']);
    7575            add_action('wp_ajax_clear_page_cache', [$this, 'clear_page_cache']);
    7676            add_action('wp_ajax_mark_faqs_read', [$this, 'mark_faqs_read']);
    7777            add_action('wp_ajax_mark_notice_read', [$this, 'mark_notice_read']);
    7878            add_action('wp_ajax_frontend_logs', [$this, 'frontend_logs']);
    7979            add_action('wp_ajax_uucss_logs', [$this, 'uucss_logs']);
    8080            add_action('wp_ajax_clear_uucss_logs', [$this, 'clear_uucss_logs']);
    8181            add_action( "wp_ajax_uucss_test_url", [ $this, 'uucss_test_url' ] );
    8282            add_action( "wp_ajax_uucss_run_gpsi_status_check_for_all", [ $this, 'run_gpsi_status_check_for_all' ] );
    8383            add_action( "wp_ajax_uucss_data", [ $this, 'uucss_data' ] );
    8484            add_action( "wp_ajax_uucss_license", [ $this, 'uucss_license' ] );
    8585            add_action( "wp_ajax_uucss_status", [ $this, 'uucss_status' ] );
    8686            add_action( "wp_ajax_uucss_rule_stats", [ $this, 'uucss_rule_stats' ] );
    8787            add_action( "wp_ajax_suggest_whitelist_packs", [ $this, 'suggest_whitelist_packs' ] );
    8888            add_action( "wp_ajax_verify_api_key", [ $this, 'verify_api_key' ] );
    8989            add_action( "wp_ajax_uucss_deactivate", [ $this, 'ajax_deactivate' ] );
    9090            add_action( "wp_ajax_uucss_connect", [ $this, 'uucss_connect' ] );
    9191            add_action( "wp_ajax_attach_rule", [ $this, 'attach_rule' ] );
    9292            add_action( "wp_ajax_uucss_update_rule", [ $this, 'uucss_update_rule' ] );
    9393            add_action( 'wp_ajax_uucss_queue', [$this, 'queue_posts']);
    9494            add_action( 'wp_ajax_rapidload_notifications', [$this, 'rapidload_notifications']);
    9595            add_action( 'admin_notices', [ $this, 'first_uucss_job' ] );
    9696            add_action( 'updated_option', [ $this, 'clear_cache_on_option_update' ], 10, 3 );
    9797        }
    9898
    9999        add_action( 'uucss_sitemap_queue', [$this, 'queue_sitemap'], 10, 1);
    100100
    101101    }
    102102
    103103    function rapidload_notifications(){
    104104
     105        self::verify_nonce();
     106
    105107        wp_send_json_success([
    106108            'faqs' => $this->get_faqs(),
    107109            'notifications' => $this->get_public_notices()
    108110        ]);
    109111
    110112    }
    111113
    112114    function queue_posts(){
     115
     116        self::verify_nonce();
    113117
    114118        if(!isset($_REQUEST['post_type'])) {
    115119            wp_send_json_error('post type not found');
    116120        }
    117121
    118122        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path';
    119123        $rule = isset($_REQUEST['rule']) ? $_REQUEST['rule'] : false;
    120124        $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : false;
    121125
    122126        $post_type = sanitize_text_field($_REQUEST['post_type']);
    123127
    124128        $list = isset($_POST['url_list']) ? $_POST['url_list'] : null;
    125129
    126130        $posts = null;
    127131
    128132        global $uucss;
    129133
    130134        if(isset($list) && is_array($list) && !empty($list)){
    131135
    132136            if($type == 'path'){
    133137                UnusedCSS_DB::requeue_urls($list);
    134138            }else{
    135139                UnusedCSS_DB::requeue_rules($list);
    136140            }
    137141
    138142            $this->uucss->cleanCacheFiles();
    139143
    140144            wp_send_json_success('successfully links added to the queue');
    141145        }else if($post_type == 'current'){
    142146
    143147            if($type == 'path'){
    144148                RapidLoad_Settings::clear_links(true);
    145149            }else{
    146150                UnusedCSS_DB::clear_rules(true);
    147151            }
    148152
    149153            $this->uucss->cleanCacheFiles();
    150154
    151155            wp_send_json_success('successfully links added to the queue');
    152156
    153157        }else if($post_type == 'processing'){
    154158
    155159            if($type == 'path'){
    156160                UnusedCSS_DB::requeue_jobs('processing');
    157161                UnusedCSS_DB::requeue_jobs('waiting');
    158162            }else{
    159163                UnusedCSS_DB::requeue_rule_jobs('processing');
    160164                UnusedCSS_DB::requeue_rule_jobs('waiting');
    161165            }
    162166
    163167            $this->uucss->cleanCacheFiles();
    164168
    165169            wp_send_json_success('successfully links added to the queue');
    166170
    167171        }else if($post_type == 'warnings'){
    168172
    169173            if($type == 'path'){
    170174                UnusedCSS_DB::requeue_jobs('warnings');
    171175            }else{
    172176                UnusedCSS_DB::requeue_rule_jobs('warnings');
    173177            }
    174178
    175179            $this->uucss->cleanCacheFiles();
    176180
    177181            wp_send_json_success('successfully links added to the queue');
    178182
    179183        }else if($post_type == 'failed'){
    180184
    181185            if($type == 'path'){
    182186                UnusedCSS_DB::requeue_jobs();
    183187            }else{
    184188                UnusedCSS_DB::requeue_rule_jobs();
    185189            }
    186190
    187191            $this->uucss->cleanCacheFiles();
    188192
    189193            wp_send_json_success('successfully links added to the queue');
    190194
    191195        }else if($post_type == 'url'){
    192196
    193197            $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
    194198
    195199            if($url && !$this->is_url_allowed($url)){
    196200                wp_send_json_error('url is excluded');
    197201            }
    198202
    199203            $url_object = false;
    200204
    201205            if($type == 'path'){
    202206
    203207                $url_object = new UnusedCSS_Path([
    204208                    'url' => $url
    205209                ]);
    206210
    207211            }else{
    208212
    209213                $url_object = new UnusedCSS_Rule([
    210214                    'rule' => $rule,
    211215                    'regex' => $regex
    212216                ]);
    213217
    214218            }
    215219
    216220            if(!$url_object){
    217221
    218222                wp_send_json_error('Invalid URL');
    219223
    220224            }
    221225
    222226            $url_object->requeue();
    223227            $url_object->save();
    224228
    225229            wp_send_json_success('successfully link added to the queue');
    226230
    227231        }else if($post_type == 'site_map'){
    228232
    229233            $sitemap = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
    230234
    231235            if(!$sitemap){
    232236
    233237                wp_send_json_error('site map url required');
    234238            }
    235239
    236240            $spawned = $this->schedule_cron('uucss_sitemap_queue',[
    237241                'url' => $sitemap
    238242            ]);
    239243
    240244            wp_send_json_success('Sitemap links scheduled to be added to the queue.');
    241245
    242246        }else{
    243247
    244248            $posts = new WP_Query(array(
    245249                'post_type'=> $post_type,
    246250                'posts_per_page' => -1
    247251            ));
    248252
    249253        }
    250254
    251255        if($posts && $posts->have_posts()){
    252256            while ($posts->have_posts()){
    253257                $posts->the_post();
    254258
    255259                $url = $this->transform_url(get_the_permalink(get_the_ID()));
    256260
    257261                if($this->is_url_allowed($url)){
    258262                    new UnusedCSS_Path([
    259263                        'url' => $url
    260264                    ]);
    261265                }
    262266
    263267            }
    264268        }
    265269
    266270        wp_reset_query();
    267271
    268272        wp_send_json_success('successfully links added to the queue');
    269273
    270274    }
    271275
    272276    function queue_sitemap($url = false){
    273277
    274278        if(!$url){
    275279
    276280            $url = apply_filters('uucss/sitemap/default', stripslashes(get_site_url(get_current_blog_id())) . '/sitemap_index.xml');
    277281        }
    278282
    279283        $site_map = new RapidLoad_Sitemap();
    280284        $urls = $site_map->process_site_map($url);
    281285
    282286        global $uucss;
    283287
    284288        if(isset($urls) && !empty($urls)){
    285289
    286290            foreach ($urls as $url){
    287291
    288292                if($this->is_url_allowed($this->transform_url($url))){
    289293
    290294                    new UnusedCSS_Path([
    291295                        'url' => $url
    292296                    ]);
    293297                }
    294298
    295299            }
    296300        }
    297301    }
    298302
    299303    public function add_uucss_option_page() {
    300304
    301305        add_submenu_page( 'options-general.php', 'RapidLoad', 'RapidLoad', 'manage_options', 'uucss', function () {
    302306            wp_enqueue_script( 'post' );
    303307
    304308            ?>
    305309            <div class="wrap">
    306310                <h1><?php _e( 'RapidLoad Settings', 'autoptimize' ); ?></h1>
    307311                <?php
    308312                    do_action('uucss/options/before_render_form');
    309313                ?>
    310314                <div>
    311315                    <?php $this->render_form() ?>
    312316                </div>
    313317            </div>
    314318
    315319            <?php
    316320        });
    317321
    318322        register_setting('autoptimize_uucss_settings', 'autoptimize_uucss_settings');
    319323
    320324    }
    321325
    322326    public function render_form() {
    323327        $options = RapidLoad_Base::fetch_options();
    324328
    325329        include('parts/options-page.html.php');
    326330    }
    327331
    328332    public function uucss_rule_stats(){
    329333
     334        self::verify_nonce();
     335
    330336        wp_send_json_success([
    331337            'duplicateFiles' => UnusedCSS_DB::get_duplicate_files()
    332338        ]);
    333339
    334340    }
    335341
    336342    public function uucss_status(){
     343
     344        self::verify_nonce();
    337345
    338346        $job_counts = UnusedCSS_DB::get_job_counts();
    339347
    340348        wp_send_json_success([
    341349            'cssStyleSheetsCount' => $this->uucss->cache_file_count(),
    342350            'cssStyleSheetsSize' => $this->uucss->size(),
    343351            'hits' => $job_counts->hits,
    344352            'success' => $job_counts->success,
    345353            'ruleBased' => $job_counts->rule_based,
    346354            'queued' => $job_counts->queued,
    347355            'waiting' => $job_counts->waiting,
    348356            'processing' => $job_counts->processing,
    349357            'warnings' => $job_counts->warnings,
    350358            'failed' => $job_counts->failed,
    351359            'total' => $job_counts->total,
    352360        ]);
    353361    }
    354362
    355363    public function uucss_update_rule(){
    356364
     365        self::verify_nonce();
     366
    357367        if( !isset($_REQUEST['rule']) || empty($_REQUEST['rule']) ||
    358368            !isset($_REQUEST['url']) || empty($_REQUEST['url'])
    359369        ){
    360370            wp_send_json_error('Required fields missing');
    361371        }
    362372
    363373        $rule = $_REQUEST['rule'];
    364374        $url = $_REQUEST['url'];
    365375        $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : '/';
    366376
    367377        $url = $this->transform_url($url);
    368378
    369379        global $uucss;
    370380
    371381        if(!$this->is_url_allowed($url)){
    372382            wp_send_json_error('URL not allowed');
    373383        }
    374384
    375385        if(!self::is_url_glob_matched($url, $regex)){
    376386            wp_send_json_error('Invalid regex for the url');
    377387        }
    378388
    379389        $ruleObject = false;
    380390        $update_mode = 'create';
    381391
    382392        if(isset($_REQUEST['old_rule']) && isset($_REQUEST['old_regex'])){
    383393
    384394            $old_rule = $_REQUEST['old_rule'];
    385395            $old_regex = $_REQUEST['old_regex'];
    386396            $old_url = $_REQUEST['old_url'];
    387397
    388398            if(UnusedCSS_DB::rule_exists_with_error( $old_rule, $old_regex)){
    389399
    390400                $ruleObject = new UnusedCSS_Rule([
    391401                   'rule' => $old_rule,
    392402                   'regex' => $old_regex
    393403                ]);
    394404
    395405                if(isset($_REQUEST['old_url']) && $_REQUEST['old_url'] != $url ||
    396406                    $_REQUEST['old_rule'] != $rule || $_REQUEST['old_regex'] != $regex){
    397407                    if(isset($_REQUEST['requeue']) && $_REQUEST['requeue'] == "1"){
    398408                        error_log($_REQUEST['requeue']);
    399409                        $ruleObject->requeue();
    400410                        $ruleObject->releaseRule();
    401411                    }
    402412                }
    403413
    404414                $ruleObject->url = $url;
    405415                $ruleObject->rule = $rule;
    406416                $ruleObject->regex = $regex;
    407417                $ruleObject->save();
    408418                $update_mode = 'update';
    409419
    410420
    411421
    412422                do_action('uucss/rule/saved', $ruleObject, [
    413423                    'rule' => $old_rule,
    414424                    'regex' => $old_regex,
    415425                    'url' => $old_url
    416426                ]);
    417427
    418428                wp_send_json_success('Rule updated successfully');
    419429            }
    420430
    421431        }
    422432
    423433        if(UnusedCSS_DB::rule_exists_with_error($_REQUEST['rule'], $_REQUEST['regex'])){
    424434            wp_send_json_error('Rule already exist');
    425435        }
    426436
    427437        $ruleObject = new UnusedCSS_Rule([
    428438            'rule' => $rule,
    429439            'url' => $url,
    430440            'regex' => $regex,
    431441        ]);
    432442
    433443        do_action('uucss/rule/saved', $ruleObject, false);
    434444
    435445        wp_send_json_success('Rule updated successfully');
    436446    }
    437447
    438448    public function attach_rule(){
     449
     450        self::verify_nonce();
    439451
    440452        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false;
    441453        $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
    442454        $rule_id = isset($_REQUEST['rule_id']) ? $_REQUEST['rule_id'] : false;
    443455
    444456        if(!$type || !$url){
    445457            wp_send_json_error('Required field missing');
    446458        }
    447459
    448460        if($type == 'detach' && UnusedCSS_DB::rule_exist_by_url($url)){
    449461            wp_send_json_error('Rule exist with same url');
    450462        }
    451463
    452464        if($type == 'detach' && UnusedCSS_DB::link_exists_with_error($url)){
    453465
    454466            $path = new UnusedCSS_Path([
    455467               'url' => $url
    456468            ]);
    457469            $path->attach_rule();
    458470            $path->save();
    459471            wp_send_json_success('Successfully detached from rule');
    460472        }
    461473
    462474        if(!is_numeric($rule_id) || !$type || $type == 'attach' && !$rule_id){
    463475            wp_send_json_error('Required field missing');
    464476        }
    465477
    466478        if($type == 'attach'){
    467479
    468480            $rule = UnusedCSS_Rule::get_rule_from_id($rule_id);
    469481
    470482            if(!$rule){
    471483                wp_send_json_error('Rule not found');
    472484            }
    473485
    474486            $path = new UnusedCSS_Path([
    475487                'url' => $url
    476488            ]);
    477489
    478490            if(!self::is_url_glob_matched($url, $rule->regex)){
    479491                wp_send_json_success('Pattern not matched');
    480492            }
    481493
    482494            $path->attach_rule($rule->id, $rule->rule);
    483495            $path->save();
    484496            wp_send_json_success('Successfully attached to rule');
    485497        }
    486498
    487499    }
    488500
    489501    public static function is_domain_verified(){
    490502        $options = self::get_site_option( 'autoptimize_uucss_settings' );
    491503        return  $options['valid_domain'];
    492504    }
    493505
    494506    public function clear_cache_on_option_update( $option, $old_value, $value ) {
    495507
    496508        if ( $option == 'autoptimize_uucss_settings' && $this->uucss ) {
    497509
    498510            $needs_to_cleared = false;
    499511
    500512            $diffs = [];
    501513            $diffs_invert = [];
    502514
    503515            if ( $old_value && $value ) {
    504516                $diffs        = array_diff_key( $old_value, $value );
    505517                $diffs_invert = array_diff_key( $value, $old_value );
    506518            }
    507519
    508520            if ( isset( $diffs_invert['valid_domain'] ) ) {
    509521                unset( $diffs_invert['valid_domain'] );
    510522            }
    511523            if ( isset( $diffs['valid_domain'] ) ) {
    512524                unset( $diffs['valid_domain'] );
    513525            }
    514526
    515527            $diffs = array_merge( $diffs, $diffs_invert );
    516528
    517529            // if these settings are changed cache will be cleared
    518530            if ( isset( $diffs['uucss_minify'] ) ||
    519531                isset( $diffs['uucss_keyframes'] ) ||
    520532                isset( $diffs['uucss_fontface'] ) ||
    521533                isset( $diffs['uucss_analyze_javascript'] ) ||
    522534                isset( $diffs['uucss_safelist'] ) ||
    523535                isset( $diffs['whitelist_packs'] ) ||
    524536                isset( $diffs['uucss_blocklist'] ) ||
    525537                isset( $diffs['uucss_variables'] ) ) {
    526538                $needs_to_cleared = true;
    527539            }
    528540
    529541            foreach ( [ 'whitelist_packs', 'uucss_safelist', 'uucss_blocklist' ] as $compare_value ) {
    530542                if ( isset( $value[ $compare_value ] ) && isset( $old_value[ $compare_value ] ) && $old_value[ $compare_value ] !== $value[ $compare_value ] ) {
    531543                    $needs_to_cleared = true;
    532544                    break;
    533545                }
    534546            }
    535547
    536548            if(isset( $diffs['uucss_enable_rules'] )){
    537549                UnusedCSS_DB::detach_all_rules();
    538550            }
    539551
    540552            if ( $needs_to_cleared ) {
    541553
    542554                $this->uucss->clear_cache( null, [
    543555                    'soft' => true
    544556                ] );
    545557            }
    546558
    547559            RapidLoad_Base::fetch_options(false);
    548560        }
    549561
    550562    }
    551563
    552564    public function deactivate() {
    553565
    554566        if ( ! isset( $_REQUEST['deactivated'] ) || empty( $_REQUEST['deactivated'] ) ) {
    555567            return;
    556568        }
    557569
    558570        if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'uucss_activation' ) ) {
    559571            self::add_admin_notice( 'RapidLoad : Request verification failed for Activation. Contact support if the problem persists.', 'error' );
    560572
    561573            return;
    562574        }
    563575
    564576        $options = self::get_site_option( 'autoptimize_uucss_settings' );
    565577
    566578        unset( $options['uucss_api_key_verified'] );
    567579        unset( $options['uucss_api_key'] );
    568580        unset( $options['whitelist_packs'] );
    569581
    570582        self::update_site_option( 'autoptimize_uucss_settings', $options );
    571583
    572584        $cache_key = 'pand-' . md5( 'first-uucss-job' );
    573585        self::delete_site_option( $cache_key );
    574586
    575587        $this->uucss->vanish();
    576588
    577589        self::$deactivating = true;
    578590
    579591        $notice = [
    580592            'action'      => 'activate',
    581593            'message'     => 'RapidLoad : Deactivated your license for this site.',
    582594            'main_action' => [
    583595                'key'   => 'Reactivate',
    584596                'value' => self::activation_url( 'authorize' )
    585597            ],
    586598            'type'        => 'success'
    587599        ];
    588600        self::add_advanced_admin_notice( $notice );
    589601
    590602        return;
    591603    }
    592604
    593605    public function first_uucss_job() {
    594606
    595607        if ( class_exists('PAnD') && ! PAnD::is_admin_notice_active( 'first-uucss-job-forever' ) ) {
    596608            return;
    597609        }
    598610
    599611        if(get_current_screen() && get_current_screen()->base == 'settings_page_uucss'){
    600612            return;
    601613        }
    602614
    603615        $job = RapidLoad_Settings::get_first_link();
    604616
    605617        if ( $job && $job['status'] == 'success' ) : ?>
    606618            <div data-dismissible="first-uucss-job-forever"
    607619                 class="updated notice uucss-notice notice-success is-dismissible">
    608620                <h4><span class="dashicons dashicons-yes-alt"></span> RapidLoad successfully ran your first job!</h4>
    609621                <p><?php _e( 'You slashed <strong>' . $job['meta']['stats']->reductionSize . ' </strong> of unused CSS - that\'s <strong>' . $job['meta']['stats']->reduction . '% </strong> of your total CSS file size. Way to go 👏', 'sample-text-domain' ); ?></p>
    610622            </div>
    611623        <?php endif;
    612624
    613625        if ( $job && $job['status'] == 'failed' ) : ?>
    614626            <div data-dismissible="first-uucss-job-forever"
    615627                 class="error notice uucss-notice notice-error is-dismissible">
    616628                <h4><span class="dashicons dashicons-no-alt"></span> RapidLoad : We were unable to remove unused css
    617629                    from
    618630                    your site 🤕</h4>
    619631
    620632                <div>
    621633                    <p> Our team can help. Get in touch with support <a target="_blank"
    622634                                                                        href="https://rapidload.zendesk.com/hc/en-us/requests/new">here</a>
    623635                    </p>
    624636                    <blockquote class="error notice">
    625637                        <strong>Link :</strong> <?php echo $job['url'] ?> <br>
    626638                        <strong>Error :</strong> <?php echo $job['meta']['error']['code'] ?> <br>
    627639                        <strong>Message :</strong> <?php echo $job['meta']['error']['message'] ?>
    628640                    </blockquote>
    629641                </div>
    630642
    631643            </div>
    632644        <?php endif;
    633645    }
    634646
    635647    public function uucss_connect(){
    636648
     649        self::verify_nonce();
     650
    637651        if ( ! isset( $_REQUEST['license_key'] ) || empty( $_REQUEST['license_key'] ) ) {
    638652            wp_send_json_error( 'License Key required' );
    639653        }
    640654
    641655        $license_key = $_REQUEST['license_key'];
    642656
    643657        $uucss_api         = new RapidLoad_Api();
    644658        $uucss_api->apiKey = $license_key;
    645659        $results           = $uucss_api->post( 'connect', [ 'url' => $this->transform_url(get_site_url()), 'type' => 'wordpress' ] );
    646660
    647661        if ( $uucss_api->is_error( $results ) ) {
    648662            if(isset($results->errors) && isset($results->errors[0])){
    649663                wp_send_json_error($results->errors[0]->detail);
    650664            }else{
    651665                wp_send_json_error('License Key verification fail');
    652666            }
    653667        }
    654668
    655669        wp_send_json_success([
    656670            'success' => true,
    657671            'message' => 'License Key verification success',
    658672            'activation_nonce' => wp_create_nonce( 'uucss_activation' ),
    659673        ]);
    660674    }
    661675
    662676    public function ajax_deactivate() {
     677
     678        self::verify_nonce();
    663679
    664680        $options = self::get_site_option( 'autoptimize_uucss_settings' );
    665681
    666682        $cache_key = 'pand-' . md5( 'first-uucss-job' );
    667683        self::delete_site_option( $cache_key );
    668684
    669685        $this->uucss->vanish();
    670686
    671687        $api = new RapidLoad_Api();
    672688
    673689        // remove domain from authorized list
    674690        $api->post( 'deactivate', [
    675691            'url' => site_url()
    676692        ] );
    677693
    678694        unset( $options['uucss_api_key_verified'] );
    679695        unset( $options['uucss_api_key'] );
    680696        unset( $options['whitelist_packs'] );
    681697
    682698        self::update_site_option( 'autoptimize_uucss_settings', $options );
    683699
    684700        wp_send_json_success( true );
    685701    }
    686702
    687703    public function validate_domain() {
    688704
    689705        if ( get_current_screen() && get_current_screen()->base != 'settings_page_uucss' ) {
    690706            return;
    691707        }
    692708
    693709        $options   = self::get_site_option( 'autoptimize_uucss_settings' );
    694710
    695711        if(!isset( $options['uucss_api_key_verified'] ) || $options['uucss_api_key_verified'] != '1'){
    696712            return;
    697713        }
    698714
    699715        $uucss_api = new RapidLoad_Api();
    700716
    701717        if ( ! isset( $options['uucss_api_key'] ) ) {
    702718            return;
    703719        }
    704720
    705721        $results = $uucss_api->get( 'verify', [ 'url' => site_url(), 'token' => $options['uucss_api_key'] ] );
    706722
    707723        if($uucss_api->is_error($results)){
    708724            $options['valid_domain'] = false;
    709725            self::update_site_option('autoptimize_uucss_settings', $options);
    710726            return;
    711727        }
    712728
    713729        if(!isset($options['valid_domain']) || !$options['valid_domain']){
    714730            $options['valid_domain'] = true;
    715731            self::update_site_option('autoptimize_uucss_settings', $options);
    716732        }
    717733    }
    718734
    719735    public function uucss_data() {
    720736
    721737        if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'uucss_nonce' ) ) {
    722738            wp_send_json_error( 'UnusedCSS - Malformed Request Detected, Contact Support.' );
    723739        }
    724740
    725741        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path';
    726742
    727743        $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
    728744        $length = isset($_REQUEST['length']) ? $_REQUEST['length'] : 10;
    729745        $draw = isset($_REQUEST['draw']) ? $_REQUEST['draw'] : 1;
    730746
    731747        $status_filter = isset($_REQUEST['columns']) &&
    732748        isset($_REQUEST['columns'][0]) &&
    733749        isset($_REQUEST['columns'][0]['search']) &&
    734750        isset($_REQUEST['columns'][0]['search']['value']) ?
    735751            $_REQUEST['columns'][0]['search']['value'] : false;
    736752
    737753        $filters = [];
    738754
    739755        if($status_filter){
    740756
    741757            if($status_filter == 'warning'){
    742758
    743759                $filters[] = " warnings IS NOT NULL ";
    744760            }else{
    745761
    746762                $filters[] = " status = '". $status_filter . "' AND warnings IS NULL ";
    747763            }
    748764
    749765        }else{
    750766
    751767            $filters[] = " status != 'rule-based' ";
    752768
    753769        }
    754770
    755771        $url_filter = isset($_REQUEST['columns']) &&
    756772        isset($_REQUEST['columns'][1]) &&
    757773        isset($_REQUEST['columns'][1]['search']) &&
    758774        isset($_REQUEST['columns'][1]['search']['value']) ?
    759775            $_REQUEST['columns'][1]['search']['value'] : false;
    760776
    761777        $url_regex = isset($_REQUEST['columns']) &&
    762778        isset($_REQUEST['columns'][1]) &&
    763779        isset($_REQUEST['columns'][1]['search']) &&
    764780        isset($_REQUEST['columns'][1]['search']['regex']) ?
    765781            $_REQUEST['columns'][1]['search']['regex'] : false;
    766782
    767783        if($url_regex == 'true' && $url_filter){
    768784
    769785            $filters[] = " url = '". $url_filter . "' ";
    770786
    771787        }
    772788
    773789        if($url_regex == 'false' && $url_filter){
    774790
    775791            $filters[] = " url LIKE '%". $url_filter . "%' ";
    776792
    777793        }
    778794
    779795        $where_clause = '';
    780796
    781797        foreach ($filters as $key => $filter){
    782798
    783799            if($key == 0){
    784800
    785801                $where_clause = ' WHERE ';
    786802                $where_clause .= $filter;
    787803            }else{
    788804
    789805                $where_clause .= ' AND ';
    790806                $where_clause .= $filter;
    791807            }
    792808
    793809        }
    794810
    795811        $data  = $type == 'path' ?
    796812            UnusedCSS_DB::get_links($start, $length, $where_clause):
    797813            UnusedCSS_DB::get_rules($start, $length, $where_clause);
    798814
    799815        wp_send_json([
    800816            'data' => $data,
    801817            "draw" => (int)$draw,
    802818            "recordsTotal" => $type == 'path' ? UnusedCSS_DB::get_total_job_count() : UnusedCSS_DB::get_total_rule_count(),
    803819            "recordsFiltered" => $type == 'path' ? UnusedCSS_DB::get_total_job_count($where_clause) : UnusedCSS_DB::get_total_rule_count($where_clause),
    804820            "success" => true
    805821        ]);
    806822    }
    807823
    808824    public function enqueueScripts() {
    809825
    810826        $deregister_scripts = apply_filters('uucss/scripts/deregister', ['select2']);
    811827
    812828        if(isset($deregister_scripts) && is_array($deregister_scripts)){
    813829            foreach ($deregister_scripts as $deregister_script){
    814830                wp_dequeue_script($deregister_script);
    815831                wp_deregister_script($deregister_script);
    816832            }
    817833        }
    818834
    819835        wp_enqueue_script( 'select2', UUCSS_PLUGIN_URL . 'assets/libs/select2/select2.min.js', array( 'jquery' ) );
    820836
    821837        wp_enqueue_script( 'datatables', UUCSS_PLUGIN_URL . 'assets/libs/datatables/jquery.dataTables.min.js', array(
    822838            'jquery',
    823839            'uucss_admin'
    824840        ) );
    825841        wp_enqueue_style( 'datatables', UUCSS_PLUGIN_URL . 'assets/libs/datatables/jquery.dataTables.min.css' );
    826842
    827843        wp_register_script( 'uucss_admin', UUCSS_PLUGIN_URL . 'assets/js/uucss_admin.js', array(
    828844            'jquery',
    829845            'wp-util'
    830846        ), UUCSS_VERSION );
    831847
    832848        wp_register_script( 'uucss_log', UUCSS_PLUGIN_URL . 'assets/js/uucss_log.js', array(
    833849            'jquery',
    834850            'wp-util'
    835851        ), UUCSS_VERSION );
    836852
    837853        $deregister_styles = apply_filters('uucss/styles/deregister',[]);
    838854
    839855        if(isset($deregister_styles) && is_array($deregister_styles)){
    840856            foreach ($deregister_styles as $deregister_style){
    841857                wp_dequeue_style($deregister_style);
    842858            }
    843859        }
    844860
    845861        wp_enqueue_style( 'uucss_admin', UUCSS_PLUGIN_URL . 'assets/css/uucss_admin.css', [], UUCSS_VERSION );
    846862
    847863        global $rapidload;
    848864
    849865        $data = array(
    850866            'api' => RapidLoad_Api::get_key(),
    851867            'nonce' => wp_create_nonce( 'uucss_nonce' ),
    852868            'url' => site_url(),
    853869            'ajax_url'          => admin_url( 'admin-ajax.php' ),
    854870            'setting_url'       => admin_url( 'options-general.php?page=uucss' ),
    855871            'on_board_complete' => apply_filters('uucss/on-board/complete', false),
    856872            'api_key_verified' => self::is_api_key_verified(),
    857873            'notifications' => $this->getNotifications(),
    858874            'faqs' => [],
    859875            'public_notices' => [],
    860876            'dev_mode' => apply_filters('uucss/dev_mode', isset($this->uucss->options['uucss_dev_mode'])) && $this->uucss->options['uucss_dev_mode'] == "1",
    861877            'rules_enabled' => $rapidload->rules_enabled(),
    862878            'cpcss_enabled' => $rapidload->critical_css_enabled(),
    863879            'home_url' => home_url(),
    864880            'uucss_enable_debug' => ! empty( $this->uucss->options['uucss_enable_debug'] ) && '1' === $this->uucss->options['uucss_enable_debug'],
    865881        );
    866882
    867883        wp_localize_script( 'uucss_admin', 'uucss', $data );
    868884
    869885        wp_enqueue_script( 'uucss_admin' );
    870886        wp_enqueue_script( 'uucss_log' );
    871887
    872888        wp_enqueue_style( 'select2', UUCSS_PLUGIN_URL . 'assets/libs/select2/select2.min.css' );
    873889
    874890    }
    875891
    876892    public function getNotifications() {
    877893
    878894        return apply_filters('uucss/notifications', []);
    879895    }
    880896
    881897    public function run_gpsi_test_for_all(){
    882898
    883899        $links = UnusedCSS_DB::get_links_where(" WHERE status IN('success','rule-based') ");
    884900
    885901        if(!empty($links)){
    886902
    887903            foreach ($links as $link){
    888904
    889905                if(isset($link['meta']) &&
    890906                    isset($link['meta']['stats']) &&
    891907                    isset($link['meta']['stats']->success_count) &&
    892908                    $link['meta']['stats']->success_count > 0 ||
    893909                    isset($link['success_count']) && $link['success_count']
    894910                ){
    895911                    continue;
    896912                }
    897913
    898914                $this->get_gpsi_test_result($link);
    899915
    900916            }
    901917
    902918        }
    903919
    904920    }
    905921
    906922    public function run_gpsi_status_check_for_all(){
    907923
     924        self::verify_nonce();
     925
    908926        $spawned = wp_schedule_single_event( time() + 5, 'uucss_run_gpsi_test_for_all');
    909927
    910928        wp_send_json_success([
    911929            'spawned' => $spawned
    912930        ]);
    913931    }
    914932
    915933    public function get_public_notices(){
    916934
    917935        $api = new RapidLoad_Api();
    918936
    919937        $result = $api->get('notification');
    920938
    921939        $data = !$api->is_error($result) && isset($result->data) ? $result->data : [];
    922940
    923941        $data = array_filter($data, function ($notice){
    924942            $notice_read = UnusedCSS_Admin::get_site_option('uucss_notice_' . $notice->id . '_read');
    925943            return empty($notice_read);
    926944        });
    927945
    928946        $keys = array_keys($data);
    929947
    930948        if(empty($keys)){
    931949            return $data;
    932950        }
    933951
    934952        $notices = [];
    935953
    936954        foreach ($data as $key => $notice){
    937955            array_push($notices, $notice);
    938956        }
    939957
    940958        return $notices;
    941959    }
    942960
    943961    public function get_gpsi_test_result($link){
    944962
    945963        $uucss_api = new RapidLoad_Api();
    946964
    947965        $cached_files = [];
    948966        $original_files = [];
    949967
    950968        if(isset($link['files']) && !empty($link['files'])){
    951969
    952970            $cached_files = array_filter($link['files'], function ($file){
    953971                return !$this->str_contains($file['original'], '//inline-style@');
    954972            });
    955973
    956974            $original_files = array_filter($link['files'], function ($file){
    957975                return !$this->str_contains($file['original'], '//inline-style@');
    958976            });
    959977        }
    960978
    961979        do_action( 'uucss/cached', [
    962980            'url' => $link['url']
    963981        ]);
    964982
    965983        return $uucss_api->post( 'test/wordpress',
    966984            [
    967985                'url' => urldecode($link['url']),
    968986                'files' => !empty($cached_files) ? array_column($cached_files, 'uucss') : [],
    969987                'aoFiles' => !empty($original_files) ? array_column($original_files, 'original') : []
    970988            ]);
    971989
    972990    }
    973991
    974992    public function uucss_test_url(){
     993
     994        self::verify_nonce();
    975995
    976996        global $uucss;
    977997
    978998        if(!isset($_REQUEST['url'])){
    979999            wp_send_json_error('url required');
    9801000        }
    9811001
    9821002        $url = $_REQUEST['url'];
    9831003        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path';
    9841004
    9851005        if($type == 'rule'){
    9861006
    9871007            if(!isset($_REQUEST['rule']) || !isset($_REQUEST['regex'])){
    9881008                wp_send_json_error('rule and regex required');
    9891009            }
    9901010
    9911011        }
    9921012
    9931013        $uucss_api = new RapidLoad_Api();
    9941014
    9951015        $link = $type == 'path' ? UnusedCSS_DB::get_link($url) : UnusedCSS_DB::get_rule($_REQUEST['rule'],$_REQUEST['regex']);
    9961016
    9971017        $result = $this->get_gpsi_test_result($link);
    9981018
    9991019        if ( $uucss_api->is_error( $result ) ) {
    10001020            if(isset($result->errors) && isset($result->errors[0])){
    10011021                wp_send_json_error($result->errors[0]->detail);
    10021022            }else{
    10031023                wp_send_json_error($result);
    10041024            }
    10051025        }
    10061026
    10071027        wp_send_json_success($result);
    10081028    }
    10091029
    10101030    public function get_faqs(){
    10111031
    10121032        $rapidload_faqs_read = self::get_site_option('rapidload_faqs_read');
    10131033
    10141034        if(!empty($rapidload_faqs_read)){
    10151035            return [];
    10161036        }
    10171037
    10181038        $api = new RapidLoad_Api();
    10191039
    10201040        $result = $api->get('faqs');
    10211041
    10221042        $default = [
    10231043            [
    10241044                "title" => "I enabled RapidLoad and now my site is broken. What do I do?",
    10251045                "message" => "If you are encountering layout or styling issues on a RapidLoad optimized page, try enabling the “Load Original CSS Files” option or <a href='https://rapidload.zendesk.com/hc/en-us/articles/360063292673-Sitewide-Safelists-Blocklists'>adding safelist rules</a> for affected elements in the plugin Advanced Settings. Always remember to requeue affected pages after making plugin changes. Need more help? Head over to the RapidLoad docs for more information or to submit a Support request: <a href='https://rapidload.zendesk.com/hc/en-us'>https://rapidload.zendesk.com/hc/en-us</a>",
    10261046            ],
    10271047            [
    10281048                "title" => "Why am I still seeing the “Removed unused CSS” flag in Google Page Speed Insights?",
    10291049                "message" => "It’s possible that the RapidLoad optimized version of the page is not yet being served. Try clearing your page cache and running the GPSI test again.",
    10301050            ],
    10311051            [
    10321052                "title" => "Will this plugin work with other caching plugins?",
    10331053                "message" => "RapidLoad works with all major caching plugins. If you are using a little known caching plugin and are experiencing issues with RapidLoad, please submit your issue and caching plugin name to our support team and we will review.",
    10341054            ],
    10351055            [
    10361056                "title" => "Do I need to run this every time I make a change?",
    10371057                "message" => "No! RapidLoad works in the background, so any new stylesheets that are added will be analyzed and optimized on the fly. Just set it and forget it!",
    10381058            ],
    10391059            [
    10401060                "title" => "Do you offer support if I need it?",
    10411061                "message" => "Yes, our team is standing by to assist you! Submit a support ticket any time from the Support tab in the plugin and we’ll be happy to help.",
    10421062            ]
    10431063        ];
    10441064
    10451065        return !$api->is_error($result) && isset($result->data) ? $result->data : $default;
    10461066    }
    10471067
    10481068    public function clear_uucss_logs(){
     1069
     1070        self::verify_nonce();
     1071
    10491072        $file_system = new RapidLoad_FileSystem();
    10501073
    10511074        if(!$file_system->exists(WP_CONTENT_DIR . '/uploads/rapidload/')){
    10521075            wp_send_json_success(true);
    10531076        }
    10541077
    10551078        $file_system->delete_folder(WP_CONTENT_DIR . '/uploads/rapidload/');
    10561079        wp_send_json_success(true);
    10571080    }
    10581081
    10591082    public function uucss_logs(){
    10601083
     1084        self::verify_nonce();
     1085
    10611086        $file_system = new RapidLoad_FileSystem();
    10621087
    10631088        if(!$file_system->exists(UUCSS_LOG_DIR . 'debug.log')){
    10641089            wp_send_json_success([]);
    10651090        }
    10661091
    10671092        $data = $file_system->get_contents(UUCSS_LOG_DIR . 'debug.log');
    10681093
    10691094        if(empty($data)){
    10701095            wp_send_json_success([]);
    10711096        }
    10721097
    10731098        $data = '[' . $data . ']';
    10741099
    10751100        wp_send_json_success(json_decode($data));
    10761101    }
    10771102
    10781103    public function frontend_logs(){
     1104
     1105        self::verify_nonce();
    10791106
    10801107        $args = [];
    10811108
    10821109        $args['type'] = isset($_REQUEST['type']) && !empty($_REQUEST['type']) ? $_REQUEST['type'] : 'frontend';
    10831110        $args['log'] = isset($_REQUEST['log']) && !empty($_REQUEST['log']) ? $_REQUEST['log'] : '';
    10841111        $args['url'] = isset($_REQUEST['url']) && !empty($_REQUEST['url']) ? $_REQUEST['url'] : '';
    10851112
    10861113        self::log($args);
    10871114
    10881115        wp_send_json_success(true);
    10891116    }
    10901117
    10911118    public function mark_faqs_read(){
    10921119
     1120        self::verify_nonce();
     1121
    10931122        self::update_site_option('rapidload_faqs_read', true);
    10941123        wp_send_json_success(true);
    10951124    }
    10961125
    10971126    public function mark_notice_read(){
    10981127
    10991128        $notice_id = isset($_REQUEST['notice_id']) ? $_REQUEST['notice_id'] : false;
    11001129
    11011130        if($notice_id){
    11021131            self::update_site_option('uucss_notice_' . $notice_id . '_read', true);
    11031132        }
    11041133
    11051134        wp_send_json_success(true);
    11061135    }
    11071136
    11081137    public function clear_page_cache(){
     1138
     1139        self::verify_nonce();
    11091140
    11101141        $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
    11111142        $rule = isset($_REQUEST['rule']) ? $_REQUEST['rule'] : false;
    11121143        $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : false;
    11131144
    11141145        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : false;
    11151146
    11161147        $type = isset($_REQUEST['type']) ? $_REQUEST['status'] : 'path';
    11171148
    11181149        if($url){
    11191150
    11201151            UnusedCSS_DB::reset_hits($url);
    11211152            do_action( 'uucss/cached', [
    11221153                'url' => $url
    11231154            ] );
    11241155        }
    11251156
    11261157        $links = false;
    11271158
    11281159        if($rule && $regex){
    11291160
    11301161            $rule = UnusedCSS_DB::get_rule($rule, $regex);
    11311162
    11321163            if(isset($rule['id'])){
    11331164
    11341165                $links = UnusedCSS_DB::get_links_where(" WHERE rule_id = " . $rule['id']);
    11351166
    11361167            }
    11371168        }
    11381169
    11391170        if($status){
    11401171
    11411172            UnusedCSS_DB::reset_hits();
    11421173
    11431174            if($type == 'path'){
    11441175
    11451176                $links = UnusedCSS_DB::get_links_where(' ');
    11461177
    11471178            }else{
    11481179
    11491180                $links = UnusedCSS_DB::get_rules_where(' ');
    11501181
    11511182            }
    11521183
    11531184        }
    11541185
    11551186        if($links && !empty($links)){
    11561187
    11571188            foreach ($links as $link){
    11581189
    11591190                if(isset($link['url'])){
    11601191                    self::uucss_log($link['url']);
    11611192                    do_action( 'uucss/cached', [
    11621193                        'url' => $link['url']
    11631194                    ] );
    11641195                }
    11651196            }
    11661197        }
    11671198
    11681199        wp_send_json_success('page cache cleared');
    11691200    }
    11701201
    11711202    public static function is_api_key_verified() {
    11721203
    11731204        $api_key_status = isset( RapidLoad_Base::fetch_options()['uucss_api_key_verified'] ) ? RapidLoad_Base::fetch_options()['uucss_api_key_verified'] : '';
    11741205
    11751206        return $api_key_status == '1';
    11761207
    11771208    }
    11781209
    11791210    public function add_plugin_action_link( $links ) {
    11801211
    11811212        $_links = array(
    11821213            '<a href="' . admin_url( 'options-general.php?page=uucss' ) . '">Settings</a>',
    11831214        );
    11841215
    11851216        return array_merge( $_links, $links );
    11861217    }
    11871218
    11881219    public function add_meta_boxes()
    11891220    {
    11901221        add_meta_box(
    11911222            'uucss-options',
    11921223            __( 'RapidLoad Options', 'uucss' ),
    11931224            [$this, 'meta_box'],
    11941225            get_post_types(),
    11951226            'side'
    11961227        );
    11971228    }
    11981229
    11991230    public function meta_box( $post ) {
    12001231
    12011232        $options = RapidLoad_Base::get_page_options($post->ID);
    12021233
    12031234        include('parts/admin-post.html.php');
    12041235    }
    12051236
    12061237    public function save_meta_box_options($post_id, $post)
    12071238    {
    12081239        if ( !isset( $_POST['uucss_nonce'] ) || !wp_verify_nonce( $_POST['uucss_nonce'], 'uucss_option_save' ) ) {
    12091240            return;
    12101241        }
    12111242
    12121243        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    12131244            return;
    12141245        }
    12151246
    12161247        $this->update_meta($post_id);
    12171248
    12181249    }
    12191250
    12201251    public function cache_trigger_hooks() {
    12211252        add_action( 'save_post', [ $this, 'cache_on_actions' ], 110, 3 );
    12221253        add_action( 'untrash_post', [ $this, 'cache_on_actions' ], 10, 1 );
    12231254        add_action( 'wp_trash_post', [ $this, 'clear_on_actions' ], 10, 1 );
    12241255        add_action( "wp_ajax_uucss_purge_url", [ $this, 'ajax_purge_url' ] );
    12251256    }
    12261257
    12271258    public static function suggest_whitelist_packs() {
    12281259
     1260        self::verify_nonce();
     1261
    12291262        if ( ! function_exists( 'get_plugins' ) ) {
    12301263            require_once ABSPATH . 'wp-admin/includes/plugin.php';
    12311264        }
    12321265
    12331266        $plugins        = get_plugins();
    12341267        $active_plugins = array_map( function ( $key, $item ) {
    12351268
    12361269            $item['slug'] = $key;
    12371270
    12381271            return $item;
    12391272        }, array_keys( $plugins ), $plugins );
    12401273
    12411274        $api = new RapidLoad_Api();
    12421275
    12431276        $data = $api->post( 'whitelist-packs/wp-suggest', [
    12441277            'plugins' => $active_plugins,
    12451278            'theme'   => get_template(),
    12461279            'url'     => site_url()
    12471280        ] );
    12481281
    12491282        if ( wp_doing_ajax() ) {
    12501283            wp_send_json_success( $data->data );
    12511284        }
    12521285
    12531286        return isset($data) && is_array($data) ? $data : [];
    12541287    }
    12551288
    12561289    public function uucss_license() {
     1290
     1291        self::verify_nonce();
    12571292
    12581293        $api = new RapidLoad_Api();
    12591294
    12601295        $data = $api->get( 'license', [
    12611296            'url' => $this->transform_url(get_site_url()),
    12621297            'version' => UUCSS_VERSION,
    12631298            'db_version' => RapidLoad_DB::$db_version,
    12641299            'db_version_exist' => RapidLoad_DB::$current_version
    12651300        ] );
    12661301
    12671302        if ( ! is_wp_error( $data ) ) {
    12681303
    12691304            if ( isset( $data->errors ) ) {
    12701305                wp_send_json_error( $data->errors[0]->detail );
    12711306            }
    12721307
    12731308            if ( gettype( $data ) === 'string' ) {
    12741309                wp_send_json_error( $data );
    12751310            }
    12761311
    12771312            do_action( 'uucss/license-verified' );
    12781313
    12791314            wp_send_json_success( $data->data );
    12801315        }
    12811316
    12821317        wp_send_json_error( 'unknown error occurred' );
    12831318    }
    12841319
    12851320    public function verify_api_key() {
     1321
     1322        self::verify_nonce();
    12861323
    12871324        if ( ! isset( $_POST['api_key'] ) ) {
    12881325            wp_send_json_error();
    12891326
    12901327            return;
    12911328        }
    12921329
    12931330        $uucss_api         = new RapidLoad_Api();
    12941331        $uucss_api->apiKey = sanitize_text_field( $_POST['api_key'] );
    12951332
    12961333        $results = $uucss_api->get( 'verify' );
    12971334
    12981335        if ( isset( $results->data ) ) {
    12991336            wp_send_json_success( true );
    13001337        }
    13011338
    13021339        wp_send_json_error();
    13031340
    13041341    }
    13051342
    13061343    public function ajax_purge_url() {
    13071344
    13081345        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uucss_nonce' ) ) {
    13091346            wp_send_json_error( 'authentication failed' );
    13101347
    13111348            return;
    13121349        }
    13131350
    13141351        $args = isset($_POST['args']) ? $_POST['args'] : [];
    13151352
    13161353        if ( ! isset( $_POST['url'] ) ) {
    13171354            wp_send_json_error();
    13181355
    13191356            return;
    13201357        }
    13211358
    13221359        if ( isset( $_POST['args'] ) ) {
    13231360            $args['post_id'] = ( isset( $_POST['args']['post_id'] ) ) ? intval( $_POST['args']['post_id'] ) : null;
    13241361        }
    13251362
    13261363        $url = esc_url_raw( $_POST['url'] );
    13271364
    13281365        if(isset($args['rule_id'])){
    13291366            $rule = UnusedCSS_Rule::get_rule_from_id($args['rule_id']);
    13301367            if($rule){
    13311368                $url = $rule->url;
    13321369                $args['rule'] = $rule->rule;
    13331370                $args['regex'] = $rule->regex;
    13341371            }
    13351372        }
    13361373
    13371374        if ( isset( $_POST['clear'] ) && boolval($_POST['clear'] == 'true') ) {
    13381375            $list = isset($_POST['url_list']) ? $_POST['url_list'] : null;
    13391376
    13401377            if(isset($list) && is_array($list) && !empty($list)){
    13411378                foreach ($list as $item){
    13421379
    13431380                    $url = is_array($item) && isset($item['url']) ? $item['url'] : $item;
    13441381
    13451382                    if(is_array($item) && isset($item['rule'])){
    13461383                        $args['rule'] = $item['rule'];
    13471384                    }
    13481385
    13491386                    if(is_array($item) && isset($item['regex'])){
    13501387                        $args['regex'] = $item['regex'];
    13511388                    }
    13521389
    13531390                    $this->uucss->clear_cache( $url, $args );
    13541391                }
    13551392            }else{
    13561393                $this->uucss->clear_cache( $url, $args );
    13571394            }
    13581395
    13591396            wp_send_json_success( true );
    13601397            return;
    13611398        }
    13621399
    13631400        if ( isset( $args["post_id"] ) ) {
    13641401            $args['options'] = $this->uucss->api_options( $args["post_id"] );
    13651402        }
    13661403
    13671404        $args['immediate'] = true;
    13681405        $args['priority'] = true;
    13691406
    13701407        wp_send_json_success( $this->uucss->cache( $url, $args ) );
    13711408    }
    13721409
    13731410    /**
    13741411     * @param $post_id
    13751412     * @param $post WP_Post
    13761413     * @param $update
    13771414     */
    13781415    public function cache_on_actions($post_id, $post = null, $update = null)
    13791416    {
    13801417
    13811418        $post = get_post($post_id);
    13821419
    13831420        if($post->post_status == "publish") {
    13841421
    13851422            $this->clear_on_actions( $post->ID );
    13861423
    13871424            $url = get_permalink( $post );
    13881425
    13891426            if(UnusedCSS_DB::link_exists_with_error($url) || !RapidLoad_Base::get()->rules_enabled()){
    13901427                $this->uucss->cache( $url );
    13911428            }
    13921429
    13931430        }
    13941431    }
    13951432
    13961433    public function clear_on_actions($post_ID)
    13971434    {
    13981435        $link = get_permalink($post_ID);
    13991436
    14001437        if($link){
    14011438            $this->uucss->clear_cache($link);
    14021439        }
    14031440    }
    14041441
    14051442    public function update_meta($post_id)
    14061443    {
    14071444        foreach (self::$page_options as $option) {
    14081445
    14091446            if ( ! isset( $_POST[ 'uucss_' . $option ] ) ) {
    14101447                delete_post_meta( $post_id, '_uucss_' . $option );
    14111448                continue;
    14121449            }
    14131450
    14141451            $value = sanitize_text_field( $_POST[ 'uucss_' . $option ] );
    14151452
    14161453            update_post_meta( $post_id, '_uucss_' . $option, $value );
    14171454        }
    14181455    }
    14191456
    14201457    public static function get_site_option($name)
    14211458    {
    14221459        if(is_multisite()){
    14231460
    14241461            return get_blog_option(get_current_blog_id(), $name, false);
    14251462
    14261463        }
    14271464        return get_site_option( $name, false );
    14281465    }
    14291466
    14301467    public static function update_site_option($name, $value){
    14311468
    14321469        if(is_multisite()){
    14331470
    14341471            return update_blog_option(get_current_blog_id(), $name, $value);
    14351472
    14361473        }
    14371474        return update_site_option($name, $value);
    14381475    }
    14391476
    14401477    public static function delete_site_option($name){
    14411478
    14421479        if(is_multisite()){
    14431480
    14441481            return delete_blog_option(get_current_blog_id(), $name);
    14451482
    14461483        }
    14471484        return delete_site_option($name);
    14481485    }
    14491486
    14501487    public static function first_job_done(){
    14511488        return (RapidLoad_Settings::get_first_link() ? true :  false);
    14521489    }
    14531490}
Note: See TracChangeset for help on using the changeset viewer.