Skip to content

Commit

Permalink
Extract PluginActivationSiteScan code to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
delawski committed Nov 23, 2021
1 parent 17bedd5 commit 3857c7a
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 99 deletions.
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'paired_routing' => \AmpProject\AmpWP\PairedRouting::class,
'paired_url' => \AmpProject\AmpWP\PairedUrl::class,
'plugin_activation_notice' => \AmpProject\AmpWP\Admin\PluginActivationNotice::class,
'plugin_activation_site_scan' => \AmpProject\AmpWP\Admin\PluginActivationSiteScan::class,
'plugin_registry' => \AmpProject\AmpWP\PluginRegistry::class,
'plugin_suppression' => \AmpProject\AmpWP\PluginSuppression::class,
'reader_theme_loader' => \AmpProject\AmpWP\ReaderThemeLoader::class,
Expand Down
79 changes: 0 additions & 79 deletions includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ public static function init() {
AMP_Validation_Error_Taxonomy::register();

add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_validation' ] );
add_action( 'pre_current_active_plugins', [ __CLASS__, 'print_site_scan_notice' ] );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_site_scan_notice_assets' ] );
add_action( 'admin_bar_menu', [ __CLASS__, 'add_admin_bar_menu_items' ], 101 );
add_action( 'wp', [ __CLASS__, 'maybe_fail_validate_request' ] );
add_action( 'wp', [ __CLASS__, 'maybe_send_cached_validate_response' ], 20 );
Expand Down Expand Up @@ -2565,83 +2563,6 @@ public static function get_validate_url_error_message( $error_code, $error_messa
}
}

