ettings'] ) { return $value; } if ( 0 === (int) $value['cloudflare_auto_settings'] ) { return $value; } $cloudflare_zone_id = key_exists( 'cloudflare_zone_id', $value ) ? $value['cloudflare_zone_id'] : ''; if ( is_wp_error( $this->cloudflare->check_connection( $cloudflare_zone_id ) ) ) { return $value; } $cf_settings = $this->cloudflare->get_settings(); $value['cloudflare_old_settings'] = ! is_wp_error( $cf_settings ) ? implode( ',', array_filter( $cf_settings ) ) : ''; return $value; } /** * Change the authentification. * * @param array $value An array of previous values for the settings. * @param array $old_value An array of submitted values for the settings. * * @return mixed */ public function change_auth( $value, $old_value ) { $auth = $this->auth_factory->create( $value ); $this->cloudflare->change_auth( $auth ); return $value; } /** * Delete the transient CF connection status when API Key, Email or Zone ID is changed * * @param array $value An array of previous values for the settings. * @param array $old_value An array of submitted values for the settings. * * @return array */ public function delete_connection_transient( $value, $old_value ) { $fields = [ 'cloudflare_api_key', 'cloudflare_email', 'cloudflare_zone_id', 'cloudflare_devmode', 'cloudflare_auto_settings', 'cloudflare_protocol_rewrite', ]; $change = false; foreach ( $fields as $field ) { $change |= ! isset( $old_value[ $field ], $value[ $field ] ) || $old_value[ $field ] !== $value[ $field ]; } if ( ! $change ) { return $value; } delete_transient( get_current_user_id() . '_cloudflare_update_settings' ); delete_transient( 'rocket_cloudflare_is_api_keys_valid' ); return $value; } /** * Display the error notice. * * @param array $value An array of previous values for the settings. * @param array $old_value An array of submitted values for the settings. * * @return mixed */ public function display_settings_notice( $value, $old_value ) { if ( ! key_exists( 'cloudflare_zone_id', $value ) ) { return $value; } $connection = $this->cloudflare->check_connection( $value['cloudflare_zone_id'] ); if ( is_wp_error( $connection ) ) { add_settings_error( 'general', 'cloudflare_api_key_invalid', __( 'WP Rocket: ', 'rocket' ) . '' . $connection->get_error_message() . '', 'error' ); } return $value; } /** * Remove HTTP protocol on script, link, img and form tags. * * @param string $buffer HTML content. * * @return string */ public function protocol_rewrite( $buffer ) { if ( ! $this->can_protocol_rewrite() ) { return $buffer; } $return = preg_replace( "/(<(script|link|img|form)(?!.*?[\"']\bcanonical\b[\"'])([^>]*)(href|src|action)=[\"'])https?:\\/\\//i", '$1//', $buffer ); if ( $return ) { $buffer = $return; } return $buffer; } /** * Remove HTTP protocol on srcset attribute generated by WordPress * * @param array $sources an Array of images sources for srcset. * * @return array */ public function protocol_rewrite_srcset( $sources ) { if ( ! $this->can_protocol_rewrite() ) { return $sources; } if ( empty( $sources ) ) { return $sources; } foreach ( $sources as $i => $source ) { $sources[ $i ]['url'] = str_replace( [ 'http:', 'https:' ], '', $source['url'] ); } return $sources; } /** * Can rewrite protocol * * @return bool */ private function can_protocol_rewrite(): bool { return $this->options->get( 'do_cloudflare', 0 ) && ( $this->options->get( 'cloudflare_protocol_rewrite', 0 ) || apply_filters( 'do_rocket_protocol_rewrite', false ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound ); } /** * Add the helper message on the CDN settings. * * @param string[] $addons Name from the addon that requires the helper message. * @return string[] */ public function add_cdn_helper_message( array $addons ): array { $addons[] = 'Cloudflare'; return $addons; } }