Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent PHP 8.1 deprecation notices #4732

Merged
merged 9 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion inc/3rd-party/3rd-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/mobile/wp-appkit.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/seopress.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/rank-math-seo.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/yoast-seo.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/the-seo-framework.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/all-in-one-seo-pack.php';
require WP_ROCKET_3RD_PARTY_PATH . 'plugins/seo/premium-seo-pack.php';
Expand Down
99 changes: 0 additions & 99 deletions inc/3rd-party/plugins/seo/yoast-seo.php

This file was deleted.

1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private function init_common_subscribers() {
'ezoic',
'thirstyaffiliates',
'pwa',
'yoast_seo',
'flatsome',
];

Expand Down
170 changes: 170 additions & 0 deletions inc/ThirdParty/Plugins/SEO/Yoast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\ThirdParty\Plugins\SEO;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Settings\Settings;
use WP_Rocket\Event_Management\Subscriber_Interface;

class Yoast implements Subscriber_Interface {
/**
* Options Data instance
*
* @var Options_Data
*/
private $options;

/**
* Constructor
*
* @param Options_Data $options Options_Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}

/**
* Array of events this subscriber listens to
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_sitemap_preload_options' => 'add_settings_field',
'rocket_sitemap_preload_list' => 'add_sitemap',
'rocket_input_sanitize' => [ 'sanitize_option', 10, 2 ],
'rocket_first_install_options' => 'add_option',
];
}

/**
* Add Yoast SEO option to WP Rocket settings
*
* @since 2.8
*
* @param array $options WP Rocket settings array.
*
* @return array
*/
public function add_settings_field( $options ): array {
if ( ! $this->is_sitemap_enabled() ) {
return $options;
}

$options['yoast_xml_sitemap'] = [
'type' => 'checkbox',
'container_class' => [
'wpr-field--children',
],
'label' => __( 'Yoast SEO XML sitemap', 'rocket' ),
// translators: %s = Name of the plugin.
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'Yoast SEO' ),
'parent' => 'sitemap_preload',
'section' => 'preload_section',
'page' => 'preload',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
];

return $options;
}

/**
* Add Yoast SEO sitemap URL to the sitemaps to preload
*
* @since 2.8
*
* @param array $sitemaps An array of sitemaps to preload.
*
* @return array
*/
public function add_sitemap( $sitemaps ): array {
if ( ! $this->is_sitemap_enabled() ) {
return $sitemaps;
}

if ( ! $this->options->get( 'yoast_xml_sitemap', 0 ) ) {
return $sitemaps;
}

if ( ! class_exists( 'WPSEO_Sitemaps_Router' ) ) {
return $sitemaps;
}

$sitemaps[] = \WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' );

return $sitemaps;
}

/**
* Sanitize Yoast SEO sitemap option value
*
* @since 2.8
*
* @param array $input Array of values submitted from the form.
* @param Settings $settings Settings class instance.
*
* @return array
*/
public function sanitize_option( $input, Settings $settings ): array {
if ( ! $this->is_sitemap_enabled() ) {
return $input;
}

$input['yoast_xml_sitemap'] = $settings->sanitize_checkbox( $input, 'yoast_xml_sitemap' );

return $input;
}

/**
* Add Yoast SEO sitemap option to WP Rocket default options
*
* @since 2.8
*
* @param array $options WP Rocket options array.
*
* @return array
*/
public function add_option( $options ): array {
if ( ! $this->is_sitemap_enabled() ) {
return $options;
}

$options['yoast_xml_sitemap'] = 0;

return $options;
}

/**
* Checks if sitemap is enabled in Yoast SEO
*
* @since 3.11.1
*
* @return bool
*/
private function is_sitemap_enabled(): bool {
static $enabled = null;

if ( ! is_null( $enabled ) ) {
return $enabled;
}

if ( ! rocket_has_constant( 'WPSEO_VERSION' ) ) {
$enabled = false;

return $enabled;
}

$yoast_seo_xml = get_option( 'wpseo_xml', [] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
engahmeds3ed marked this conversation as resolved.
Show resolved Hide resolved

if ( version_compare( rocket_get_constant( 'WPSEO_VERSION', '' ), '7.0' ) >= 0 ) {
$yoast_seo = get_option( 'wpseo', [] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$yoast_seo_xml['enablexmlsitemap'] = isset( $yoast_seo['enable_xml_sitemap'] ) && $yoast_seo['enable_xml_sitemap']; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}

$enabled = (bool) $yoast_seo_xml['enablexmlsitemap'];

return $enabled;
}
}
4 changes: 4 additions & 0 deletions inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public function register() {
$this->getContainer()
->share( 'pwa', 'WP_Rocket\ThirdParty\Plugins\PWA' )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'yoast_seo', 'WP_Rocket\ThirdParty\Plugins\SEO\Yoast' )
->addArgument( $options )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'flatsome', 'WP_Rocket\ThirdParty\Themes\Flatsome' )
->addTag( 'common_subscriber' );
Expand Down
2 changes: 1 addition & 1 deletion inc/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function rocket_pre_main_option( $newvalue, $oldvalue ) {
$rocket_settings_errors = [];

// Make sure that fields that allow users to enter patterns are well formatted.
$is_form_submit = filter_input( INPUT_POST, 'option_page', FILTER_SANITIZE_STRING );
$is_form_submit = isset( $_POST['option_page'] ) ? sanitize_key( $_POST['option_page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$is_form_submit = WP_ROCKET_PLUGIN_SLUG === $is_form_submit;
$errors = [];
$pattern_labels = [
Expand Down
2 changes: 1 addition & 1 deletion inc/admin/upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function rocket_upgrader() {
update_option( WP_ROCKET_SLUG, $options );
}

$page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if (
'wprocket' === $page
Expand Down
2 changes: 1 addition & 1 deletion inc/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function rocket_has_constant( $constant_name ) {
* @param string $constant_name Name of the constant to check.
* @param mixed|null $default Optional. Default value to return if constant is not defined.
*
* @return bool true when constant is defined; else, false.
* @return mixed
*/
function rocket_get_constant( $constant_name, $default = null ) {
if ( ! rocket_has_constant( $constant_name ) ) {
Expand Down
Loading