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

Implement dependency resolution for services #8513

Merged
merged 62 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
7b4db96
Implement dependency resolution for services
swissspidy Jul 29, 2021
bc5472b
Fix incorrect test
swissspidy Jul 29, 2021
b6421cb
LInt fix
swissspidy Jul 30, 2021
45cacb2
Make experiment toggling more robust
swissspidy Jul 30, 2021
70d5119
Merge branch 'main' into add/services-dependencies
swissspidy Jul 30, 2021
f8a3a1b
Fix `Settings::register()`
swissspidy Aug 2, 2021
977631a
Merge branch 'main' into add/services-dependencies
swissspidy Aug 4, 2021
2127db6
Add some types
swissspidy Aug 4, 2021
1b9590c
Pull upstream changes
swissspidy Aug 4, 2021
f99c380
Merge branch 'main' into add/services-dependencies
swissspidy Aug 11, 2021
a7a4233
Merge branch 'main' into add/services-dependencies
swissspidy Aug 17, 2021
f4bd461
Merge branch 'main' into add/services-dependencies
swissspidy Aug 17, 2021
7c6527d
Merge branch 'main' into add/services-dependencies
swissspidy Aug 20, 2021
c50f20c
Update docs
swissspidy Aug 20, 2021
9686c90
Ensure services that are delayed and have requirements are scheduled …
swissspidy Aug 20, 2021
e8db36d
Fix PHPDoc
swissspidy Aug 20, 2021
fbbd35f
Fix typo
swissspidy Aug 20, 2021
617873c
Add some more robust assertions
swissspidy Aug 20, 2021
9955cbe
Move `rest_api_init` to `wpSetUpBeforeClass`
swissspidy Aug 20, 2021
0689fc3
Merge branch 'main' into add/services-dependencies
swissspidy Sep 8, 2021
62bd00f
Add return type
swissspidy Sep 8, 2021
1fbd86f
Merge branch 'main' into add/services-dependencies
swissspidy Sep 22, 2021
da6f69f
Code & test fixes
swissspidy Sep 22, 2021
2e06989
Implement `HasRequirements`
swissspidy Sep 22, 2021
700d36d
Add requirement for KSES class
swissspidy Sep 23, 2021
e841195
Merge branch 'main' into add/services-dependencies
swissspidy Sep 23, 2021
860813b
Merge branch 'main' into add/services-dependencies
swissspidy Sep 30, 2021
1aede89
Add missing call
swissspidy Sep 30, 2021
d5f2476
Merge branch 'main' into add/services-dependencies
swissspidy Sep 30, 2021
3a6c1c3
More dependency injection
swissspidy Sep 30, 2021
6e8263f
Some cleanup
swissspidy Sep 30, 2021
371dff4
Delete user meta in tear down to prevent leaking
swissspidy Sep 30, 2021
431bc40
More dependency injection
swissspidy Sep 30, 2021
3d2e79f
Merge branch 'main' into add/services-dependencies
swissspidy Sep 30, 2021
11782d4
Further improve tests
swissspidy Oct 1, 2021
9ed3eb8
Merge branch 'main' into add/services-dependencies
swissspidy Oct 1, 2021
26daf06
Remove unneeded code
swissspidy Oct 1, 2021
0490ff2
Remove count
swissspidy Oct 1, 2021
6d51579
Remove unneeded function call
swissspidy Oct 1, 2021
ffca829
Fix fixture setup when running tests in random order
swissspidy Oct 1, 2021
7e6c49b
Merge branch 'main' into add/services-dependencies
swissspidy Oct 1, 2021
03dc2fd
Remove debug cruft
swissspidy Oct 1, 2021
2d001bd
Update variables
swissspidy Oct 1, 2021
435cc1c
Add missing `parent::set_up()` call
swissspidy Oct 1, 2021
3ad286f
Use `DependencyInjectedTestCase`
swissspidy Oct 1, 2021
7256371
More test improvements
swissspidy Oct 1, 2021
e8131a3
Merge branch 'main' into add/services-dependencies
swissspidy Oct 1, 2021
f40b47a
Merge branch 'main' into add/services-dependencies
swissspidy Oct 1, 2021
73267e9
Get settings name via instance
swissspidy Oct 4, 2021
042279f
More test updates
swissspidy Oct 4, 2021
cd9a197
Merge branch 'main' into add/services-dependencies
swissspidy Oct 4, 2021
8d84f58
Lint fixes
swissspidy Oct 4, 2021
0bd0263
Fix kses
swissspidy Oct 4, 2021
4571e94
Merge branch 'main' into add/services-dependencies
swissspidy Oct 4, 2021
f013709
Merge branch 'main' into add/services-dependencies
swissspidy Oct 5, 2021
3ff2c56
Update rest tests
swissspidy Oct 5, 2021
66dbc99
Update list of shared instances
swissspidy Oct 5, 2021
b792d07
Merge branch 'main' into add/services-dependencies
swissspidy Oct 5, 2021
d4ebbe7
Multi-line function call
swissspidy Oct 5, 2021
345ed48
Extract variable
swissspidy Oct 5, 2021
4b7eb43
Merge branch 'main' into add/services-dependencies
swissspidy Oct 5, 2021
da24557
Remove alias
swissspidy Oct 5, 2021
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
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