if ( ! $status ) { $check_response = $this->process_cdn_status_response( $smush->api()->check() ); if ( is_wp_error( $check_response ) ) { return $check_response; } $this->settings->set_setting( 'wp-smush-cdn_status', $check_response ); } elseif ( empty( $status->endpoint_url ) ) { $enable_response = $this->process_cdn_status_response( $smush->api()->enable( true ) ); if ( is_wp_error( $enable_response ) ) { return $enable_response; } $this->settings->set_setting( 'wp-smush-cdn_status', $enable_response ); } } else { // Remove CDN settings if disabling. $this->settings->delete_setting( 'wp-smush-cdn_status' ); } do_action( 'wp_smush_cdn_status_changed' ); return true; } public function ajax_toggle_cdn() { check_ajax_referer( 'save_wp_smush_options' ); if ( ! Helper::is_user_allowed() ) { wp_send_json_error( array( 'message' => __( 'User can not modify options', 'wp-smushit' ), ), 403 ); } $enable = filter_input( INPUT_POST, 'param', FILTER_VALIDATE_BOOLEAN ); $toggled = $this->toggle_cdn( $enable ); if ( is_wp_error( $toggled ) ) { wp_send_json_error( array( 'message' => $toggled->get_error_message(), ) ); } wp_send_json_success(); } public function register_cdn_transform( $transforms ) { $transforms['cdn'] = new CDN_Transform(); return $transforms; } /** * Add CDN url to header for better speed. * * @param array $urls URLs to print for resource hints. * @param string $relation_type The relation type the URLs are printed. * * @return array * @since 3.0 * */ public function dns_prefetch( $urls, $relation_type ) { // Add only if CDN active. if ( 'dns-prefetch' === $relation_type && $this->cdn_helper->is_cdn_active() && ! empty( $this->cdn_helper->get_cdn_base_url() ) ) { $urls[] = $this->cdn_helper->get_cdn_base_url(); } return $urls; } public function add_lcp_allowed_hostname( $hostnames ) { $cdn_base_url = $this->cdn_helper->get_cdn_base_url(); if ( ! empty( $cdn_base_url ) ) { $cdn_hostname = parse_url( $cdn_base_url, PHP_URL_HOST ); if ( ! in_array( $cdn_hostname, $hostnames, true ) ) { $hostnames[] = $cdn_hostname; } } return $hostnames; } public function find_image_dimensions_for_cdn_url( $actual_dimensions, $maybe_cdn_url ) { $dimensions = $actual_dimensions; if ( $this->cdn_helper->is_cdn_url( $maybe_cdn_url ) ) { $query_string = wp_parse_url( $maybe_cdn_url, PHP_URL_QUERY ); parse_str( $query_string, $query_params ); if ( ! empty( $query_params['size'] ) ) { $size_parts = explode( 'x', $query_params['size'] ); if ( $size_parts && count( $size_parts ) === 2 ) { $dimensions = array( (int) $size_parts[0], (int) $size_parts[1] ); } } } return $dimensions; } public function return_original_url_from_image_dimensions( $image_url ) { $original_url = false; if ( $this->cdn_helper->is_cdn_url( $image_url ) ) { $original_url = $this->cdn_helper->get_original_url( $image_url ); } return $original_url ?: $image_url; } }