-
Notifications
You must be signed in to change notification settings - Fork 384
/
ValidationCounts.php
141 lines (124 loc) · 3.36 KB
/
ValidationCounts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Class ValidationCounts.
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP\Admin;
use AMP_Options_Manager;
use AMP_Validated_URL_Post_Type;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Delayed;
use AmpProject\AmpWP\Infrastructure\HasRequirements;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use AmpProject\AmpWP\Services;
/**
* Loads assets necessary to retrieve and show the unreviewed counts for validated URLs and validation errors in
* the AMP admin menu.
*
* @since 2.1
* @internal
*/
final class ValidationCounts implements Service, Registerable, Conditional, Delayed, HasRequirements {
/**
* Assets handle.
*
* @var string
*/
const ASSETS_HANDLE = 'amp-validation-counts';
/**
* RESTPreloader instance.
*
* @var RESTPreloader
*/
private $rest_preloader;
/**
* ValidationCounts constructor.
*
* @param RESTPreloader $rest_preloader An instance of the RESTPreloader class.
*/
public function __construct( RESTPreloader $rest_preloader ) {
$this->rest_preloader = $rest_preloader;
}
/**
* Get the action to use for registering the service.
*
* @return string Registration action to use.
*/
public static function get_registration_action() {
return 'admin_enqueue_scripts';
}
/**
* Get the list of service IDs required for this service to be registered.
*
* @return string[] List of required services.
*/
public static function get_requirements() {
return [
'dependency_support',
'dev_tools.user_access',
];
}
/**
* Check whether the conditional object is currently needed.
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed() {
return Services::get( 'dependency_support' )->has_support() && Services::get( 'dev_tools.user_access' )->is_user_enabled();
}
/**
* Runs on instantiation.
*/
public function register() {
$this->enqueue_scripts();
}
/**
* Enqueue admin assets.
*/
public function enqueue_scripts() {
if ( $this->is_options_menu() ) {
do_action( 'amp_register_polyfills' );
}
$asset_file = AMP__DIR__ . '/assets/js/' . self::ASSETS_HANDLE . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];
wp_enqueue_script(
self::ASSETS_HANDLE,
amp_get_asset_url( 'js/' . self::ASSETS_HANDLE . '.js' ),
$dependencies,
$version,
true
);
wp_enqueue_style(
self::ASSETS_HANDLE,
amp_get_asset_url( 'css/' . self::ASSETS_HANDLE . '.css' ),
false,
$version
);
$this->maybe_add_preload_rest_paths();
}
/**
* Adds REST paths to preload.
*
* Preload validation counts data on an admin screen that has the AMP Options page as a parent or on any admin
* screen related to `amp_validation_error` post type (which includes the `amp_validation_error` taxonomy).
*/
protected function maybe_add_preload_rest_paths() {
if ( $this->is_options_menu() ) {
$this->rest_preloader->add_preloaded_path( '/amp/v1/unreviewed-validation-counts' );
}
}
/**
* Whether the current screen is pages inside the AMP Options menu.
*/
public function is_options_menu() {
return (
AMP_Options_Manager::OPTION_NAME === get_admin_page_parent()
||
AMP_Validated_URL_Post_Type::POST_TYPE_SLUG === get_current_screen()->post_type
);
}
}