diff --git a/bin/local-env/install-wordpress.sh b/bin/local-env/install-wordpress.sh index 128385616480..73e50733bc15 100755 --- a/bin/local-env/install-wordpress.sh +++ b/bin/local-env/install-wordpress.sh @@ -209,6 +209,9 @@ wp media import /var/www/html/wp-content/e2e-assets/example-1.jpg --quiet wp media import /var/www/html/wp-content/e2e-assets/example-2.jpg --quiet wp media import /var/www/html/wp-content/e2e-assets/example-3.png --quiet +# Ensures that the patch command below always works. +wp option update web_stories_experiments '{}' --format=json + wp option patch insert web_stories_experiments enableSVG 1 wp media import /var/www/html/wp-content/e2e-assets/video-play.svg wp option patch insert web_stories_experiments enableSVG 0 diff --git a/includes/AMP/Output_Buffer.php b/includes/AMP/Output_Buffer.php index 9ddf4fd6b4b8..382a77d692cd 100644 --- a/includes/AMP/Output_Buffer.php +++ b/includes/AMP/Output_Buffer.php @@ -26,7 +26,6 @@ namespace Google\Web_Stories\AMP; -use DOMElement; use Exception; use Google\Web_Stories\Exception\SanitizationException; use Google\Web_Stories\Infrastructure\Conditional; diff --git a/includes/AMP/Sanitization.php b/includes/AMP/Sanitization.php index 69b1685054b6..244e545b9243 100644 --- a/includes/AMP/Sanitization.php +++ b/includes/AMP/Sanitization.php @@ -26,12 +26,10 @@ namespace Google\Web_Stories\AMP; -use Google\Web_Stories\Experiments; -use Google\Web_Stories\Media\Image_Sizes; use Google\Web_Stories\Model\Story; +use Google\Web_Stories\Services; use Google\Web_Stories\Settings; use Google\Web_Stories\Story_Post_Type; -use Google\Web_Stories\Traits\Publisher; use Google\Web_Stories_Dependencies\AMP_Allowed_Tags_Generated; use Google\Web_Stories_Dependencies\AMP_Content_Sanitizer; use Google\Web_Stories_Dependencies\AMP_Dev_Mode_Sanitizer; @@ -46,7 +44,6 @@ use Google\Web_Stories_Dependencies\AmpProject\Extension; use Google\Web_Stories_Dependencies\AmpProject\Tag; use DOMElement; -use WP_Post; /** * Sanitization class. @@ -58,6 +55,26 @@ * @see \AMP_Theme_Support */ class Sanitization { + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + + /** + * Analytics constructor. + * + * @since 1.12.0 + * + * @param Settings $settings Settings instance. + * + * @return void + */ + public function __construct( Settings $settings ) { + $this->settings = $settings; + } + /** * Sanitizes a document. * @@ -410,7 +427,7 @@ protected function get_sanitizers(): array { $post = get_queried_object(); if ( $post instanceof \WP_Post && Story_Post_Type::POST_TYPE_SLUG === get_post_type( $post ) ) { - $video_cache_enabled = (bool) get_option( Settings::SETTING_NAME_VIDEO_CACHE ); + $video_cache_enabled = (bool) $this->settings->get_setting( $this->settings::SETTING_NAME_VIDEO_CACHE ); $story = new Story(); $story->load_from_post( $post ); diff --git a/includes/AdSense.php b/includes/AdSense.php index ef4089580a1b..db3a40cc2863 100644 --- a/includes/AdSense.php +++ b/includes/AdSense.php @@ -30,6 +30,26 @@ * Class AdSense */ class AdSense extends Service_Base { + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + + /** + * Analytics constructor. + * + * @since 1.12.0 + * + * @param Settings $settings Settings instance. + * + * @return void + */ + public function __construct( Settings $settings ) { + $this->settings = $settings; + } + /** * Initializes all hooks. * @@ -49,7 +69,7 @@ public function register() { * @return string Publisher ID. */ private function get_publisher_id(): string { - return (string) get_option( Settings::SETTING_NAME_ADSENSE_PUBLISHER_ID ); + return (string) $this->settings->get_setting( $this->settings::SETTING_NAME_ADSENSE_PUBLISHER_ID ); } /** @@ -60,7 +80,7 @@ private function get_publisher_id(): string { * @return string Slot ID. */ private function get_slot_id(): string { - return (string) get_option( Settings::SETTING_NAME_ADSENSE_SLOT_ID ); + return (string) $this->settings->get_setting( $this->settings::SETTING_NAME_ADSENSE_SLOT_ID ); } /** @@ -71,7 +91,7 @@ private function get_slot_id(): string { * @return bool */ private function is_enabled(): bool { - return ( 'adsense' === (string) get_option( Settings::SETTING_NAME_AD_NETWORK, 'none' ) ); + return ( 'adsense' === (string) $this->settings->get_setting( $this->settings::SETTING_NAME_AD_NETWORK, 'none' ) ); } /** diff --git a/includes/Ad_Manager.php b/includes/Ad_Manager.php index 95ec3e2abdcf..56ae14bfdd91 100644 --- a/includes/Ad_Manager.php +++ b/includes/Ad_Manager.php @@ -30,6 +30,26 @@ * Class Ad_Manager */ class Ad_Manager extends Service_Base { + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + + /** + * Analytics constructor. + * + * @since 1.12.0 + * + * @param Settings $settings Settings instance. + * + * @return void + */ + public function __construct( Settings $settings ) { + $this->settings = $settings; + } + /** * Initializes all hooks. * @@ -49,7 +69,7 @@ public function register() { * @return string Slot ID. */ private function get_slot_id(): string { - return (string) get_option( Settings::SETTING_NAME_AD_MANAGER_SLOT_ID ); + return (string) $this->settings->get_setting( $this->settings::SETTING_NAME_AD_MANAGER_SLOT_ID ); } /** @@ -60,7 +80,7 @@ private function get_slot_id(): string { * @return bool */ private function is_enabled(): bool { - return ( 'admanager' === (string) get_option( Settings::SETTING_NAME_AD_NETWORK, 'none' ) ); + return ( 'admanager' === (string) $this->settings->get_setting( $this->settings::SETTING_NAME_AD_NETWORK, 'none' ) ); } /** diff --git a/includes/Admin/Activation_Flag.php b/includes/Admin/Activation_Flag.php index cd9d393e6d1d..ddfc51b178f2 100644 --- a/includes/Admin/Activation_Flag.php +++ b/includes/Admin/Activation_Flag.php @@ -27,12 +27,12 @@ namespace Google\Web_Stories\Admin; use Google\Web_Stories\Infrastructure\Registerable; -use Google\Web_Stories\Infrastructure\Service as ServiceInterface; +use Google\Web_Stories\Infrastructure\Service; /** * Class Activation_Flag. */ -class Activation_Flag implements ServiceInterface, Registerable { +class Activation_Flag implements Service, Registerable { const OPTION_SHOW_ACTIVATION_NOTICE = 'web_stories_show_activation_notice'; /** diff --git a/includes/Admin/Cross_Origin_Isolation.php b/includes/Admin/Cross_Origin_Isolation.php index 36d4bf63b781..13cf6b11e4a0 100644 --- a/includes/Admin/Cross_Origin_Isolation.php +++ b/includes/Admin/Cross_Origin_Isolation.php @@ -29,7 +29,9 @@ namespace Google\Web_Stories\Admin; use Google\Web_Stories\Infrastructure\Conditional; +use Google\Web_Stories\Infrastructure\HasRequirements; use Google\Web_Stories\Service_Base; +use Google\Web_Stories\Services; use Google\Web_Stories\Traits\Screen; use Google\Web_Stories\User\Preferences; @@ -38,7 +40,7 @@ * * @package Google\Web_Stories */ -class Cross_Origin_Isolation extends Service_Base implements Conditional { +class Cross_Origin_Isolation extends Service_Base implements Conditional, HasRequirements { use Screen; /** @@ -81,6 +83,44 @@ public static function get_registration_action_priority(): int { return 11; } + /** + * Check whether the conditional object is currently needed. + * + * @since 1.6.0 + * + * @return bool Whether the conditional object is needed. + */ + public static function is_needed(): bool { + $user_id = get_current_user_id(); + if ( ! $user_id ) { + return false; + } + + // Cross-origin isolation is not needed if users can't upload files anyway. + if ( ! user_can( $user_id, 'upload_files' ) ) { + return false; + } + + $user_preferences = Services::get( 'user_preferences' ); + + return rest_sanitize_boolean( + $user_preferences->get_preference( $user_id, $user_preferences::MEDIA_OPTIMIZATION_META_KEY ) + ); + } + + /** + * Get the list of service IDs required for this service to be registered. + * + * Needed because the service is used in the static `is_needed()` method. + * + * @since 1.12.0 + * + * @return string[] List of required services. + */ + public static function get_requirements(): array { + return [ 'user_preferences' ]; + } + /** * Start output buffer and add headers. * @@ -293,27 +333,4 @@ public function custom_print_media_templates() { private function starts_with( string $string, string $start_string ): bool { return 0 === strpos( $string, $start_string ); } - - /** - * Check whether the conditional object is currently needed. - * - * @since 1.6.0 - * - * @return bool Whether the conditional object is needed. - */ - public static function is_needed(): bool { - $user_id = get_current_user_id(); - if ( ! $user_id ) { - return false; - } - - // Cross-origin isolation is not needed if users can't upload files anyway. - if ( ! user_can( $user_id, 'upload_files' ) ) { - return false; - } - - $check = get_user_meta( $user_id, Preferences::MEDIA_OPTIMIZATION_META_KEY, true ); - - return rest_sanitize_boolean( $check ); - } } diff --git a/includes/Admin/Customizer.php b/includes/Admin/Customizer.php index 2769054d8b6a..c17b4e59a049 100644 --- a/includes/Admin/Customizer.php +++ b/includes/Admin/Customizer.php @@ -26,6 +26,8 @@ namespace Google\Web_Stories\Admin; +use Google\Web_Stories\Services; +use Google\Web_Stories\Settings; use Google\Web_Stories\Story_Query; use Google\Web_Stories\Service_Base; use Google\Web_Stories\Traits\Layout; @@ -71,6 +73,26 @@ class Customizer extends Service_Base { */ private $wp_customize; + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + + /** + * Analytics constructor. + * + * @since 1.12.0 + * + * @param Settings $settings Settings instance. + * + * @return void + */ + public function __construct( Settings $settings ) { + $this->settings = $settings; + } + /** * Initializes the customizer logic. * @@ -536,7 +558,7 @@ public function validate_number_of_columns( $validity, $value ) { * @return string */ public function render_stories(): string { - $options = get_option( self::STORY_OPTION ); + $options = $this->settings->get_setting( self::STORY_OPTION ); if ( empty( $options['show_stories'] ) || true !== $options['show_stories'] ) { return ''; diff --git a/includes/Analytics.php b/includes/Analytics.php index 2a62c22cc1c1..f647d429bbed 100644 --- a/includes/Analytics.php +++ b/includes/Analytics.php @@ -30,6 +30,13 @@ * Class Analytics */ class Analytics extends Service_Base { + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + /** * Experiments instance. * @@ -42,13 +49,16 @@ class Analytics extends Service_Base { * * @since 1.12.0 * + * @param Settings $settings Settings instance. * @param Experiments $experiments Experiments instance. * * @return void */ - public function __construct( Experiments $experiments ) { + public function __construct( Settings $settings, Experiments $experiments ) { + $this->settings = $settings; $this->experiments = $experiments; } + /** * Initializes all hooks. * @@ -68,7 +78,7 @@ public function register() { * @return string Tracking ID. */ public function get_tracking_id(): string { - return (string) get_option( Settings::SETTING_NAME_TRACKING_ID ); + return (string) $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_ID ); } /** @@ -229,7 +239,7 @@ public function print_analytics_tag() { } if ( - (bool) get_option( Settings::SETTING_NAME_USING_LEGACY_ANALYTICS ) || + (bool) $this->settings->get_setting( $this->settings::SETTING_NAME_USING_LEGACY_ANALYTICS ) || ! $this->experiments->is_experiment_enabled( 'enableAutoAnalyticsMigration' ) ) { $this->print_amp_analytics_tag( $tracking_id ); diff --git a/includes/Experiments.php b/includes/Experiments.php index 692b966e1026..f2d317bde6ef 100644 --- a/includes/Experiments.php +++ b/includes/Experiments.php @@ -46,6 +46,26 @@ class Experiments extends Service_Base { */ private $hook_suffix; + /** + * Settings instance. + * + * @var Settings Settings instance. + */ + private $settings; + + /** + * Analytics constructor. + * + * @since 1.12.0 + * + * @param Settings $settings Settings instance. + * + * @return void + */ + public function __construct( Settings $settings ) { + $this->settings = $settings; + } + /** * Initializes experiments * @@ -165,7 +185,7 @@ public function display_experiment_field( array $args ) {