/**
* Check if the Site Scan notice should be displayed.
*
* The notice should be shown only after activating a plugin.
*
* @return bool
*/
private static function should_render_site_scan_notice() {
return ! empty( $_GET['activate'] ) || ! empty( $_GET['activate-multi'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}

/**
* On activating a plugin, render an admin notice that will do an async client-side Site Scan.
*
* @return void
*/
public static function print_site_scan_notice() {
if ( self::should_render_site_scan_notice() ) {
echo '<div id="site-scan-notice"></div>';
}
}

/**
* Enqueue Site Scan notice assets.
*
* @param string $hook The current admin page.
*/
public static function enqueue_site_scan_notice_assets( $hook ) {
if ( 'plugins.php' !== $hook || ! self::should_render_site_scan_notice() ) {
return;
}

$slug = 'amp-site-scan-notice';

$asset_file = AMP__DIR__ . '/assets/js/' . $slug . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];

wp_enqueue_script(
$slug,
amp_get_asset_url( "js/{$slug}.js" ),
$dependencies,
$version,
true
);

wp_enqueue_style(
$slug,
amp_get_asset_url( "css/{$slug}.css" ),
false,
AMP__VERSION
);

$data = [
'AMP_COMPATIBLE_PLUGINS_URL' => current_user_can( 'install_plugins' ) ? admin_url( '/plugin-install.php?tab=amp-compatible' ) : 'https://amp-wp.org/ecosystem/plugins/',
'APP_ROOT_ID' => 'site-scan-notice',
'OPTIONS_REST_PATH' => '/amp/v1/options',
'SETTINGS_LINK' => menu_page_url( AMP_Options_Manager::OPTION_NAME, false ),
'SCANNABLE_URLS_REST_PATH' => '/amp/v1/scannable-urls',
'USER_FIELD_DEVELOPER_TOOLS_ENABLED' => UserAccess::USER_FIELD_DEVELOPER_TOOLS_ENABLED,
'USERS_RESOURCE_REST_PATH' => '/wp/v2/users',
'VALIDATE_NONCE' => self::get_amp_validate_nonce(),
];

wp_add_inline_script(
$slug,
sprintf(
'var ampSiteScanNotice = %s;',
wp_json_encode( $data )
),
'before'
);

// @todo: Add REST preloader.
}

/**
* Enqueues the block validation script.
*
Expand Down
188 changes: 188 additions & 0 deletions src/Admin/PluginActivationSiteScan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php
/**
* Class PluginActivationSiteScan.
*
* Does an async Site Scan whenever any plugin is activated.
*
* @since 2.2
*
* @package AMP
*/

namespace AmpProject\AmpWP\Admin;

use AMP_Options_Manager;
use AMP_Validation_Manager;
use AmpProject\AmpWP\DevTools\UserAccess;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Delayed;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;

/**
* Class PluginActivationSiteScan
*
* @since 2.2
* @internal
*/
final class PluginActivationSiteScan implements Conditional, Delayed, Service, Registerable {
/**
* Handle for JS file.
*
* @var string
*/
const ASSET_HANDLE = 'amp-site-scan-notice';

/**
* HTML ID for the app root element.
*
* @var string
*/
const APP_ROOT_ID = 'amp-site-scan-notice';

/**
* RESTPreloader instance.
*
* @var RESTPreloader
*/
private $rest_preloader;

/**
* OnboardingWizardSubmenuPage constructor.
*
* @param RESTPreloader $rest_preloader An instance of the RESTPreloader class.
*/
public function __construct( RESTPreloader $rest_preloader ) {
$this->rest_preloader = $rest_preloader;
}

/**
* Check whether the conditional object is currently needed.
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed() {
global $pagenow;

return (
is_admin()
&&
'plugins.php' === $pagenow
&&
(
! empty( $_GET['activate'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
! empty( $_GET['activate-multi'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
)
);
}

/**
* Get the action to use for registering the service.
*
* @return string Registration action to use.
*/
public static function get_registration_action() {
return 'admin_init';
}

/**
* Runs on instantiation.
*/
public function register() {
add_action( 'pre_current_active_plugins', [ $this, 'render_notice' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
}

/**
* Render an admin notice that will do an async Site Scan.
*/
public function render_notice() {
?>
<div id="<?php echo esc_attr( self::APP_ROOT_ID ); ?>"></div>
<?php
}

/**
* Enqueue notice assets.
*/
public function enqueue_assets() {
$asset_file = AMP__DIR__ . '/assets/js/' . self::ASSET_HANDLE . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];

wp_enqueue_script(
self::ASSET_HANDLE,
amp_get_asset_url( 'js/' . self::ASSET_HANDLE . '.js' ),
$dependencies,
$version,
true
);

wp_enqueue_style(
self::ASSET_HANDLE,
amp_get_asset_url( 'css/' . self::ASSET_HANDLE . '.css' ),
false,
AMP__VERSION
);

$data = [
'AMP_COMPATIBLE_PLUGINS_URL' => $this->get_amp_compatible_plugins_url(),
'APP_ROOT_ID' => self::APP_ROOT_ID,
'OPTIONS_REST_PATH' => '/amp/v1/options',
'SETTINGS_LINK' => menu_page_url( AMP_Options_Manager::OPTION_NAME, false ),
'SCANNABLE_URLS_REST_PATH' => '/amp/v1/scannable-urls',
'USER_FIELD_DEVELOPER_TOOLS_ENABLED' => UserAccess::USER_FIELD_DEVELOPER_TOOLS_ENABLED,
'USERS_RESOURCE_REST_PATH' => '/wp/v2/users',
'VALIDATE_NONCE' => AMP_Validation_Manager::get_amp_validate_nonce(),
];

wp_add_inline_script(
self::ASSET_HANDLE,
sprintf(
'var ampSiteScanNotice = %s;',
wp_json_encode( $data )
),
'before'
);

$this->add_preload_rest_paths();
}

/**
* Get a URL to AMP compatible plugins directory.
*
* For users capable of installing plugins, the link should lead to the Plugin install page.
* Other users will be directed to the plugins page on amp-wp.org.
*
* @return string URL to AMP compatible plugins directory.
*/
protected function get_amp_compatible_plugins_url() {
if ( current_user_can( 'install_plugins' ) ) {
return admin_url( '/plugin-install.php?tab=amp-compatible' );
}

return 'https://amp-wp.org/ecosystem/plugins/';
}

/**
* Adds REST paths to preload.
*/
protected function add_preload_rest_paths() {
$paths = [
'/amp/v1/options',
add_query_arg(
[
'_fields' => [ 'url', 'amp_url', 'type', 'label' ],
],
'/amp/v1/scannable-urls'
),
'/wp/v2/users/me',
];

foreach ( $paths as $path ) {
$this->rest_preloader->add_preloaded_path( $path );
}
}
}
1 change: 1 addition & 0 deletions src/AmpWpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
'paired_routing' => PairedRouting::class,
'paired_url' => PairedUrl::class,
'plugin_activation_notice' => Admin\PluginActivationNotice::class,
'plugin_activation_site_scan' => Admin\PluginActivationSiteScan::class,
'plugin_registry' => PluginRegistry::class,
'plugin_suppression' => PluginSuppression::class,
'reader_theme_loader' => ReaderThemeLoader::class,
Expand Down
Loading

0 comments on commit 3857c7a

Please sign in to comment.