Skip to content

Commit

Permalink
Implement dependency resolution for services (#8513)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Oct 5, 2021
1 parent 9c52fb2 commit 720a39a
Show file tree
Hide file tree
Showing 95 changed files with 2,108 additions and 1,007 deletions.
3 changes: 3 additions & 0 deletions bin/local-env/install-wordpress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion includes/AMP/Output_Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 22 additions & 5 deletions includes/AMP/Sanitization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand Down
26 changes: 23 additions & 3 deletions includes/AdSense.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand All @@ -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' ) );
}

/**
Expand Down
24 changes: 22 additions & 2 deletions includes/Ad_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 );
}

/**
Expand All @@ -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' ) );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/Admin/Activation_Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down
65 changes: 41 additions & 24 deletions includes/Admin/Cross_Origin_Isolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}
}
24 changes: 23 additions & 1 deletion includes/Admin/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 '';
Expand Down
16 changes: 13 additions & 3 deletions includes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
* Class Analytics
*/
class Analytics extends Service_Base {
/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private $settings;

/**
* Experiments instance.
*
Expand All @@ -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.
*
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
Expand Down
Loading

0 comments on commit 720a39a

Please sign in to comment.