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

Settings: Prime options to reduce database queries #13700

Merged
merged 17 commits into from
May 23, 2024
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
15 changes: 3 additions & 12 deletions includes/Admin/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<string,string|bool> $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 '';
Expand Down
9 changes: 8 additions & 1 deletion includes/Admin/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions includes/Integrations/Core_Themes_Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,8 +97,8 @@
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, [] );
}

Check warning on line 101 in includes/Integrations/Core_Themes_Support.php

View check run for this annotation

Codecov / codecov/patch

includes/Integrations/Core_Themes_Support.php#L100-L101

Added lines #L100 - L101 were not covered by tests
?>
<div class="web-stories-theme-header-section">
<?php render_theme_stories(); ?>
Expand Down Expand Up @@ -134,13 +133,14 @@
* @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.
*
Expand All @@ -165,6 +165,6 @@
* @return string Registration action to use.
*/
public static function get_registration_action(): string {
return 'after_setup_theme';
return 'wp_head';
}
}
36 changes: 22 additions & 14 deletions includes/Integrations/Site_Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,29 @@
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;

Check warning on line 104 in includes/Integrations/Site_Kit.php

View check run for this annotation

Codecov / codecov/patch

includes/Integrations/Site_Kit.php#L103-L104

Added lines #L103 - L104 were not covered by tests
}
);
} elseif ( 'site-kit' === $handler && $this->is_analytics_module_active() ) {
remove_action( 'web_stories_print_analytics', [ $this->analytics, 'print_analytics_tag' ] );
}

return $blocked;

Check warning on line 107 in includes/Integrations/Site_Kit.php

View check run for this annotation

Codecov / codecov/patch

includes/Integrations/Site_Kit.php#L107

Added line #L107 was not covered by tests
}
);

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' ] );

Check warning on line 116 in includes/Integrations/Site_Kit.php

View check run for this annotation

Codecov / codecov/patch

includes/Integrations/Site_Kit.php#L114-L116

Added lines #L114 - L116 were not covered by tests
}
},
5
);
}

/**
Expand Down
41 changes: 34 additions & 7 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
*/
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.
*
Expand All @@ -165,6 +170,18 @@
$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 );

Check warning on line 181 in includes/Settings.php

View check run for this annotation

Codecov / codecov/patch

includes/Settings.php#L178-L181

Added lines #L178 - L181 were not covered by tests
}
}

/**
* Register settings.
*
Expand Down Expand Up @@ -336,18 +353,15 @@
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,
]
);

Expand Down Expand Up @@ -424,6 +438,19 @@
],
]
);

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' ] );
}

/**
Expand Down
20 changes: 9 additions & 11 deletions includes/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}

Expand Down
20 changes: 17 additions & 3 deletions includes/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@
*/
private WooCommerce $woocommerce;

/**
* Context instance.
*
* @var Context Context instance.
*/
private Context $context;

/**
* Tracking constructor.
*
Expand All @@ -107,27 +114,30 @@
* @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,
Site_Kit $site_kit,
Assets $assets,
Settings $settings,
Preferences $preferences,
WooCommerce $woocommerce
WooCommerce $woocommerce,
Context $context
) {
$this->assets = $assets;
$this->experiments = $experiments;
$this->site_kit = $site_kit;
$this->settings = $settings;
$this->preferences = $preferences;
$this->woocommerce = $woocommerce;
$this->context = $context;

Check warning on line 134 in includes/Tracking.php

View check run for this annotation

Codecov / codecov/patch

includes/Tracking.php#L134

Added line #L134 was not covered by tests
}

/**
* Initializes tracking.
*
* Registers the setting in WordPress.
* Registers the script in WordPress.
*
* @since 1.0.0
*/
Expand All @@ -141,6 +151,10 @@
false
);

if ( ! $this->context->is_story_editor() && 'web-story' !== $this->context->get_screen_post_type() ) {
return;

Check warning on line 155 in includes/Tracking.php

View check run for this annotation

Codecov / codecov/patch

includes/Tracking.php#L155

Added line #L155 was not covered by tests
}

wp_add_inline_script(
self::SCRIPT_HANDLE,
'window.webStoriesTrackingSettings = ' . wp_json_encode( $this->get_settings() ) . ';'
Expand All @@ -155,7 +169,7 @@
* @return string Registration action to use.
*/
public static function get_registration_action(): string {
return 'admin_init';
return 'admin_head';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/integration/tests/Integrations/Site_Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] ) );
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/phpunit/integration/tests/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,21 +71,27 @@ 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,
$this->site_kit,
$assets,
$settings,
$preferences,
$this->woocommerce
$this->woocommerce,
$context
);
}

/**
* @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,
Expand Down