| @@ -6,40 +6,61 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Extensions\Seo; |
| 9 | 9 | |
| 10 | -use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 11 | -use Automattic\Jetpack\Status; | |
| 12 | -use Automattic\Jetpack\Status\Host; | |
| 13 | -use Jetpack_Gutenberg; | |
| 10 | +use Automattic\Jetpack\Modules; | |
| 14 | 11 | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 14 | +} | |
| 15 | + | |
| 16 | +add_action( | |
| 17 | + 'jetpack_register_gutenberg_extensions', | |
| 18 | + function () { | |
| 19 | + if ( is_seo_disabled() ) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + | |
| 23 | + \Jetpack_Gutenberg::set_availability_for_plan( 'advanced-seo' ); | |
| 24 | + } | |
| 25 | +); | |
| 26 | + | |
| 15 | 27 | /** |
| 16 | - * Register SEO plugin. | |
| 28 | + * We only want to enable the SEO extension (and display SEO settings) | |
| 29 | + * if the 'jetpack_disable_seo_tools' filter is not set to false. | |
| 17 | 30 | * |
| 18 | - * @return void | |
| 31 | + * This is done on after_setup_theme to ensure that we have access to the hook | |
| 32 | + * that is used by SEO plugins to disable the conflicting output of SEO Tools. | |
| 19 | 33 | */ |
| 20 | -function register_plugins() { | |
| 21 | - // Setting availability for Advanced SEO plan. | |
| 22 | - Jetpack_Gutenberg::set_availability_for_plan( 'advanced-seo' ); | |
| 23 | - | |
| 24 | - // On WPCOM sites we handle plan with Upsell message if needed. | |
| 25 | - // On Self-Hosted we check if there's an active connection and if the site is not in offline mode. | |
| 26 | - $is_wpcom_platform = ( new Host() )->is_wpcom_platform(); | |
| 27 | - if ( | |
| 28 | - $is_wpcom_platform | |
| 29 | - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) | |
| 30 | - ) { | |
| 31 | - Jetpack_Gutenberg::set_extension_available( 'jetpack-seo' ); | |
| 34 | +add_action( | |
| 35 | + 'after_setup_theme', | |
| 36 | + function () { | |
| 37 | + if ( | |
| 38 | + ! is_seo_disabled() | |
| 39 | + /** This filter is already documented in modules/seo-tools/class-jetpack-seo-utils.php */ | |
| 40 | + && ! apply_filters( 'jetpack_disable_seo_tools', false ) | |
| 41 | + ) { | |
| 42 | + \Jetpack_Gutenberg::set_extension_available( 'jetpack-seo' ); | |
| 43 | + } | |
| 32 | 44 | } |
| 33 | -} | |
| 45 | +); | |
| 34 | 46 | |
| 35 | -add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugins' ); | |
| 36 | - | |
| 37 | 47 | add_filter( |
| 38 | 48 | 'jetpack_set_available_extensions', |
| 39 | 49 | function ( $extensions ) { |
| 40 | 50 | return array_merge( |
| 41 | - $extensions, | |
| 51 | + (array) $extensions, | |
| 42 | 52 | array( 'advanced-seo' ) |
| 43 | 53 | ); |
| 44 | 54 | } |
| 45 | 55 | ); |
| 56 | + | |
| 57 | +/** | |
| 58 | + * SEO tools should be hidden from non-admins if the module is not active. | |
| 59 | + * | |
| 60 | + * @since 14.6 | |
| 61 | + * | |
| 62 | + * @return bool | |
| 63 | + */ | |
| 64 | +function is_seo_disabled(): bool { | |
| 65 | + return ! ( new Modules() )->is_active( 'seo-tools' ) && ! current_user_can( 'jetpack_activate_modules' ); | |
| 66 | +} | |