Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wp-smush.../core/cdn
File: class-cdn-controller.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Smush\Core\CDN;
[2] Fix | Delete
[3] Fix | Delete
use Smush\Core\Controller;
[4] Fix | Delete
use Smush\Core\Helper;
[5] Fix | Delete
use Smush\Core\Settings;
[6] Fix | Delete
use WP_Error;
[7] Fix | Delete
use WP_Smush;
[8] Fix | Delete
[9] Fix | Delete
class CDN_Controller extends Controller {
[10] Fix | Delete
const CDN_TRANSFORM_PRIORITY = 10;
[11] Fix | Delete
/**
[12] Fix | Delete
* @var CDN_Helper
[13] Fix | Delete
*/
[14] Fix | Delete
private $cdn_helper;
[15] Fix | Delete
/**
[16] Fix | Delete
* @var Settings|null
[17] Fix | Delete
*/
[18] Fix | Delete
private $settings;
[19] Fix | Delete
/**
[20] Fix | Delete
* Static instance
[21] Fix | Delete
*
[22] Fix | Delete
* @var self
[23] Fix | Delete
*/
[24] Fix | Delete
private static $instance;
[25] Fix | Delete
[26] Fix | Delete
public function __construct() {
[27] Fix | Delete
$this->cdn_helper = CDN_Helper::get_instance();
[28] Fix | Delete
$this->settings = Settings::get_instance();
[29] Fix | Delete
[30] Fix | Delete
$this->register_filter( 'wp_smush_content_transforms', array(
[31] Fix | Delete
$this,
[32] Fix | Delete
'register_cdn_transform',
[33] Fix | Delete
), self::CDN_TRANSFORM_PRIORITY );
[34] Fix | Delete
$this->register_action( 'wp_ajax_get_cdn_stats', array( $this, 'ajax_update_stats' ) );
[35] Fix | Delete
$this->register_action( 'smush_update_cdn_stats', array( $this, 'cron_update_stats' ) );
[36] Fix | Delete
$this->register_action( 'wp_ajax_smush_toggle_cdn', array( $this, 'ajax_toggle_cdn' ) );
[37] Fix | Delete
$this->register_filter( 'wp_resource_hints', array( $this, 'dns_prefetch' ), 99, 2 );
[38] Fix | Delete
[39] Fix | Delete
if ( $this->cdn_helper->is_cdn_active() ) {
[40] Fix | Delete
$this->register_action( 'admin_init', array( $this, 'schedule_cron' ) );
[41] Fix | Delete
}
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Static instance getter
[46] Fix | Delete
*/
[47] Fix | Delete
public static function get_instance() {
[48] Fix | Delete
if ( empty( self::$instance ) ) {
[49] Fix | Delete
self::$instance = new self();
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
return self::$instance;
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
public function ajax_update_stats() {
[56] Fix | Delete
$status = $this->cdn_helper->get_cdn_status_setting();
[57] Fix | Delete
$smush = WP_Smush::get_instance();
[58] Fix | Delete
if ( isset( $status->cdn_enabling ) && $status->cdn_enabling ) {
[59] Fix | Delete
$new_status = $this->process_cdn_status_response( $smush->api()->enable() );
[60] Fix | Delete
[61] Fix | Delete
if ( is_wp_error( $new_status ) ) {
[62] Fix | Delete
$code = is_numeric( $new_status->get_error_code() ) ? $new_status->get_error_code() : null;
[63] Fix | Delete
wp_send_json_error( array(
[64] Fix | Delete
'message' => $new_status->get_error_message(),
[65] Fix | Delete
), $code );
[66] Fix | Delete
} else {
[67] Fix | Delete
$this->settings->set_setting( 'wp-smush-cdn_status', $new_status );
[68] Fix | Delete
wp_send_json_success( $new_status );
[69] Fix | Delete
}
[70] Fix | Delete
} else {
[71] Fix | Delete
wp_send_json_success( $status );
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
public function cron_update_stats() {
[76] Fix | Delete
$status = $this->cdn_helper->get_cdn_status_setting();
[77] Fix | Delete
$smush = WP_Smush::get_instance();
[78] Fix | Delete
$cdn_enabling = isset( $status->cdn_enabling ) && $status->cdn_enabling;
[79] Fix | Delete
$raw_api_response = $cdn_enabling ? $smush->api()->enable() : $smush->api()->check();
[80] Fix | Delete
$new_status = $this->process_cdn_status_response( $raw_api_response );
[81] Fix | Delete
[82] Fix | Delete
if ( $new_status && ! is_wp_error( $new_status ) ) {
[83] Fix | Delete
$this->settings->set_setting( 'wp-smush-cdn_status', $new_status );
[84] Fix | Delete
}
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
public function process_cdn_status_response( $status ) {
[88] Fix | Delete
if ( is_wp_error( $status ) ) {
[89] Fix | Delete
return $status;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$status = json_decode( $status['body'] );
[93] Fix | Delete
[94] Fix | Delete
// Too many requests.
[95] Fix | Delete
if ( is_null( $status ) ) {
[96] Fix | Delete
return new WP_Error( 'too_many_requests', __( 'Too many requests, please try again in a moment.', 'wp-smushit' ) );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
// Some other error from API.
[100] Fix | Delete
if ( ! $status->success ) {
[101] Fix | Delete
return new WP_Error( $status->data->error_code, $status->data->message );
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
return $status->data;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
public function schedule_cron() {
[108] Fix | Delete
if ( ! wp_next_scheduled( 'smush_update_cdn_stats' ) ) {
[109] Fix | Delete
// Schedule first run for next day, as we've already checked just now.
[110] Fix | Delete
wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'smush_update_cdn_stats' );
[111] Fix | Delete
}
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
public static function unschedule_cron() {
[115] Fix | Delete
$timestamp = wp_next_scheduled( 'smush_update_cdn_stats' );
[116] Fix | Delete
wp_unschedule_event( $timestamp, 'smush_update_cdn_stats' );
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
public function toggle_cdn( $enable ) {
[120] Fix | Delete
$this->settings->set( 'cdn', $enable );
[121] Fix | Delete
[122] Fix | Delete
if ( $enable ) {
[123] Fix | Delete
$smush = WP_Smush::get_instance();
[124] Fix | Delete
$status = $this->cdn_helper->get_cdn_status_setting();
[125] Fix | Delete
if ( ! $status ) {
[126] Fix | Delete
$check_response = $this->process_cdn_status_response( $smush->api()->check() );
[127] Fix | Delete
if ( is_wp_error( $check_response ) ) {
[128] Fix | Delete
return $check_response;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
$this->settings->set_setting( 'wp-smush-cdn_status', $check_response );
[132] Fix | Delete
} elseif ( empty( $status->endpoint_url ) ) {
[133] Fix | Delete
$enable_response = $this->process_cdn_status_response( $smush->api()->enable( true ) );
[134] Fix | Delete
if ( is_wp_error( $enable_response ) ) {
[135] Fix | Delete
return $enable_response;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
$this->settings->set_setting( 'wp-smush-cdn_status', $enable_response );
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
$this->schedule_cron();
[142] Fix | Delete
} else {
[143] Fix | Delete
// Remove CDN settings if disabling.
[144] Fix | Delete
$this->settings->delete_setting( 'wp-smush-cdn_status' );
[145] Fix | Delete
[146] Fix | Delete
self::unschedule_cron();
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
do_action( 'wp_smush_cdn_status_changed' );
[150] Fix | Delete
[151] Fix | Delete
return true;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
public function ajax_toggle_cdn() {
[155] Fix | Delete
check_ajax_referer( 'save_wp_smush_options' );
[156] Fix | Delete
[157] Fix | Delete
if ( ! Helper::is_user_allowed() ) {
[158] Fix | Delete
wp_send_json_error( array(
[159] Fix | Delete
'message' => __( 'User can not modify options', 'wp-smushit' ),
[160] Fix | Delete
), 403 );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
$enable = filter_input( INPUT_POST, 'param', FILTER_VALIDATE_BOOLEAN );
[164] Fix | Delete
$toggled = $this->toggle_cdn( $enable );
[165] Fix | Delete
[166] Fix | Delete
if ( is_wp_error( $toggled ) ) {
[167] Fix | Delete
wp_send_json_error( array(
[168] Fix | Delete
'message' => $toggled->get_error_message(),
[169] Fix | Delete
) );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
wp_send_json_success();
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
public function register_cdn_transform( $transforms ) {
[176] Fix | Delete
$transforms['cdn'] = new CDN_Transform();
[177] Fix | Delete
[178] Fix | Delete
return $transforms;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Add CDN url to header for better speed.
[183] Fix | Delete
*
[184] Fix | Delete
* @param array $urls URLs to print for resource hints.
[185] Fix | Delete
* @param string $relation_type The relation type the URLs are printed.
[186] Fix | Delete
*
[187] Fix | Delete
* @return array
[188] Fix | Delete
* @since 3.0
[189] Fix | Delete
*
[190] Fix | Delete
*/
[191] Fix | Delete
public function dns_prefetch( $urls, $relation_type ) {
[192] Fix | Delete
// Add only if CDN active.
[193] Fix | Delete
if ( 'dns-prefetch' === $relation_type && $this->cdn_helper->is_cdn_active() && ! empty( $this->cdn_helper->get_cdn_base_url() ) ) {
[194] Fix | Delete
$urls[] = $this->cdn_helper->get_cdn_base_url();
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
return $urls;
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function