diff --git a/includes/Admin/Customizer.php b/includes/Admin/Customizer.php index eeb0f9e00c5a..98a582a5df1a 100644 --- a/includes/Admin/Customizer.php +++ b/includes/Admin/Customizer.php @@ -30,7 +30,6 @@ use Google\Web_Stories\Infrastructure\Conditional; use Google\Web_Stories\Service_Base; -use Google\Web_Stories\Settings; use Google\Web_Stories\Stories_Script_Data; use Google\Web_Stories\Story_Post_Type; use Google\Web_Stories\Story_Query; @@ -106,13 +105,6 @@ class Customizer extends Service_Base implements Conditional { */ private WP_Customize_Manager $wp_customize; - /** - * Settings instance. - * - * @var Settings Settings instance. - */ - private Settings $settings; - /** * Story_Post_Type instance. * @@ -132,17 +124,14 @@ class Customizer extends Service_Base implements Conditional { * * @since 1.12.0 * - * @param Settings $settings Settings instance. * @param Story_Post_Type $story_post_type Story_Post_Type instance. * @param Stories_Script_Data $stories_script_data Stories_Script_Data instance. * @return void */ public function __construct( - Settings $settings, Story_Post_Type $story_post_type, Stories_Script_Data $stories_script_data ) { - $this->settings = $settings; $this->story_post_type = $story_post_type; $this->stories_script_data = $stories_script_data; } @@ -548,13 +537,15 @@ public function validate_number_of_columns( WP_Error $validity, int $value ): WP * @since 1.5.0 */ public function render_stories(): string { + // Not using Settings::get_setting() to avoid calling rest_sanitize_value_from_schema(). + /** * Render options. * * @var array $options * @phpstan-var StoryAttributes */ - $options = (array) $this->settings->get_setting( self::STORY_OPTION ); + $options = (array) get_option( self::STORY_OPTION ); if ( empty( $options['show_stories'] ) || true !== $options['show_stories'] ) { return ''; diff --git a/includes/Admin/Dashboard.php b/includes/Admin/Dashboard.php index 78af490f3f62..634a81c0ca8f 100644 --- a/includes/Admin/Dashboard.php +++ b/includes/Admin/Dashboard.php @@ -237,7 +237,14 @@ public function get_hook_suffix( string $key ) { public function add_menu_page(): void { $parent = 'edit.php?post_type=' . $this->story_post_type->get_slug(); - $settings = $this->get_dashboard_settings(); + // Not using get_dashboard_settings() to avoid an extra database query. + + $settings = [ + 'canViewDefaultTemplates' => true, + ]; + + /** This filter is documented in includes/Admin/Dashboard.php */ + $settings = apply_filters( 'web_stories_dashboard_settings', $settings ); /** * The edit_posts capability. diff --git a/includes/Integrations/Core_Themes_Support.php b/includes/Integrations/Core_Themes_Support.php index a199e3087176..08c98115197e 100644 --- a/includes/Integrations/Core_Themes_Support.php +++ b/includes/Integrations/Core_Themes_Support.php @@ -30,7 +30,6 @@ use Google\Web_Stories\Admin\Customizer; use Google\Web_Stories\Assets; -use Google\Web_Stories\Renderer\Stories\Renderer; use Google\Web_Stories\Service_Base; use function Google\Web_Stories\render_theme_stories; @@ -98,7 +97,7 @@ public function extend_theme_support(): void { public function embed_web_stories(): void { $stylesheet = get_stylesheet(); if ( is_readable( sprintf( '%sassets/css/web-stories-theme-style-%s.css', WEBSTORIES_PLUGIN_DIR_PATH, $stylesheet ) ) ) { - $this->assets->enqueue_style_asset( 'web-stories-theme-style-' . $stylesheet, [ Renderer::STYLE_HANDLE ] ); + $this->assets->enqueue_style_asset( 'web-stories-theme-style-' . $stylesheet, [] ); } ?>
@@ -134,13 +133,14 @@ public function add_core_theme_classes( $classes ) { * @since 1.5.0 */ public function register(): void { - if ( ! \in_array( get_stylesheet(), self::$supported_themes, true ) ) { return; } $this->extend_theme_support(); + // Not using Settings::get_setting() to avoid calling rest_sanitize_value_from_schema(). + /** * Customizer options. * @@ -165,6 +165,6 @@ public function register(): void { * @return string Registration action to use. */ public static function get_registration_action(): string { - return 'after_setup_theme'; + return 'wp_head'; } } diff --git a/includes/Integrations/Site_Kit.php b/includes/Integrations/Site_Kit.php index 145a05e5ccbc..69c9aee422dc 100644 --- a/includes/Integrations/Site_Kit.php +++ b/includes/Integrations/Site_Kit.php @@ -95,21 +95,29 @@ public function __construct( Analytics $analytics, Context $context, Plugin_Stat public function register(): void { add_filter( 'googlesitekit_amp_gtag_opt', [ $this, 'filter_site_kit_gtag_opt' ] ); - $handler = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_HANDLER ); - - if ( 'web-stories' === $handler ) { - add_filter( - 'googlesitekit_analytics-4_tag_amp_blocked', - function ( $blocked ) { - if ( $this->context->is_web_story() ) { - return true; - } - return $blocked; + add_filter( + 'googlesitekit_analytics-4_tag_amp_blocked', + function ( $blocked ) { + $handler = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_HANDLER ); + + if ( 'web-stories' === $handler && $this->context->is_web_story() ) { + return true; } - ); - } elseif ( 'site-kit' === $handler && $this->is_analytics_module_active() ) { - remove_action( 'web_stories_print_analytics', [ $this->analytics, 'print_analytics_tag' ] ); - } + + return $blocked; + } + ); + + add_action( + 'web_stories_print_analytics', + function (): void { + $handler = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_HANDLER ); + if ( 'site-kit' === $handler && $this->is_analytics_module_active() ) { + remove_action( 'web_stories_print_analytics', [ $this->analytics, 'print_analytics_tag' ] ); + } + }, + 5 + ); } /** diff --git a/includes/Settings.php b/includes/Settings.php index d61e424c977e..f7a6ffd9d9a4 100644 --- a/includes/Settings.php +++ b/includes/Settings.php @@ -149,6 +149,11 @@ class Settings implements Service, Registerable, PluginUninstallAware { */ public const SETTING_NAME_TRACKING_HANDLER = 'web_stories_ga_tracking_handler'; + /** + * Customizer settings. + */ + public const SETTING_NAME_CUSTOMIZER_SETTINGS = 'web_stories_customizer_settings'; + /** * Shopping_Vendors instance. * @@ -165,6 +170,18 @@ public function __construct( Shopping_Vendors $shopping_vendors ) { $this->shopping_vendors = $shopping_vendors; } + /** + * Primes option caches for specified groups if the function exists. + * + * @since 1.37.0 + */ + public function prime_option_caches(): void { + if ( \function_exists( 'wp_prime_option_caches_by_group' ) ) { + wp_prime_option_caches_by_group( self::SETTING_GROUP ); + wp_prime_option_caches_by_group( self::SETTING_GROUP_EXPERIMENTS ); + } + } + /** * Register settings. * @@ -336,18 +353,15 @@ public function register(): void { self::SETTING_GROUP_EXPERIMENTS, self::SETTING_NAME_EXPERIMENTS, [ - 'description' => __( 'Experiments', 'web-stories' ), - 'type' => 'object', - 'default' => [], - 'show_in_rest' => [ + 'description' => __( 'Experiments', 'web-stories' ), + 'type' => 'object', + 'default' => [], + 'show_in_rest' => [ 'schema' => [ 'properties' => [], 'additionalProperties' => true, ], ], - // WPGraphQL errors when encountering array or object types. - // See https://github.com/wp-graphql/wp-graphql/issues/2065. - 'show_in_graphql' => false, ] ); @@ -424,6 +438,19 @@ public function register(): void { ], ] ); + + register_setting( + self::SETTING_GROUP, + self::SETTING_NAME_CUSTOMIZER_SETTINGS, + [ + 'description' => __( 'Customizer settings', 'web-stories' ), + 'type' => 'array', + 'default' => [], + 'show_in_rest' => false, + ] + ); + + add_action( 'init', [ $this, 'prime_option_caches' ] ); } /** diff --git a/includes/Story_Post_Type.php b/includes/Story_Post_Type.php index a7d50e786111..967e3ca0d4c2 100644 --- a/includes/Story_Post_Type.php +++ b/includes/Story_Post_Type.php @@ -287,20 +287,18 @@ public function clear_user_posts_count( int $post_id, WP_Post $post ): void { * @return bool|string Whether the post type should have an archive, or archive slug. */ public function get_has_archive() { - $archive_page_option = $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE ); - $custom_archive_page_id = (int) $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); - $has_archive = true; + $archive_page_option = $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE ); + $has_archive = true; if ( 'disabled' === $archive_page_option ) { $has_archive = false; - } elseif ( - 'custom' === $archive_page_option && - $custom_archive_page_id && - 'publish' === get_post_status( $custom_archive_page_id ) - ) { - $uri = get_page_uri( $custom_archive_page_id ); - if ( $uri ) { - $has_archive = urldecode( $uri ); + } elseif ( 'custom' === $archive_page_option ) { + $custom_archive_page_id = (int) $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + if ( $custom_archive_page_id && 'publish' === get_post_status( $custom_archive_page_id ) ) { + $uri = get_page_uri( $custom_archive_page_id ); + if ( $uri ) { + $has_archive = urldecode( $uri ); + } } } diff --git a/includes/Tracking.php b/includes/Tracking.php index 6184c09bb6ea..f59c9a43e19a 100644 --- a/includes/Tracking.php +++ b/includes/Tracking.php @@ -96,6 +96,13 @@ class Tracking extends Service_Base { */ private WooCommerce $woocommerce; + /** + * Context instance. + * + * @var Context Context instance. + */ + private Context $context; + /** * Tracking constructor. * @@ -107,6 +114,7 @@ class Tracking extends Service_Base { * @param Settings $settings Settings instance. * @param Preferences $preferences Preferences instance. * @param WooCommerce $woocommerce WooCommerce instance. + * @param Context $context Context instance. */ public function __construct( Experiments $experiments, @@ -114,7 +122,8 @@ public function __construct( Assets $assets, Settings $settings, Preferences $preferences, - WooCommerce $woocommerce + WooCommerce $woocommerce, + Context $context ) { $this->assets = $assets; $this->experiments = $experiments; @@ -122,12 +131,13 @@ public function __construct( $this->settings = $settings; $this->preferences = $preferences; $this->woocommerce = $woocommerce; + $this->context = $context; } /** * Initializes tracking. * - * Registers the setting in WordPress. + * Registers the script in WordPress. * * @since 1.0.0 */ @@ -141,6 +151,10 @@ public function register(): void { false ); + if ( ! $this->context->is_story_editor() && 'web-story' !== $this->context->get_screen_post_type() ) { + return; + } + wp_add_inline_script( self::SCRIPT_HANDLE, 'window.webStoriesTrackingSettings = ' . wp_json_encode( $this->get_settings() ) . ';' @@ -155,7 +169,7 @@ public function register(): void { * @return string Registration action to use. */ public static function get_registration_action(): string { - return 'admin_init'; + return 'admin_head'; } /** diff --git a/tests/phpunit/integration/tests/Integrations/Site_Kit.php b/tests/phpunit/integration/tests/Integrations/Site_Kit.php index 27d22a9cb1a0..fd95689b6990 100644 --- a/tests/phpunit/integration/tests/Integrations/Site_Kit.php +++ b/tests/phpunit/integration/tests/Integrations/Site_Kit.php @@ -72,7 +72,7 @@ public function test_init_analytics_module_active(): void { $this->instance->register(); $this->assertSame( 10, has_filter( 'googlesitekit_amp_gtag_opt', [ $this->instance, 'filter_site_kit_gtag_opt' ] ) ); - $this->assertFalse( has_action( 'web_stories_print_analytics', [ $analytics, 'print_analytics_tag' ] ) ); + $this->assertSame( 10, has_action( 'web_stories_print_analytics', [ $analytics, 'print_analytics_tag' ] ) ); } /** diff --git a/tests/phpunit/integration/tests/Tracking.php b/tests/phpunit/integration/tests/Tracking.php index a168b642bd76..1fd7b1f821a3 100644 --- a/tests/phpunit/integration/tests/Tracking.php +++ b/tests/phpunit/integration/tests/Tracking.php @@ -22,10 +22,12 @@ use _WP_Dependency; use Google\Web_Stories\Assets; +use Google\Web_Stories\Context; use Google\Web_Stories\Experiments; use Google\Web_Stories\Integrations\Site_Kit; use Google\Web_Stories\Integrations\WooCommerce; use Google\Web_Stories\Settings; +use Google\Web_Stories\Story_Post_Type; use Google\Web_Stories\User\Preferences; use PHPUnit\Framework\MockObject\MockObject; use WP_UnitTest_Factory; @@ -69,6 +71,7 @@ public function set_up(): void { $assets = $this->injector->make( Assets::class ); $settings = $this->injector->make( Settings::class ); $preferences = $this->injector->make( Preferences::class ); + $context = $this->injector->make( Context::class ); $this->woocommerce = $this->createMock( WooCommerce::class ); $this->instance = new \Google\Web_Stories\Tracking( $this->experiments, @@ -76,7 +79,8 @@ public function set_up(): void { $assets, $settings, $preferences, - $this->woocommerce + $this->woocommerce, + $context ); } @@ -84,6 +88,10 @@ public function set_up(): void { * @covers ::register */ public function test_register_tracking_script(): void { + global $current_screen; + + $current_screen = convert_to_screen( Story_Post_Type::POST_TYPE_SLUG ); + $this->site_kit->method( 'get_plugin_status' )->willReturn( [ 'installed' => true,