Skip to content

Commit

Permalink
Refactor ScannableURLProvider and URLValidationProvider into services
Browse files Browse the repository at this point in the history
* Eliminate extraneous URLScanningContext.
* Use template conditionals as type labels in WP-CLI output.
* Prepare ScannableURLProvider to return URLs that don't correspond to the currently-set template mode for Site Scan in Onboarding Wizard.
  • Loading branch information
westonruter committed Oct 15, 2021
1 parent da77a0d commit 5bbadea
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 342 deletions.
2 changes: 2 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
'url_validation_cron' => \AmpProject\AmpWP\Validation\URLValidationCron::class,
'url_validation_rest_controller' => \AmpProject\AmpWP\Validation\URLValidationRESTController::class,
'validated_url_stylesheet_gc' => \AmpProject\AmpWP\BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'validation.scannable_url_provider' => \AmpProject\AmpWP\Validation\ScannableURLProvider::class,
'validation.url_validation_provider' => \AmpProject\AmpWP\Validation\URLValidationProvider::class,
] )
);

Expand Down
4 changes: 4 additions & 0 deletions src/AmpWpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use AmpProject\AmpWP\Support\SupportCliCommand;
use AmpProject\AmpWP\Support\SupportRESTController;
use AmpProject\AmpWP\Validation\SavePostValidationEvent;
use AmpProject\AmpWP\Validation\ScannableURLProvider;
use AmpProject\AmpWP\Validation\URLValidationCron;
use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;
use AmpProject\AmpWP\Validation\URLValidationProvider;
use AmpProject\Optimizer;

use AmpProject\RemoteGetRequest;
Expand Down Expand Up @@ -121,6 +123,8 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
'url_validation_cron' => URLValidationCron::class,
'url_validation_rest_controller' => Validation\URLValidationRESTController::class,
'validated_url_stylesheet_gc' => BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'validation.scannable_url_provider' => ScannableURLProvider::class,
'validation.url_validation_provider' => URLValidationProvider::class,
];

/**
Expand Down
124 changes: 31 additions & 93 deletions src/Cli/ValidationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

namespace AmpProject\AmpWP\Cli;

use AMP_Options_Manager;
use AMP_Theme_Support;
use AMP_Validated_URL_Post_Type;
use AMP_Validation_Error_Taxonomy;
use AMP_Validation_Manager;
use AmpProject\AmpWP\Admin\ReaderThemes;
use AmpProject\AmpWP\Infrastructure\CliCommand;
use AmpProject\AmpWP\Infrastructure\Service;
use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\Validation\URLValidationProvider;
use AmpProject\AmpWP\Validation\ScannableURLProvider;
use AmpProject\AmpWP\Validation\URLScanningContext;
use AmpProject\AmpWP\Validation\URLValidationProvider;
use Exception;
use WP_CLI;
use WP_CLI\Utils;
Expand Down Expand Up @@ -107,6 +102,17 @@ public static function get_command_name() {
return 'amp validation';
}

/**
* Construct.
*
* @param URLValidationProvider $url_validation_provider URL validation provider.
* @param ScannableURLProvider $scannable_url_provider Scannable URL provider.
*/
public function __construct( URLValidationProvider $url_validation_provider, ScannableURLProvider $scannable_url_provider ) {
$this->url_validation_provider = $url_validation_provider;
$this->scannable_url_provider = $scannable_url_provider;
}

