- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
unusedcss/trunk/includes/modules/unused-css/UnusedCSS_Admin.php
r2847136 r2877726 1 1 <?php 2 2 3 3 defined( 'ABSPATH' ) or die(); 4 4 5 5 /** 6 6 * Class UnusedCSS 7 7 */ 8 8 abstract class UnusedCSS_Admin { 9 9 10 10 use RapidLoad_Utils; 11 11 12 12 /** 13 13 * @var UnusedCSS_Autoptimize 14 14 */ 15 15 public $uucss; 16 16 17 17 /** 18 18 * @var bool 19 19 */ 20 20 public static $enabled = true; 21 21 22 22 /** 23 23 * Page related meta options 24 24 * @var array 25 25 */ 26 26 public static $page_options = [ 27 27 'safelist', 28 28 'exclude', 29 29 'blocklist' 30 30 ]; 31 31 32 32 /** 33 33 * UnusedCSS constructor. 34 34 * @param UnusedCSS $uucss 35 35 */ 36 36 public function __construct($uucss) 37 37 { 38 38 39 39 $this->uucss = $uucss; 40 40 41 41 if(is_admin()){ 42 42 43 43 add_action( 'admin_menu', array( $this, 'add_uucss_option_page' ) ); 44 44 45 45 } 46 46 47 47 48 48 if (!self::$enabled) { 49 49 return; 50 50 } 51 51 52 52 add_action( 'current_screen', function () { 53 53 54 54 if ( get_current_screen() && get_current_screen()->base == 'settings_page_uucss' ) { 55 55 add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] ); 56 56 } 57 57 } ); 58 58 59 59 $this->cache_trigger_hooks(); 60 60 61 61 add_action( 'add_meta_boxes', [$this, 'add_meta_boxes'] ); 62 62 add_action( 'save_post', [$this, 'save_meta_box_options'] , 10, 2); 63 63 add_action( "uucss_run_gpsi_test_for_all", [ $this, 'run_gpsi_test_for_all' ]); 64 64 65 65 add_filter( 'plugin_action_links_' . plugin_basename( UUCSS_PLUGIN_FILE ), [ 66 66 $this, 67 67 'add_plugin_action_link' 68 68 ] ); 69 69 70 70 if(is_admin()){ 71 71 72 72 $this->deactivate(); 73 73 74 74 add_action('current_screen', [$this, 'validate_domain']); 75 75 add_action('wp_ajax_clear_page_cache', [$this, 'clear_page_cache']); 76 76 add_action('wp_ajax_mark_faqs_read', [$this, 'mark_faqs_read']); 77 77 add_action('wp_ajax_mark_notice_read', [$this, 'mark_notice_read']); 78 78 add_action('wp_ajax_frontend_logs', [$this, 'frontend_logs']); 79 79 add_action('wp_ajax_uucss_logs', [$this, 'uucss_logs']); 80 80 add_action('wp_ajax_clear_uucss_logs', [$this, 'clear_uucss_logs']); 81 81 add_action( "wp_ajax_uucss_test_url", [ $this, 'uucss_test_url' ] ); 82 82 add_action( "wp_ajax_uucss_run_gpsi_status_check_for_all", [ $this, 'run_gpsi_status_check_for_all' ] ); 83 83 add_action( "wp_ajax_uucss_data", [ $this, 'uucss_data' ] ); 84 84 add_action( "wp_ajax_uucss_license", [ $this, 'uucss_license' ] ); 85 85 add_action( "wp_ajax_uucss_status", [ $this, 'uucss_status' ] ); 86 86 add_action( "wp_ajax_uucss_rule_stats", [ $this, 'uucss_rule_stats' ] ); 87 87 add_action( "wp_ajax_suggest_whitelist_packs", [ $this, 'suggest_whitelist_packs' ] ); 88 88 add_action( "wp_ajax_verify_api_key", [ $this, 'verify_api_key' ] ); 89 89 add_action( "wp_ajax_uucss_deactivate", [ $this, 'ajax_deactivate' ] ); 90 90 add_action( "wp_ajax_uucss_connect", [ $this, 'uucss_connect' ] ); 91 91 add_action( "wp_ajax_attach_rule", [ $this, 'attach_rule' ] ); 92 92 add_action( "wp_ajax_uucss_update_rule", [ $this, 'uucss_update_rule' ] ); 93 93 add_action( 'wp_ajax_uucss_queue', [$this, 'queue_posts']); 94 94 add_action( 'wp_ajax_rapidload_notifications', [$this, 'rapidload_notifications']); 95 95 add_action( 'admin_notices', [ $this, 'first_uucss_job' ] ); 96 96 add_action( 'updated_option', [ $this, 'clear_cache_on_option_update' ], 10, 3 ); 97 97 } 98 98 99 99 add_action( 'uucss_sitemap_queue', [$this, 'queue_sitemap'], 10, 1); 100 100 101 101 } 102 102 103 103 function rapidload_notifications(){ 104 104 105 self::verify_nonce(); 106 105 107 wp_send_json_success([ 106 108 'faqs' => $this->get_faqs(), 107 109 'notifications' => $this->get_public_notices() 108 110 ]); 109 111 110 112 } 111 113 112 114 function queue_posts(){ 115 116 self::verify_nonce(); 113 117 114 118 if(!isset($_REQUEST['post_type'])) { 115 119 wp_send_json_error('post type not found'); 116 120 } 117 121 118 122 $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path'; 119 123 $rule = isset($_REQUEST['rule']) ? $_REQUEST['rule'] : false; 120 124 $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : false; 121 125 122 126 $post_type = sanitize_text_field($_REQUEST['post_type']); 123 127 124 128 $list = isset($_POST['url_list']) ? $_POST['url_list'] : null; 125 129 126 130 $posts = null; 127 131 128 132 global $uucss; 129 133 130 134 if(isset($list) && is_array($list) && !empty($list)){ 131 135 132 136 if($type == 'path'){ 133 137 UnusedCSS_DB::requeue_urls($list); 134 138 }else{ 135 139 UnusedCSS_DB::requeue_rules($list); 136 140 } 137 141 138 142 $this->uucss->cleanCacheFiles(); 139 143 140 144 wp_send_json_success('successfully links added to the queue'); 141 145 }else if($post_type == 'current'){ 142 146 143 147 if($type == 'path'){ 144 148 RapidLoad_Settings::clear_links(true); 145 149 }else{ 146 150 UnusedCSS_DB::clear_rules(true); 147 151 } 148 152 149 153 $this->uucss->cleanCacheFiles(); 150 154 151 155 wp_send_json_success('successfully links added to the queue'); 152 156 153 157 }else if($post_type == 'processing'){ 154 158 155 159 if($type == 'path'){ 156 160 UnusedCSS_DB::requeue_jobs('processing'); 157 161 UnusedCSS_DB::requeue_jobs('waiting'); 158 162 }else{ 159 163 UnusedCSS_DB::requeue_rule_jobs('processing'); 160 164 UnusedCSS_DB::requeue_rule_jobs('waiting'); 161 165 } 162 166 163 167 $this->uucss->cleanCacheFiles(); 164 168 165 169 wp_send_json_success('successfully links added to the queue'); 166 170 167 171 }else if($post_type == 'warnings'){ 168 172 169 173 if($type == 'path'){ 170 174 UnusedCSS_DB::requeue_jobs('warnings'); 171 175 }else{ 172 176 UnusedCSS_DB::requeue_rule_jobs('warnings'); 173 177 } 174 178 175 179 $this->uucss->cleanCacheFiles(); 176 180 177 181 wp_send_json_success('successfully links added to the queue'); 178 182 179 183 }else if($post_type == 'failed'){ 180 184 181 185 if($type == 'path'){ 182 186 UnusedCSS_DB::requeue_jobs(); 183 187 }else{ 184 188 UnusedCSS_DB::requeue_rule_jobs(); 185 189 } 186 190 187 191 $this->uucss->cleanCacheFiles(); 188 192 189 193 wp_send_json_success('successfully links added to the queue'); 190 194 191 195 }else if($post_type == 'url'){ 192 196 193 197 $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false; 194 198 195 199 if($url && !$this->is_url_allowed($url)){ 196 200 wp_send_json_error('url is excluded'); 197 201 } 198 202 199 203 $url_object = false; 200 204 201 205 if($type == 'path'){ 202 206 203 207 $url_object = new UnusedCSS_Path([ 204 208 'url' => $url 205 209 ]); 206 210 207 211 }else{ 208 212 209 213 $url_object = new UnusedCSS_Rule([ 210 214 'rule' => $rule, 211 215 'regex' => $regex 212 216 ]); 213 217 214 218 } 215 219 216 220 if(!$url_object){ 217 221 218 222 wp_send_json_error('Invalid URL'); 219 223 220 224 } 221 225 222 226 $url_object->requeue(); 223 227 $url_object->save(); 224 228 225 229 wp_send_json_success('successfully link added to the queue'); 226 230 227 231 }else if($post_type == 'site_map'){ 228 232 229 233 $sitemap = isset($_REQUEST['url']) ? $_REQUEST['url'] : false; 230 234 231 235 if(!$sitemap){ 232 236 233 237 wp_send_json_error('site map url required'); 234 238 } 235 239 236 240 $spawned = $this->schedule_cron('uucss_sitemap_queue',[ 237 241 'url' => $sitemap 238 242 ]); 239 243 240 244 wp_send_json_success('Sitemap links scheduled to be added to the queue.'); 241 245 242 246 }else{ 243 247 244 248 $posts = new WP_Query(array( 245 249 'post_type'=> $post_type, 246 250 'posts_per_page' => -1 247 251 )); 248 252 249 253 } 250 254 251 255 if($posts && $posts->have_posts()){ 252 256 while ($posts->have_posts()){ 253 257 $posts->the_post(); 254 258 255 259 $url = $this->transform_url(get_the_permalink(get_the_ID())); 256 260 257 261 if($this->is_url_allowed($url)){ 258 262 new UnusedCSS_Path([ 259 263 'url' => $url 260 264 ]); 261 265 } 262 266 263 267 } 264 268 } 265 269 266 270 wp_reset_query(); 267 271 268 272 wp_send_json_success('successfully links added to the queue'); 269 273 270 274 } 271 275 272 276 function queue_sitemap($url = false){ 273 277 274 278 if(!$url){ 275 279 276 280 $url = apply_filters('uucss/sitemap/default', stripslashes(get_site_url(get_current_blog_id())) . '/sitemap_index.xml'); 277 281 } 278 282 279 283 $site_map = new RapidLoad_Sitemap(); 280 284 $urls = $site_map->process_site_map($url); 281 285 282 286 global $uucss; 283 287 284 288 if(isset($urls) && !empty($urls)){ 285 289 286 290 foreach ($urls as $url){ 287 291 288 292 if($this->is_url_allowed($this->transform_url($url))){ 289 293 290 294 new UnusedCSS_Path([ 291 295 'url' => $url 292 296 ]); 293 297 } 294 298 295 299 } 296 300 } 297 301 } 298 302 299 303 public function add_uucss_option_page() { 300 304 301 305 add_submenu_page( 'options-general.php', 'RapidLoad', 'RapidLoad', 'manage_options', 'uucss', function () { 302 306 wp_enqueue_script( 'post' ); 303 307 304 308 ?> 305 309 <div class="wrap"> 306 310 <h1><?php _e( 'RapidLoad Settings', 'autoptimize' ); ?></h1> 307 311 <?php 308 312 do_action('uucss/options/before_render_form'); 309 313 ?> 310 314 <div> 311 315 <?php $this->render_form() ?> 312 316 </div> 313 317 </div> 314 318 315 319 <?php 316 320 }); 317 321 318 322 register_setting('autoptimize_uucss_settings', 'autoptimize_uucss_settings'); 319 323 320 324 } 321 325 322 326 public function render_form() { 323 327 $options = RapidLoad_Base::fetch_options(); 324 328 325 329 include('parts/options-page.html.php'); 326 330 } 327 331 328 332 public function uucss_rule_stats(){ 329 333 334 self::verify_nonce(); 335 330 336 wp_send_json_success([ 331 337 'duplicateFiles' => UnusedCSS_DB::get_duplicate_files() 332 338 ]); 333 339 334 340 } 335 341 336 342 public function uucss_status(){ 343 344 self::verify_nonce(); 337 345 338 346 $job_counts = UnusedCSS_DB::get_job_counts(); 339 347 340 348 wp_send_json_success([ 341 349 'cssStyleSheetsCount' => $this->uucss->cache_file_count(), 342 350 'cssStyleSheetsSize' => $this->uucss->size(), 343 351 'hits' => $job_counts->hits, 344 352 'success' => $job_counts->success, 345 353 'ruleBased' => $job_counts->rule_based, 346 354 'queued' => $job_counts->queued, 347 355 'waiting' => $job_counts->waiting, 348 356 'processing' => $job_counts->processing, 349 357 'warnings' => $job_counts->warnings, 350 358 'failed' => $job_counts->failed, 351 359 'total' => $job_counts->total, 352 360 ]); 353 361 } 354 362 355 363 public function uucss_update_rule(){ 356 364 365 self::verify_nonce(); 366 357 367 if( !isset($_REQUEST['rule']) || empty($_REQUEST['rule']) || 358 368 !isset($_REQUEST['url']) || empty($_REQUEST['url']) 359 369 ){ 360 370 wp_send_json_error('Required fields missing'); 361 371 } 362 372 363 373 $rule = $_REQUEST['rule']; 364 374 $url = $_REQUEST['url']; 365 375 $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : '/'; 366 376 367 377 $url = $this->transform_url($url); 368 378 369 379 global $uucss; 370 380 371 381 if(!$this->is_url_allowed($url)){ 372 382 wp_send_json_error('URL not allowed'); 373 383 } 374 384 375 385 if(!self::is_url_glob_matched($url, $regex)){ 376 386 wp_send_json_error('Invalid regex for the url'); 377 387 } 378 388 379 389 $ruleObject = false; 380 390 $update_mode = 'create'; 381 391 382 392 if(isset($_REQUEST['old_rule']) && isset($_REQUEST['old_regex'])){ 383 393 384 394 $old_rule = $_REQUEST['old_rule']; 385 395 $old_regex = $_REQUEST['old_regex']; 386 396 $old_url = $_REQUEST['old_url']; 387 397 388 398 if(UnusedCSS_DB::rule_exists_with_error( $old_rule, $old_regex)){ 389 399 390 400 $ruleObject = new UnusedCSS_Rule([ 391 401 'rule' => $old_rule, 392 402 'regex' => $old_regex 393 403 ]); 394 404 395 405 if(isset($_REQUEST['old_url']) && $_REQUEST['old_url'] != $url || 396 406 $_REQUEST['old_rule'] != $rule || $_REQUEST['old_regex'] != $regex){ 397 407 if(isset($_REQUEST['requeue']) && $_REQUEST['requeue'] == "1"){ 398 408 error_log($_REQUEST['requeue']); 399 409 $ruleObject->requeue(); 400 410 $ruleObject->releaseRule(); 401 411 } 402 412 } 403 413 404 414 $ruleObject->url = $url; 405 415 $ruleObject->rule = $rule; 406 416 $ruleObject->regex = $regex; 407 417 $ruleObject->save(); 408 418 $update_mode = 'update'; 409 419 410 420 411 421 412 422 do_action('uucss/rule/saved', $ruleObject, [ 413 423 'rule' => $old_rule, 414 424 'regex' => $old_regex, 415 425 'url' => $old_url 416 426 ]); 417 427 418 428 wp_send_json_success('Rule updated successfully'); 419 429 } 420 430 421 431 } 422 432 423 433 if(UnusedCSS_DB::rule_exists_with_error($_REQUEST['rule'], $_REQUEST['regex'])){ 424 434 wp_send_json_error('Rule already exist'); 425 435 } 426 436 427 437 $ruleObject = new UnusedCSS_Rule([ 428 438 'rule' => $rule, 429 439 'url' => $url, 430 440 'regex' => $regex, 431 441 ]); 432 442 433 443 do_action('uucss/rule/saved', $ruleObject, false); 434 444 435 445 wp_send_json_success('Rule updated successfully'); 436 446 } 437 447 438 448 public function attach_rule(){ 449 450 self::verify_nonce(); 439 451 440 452 $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false; 441 453 $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false; 442 454 $rule_id = isset($_REQUEST['rule_id']) ? $_REQUEST['rule_id'] : false; 443 455 444 456 if(!$type || !$url){ 445 457 wp_send_json_error('Required field missing'); 446 458 } 447 459 448 460 if($type == 'detach' && UnusedCSS_DB::rule_exist_by_url($url)){ 449 461 wp_send_json_error('Rule exist with same url'); 450 462 } 451 463 452 464 if($type == 'detach' && UnusedCSS_DB::link_exists_with_error($url)){ 453 465 454 466 $path = new UnusedCSS_Path([ 455 467 'url' => $url 456 468 ]); 457 469 $path->attach_rule(); 458 470 $path->save(); 459 471 wp_send_json_success('Successfully detached from rule'); 460 472 } 461 473 462 474 if(!is_numeric($rule_id) || !$type || $type == 'attach' && !$rule_id){ 463 475 wp_send_json_error('Required field missing'); 464 476 } 465 477 466 478 if($type == 'attach'){ 467 479 468 480 $rule = UnusedCSS_Rule::get_rule_from_id($rule_id); 469 481 470 482 if(!$rule){ 471 483 wp_send_json_error('Rule not found'); 472 484 } 473 485 474 486 $path = new UnusedCSS_Path([ 475 487 'url' => $url 476 488 ]); 477 489 478 490 if(!self::is_url_glob_matched($url, $rule->regex)){ 479 491 wp_send_json_success('Pattern not matched'); 480 492 } 481 493 482 494 $path->attach_rule($rule->id, $rule->rule); 483 495 $path->save(); 484 496 wp_send_json_success('Successfully attached to rule'); 485 497 } 486 498 487 499 } 488 500 489 501 public static function is_domain_verified(){ 490 502 $options = self::get_site_option( 'autoptimize_uucss_settings' ); 491 503 return $options['valid_domain']; 492 504 } 493 505 494 506 public function clear_cache_on_option_update( $option, $old_value, $value ) { 495 507 496 508 if ( $option == 'autoptimize_uucss_settings' && $this->uucss ) { 497 509 498 510 $needs_to_cleared = false; 499 511 500 512 $diffs = []; 501 513 $diffs_invert = []; 502 514 503 515 if ( $old_value && $value ) { 504 516 $diffs = array_diff_key( $old_value, $value ); 505 517 $diffs_invert = array_diff_key( $value, $old_value ); 506 518 } 507 519 508 520 if ( isset( $diffs_invert['valid_domain'] ) ) { 509 521 unset( $diffs_invert['valid_domain'] ); 510 522 } 511 523 if ( isset( $diffs['valid_domain'] ) ) { 512 524 unset( $diffs['valid_domain'] ); 513 525 } 514 526 515 527 $diffs = array_merge( $diffs, $diffs_invert ); 516 528 517 529 // if these settings are changed cache will be cleared 518 530 if ( isset( $diffs['uucss_minify'] ) || 519 531 isset( $diffs['uucss_keyframes'] ) || 520 532 isset( $diffs['uucss_fontface'] ) || 521 533 isset( $diffs['uucss_analyze_javascript'] ) || 522 534 isset( $diffs['uucss_safelist'] ) || 523 535 isset( $diffs['whitelist_packs'] ) || 524 536 isset( $diffs['uucss_blocklist'] ) || 525 537 isset( $diffs['uucss_variables'] ) ) { 526 538 $needs_to_cleared = true; 527 539 } 528 540 529 541 foreach ( [ 'whitelist_packs', 'uucss_safelist', 'uucss_blocklist' ] as $compare_value ) { 530 542 if ( isset( $value[ $compare_value ] ) && isset( $old_value[ $compare_value ] ) && $old_value[ $compare_value ] !== $value[ $compare_value ] ) { 531 543 $needs_to_cleared = true; 532 544 break; 533 545 } 534 546 } 535 547 536 548 if(isset( $diffs['uucss_enable_rules'] )){ 537 549 UnusedCSS_DB::detach_all_rules(); 538 550 } 539 551 540 552 if ( $needs_to_cleared ) { 541 553 542 554 $this->uucss->clear_cache( null, [ 543 555 'soft' => true 544 556 ] ); 545 557 } 546 558 547 559 RapidLoad_Base::fetch_options(false); 548 560 } 549 561 550 562 } 551 563 552 564 public function deactivate() { 553 565 554 566 if ( ! isset( $_REQUEST['deactivated'] ) || empty( $_REQUEST['deactivated'] ) ) { 555 567 return; 556 568 } 557 569 558 570 if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'uucss_activation' ) ) { 559 571 self::add_admin_notice( 'RapidLoad : Request verification failed for Activation. Contact support if the problem persists.', 'error' ); 560 572 561 573 return; 562 574 } 563 575 564 576 $options = self::get_site_option( 'autoptimize_uucss_settings' ); 565 577 566 578 unset( $options['uucss_api_key_verified'] ); 567 579 unset( $options['uucss_api_key'] ); 568 580 unset( $options['whitelist_packs'] ); 569 581 570 582 self::update_site_option( 'autoptimize_uucss_settings', $options ); 571 583 572 584 $cache_key = 'pand-' . md5( 'first-uucss-job' ); 573 585 self::delete_site_option( $cache_key ); 574 586 575 587 $this->uucss->vanish(); 576 588 577 589 self::$deactivating = true; 578 590 579 591 $notice = [ 580 592 'action' => 'activate', 581 593 'message' => 'RapidLoad : Deactivated your license for this site.', 582 594 'main_action' => [ 583 595 'key' => 'Reactivate', 584 596 'value' => self::activation_url( 'authorize' ) 585 597 ], 586 598 'type' => 'success' 587 599 ]; 588 600 self::add_advanced_admin_notice( $notice ); 589 601 590 602 return; 591 603 } 592 604 593 605 public function first_uucss_job() { 594 606 595 607 if ( class_exists('PAnD') && ! PAnD::is_admin_notice_active( 'first-uucss-job-forever' ) ) { 596 608 return; 597 609 } 598 610 599 611 if(get_current_screen() && get_current_screen()->base == 'settings_page_uucss'){ 600 612 return; 601 613 } 602 614 603 615 $job = RapidLoad_Settings::get_first_link(); 604 616 605 617 if ( $job && $job['status'] == 'success' ) : ?> 606 618 <div data-dismissible="first-uucss-job-forever" 607 619 class="updated notice uucss-notice notice-success is-dismissible"> 608 620 <h4><span class="dashicons dashicons-yes-alt"></span> RapidLoad successfully ran your first job!</h4> 609 621 <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> 610 622 </div> 611 623 <?php endif; 612 624 613 625 if ( $job && $job['status'] == 'failed' ) : ?> 614 626 <div data-dismissible="first-uucss-job-forever" 615 627 class="error notice uucss-notice notice-error is-dismissible"> 616 628 <h4><span class="dashicons dashicons-no-alt"></span> RapidLoad : We were unable to remove unused css 617 629 from 618 630 your site 🤕</h4> 619 631 620 632 <div> 621 633 <p> Our team can help. Get in touch with support <a target="_blank" 622 634 href="https://rapidload.zendesk.com/hc/en-us/requests/new">here</a> 623 635 </p> 624 636 <blockquote class="error notice"> 625 637 <strong>Link :</strong> <?php echo $job['url'] ?> <br> 626 638 <strong>Error :</strong> <?php echo $job['meta']['error']['code'] ?> <br> 627 639 <strong>Message :</strong> <?php echo $job['meta']['error']['message'] ?> 628 640 </blockquote> 629 641 </div> 630 642 631 643 </div> 632 644 <?php endif; 633 645 } 634 646 635 647 public function uucss_connect(){ 636 648 649 self::verify_nonce(); 650 637 651 if ( ! isset( $_REQUEST['license_key'] ) || empty( $_REQUEST['license_key'] ) ) { 638 652 wp_send_json_error( 'License Key required' ); 639 653 } 640 654 641 655 $license_key = $_REQUEST['license_key']; 642 656 643 657 $uucss_api = new RapidLoad_Api(); 644 658 $uucss_api->apiKey = $license_key; 645 659 $results = $uucss_api->post( 'connect', [ 'url' => $this->transform_url(get_site_url()), 'type' => 'wordpress' ] ); 646 660 647 661 if ( $uucss_api->is_error( $results ) ) { 648 662 if(isset($results->errors) && isset($results->errors[0])){ 649 663 wp_send_json_error($results->errors[0]->detail); 650 664 }else{ 651 665 wp_send_json_error('License Key verification fail'); 652 666 } 653 667 } 654 668 655 669 wp_send_json_success([ 656 670 'success' => true, 657 671 'message' => 'License Key verification success', 658 672 'activation_nonce' => wp_create_nonce( 'uucss_activation' ), 659 673 ]); 660 674 } 661 675 662 676 public function ajax_deactivate() { 677 678 self::verify_nonce(); 663 679 664 680 $options = self::get_site_option( 'autoptimize_uucss_settings' ); 665 681 666 682 $cache_key = 'pand-' . md5( 'first-uucss-job' ); 667 683 self::delete_site_option( $cache_key ); 668 684 669 685 $this->uucss->vanish(); 670 686 671 687 $api = new RapidLoad_Api(); 672 688 673 689 // remove domain from authorized list 674 690 $api->post( 'deactivate', [ 675 691 'url' => site_url() 676 692 ] ); 677 693 678 694 unset( $options['uucss_api_key_verified'] ); 679 695 unset( $options['uucss_api_key'] ); 680 696 unset( $options['whitelist_packs'] ); 681 697 682 698 self::update_site_option( 'autoptimize_uucss_settings', $options ); 683 699 684 700 wp_send_json_success( true ); 685 701 } 686 702 687 703 public function validate_domain() { 688 704 689 705 if ( get_current_screen() && get_current_screen()->base != 'settings_page_uucss' ) { 690 706 return; 691 707 } 692 708 693 709 $options = self::get_site_option( 'autoptimize_uucss_settings' ); 694 710 695 711 if(!isset( $options['uucss_api_key_verified'] ) || $options['uucss_api_key_verified'] != '1'){ 696 712 return; 697 713 } 698 714 699 715 $uucss_api = new RapidLoad_Api(); 700 716 701 717 if ( ! isset( $options['uucss_api_key'] ) ) { 702 718 return; 703 719 } 704 720 705 721 $results = $uucss_api->get( 'verify', [ 'url' => site_url(), 'token' => $options['uucss_api_key'] ] ); 706 722 707 723 if($uucss_api->is_error($results)){ 708 724 $options['valid_domain'] = false; 709 725 self::update_site_option('autoptimize_uucss_settings', $options); 710 726 return; 711 727 } 712 728 713 729 if(!isset($options['valid_domain']) || !$options['valid_domain']){ 714 730 $options['valid_domain'] = true; 715 731 self::update_site_option('autoptimize_uucss_settings', $options); 716 732 } 717 733 } 718 734 719 735 public function uucss_data() { 720 736 721 737 if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'uucss_nonce' ) ) { 722 738 wp_send_json_error( 'UnusedCSS - Malformed Request Detected, Contact Support.' ); 723 739 } 724 740 725 741 $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path'; 726 742 727 743 $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0; 728 744 $length = isset($_REQUEST['length']) ? $_REQUEST['length'] : 10; 729 745 $draw = isset($_REQUEST['draw']) ? $_REQUEST['draw'] : 1; 730 746 731 747 $status_filter = isset($_REQUEST['columns']) && 732 748 isset($_REQUEST['columns'][0]) && 733 749 isset($_REQUEST['columns'][0]['search']) && 734 750 isset($_REQUEST['columns'][0]['search']['value']) ? 735 751 $_REQUEST['columns'][0]['search']['value'] : false; 736 752 737 753 $filters = []; 738 754 739 755 if($status_filter){ 740 756 741 757 if($status_filter == 'warning'){ 742 758 743 759 $filters[] = " warnings IS NOT NULL "; 744 760 }else{ 745 761 746 762 $filters[] = " status = '". $status_filter . "' AND warnings IS NULL "; 747 763 } 748 764 749 765 }else{ 750 766 751 767 $filters[] = " status != 'rule-based' "; 752 768 753 769 } 754 770 755 771 $url_filter = isset($_REQUEST['columns']) && 756 772 isset($_REQUEST['columns'][1]) && 757 773 isset($_REQUEST['columns'][1]['search']) && 758 774 isset($_REQUEST['columns'][1]['search']['value']) ? 759 775 $_REQUEST['columns'][1]['search']['value'] : false; 760 776 761 777 $url_regex = isset($_REQUEST['columns']) && 762 778 isset($_REQUEST['columns'][1]) && 763 779 isset($_REQUEST['columns'][1]['search']) && 764 780 isset($_REQUEST['columns'][1]['search']['regex']) ? 765 781 $_REQUEST['columns'][1]['search']['regex'] : false; 766 782 767 783 if($url_regex == 'true' && $url_filter){ 768 784 769 785 $filters[] = " url = '". $url_filter . "' "; 770 786 771 787 } 772 788 773 789 if($url_regex == 'false' && $url_filter){ 774 790 775 791 $filters[] = " url LIKE '%". $url_filter . "%' "; 776 792 777 793 } 778 794 779 795 $where_clause = ''; 780 796 781 797 foreach ($filters as $key => $filter){ 782 798 783 799 if($key == 0){ 784 800 785 801 $where_clause = ' WHERE '; 786 802 $where_clause .= $filter; 787 803 }else{ 788 804 789 805 $where_clause .= ' AND '; 790 806 $where_clause .= $filter; 791 807 } 792 808 793 809 } 794 810 795 811 $data = $type == 'path' ? 796 812 UnusedCSS_DB::get_links($start, $length, $where_clause): 797 813 UnusedCSS_DB::get_rules($start, $length, $where_clause); 798 814 799 815 wp_send_json([ 800 816 'data' => $data, 801 817 "draw" => (int)$draw, 802 818 "recordsTotal" => $type == 'path' ? UnusedCSS_DB::get_total_job_count() : UnusedCSS_DB::get_total_rule_count(), 803 819 "recordsFiltered" => $type == 'path' ? UnusedCSS_DB::get_total_job_count($where_clause) : UnusedCSS_DB::get_total_rule_count($where_clause), 804 820 "success" => true 805 821 ]); 806 822 } 807 823 808 824 public function enqueueScripts() { 809 825 810 826 $deregister_scripts = apply_filters('uucss/scripts/deregister', ['select2']); 811 827 812 828 if(isset($deregister_scripts) && is_array($deregister_scripts)){ 813 829 foreach ($deregister_scripts as $deregister_script){ 814 830 wp_dequeue_script($deregister_script); 815 831 wp_deregister_script($deregister_script); 816 832 } 817 833 } 818 834 819 835 wp_enqueue_script( 'select2', UUCSS_PLUGIN_URL . 'assets/libs/select2/select2.min.js', array( 'jquery' ) ); 820 836 821 837 wp_enqueue_script( 'datatables', UUCSS_PLUGIN_URL . 'assets/libs/datatables/jquery.dataTables.min.js', array( 822 838 'jquery', 823 839 'uucss_admin' 824 840 ) ); 825 841 wp_enqueue_style( 'datatables', UUCSS_PLUGIN_URL . 'assets/libs/datatables/jquery.dataTables.min.css' ); 826 842 827 843 wp_register_script( 'uucss_admin', UUCSS_PLUGIN_URL . 'assets/js/uucss_admin.js', array( 828 844 'jquery', 829 845 'wp-util' 830 846 ), UUCSS_VERSION ); 831 847 832 848 wp_register_script( 'uucss_log', UUCSS_PLUGIN_URL . 'assets/js/uucss_log.js', array( 833 849 'jquery', 834 850 'wp-util' 835 851 ), UUCSS_VERSION ); 836 852 837 853 $deregister_styles = apply_filters('uucss/styles/deregister',[]); 838 854 839 855 if(isset($deregister_styles) && is_array($deregister_styles)){ 840 856 foreach ($deregister_styles as $deregister_style){ 841 857 wp_dequeue_style($deregister_style); 842 858 } 843 859 } 844 860 845 861 wp_enqueue_style( 'uucss_admin', UUCSS_PLUGIN_URL . 'assets/css/uucss_admin.css', [], UUCSS_VERSION ); 846 862 847 863 global $rapidload; 848 864 849 865 $data = array( 850 866 'api' => RapidLoad_Api::get_key(), 851 867 'nonce' => wp_create_nonce( 'uucss_nonce' ), 852 868 'url' => site_url(), 853 869 'ajax_url' => admin_url( 'admin-ajax.php' ), 854 870 'setting_url' => admin_url( 'options-general.php?page=uucss' ), 855 871 'on_board_complete' => apply_filters('uucss/on-board/complete', false), 856 872 'api_key_verified' => self::is_api_key_verified(), 857 873 'notifications' => $this->getNotifications(), 858 874 'faqs' => [], 859 875 'public_notices' => [], 860 876 'dev_mode' => apply_filters('uucss/dev_mode', isset($this->uucss->options['uucss_dev_mode'])) && $this->uucss->options['uucss_dev_mode'] == "1", 861 877 'rules_enabled' => $rapidload->rules_enabled(), 862 878 'cpcss_enabled' => $rapidload->critical_css_enabled(), 863 879 'home_url' => home_url(), 864 880 'uucss_enable_debug' => ! empty( $this->uucss->options['uucss_enable_debug'] ) && '1' === $this->uucss->options['uucss_enable_debug'], 865 881 ); 866 882 867 883 wp_localize_script( 'uucss_admin', 'uucss', $data ); 868 884 869 885 wp_enqueue_script( 'uucss_admin' ); 870 886 wp_enqueue_script( 'uucss_log' ); 871 887 872 888 wp_enqueue_style( 'select2', UUCSS_PLUGIN_URL . 'assets/libs/select2/select2.min.css' ); 873 889 874 890 } 875 891 876 892 public function getNotifications() { 877 893 878 894 return apply_filters('uucss/notifications', []); 879 895 } 880 896 881 897 public function run_gpsi_test_for_all(){ 882 898 883 899 $links = UnusedCSS_DB::get_links_where(" WHERE status IN('success','rule-based') "); 884 900 885 901 if(!empty($links)){ 886 902 887 903 foreach ($links as $link){ 888 904 889 905 if(isset($link['meta']) && 890 906 isset($link['meta']['stats']) && 891 907 isset($link['meta']['stats']->success_count) && 892 908 $link['meta']['stats']->success_count > 0 || 893 909 isset($link['success_count']) && $link['success_count'] 894 910 ){ 895 911 continue; 896 912 } 897 913 898 914 $this->get_gpsi_test_result($link); 899 915 900 916 } 901 917 902 918 } 903 919 904 920 } 905 921 906 922 public function run_gpsi_status_check_for_all(){ 907 923 924 self::verify_nonce(); 925 908 926 $spawned = wp_schedule_single_event( time() + 5, 'uucss_run_gpsi_test_for_all'); 909 927 910 928 wp_send_json_success([ 911 929 'spawned' => $spawned 912 930 ]); 913 931 } 914 932 915 933 public function get_public_notices(){ 916 934 917 935 $api = new RapidLoad_Api(); 918 936 919 937 $result = $api->get('notification'); 920 938 921 939 $data = !$api->is_error($result) && isset($result->data) ? $result->data : []; 922 940 923 941 $data = array_filter($data, function ($notice){ 924 942 $notice_read = UnusedCSS_Admin::get_site_option('uucss_notice_' . $notice->id . '_read'); 925 943 return empty($notice_read); 926 944 }); 927 945 928 946 $keys = array_keys($data); 929 947 930 948 if(empty($keys)){ 931 949 return $data; 932 950 } 933 951 934 952 $notices = []; 935 953 936 954 foreach ($data as $key => $notice){ 937 955 array_push($notices, $notice); 938 956 } 939 957 940 958 return $notices; 941 959 } 942 960 943 961 public function get_gpsi_test_result($link){ 944 962 945 963 $uucss_api = new RapidLoad_Api(); 946 964 947 965 $cached_files = []; 948 966 $original_files = []; 949 967 950 968 if(isset($link['files']) && !empty($link['files'])){ 951 969 952 970 $cached_files = array_filter($link['files'], function ($file){ 953 971 return !$this->str_contains($file['original'], '//inline-style@'); 954 972 }); 955 973 956 974 $original_files = array_filter($link['files'], function ($file){ 957 975 return !$this->str_contains($file['original'], '//inline-style@'); 958 976 }); 959 977 } 960 978 961 979 do_action( 'uucss/cached', [ 962 980 'url' => $link['url'] 963 981 ]); 964 982 965 983 return $uucss_api->post( 'test/wordpress', 966 984 [ 967 985 'url' => urldecode($link['url']), 968 986 'files' => !empty($cached_files) ? array_column($cached_files, 'uucss') : [], 969 987 'aoFiles' => !empty($original_files) ? array_column($original_files, 'original') : [] 970 988 ]); 971 989 972 990 } 973 991 974 992 public function uucss_test_url(){ 993 994 self::verify_nonce(); 975 995 976 996 global $uucss; 977 997 978 998 if(!isset($_REQUEST['url'])){ 979 999 wp_send_json_error('url required'); 980 1000 } 981 1001 982 1002 $url = $_REQUEST['url']; 983 1003 $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'path'; 984 1004 985 1005 if($type == 'rule'){ 986 1006 987 1007 if(!isset($_REQUEST['rule']) || !isset($_REQUEST['regex'])){ 988 1008 wp_send_json_error('rule and regex required'); 989 1009 } 990 1010 991 1011 } 992 1012 993 1013 $uucss_api = new RapidLoad_Api(); 994 1014 995 1015 $link = $type == 'path' ? UnusedCSS_DB::get_link($url) : UnusedCSS_DB::get_rule($_REQUEST['rule'],$_REQUEST['regex']); 996 1016 997 1017 $result = $this->get_gpsi_test_result($link); 998 1018 999 1019 if ( $uucss_api->is_error( $result ) ) { 1000 1020 if(isset($result->errors) && isset($result->errors[0])){ 1001 1021 wp_send_json_error($result->errors[0]->detail); 1002 1022 }else{ 1003 1023 wp_send_json_error($result); 1004 1024 } 1005 1025 } 1006 1026 1007 1027 wp_send_json_success($result); 1008 1028 } 1009 1029 1010 1030 public function get_faqs(){ 1011 1031 1012 1032 $rapidload_faqs_read = self::get_site_option('rapidload_faqs_read'); 1013 1033 1014 1034 if(!empty($rapidload_faqs_read)){ 1015 1035 return []; 1016 1036 } 1017 1037 1018 1038 $api = new RapidLoad_Api(); 1019 1039 1020 1040 $result = $api->get('faqs'); 1021 1041 1022 1042 $default = [ 1023 1043 [ 1024 1044 "title" => "I enabled RapidLoad and now my site is broken. What do I do?", 1025 1045 "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>", 1026 1046 ], 1027 1047 [ 1028 1048 "title" => "Why am I still seeing the “Removed unused CSS” flag in Google Page Speed Insights?", 1029 1049 "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.", 1030 1050 ], 1031 1051 [ 1032 1052 "title" => "Will this plugin work with other caching plugins?", 1033 1053 "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.", 1034 1054 ], 1035 1055 [ 1036 1056 "title" => "Do I need to run this every time I make a change?", 1037 1057 "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!", 1038 1058 ], 1039 1059 [ 1040 1060 "title" => "Do you offer support if I need it?", 1041 1061 "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.", 1042 1062 ] 1043 1063 ]; 1044 1064 1045 1065 return !$api->is_error($result) && isset($result->data) ? $result->data : $default; 1046 1066 } 1047 1067 1048 1068 public function clear_uucss_logs(){ 1069 1070 self::verify_nonce(); 1071 1049 1072 $file_system = new RapidLoad_FileSystem(); 1050 1073 1051 1074 if(!$file_system->exists(WP_CONTENT_DIR . '/uploads/rapidload/')){ 1052 1075 wp_send_json_success(true); 1053 1076 } 1054 1077 1055 1078 $file_system->delete_folder(WP_CONTENT_DIR . '/uploads/rapidload/'); 1056 1079 wp_send_json_success(true); 1057 1080 } 1058 1081 1059 1082 public function uucss_logs(){ 1060 1083 1084 self::verify_nonce(); 1085 1061 1086 $file_system = new RapidLoad_FileSystem(); 1062 1087 1063 1088 if(!$file_system->exists(UUCSS_LOG_DIR . 'debug.log')){ 1064 1089 wp_send_json_success([]); 1065 1090 } 1066 1091 1067 1092 $data = $file_system->get_contents(UUCSS_LOG_DIR . 'debug.log'); 1068 1093 1069 1094 if(empty($data)){ 1070 1095 wp_send_json_success([]); 1071 1096 } 1072 1097 1073 1098 $data = '[' . $data . ']'; 1074 1099 1075 1100 wp_send_json_success(json_decode($data)); 1076 1101 } 1077 1102 1078 1103 public function frontend_logs(){ 1104 1105 self::verify_nonce(); 1079 1106 1080 1107 $args = []; 1081 1108 1082 1109 $args['type'] = isset($_REQUEST['type']) && !empty($_REQUEST['type']) ? $_REQUEST['type'] : 'frontend'; 1083 1110 $args['log'] = isset($_REQUEST['log']) && !empty($_REQUEST['log']) ? $_REQUEST['log'] : ''; 1084 1111 $args['url'] = isset($_REQUEST['url']) && !empty($_REQUEST['url']) ? $_REQUEST['url'] : ''; 1085 1112 1086 1113 self::log($args); 1087 1114 1088 1115 wp_send_json_success(true); 1089 1116 } 1090 1117 1091 1118 public function mark_faqs_read(){ 1092 1119 1120 self::verify_nonce(); 1121 1093 1122 self::update_site_option('rapidload_faqs_read', true); 1094 1123 wp_send_json_success(true); 1095 1124 } 1096 1125 1097 1126 public function mark_notice_read(){ 1098 1127 1099 1128 $notice_id = isset($_REQUEST['notice_id']) ? $_REQUEST['notice_id'] : false; 1100 1129 1101 1130 if($notice_id){ 1102 1131 self::update_site_option('uucss_notice_' . $notice_id . '_read', true); 1103 1132 } 1104 1133 1105 1134 wp_send_json_success(true); 1106 1135 } 1107 1136 1108 1137 public function clear_page_cache(){ 1138 1139 self::verify_nonce(); 1109 1140 1110 1141 $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false; 1111 1142 $rule = isset($_REQUEST['rule']) ? $_REQUEST['rule'] : false; 1112 1143 $regex = isset($_REQUEST['regex']) ? $_REQUEST['regex'] : false; 1113 1144 1114 1145 $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : false; 1115 1146 1116 1147 $type = isset($_REQUEST['type']) ? $_REQUEST['status'] : 'path'; 1117 1148 1118 1149 if($url){ 1119 1150 1120 1151 UnusedCSS_DB::reset_hits($url); 1121 1152 do_action( 'uucss/cached', [ 1122 1153 'url' => $url 1123 1154 ] ); 1124 1155 } 1125 1156 1126 1157 $links = false; 1127 1158 1128 1159 if($rule && $regex){ 1129 1160 1130 1161 $rule = UnusedCSS_DB::get_rule($rule, $regex); 1131 1162 1132 1163 if(isset($rule['id'])){ 1133 1164 1134 1165 $links = UnusedCSS_DB::get_links_where(" WHERE rule_id = " . $rule['id']); 1135 1166 1136 1167 } 1137 1168 } 1138 1169 1139 1170 if($status){ 1140 1171 1141 1172 UnusedCSS_DB::reset_hits(); 1142 1173 1143 1174 if($type == 'path'){ 1144 1175 1145 1176 $links = UnusedCSS_DB::get_links_where(' '); 1146 1177 1147 1178 }else{ 1148 1179 1149 1180 $links = UnusedCSS_DB::get_rules_where(' '); 1150 1181 1151 1182 } 1152 1183 1153 1184 } 1154 1185 1155 1186 if($links && !empty($links)){ 1156 1187 1157 1188 foreach ($links as $link){ 1158 1189 1159 1190 if(isset($link['url'])){ 1160 1191 self::uucss_log($link['url']); 1161 1192 do_action( 'uucss/cached', [ 1162 1193 'url' => $link['url'] 1163 1194 ] ); 1164 1195 } 1165 1196 } 1166 1197 } 1167 1198 1168 1199 wp_send_json_success('page cache cleared'); 1169 1200 } 1170 1201 1171 1202 public static function is_api_key_verified() { 1172 1203 1173 1204 $api_key_status = isset( RapidLoad_Base::fetch_options()['uucss_api_key_verified'] ) ? RapidLoad_Base::fetch_options()['uucss_api_key_verified'] : ''; 1174 1205 1175 1206 return $api_key_status == '1'; 1176 1207 1177 1208 } 1178 1209 1179 1210 public function add_plugin_action_link( $links ) { 1180 1211 1181 1212 $_links = array( 1182 1213 '<a href="' . admin_url( 'options-general.php?page=uucss' ) . '">Settings</a>', 1183 1214 ); 1184 1215 1185 1216 return array_merge( $_links, $links ); 1186 1217 } 1187 1218 1188 1219 public function add_meta_boxes() 1189 1220 { 1190 1221 add_meta_box( 1191 1222 'uucss-options', 1192 1223 __( 'RapidLoad Options', 'uucss' ), 1193 1224 [$this, 'meta_box'], 1194 1225 get_post_types(), 1195 1226 'side' 1196 1227 ); 1197 1228 } 1198 1229 1199 1230 public function meta_box( $post ) { 1200 1231 1201 1232 $options = RapidLoad_Base::get_page_options($post->ID); 1202 1233 1203 1234 include('parts/admin-post.html.php'); 1204 1235 } 1205 1236 1206 1237 public function save_meta_box_options($post_id, $post) 1207 1238 { 1208 1239 if ( !isset( $_POST['uucss_nonce'] ) || !wp_verify_nonce( $_POST['uucss_nonce'], 'uucss_option_save' ) ) { 1209 1240 return; 1210 1241 } 1211 1242 1212 1243 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 1213 1244 return; 1214 1245 } 1215 1246 1216 1247 $this->update_meta($post_id); 1217 1248 1218 1249 } 1219 1250 1220 1251 public function cache_trigger_hooks() { 1221 1252 add_action( 'save_post', [ $this, 'cache_on_actions' ], 110, 3 ); 1222 1253 add_action( 'untrash_post', [ $this, 'cache_on_actions' ], 10, 1 ); 1223 1254 add_action( 'wp_trash_post', [ $this, 'clear_on_actions' ], 10, 1 ); 1224 1255 add_action( "wp_ajax_uucss_purge_url", [ $this, 'ajax_purge_url' ] ); 1225 1256 } 1226 1257 1227 1258 public static function suggest_whitelist_packs() { 1228 1259 1260 self::verify_nonce(); 1261 1229 1262 if ( ! function_exists( 'get_plugins' ) ) { 1230 1263 require_once ABSPATH . 'wp-admin/includes/plugin.php'; 1231 1264 } 1232 1265 1233 1266 $plugins = get_plugins(); 1234 1267 $active_plugins = array_map( function ( $key, $item ) { 1235 1268 1236 1269 $item['slug'] = $key; 1237 1270 1238 1271 return $item; 1239 1272 }, array_keys( $plugins ), $plugins ); 1240 1273 1241 1274 $api = new RapidLoad_Api(); 1242 1275 1243 1276 $data = $api->post( 'whitelist-packs/wp-suggest', [ 1244 1277 'plugins' => $active_plugins, 1245 1278 'theme' => get_template(), 1246 1279 'url' => site_url() 1247 1280 ] ); 1248 1281 1249 1282 if ( wp_doing_ajax() ) { 1250 1283 wp_send_json_success( $data->data ); 1251 1284 } 1252 1285 1253 1286 return isset($data) && is_array($data) ? $data : []; 1254 1287 } 1255 1288 1256 1289 public function uucss_license() { 1290 1291 self::verify_nonce(); 1257 1292 1258 1293 $api = new RapidLoad_Api(); 1259 1294 1260 1295 $data = $api->get( 'license', [ 1261 1296 'url' => $this->transform_url(get_site_url()), 1262 1297 'version' => UUCSS_VERSION, 1263 1298 'db_version' => RapidLoad_DB::$db_version, 1264 1299 'db_version_exist' => RapidLoad_DB::$current_version 1265 1300 ] ); 1266 1301 1267 1302 if ( ! is_wp_error( $data ) ) { 1268 1303 1269 1304 if ( isset( $data->errors ) ) { 1270 1305 wp_send_json_error( $data->errors[0]->detail ); 1271 1306 } 1272 1307 1273 1308 if ( gettype( $data ) === 'string' ) { 1274 1309 wp_send_json_error( $data ); 1275 1310 } 1276 1311 1277 1312 do_action( 'uucss/license-verified' ); 1278 1313 1279 1314 wp_send_json_success( $data->data ); 1280 1315 } 1281 1316 1282 1317 wp_send_json_error( 'unknown error occurred' ); 1283 1318 } 1284 1319 1285 1320 public function verify_api_key() { 1321 1322 self::verify_nonce(); 1286 1323 1287 1324 if ( ! isset( $_POST['api_key'] ) ) { 1288 1325 wp_send_json_error(); 1289 1326 1290 1327 return; 1291 1328 } 1292 1329 1293 1330 $uucss_api = new RapidLoad_Api(); 1294 1331 $uucss_api->apiKey = sanitize_text_field( $_POST['api_key'] ); 1295 1332 1296 1333 $results = $uucss_api->get( 'verify' ); 1297 1334 1298 1335 if ( isset( $results->data ) ) { 1299 1336 wp_send_json_success( true ); 1300 1337 } 1301 1338 1302 1339 wp_send_json_error(); 1303 1340 1304 1341 } 1305 1342 1306 1343 public function ajax_purge_url() { 1307 1344 1308 1345 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uucss_nonce' ) ) { 1309 1346 wp_send_json_error( 'authentication failed' ); 1310 1347 1311 1348 return; 1312 1349 } 1313 1350 1314 1351 $args = isset($_POST['args']) ? $_POST['args'] : []; 1315 1352 1316 1353 if ( ! isset( $_POST['url'] ) ) { 1317 1354 wp_send_json_error(); 1318 1355 1319 1356 return; 1320 1357 } 1321 1358 1322 1359 if ( isset( $_POST['args'] ) ) { 1323 1360 $args['post_id'] = ( isset( $_POST['args']['post_id'] ) ) ? intval( $_POST['args']['post_id'] ) : null; 1324 1361 } 1325 1362 1326 1363 $url = esc_url_raw( $_POST['url'] ); 1327 1364 1328 1365 if(isset($args['rule_id'])){ 1329 1366 $rule = UnusedCSS_Rule::get_rule_from_id($args['rule_id']); 1330 1367 if($rule){ 1331 1368 $url = $rule->url; 1332 1369 $args['rule'] = $rule->rule; 1333 1370 $args['regex'] = $rule->regex; 1334 1371 } 1335 1372 } 1336 1373 1337 1374 if ( isset( $_POST['clear'] ) && boolval($_POST['clear'] == 'true') ) { 1338 1375 $list = isset($_POST['url_list']) ? $_POST['url_list'] : null; 1339 1376 1340 1377 if(isset($list) && is_array($list) && !empty($list)){ 1341 1378 foreach ($list as $item){ 1342 1379 1343 1380 $url = is_array($item) && isset($item['url']) ? $item['url'] : $item; 1344 1381 1345 1382 if(is_array($item) && isset($item['rule'])){ 1346 1383 $args['rule'] = $item['rule']; 1347 1384 } 1348 1385 1349 1386 if(is_array($item) && isset($item['regex'])){ 1350 1387 $args['regex'] = $item['regex']; 1351 1388 } 1352 1389 1353 1390 $this->uucss->clear_cache( $url, $args ); 1354 1391 } 1355 1392 }else{ 1356 1393 $this->uucss->clear_cache( $url, $args ); 1357 1394 } 1358 1395 1359 1396 wp_send_json_success( true ); 1360 1397 return; 1361 1398 } 1362 1399 1363 1400 if ( isset( $args["post_id"] ) ) { 1364 1401 $args['options'] = $this->uucss->api_options( $args["post_id"] ); 1365 1402 } 1366 1403 1367 1404 $args['immediate'] = true; 1368 1405 $args['priority'] = true; 1369 1406 1370 1407 wp_send_json_success( $this->uucss->cache( $url, $args ) ); 1371 1408 } 1372 1409 1373 1410 /** 1374 1411 * @param $post_id 1375 1412 * @param $post WP_Post 1376 1413 * @param $update 1377 1414 */ 1378 1415 public function cache_on_actions($post_id, $post = null, $update = null) 1379 1416 { 1380 1417 1381 1418 $post = get_post($post_id); 1382 1419 1383 1420 if($post->post_status == "publish") { 1384 1421 1385 1422 $this->clear_on_actions( $post->ID ); 1386 1423 1387 1424 $url = get_permalink( $post ); 1388 1425 1389 1426 if(UnusedCSS_DB::link_exists_with_error($url) || !RapidLoad_Base::get()->rules_enabled()){ 1390 1427 $this->uucss->cache( $url ); 1391 1428 } 1392 1429 1393 1430 } 1394 1431 } 1395 1432 1396 1433 public function clear_on_actions($post_ID) 1397 1434 { 1398 1435 $link = get_permalink($post_ID); 1399 1436 1400 1437 if($link){ 1401 1438 $this->uucss->clear_cache($link); 1402 1439 } 1403 1440 } 1404 1441 1405 1442 public function update_meta($post_id) 1406 1443 { 1407 1444 foreach (self::$page_options as $option) { 1408 1445 1409 1446 if ( ! isset( $_POST[ 'uucss_' . $option ] ) ) { 1410 1447 delete_post_meta( $post_id, '_uucss_' . $option ); 1411 1448 continue; 1412 1449 } 1413 1450 1414 1451 $value = sanitize_text_field( $_POST[ 'uucss_' . $option ] ); 1415 1452 1416 1453 update_post_meta( $post_id, '_uucss_' . $option, $value ); 1417 1454 } 1418 1455 } 1419 1456 1420 1457 public static function get_site_option($name) 1421 1458 { 1422 1459 if(is_multisite()){ 1423 1460 1424 1461 return get_blog_option(get_current_blog_id(), $name, false); 1425 1462 1426 1463 } 1427 1464 return get_site_option( $name, false ); 1428 1465 } 1429 1466 1430 1467 public static function update_site_option($name, $value){ 1431 1468 1432 1469 if(is_multisite()){ 1433 1470 1434 1471 return update_blog_option(get_current_blog_id(), $name, $value); 1435 1472 1436 1473 } 1437 1474 return update_site_option($name, $value); 1438 1475 } 1439 1476 1440 1477 public static function delete_site_option($name){ 1441 1478 1442 1479 if(is_multisite()){ 1443 1480 1444 1481 return delete_blog_option(get_current_blog_id(), $name); 1445 1482 1446 1483 } 1447 1484 return delete_site_option($name); 1448 1485 } 1449 1486 1450 1487 public static function first_job_done(){ 1451 1488 return (RapidLoad_Settings::get_first_link() ? true : false); 1452 1489 } 1453 1490 }
Note: See TracChangeset
for help on using the changeset viewer.