diff --git a/admin/class-admin-init.php b/admin/class-admin-init.php index 168e789a45b..dbd3a983bc2 100644 --- a/admin/class-admin-init.php +++ b/admin/class-admin-init.php @@ -250,7 +250,7 @@ private function register_premium_upsell_admin_block() { * @return void */ private function load_xml_sitemaps_admin() { - if ( WPSEO_Options::get( 'enable_xml_sitemap', false ) ) { + if ( WPSEO_Options::get( 'enable_xml_sitemap', false, [ 'wpseo' ] ) ) { new WPSEO_Sitemaps_Admin(); } } diff --git a/admin/class-admin.php b/admin/class-admin.php index 5b6578edf14..824a9d5e400 100644 --- a/admin/class-admin.php +++ b/admin/class-admin.php @@ -43,15 +43,11 @@ public function __construct() { WPSEO_Options::maybe_set_multisite_defaults( false ); } - if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { - add_action( 'created_category', [ $this, 'schedule_rewrite_flush' ] ); - add_action( 'edited_category', [ $this, 'schedule_rewrite_flush' ] ); - add_action( 'delete_category', [ $this, 'schedule_rewrite_flush' ] ); - } + add_action( 'created_category', [ $this, 'schedule_rewrite_flush' ] ); + add_action( 'edited_category', [ $this, 'schedule_rewrite_flush' ] ); + add_action( 'delete_category', [ $this, 'schedule_rewrite_flush' ] ); - if ( WPSEO_Options::get( 'disable-attachment' ) === true ) { - add_filter( 'wpseo_accessible_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] ); - } + add_filter( 'wpseo_accessible_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] ); add_filter( 'plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 ); add_filter( 'network_admin_plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 ); @@ -75,8 +71,6 @@ public function __construct() { add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); } - $this->set_upsell_notice(); - $this->initialize_cornerstone_content(); if ( WPSEO_Utils::is_plugin_network_active() ) { @@ -119,6 +113,10 @@ public function __construct() { * @return void */ public function schedule_rewrite_flush() { + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { + return; + } + // Bail if this is a multisite installation and the site has been switched. if ( is_multisite() && ms_is_switched() ) { return; @@ -367,17 +365,6 @@ private function localize_admin_global_script() { ); } - /** - * Sets the upsell notice. - * - * @return void - */ - protected function set_upsell_notice() { - $upsell = new WPSEO_Product_Upsell_Notice(); - $upsell->dismiss_notice_listener(); - $upsell->initialize(); - } - /** * Whether we are on the admin dashboard page. * diff --git a/admin/filters/class-abstract-post-filter.php b/admin/filters/class-abstract-post-filter.php index be3da94431f..08232076870 100644 --- a/admin/filters/class-abstract-post-filter.php +++ b/admin/filters/class-abstract-post-filter.php @@ -61,7 +61,7 @@ public function register_hooks() { add_action( 'restrict_manage_posts', [ $this, 'render_hidden_input' ] ); } - if ( $this->is_filter_active() && $this->get_explanation() !== null ) { + if ( $this->is_filter_active() ) { add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_explanation_assets' ] ); } } @@ -83,13 +83,19 @@ public function add_filter_links() { * @return void */ public function enqueue_explanation_assets() { + $explanation = $this->get_explanation(); + + if ( $explanation === null ) { + return; + } + $asset_manager = new WPSEO_Admin_Asset_Manager(); $asset_manager->enqueue_script( 'filter-explanation' ); $asset_manager->enqueue_style( 'filter-explanation' ); $asset_manager->localize_script( 'filter-explanation', 'yoastFilterExplanation', - [ 'text' => $this->get_explanation() ] + [ 'text' => $explanation ] ); } diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index d514cdc8e28..71b0c4a4c77 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -87,8 +87,8 @@ public function __construct() { $this->editor = new WPSEO_Metabox_Editor(); $this->editor->register_hooks(); - $this->social_is_enabled = WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false ); - $this->is_advanced_metadata_enabled = WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false; + $this->social_is_enabled = WPSEO_Options::get( 'opengraph', false, [ 'wpseo_social' ] ) || WPSEO_Options::get( 'twitter', false, [ 'wpseo_social' ] ); + $this->is_advanced_metadata_enabled = WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta', null, [ 'wpseo' ] ) === false; $this->seo_analysis = new WPSEO_Metabox_Analysis_SEO(); $this->readability_analysis = new WPSEO_Metabox_Analysis_Readability(); diff --git a/inc/class-post-type.php b/inc/class-post-type.php index 54085d58b6c..944e1f669b0 100644 --- a/inc/class-post-type.php +++ b/inc/class-post-type.php @@ -51,7 +51,9 @@ public static function is_post_type_indexable( $post_type_name ) { * @return array The filtered array. */ public static function filter_attachment_post_type( array $post_types ) { - unset( $post_types['attachment'] ); + if ( WPSEO_Options::get( 'disable-attachment' ) === true ) { + unset( $post_types['attachment'] ); + } return $post_types; } diff --git a/inc/class-rewrite.php b/inc/class-rewrite.php index 82ed1206915..9622ada0aad 100644 --- a/inc/class-rewrite.php +++ b/inc/class-rewrite.php @@ -17,7 +17,7 @@ public function __construct() { add_filter( 'query_vars', [ $this, 'query_vars' ] ); add_filter( 'term_link', [ $this, 'no_category_base' ], 10, 3 ); add_filter( 'request', [ $this, 'request' ] ); - add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules' ] ); + add_filter( 'category_rewrite_rules', [ $this, 'category_rewrite_rules_wrapper' ] ); add_action( 'created_category', [ $this, 'schedule_flush' ] ); add_action( 'edited_category', [ $this, 'schedule_flush' ] ); @@ -32,7 +32,9 @@ public function __construct() { * @return void */ public function schedule_flush() { - add_action( 'shutdown', 'flush_rewrite_rules' ); + if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { + add_action( 'shutdown', 'flush_rewrite_rules' ); + } } /** @@ -45,6 +47,10 @@ public function schedule_flush() { * @return string */ public function no_category_base( $link, $term, $taxonomy ) { + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { + return $link; + } + if ( $taxonomy !== 'category' ) { return $link; } @@ -91,6 +97,10 @@ public function query_vars( $query_vars ) { * @return array The query vars. */ public function request( $query_vars ) { + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { + return $query_vars; + } + if ( ! isset( $query_vars['wpseo_category_redirect'] ) ) { return $query_vars; } @@ -99,6 +109,21 @@ public function request( $query_vars ) { return []; } + /** + * Wrapper for the category_rewrite_rules() below, so we can add the $rules param in a BC way. + * + * @param array $rules Rewrite rules generated for the current permastruct, keyed by their regex pattern. + * + * @return array The category rewrite rules. + */ + public function category_rewrite_rules_wrapper( $rules ) { + if ( WPSEO_Options::get( 'stripcategorybase' ) !== true ) { + return $rules; + } + + return $this->category_rewrite_rules(); + } + /** * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta. * diff --git a/inc/class-upgrade.php b/inc/class-upgrade.php index 25936b54290..f0788250ed9 100644 --- a/inc/class-upgrade.php +++ b/inc/class-upgrade.php @@ -103,10 +103,6 @@ public function __construct() { add_action( 'init', [ $this, 'upgrade_125' ] ); } - // Since 3.7. - $upsell_notice = new WPSEO_Product_Upsell_Notice(); - $upsell_notice->set_upgrade_notice(); - /** * Filter: 'wpseo_run_upgrade' - Runs the upgrade hook which are dependent on Yoast SEO. * @@ -154,9 +150,9 @@ protected function add_upgrade_history( $current_version, $new_version ) { */ protected function finish_up( $previous_version = null ) { if ( $previous_version ) { - WPSEO_Options::set( 'previous_version', $previous_version ); + WPSEO_Options::set( 'previous_version', $previous_version, 'wpseo' ); } - WPSEO_Options::set( 'version', WPSEO_VERSION ); + WPSEO_Options::set( 'version', WPSEO_VERSION, 'wpseo' ); // Just flush rewrites, always, to at least make them work after an upgrade. add_action( 'shutdown', 'flush_rewrite_rules' ); diff --git a/inc/class-wpseo-meta.php b/inc/class-wpseo-meta.php index 9c75815f1d2..69009ec8f11 100644 --- a/inc/class-wpseo-meta.php +++ b/inc/class-wpseo-meta.php @@ -240,7 +240,7 @@ class WPSEO_Meta { */ public static function init() { foreach ( self::$social_networks as $option => $network ) { - if ( WPSEO_Options::get( $option, false ) === true ) { + if ( WPSEO_Options::get( $option, false, [ 'wpseo_social' ] ) === true ) { foreach ( self::$social_fields as $box => $type ) { self::$meta_fields['social'][ $network . '-' . $box ] = [ 'type' => $type, diff --git a/inc/options/class-wpseo-option-social.php b/inc/options/class-wpseo-option-social.php index 220dd6f8229..a2aaa69b80c 100644 --- a/inc/options/class-wpseo-option-social.php +++ b/inc/options/class-wpseo-option-social.php @@ -107,7 +107,7 @@ public static function get_instance() { * @return void */ public function translate_defaults() { - self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' ); + self::$twitter_card_types['summary_large_image'] = 'Summary with large image'; } /** diff --git a/inc/options/class-wpseo-options.php b/inc/options/class-wpseo-options.php index eaae33400cd..41c884f21ff 100644 --- a/inc/options/class-wpseo-options.php +++ b/inc/options/class-wpseo-options.php @@ -212,10 +212,13 @@ public static function get_option_names() { /** * Retrieve all the options for the SEO plugin in one go. * + * @param array $specific_options The option groups of the option you want to get. + * * @return array Array combining the values of all the options. */ - public static function get_all() { - static::$option_values = static::get_options( static::get_option_names() ); + public static function get_all( $specific_options = [] ) { + $option_names = ( empty( $specific_options ) ) ? static::get_option_names() : $specific_options; + static::$option_values = static::get_options( $option_names ); return static::$option_values; } @@ -269,14 +272,15 @@ public static function get_option( $option_name ) { /** * Retrieve a single field from any option for the SEO plugin. Keys are always unique. * - * @param string $key The key it should return. - * @param mixed $default_value The default value that should be returned if the key isn't set. + * @param string $key The key it should return. + * @param mixed $default_value The default value that should be returned if the key isn't set. + * @param array $option_groups The option groups to retrieve the option from. * * @return mixed Returns value if found, $default_value if not. */ - public static function get( $key, $default_value = null ) { - if ( static::$option_values === null ) { - static::prime_cache(); + public static function get( $key, $default_value = null, $option_groups = [] ) { + if ( ! isset( static::$option_values[ $key ] ) ) { + static::prime_cache( $option_groups ); } if ( isset( static::$option_values[ $key ] ) ) { return static::$option_values[ $key ]; @@ -297,23 +301,26 @@ public static function clear_cache() { /** * Primes our cache. * + * @param array $option_groups The option groups to prime the cache with. + * * @return void */ - private static function prime_cache() { - static::$option_values = static::get_all(); + private static function prime_cache( $option_groups = [] ) { + static::$option_values = static::get_all( $option_groups ); static::$option_values = static::add_ms_option( static::$option_values ); } /** * Retrieve a single field from an option for the SEO plugin. * - * @param string $key The key to set. - * @param mixed $value The value to set. + * @param string $key The key to set. + * @param mixed $value The value to set. + * @param string $option_group The lookup table which represents the option_group where the key is stored. * * @return mixed|null Returns value if found, $default if not. */ - public static function set( $key, $value ) { - $lookup_table = static::get_lookup_table(); + public static function set( $key, $value, $option_group = '' ) { + $lookup_table = static::get_lookup_table( $option_group ); if ( isset( $lookup_table[ $key ] ) ) { return static::save_option( $lookup_table[ $key ], $key, $value ); @@ -562,12 +569,15 @@ protected static function is_multisite() { /** * Retrieves a lookup table to find in which option_group a key is stored. * + * @param string $option_group The option_group where the key is stored. + * * @return array The lookup table. */ - private static function get_lookup_table() { - $lookup_table = []; + private static function get_lookup_table( $option_group = '' ) { + $lookup_table = []; + $option_groups = ( $option_group === '' ) ? static::$options : [ $option_group => static::$options[ $option_group ] ]; - foreach ( array_keys( static::$options ) as $option_name ) { + foreach ( array_keys( $option_groups ) as $option_name ) { $full_option = static::get_option( $option_name ); foreach ( $full_option as $key => $value ) { $lookup_table[ $key ] = $option_name; diff --git a/src/helpers/options-helper.php b/src/helpers/options-helper.php index 1352e4343c5..210ceb322b5 100644 --- a/src/helpers/options-helper.php +++ b/src/helpers/options-helper.php @@ -28,13 +28,14 @@ public function get( $key, $default_value = null ) { /** * Sets a single field to the options. * - * @param string $key The key to set. - * @param mixed $value The value to set. + * @param string $key The key to set. + * @param mixed $value The value to set. + * @param string $option_group The lookup table which represents the option_group where the key is stored. * * @return mixed|null Returns value if found. */ - public function set( $key, $value ) { - return WPSEO_Options::set( $key, $value ); + public function set( $key, $value, $option_group = '' ) { + return WPSEO_Options::set( $key, $value, $option_group ); } /** diff --git a/tests/Unit/Admin/Admin_Features_Test.php b/tests/Unit/Admin/Admin_Features_Test.php index 91eeabe8f7e..df4aba3b4fe 100644 --- a/tests/Unit/Admin/Admin_Features_Test.php +++ b/tests/Unit/Admin/Admin_Features_Test.php @@ -5,12 +5,9 @@ use Brain\Monkey; use Mockery; use Wincher_Dashboard_Widget; -use WP_User; use WPSEO_Admin; use WPSEO_Primary_Term_Admin; use Yoast\WP\SEO\Helpers\Current_Page_Helper; -use Yoast\WP\SEO\Helpers\Product_Helper; -use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Helpers\Url_Helper; use Yoast\WP\SEO\Tests\Unit\TestCase; use Yoast_Dashboard_Widget; @@ -22,32 +19,6 @@ */ final class Admin_Features_Test extends TestCase { - /** - * Returns an instance with set expectations for the dependencies. - * - * @return WPSEO_Admin Instance to test against. - */ - private function get_admin_with_expectations() { - Monkey\Functions\expect( 'admin_url' ) - ->once() - ->with( '?page=' . WPSEO_Admin::PAGE_IDENTIFIER . '&yoast_dismiss=upsell' ) - ->andReturn( 'https://example.org' ); - - Monkey\Functions\expect( 'wp_nonce_url' ) - ->once() - ->with( 'https://example.org', 'dismiss-5star-upsell' ) - ->andReturn( 'https://example.org?_wpnonce=test-nonce' ); - - $admin_user = Mockery::mock( WP_User::class ); - $admin_user->ID = 1; - - Monkey\Functions\expect( 'get_current_user_id' ) - ->once() - ->andReturn( $admin_user->ID ); - - return new WPSEO_Admin(); - } - /** * Sets up the YoastSEO function with the right expectations. * @@ -59,26 +30,18 @@ private function setup_yoastseo_with_expectations() { $current_page_helper = Mockery::mock( Current_Page_Helper::class ); $current_page_helper->expects( 'is_yoast_seo_page' )->once()->andReturn( true ); - $product_helper = Mockery::mock( Product_Helper::class ); - $product_helper->expects( 'is_premium' )->once()->andReturn( false ); - - $short_link = Mockery::mock( Short_Link_Helper::class ); - $short_link->expects( 'get' )->times( 3 )->andReturn( 'https://www.example.com?some=var' ); - $url_helper = Mockery::mock( Url_Helper::class ); $url_helper->expects( 'is_plugin_network_active' )->twice()->andReturn( false ); $container = $this->create_container_with( [ Current_Page_Helper::class => $current_page_helper, - Product_Helper::class => $product_helper, - Short_Link_Helper::class => $short_link, Url_Helper::class => $url_helper, ] ); Monkey\Functions\expect( 'YoastSEO' ) - ->times( 7 ) + ->times( 3 ) ->andReturn( (object) [ 'helpers' => $this->create_helper_surface( $container ) ] ); Monkey\Functions\expect( 'get_user_locale' ) @@ -98,7 +61,7 @@ public function test_get_admin_features_ON_post_edit() { $this->setup_yoastseo_with_expectations(); - $class_instance = $this->get_admin_with_expectations(); + $class_instance = new WPSEO_Admin(); $admin_features = [ 'primary_category' => new WPSEO_Primary_Term_Admin(), @@ -122,7 +85,7 @@ public function test_get_admin_features_NOT_ON_post_edit() { $this->setup_yoastseo_with_expectations(); - $class_instance = $this->get_admin_with_expectations(); + $class_instance = new WPSEO_Admin(); $admin_features = [ 'dashboard_widget' => new Yoast_Dashboard_Widget(), diff --git a/wp-seo-main.php b/wp-seo-main.php index 809cf5f97ec..8495168e82c 100644 --- a/wp-seo-main.php +++ b/wp-seo-main.php @@ -221,10 +221,8 @@ function _wpseo_activate() { WPSEO_Options::ensure_options_exist(); if ( ! is_multisite() || ! ms_is_switched() ) { - if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { - // Constructor has side effects so this registers all hooks. - $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite(); - } + // Constructor has side effects so this registers all hooks. + $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite(); } add_action( 'shutdown', [ 'WPSEO_Utils', 'clear_rewrites' ] ); @@ -341,7 +339,7 @@ function wpseo_init() { WPSEO_Options::get_instance(); WPSEO_Meta::init(); - if ( version_compare( WPSEO_Options::get( 'version', 1 ), WPSEO_VERSION, '<' ) ) { + if ( version_compare( WPSEO_Options::get( 'version', 1, [ 'wpseo' ] ), WPSEO_VERSION, '<' ) ) { if ( function_exists( 'opcache_reset' ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Prevent notices when opcache.restrict_api is set. @opcache_reset(); @@ -351,11 +349,9 @@ function wpseo_init() { // Get a cleaned up version of the $options. } - if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) { - $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite(); - } + $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite(); - if ( WPSEO_Options::get( 'enable_xml_sitemap' ) === true ) { + if ( WPSEO_Options::get( 'enable_xml_sitemap', null, [ 'wpseo' ] ) === true ) { $GLOBALS['wpseo_sitemaps'] = new WPSEO_Sitemaps(); }