/**
* Crawl the entire site to get AMP validation results.
*
Expand Down Expand Up @@ -135,14 +141,21 @@ public static function get_command_name() {
public function run( /** @noinspection PhpUnusedParameterInspection */ $args, $assoc_args ) {
$this->assoc_args = $assoc_args;

$scannable_url_provider = $this->get_validation_url_provider();
$url_validation_provider = $this->get_validation_provider();
$urls = $scannable_url_provider->get_urls();

if ( Utils\get_flag_value( $this->assoc_args, self::FLAG_NAME_FORCE_VALIDATION, false ) ) {
WP_CLI::warning( sprintf( 'The --%s argument is obsolete.', self::FLAG_NAME_FORCE_VALIDATION ) );
}

$include_conditionals = Utils\get_flag_value( $this->assoc_args, self::INCLUDE_ARGUMENT, [] );
if ( is_string( $include_conditionals ) ) {
$include_conditionals = explode( ',', $include_conditionals );
}
$this->scannable_url_provider->set_include_conditionals( $include_conditionals );

$limit_per_type = Utils\get_flag_value( $this->assoc_args, self::LIMIT_URLS_ARGUMENT, 100 );
$this->scannable_url_provider->set_limit_per_type( $limit_per_type );

$urls = $this->scannable_url_provider->get_urls();

$number_urls_to_crawl = count( $urls );
if ( ! $number_urls_to_crawl ) {
if ( ! empty( Utils\get_flag_value( $this->assoc_args, self::INCLUDE_ARGUMENT, [] ) ) ) {
Expand All @@ -168,12 +181,12 @@ public function run( /** @noinspection PhpUnusedParameterInspection */ $args, $a

$this->wp_cli_progress->finish();

$key_template_type = 'Template or content type';
$key_template_type = 'Template';
$key_url_count = 'URL Count';
$key_validity_rate = 'Validity Rate';

$table_validation_by_type = [];
foreach ( $url_validation_provider->get_validity_by_type() as $type_name => $validity ) {
foreach ( $this->url_validation_provider->get_validity_by_type() as $type_name => $validity ) {
$table_validation_by_type[] = [
$key_template_type => $type_name,
$key_url_count => $validity['total'],
Expand All @@ -190,9 +203,9 @@ public function run( /** @noinspection PhpUnusedParameterInspection */ $args, $a
WP_CLI::success(
sprintf(
'%3$d crawled URLs have invalid markup kept out of %2$d total with AMP validation issue(s); %1$d URLs were crawled.',
$url_validation_provider->get_number_validated(),
$url_validation_provider->get_total_errors(),
$url_validation_provider->get_unaccepted_errors()
$this->url_validation_provider->get_number_validated(),
$this->url_validation_provider->get_total_errors(),
$this->url_validation_provider->get_unaccepted_errors()
)
);

Expand All @@ -211,93 +224,18 @@ public function run( /** @noinspection PhpUnusedParameterInspection */ $args, $a
WP_CLI::line( sprintf( 'For more details, please see: %s', $url_more_details ) );
}

/**
* Provides the ScannableURLProvider instance.
*
* @return ScannableURLProvider
* @throws WP_CLI\ExitException If templates are disallowed by current config.
*/
private function get_validation_url_provider() {
if ( ! is_null( $this->scannable_url_provider ) ) {
return $this->scannable_url_provider;
}

$include_conditionals = Utils\get_flag_value( $this->assoc_args, self::INCLUDE_ARGUMENT, [] );
if ( is_string( $include_conditionals ) ) {
$include_conditionals = explode( ',', $include_conditionals );
}

$limit_type_validate_count = Utils\get_flag_value( $this->assoc_args, self::LIMIT_URLS_ARGUMENT, 100 );

// Handle special case for Legacy Reader mode.
if (
AMP_Theme_Support::READER_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT )
&&
ReaderThemes::DEFAULT_READER_THEME === AMP_Options_Manager::get_option( Option::READER_THEME )
) {
$allowed_templates = [
'is_singular',
];
if ( 'page' === get_option( 'show_on_front' ) ) {
$allowed_templates[] = 'is_home';
$allowed_templates[] = 'is_front_page';
}

$disallowed_templates = array_diff( $include_conditionals, $allowed_templates );
if ( ! empty( $disallowed_templates ) ) {
WP_CLI::error(
sprintf(
'Templates not supported in legacy Reader mode with current configuration: %s',
implode( ',', $disallowed_templates )
)
);
}

if ( empty( $include_conditionals ) ) {
$include_conditionals = $allowed_templates;
}
}

$this->scannable_url_provider = new ScannableURLProvider(
new URLScanningContext(
$limit_type_validate_count,
$include_conditionals
)
);

return $this->scannable_url_provider;
}

/**
* Provides the site scan instance.
*
* @return URLValidationProvider
*/
private function get_validation_provider() {
if ( ! is_null( $this->url_validation_provider ) ) {
return $this->url_validation_provider;
}

$this->url_validation_provider = new URLValidationProvider();

return $this->url_validation_provider;
}

/**
* Validates the URLs.
*
* @param array $urls URLs to validate, or null to get URLs from the scannable URL provider.
*/
private function validate_urls( $urls = null ) {
$url_validation_provider = $this->get_validation_provider();

private function validate_urls( $urls = [] ) {
if ( ! $urls ) {
$scannable_url_provider = $this->get_validation_url_provider();
$urls = $scannable_url_provider->get_urls();
$urls = $this->scannable_url_provider->get_urls();
}

foreach ( $urls as $url ) {
$validity = $url_validation_provider->get_url_validation( $url['url'], $url['type'] );
$validity = $this->url_validation_provider->get_url_validation( $url['url'], $url['type'] );

if ( $this->wp_cli_progress ) {
$this->wp_cli_progress->tick();
Expand Down
Loading

0 comments on commit 5bbadea

Please sign in to comment.