PluginProbe
Plugin Check (PCP) / 1.5.0
Plugin Check (PCP) v1.5.0
2.1.0 trunk 0.1 0.2.0 0.2.1 0.2.2 0.2.3 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 ci-artifacts
plugin-check / includes / Admin / Admin_Page.php

Admin_Page.php in Plugin Check (PCP) 1.5.0, at includes/Admin/Admin_Page.php

422 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WordPress\Plugin_Check\Admin\Admin_Page
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Admin;
9
10 use WordPress\Plugin_Check\Checker\Check;
11 use WordPress\Plugin_Check\Checker\Check_Categories;
12 use WordPress\Plugin_Check\Checker\Check_Repository;
13 use WordPress\Plugin_Check\Checker\Default_Check_Repository;
14
15 /**
16 * Class is handling admin tools page functionality.
17 *
18 * @since 1.0.0
19 *
20 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
21 */
22 final class Admin_Page {
23
24 /**
25 * Admin AJAX class instance.
26 *
27 * @since 1.0.0
28 * @var Admin_AJAX
29 */
30 protected $admin_ajax;
31
32 /**
33 * Admin page hook suffix.
34 *
35 * @since 1.0.0
36 * @var string
37 */
38 protected $hook_suffix = '';
39
40 /**
41 * Constructor.
42 *
43 * @since 1.0.0
44 *
45 * @param Admin_AJAX $admin_ajax Instance of Admin_AJAX.
46 */
47 public function __construct( Admin_AJAX $admin_ajax ) {
48 $this->admin_ajax = $admin_ajax;
49 }
50
51 /**
52 * Registers WordPress hooks for the admin page.
53 *
54 * @since 1.0.0
55 */
56 public function add_hooks() {
57 add_action( 'admin_menu', array( $this, 'add_and_initialize_page' ) );
58 add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 4 );
59 add_action( 'admin_enqueue_scripts', array( $this, 'add_jump_to_line_code_editor' ) );
60
61 $this->admin_ajax->add_hooks();
62 }
63
64 /**
65 * Adds the admin page under the tools menu.
66 *
67 * @since 1.0.0
68 */
69 public function add_page() {
70 $this->hook_suffix = add_management_page(
71 __( 'Plugin Check', 'plugin-check' ),
72 __( 'Plugin Check', 'plugin-check' ),
73 'activate_plugins',
74 'plugin-check',
75 array( $this, 'render_page' )
76 );
77 }
78
79 /**
80 * Adds and initializes the admin page under the tools menu.
81 *
82 * @since 1.0.0
83 */
84 public function add_and_initialize_page() {
85 $this->add_page();
86 add_action( 'load-' . $this->get_hook_suffix(), array( $this, 'initialize_page' ) );
87 }
88
89 /**
90 * Initializes page hooks.
91 *
92 * @since 1.0.0
93 */
94 public function initialize_page() {
95 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
96 add_action( 'admin_footer', array( $this, 'admin_footer' ) );
97
98 $this->add_help_tab();
99 }
100
101 /**
102 * Adds the plugin help tab.
103 *
104 * @since 1.1.0
105 */
106 public function add_help_tab() {
107 $screen = get_current_screen();
108
109 if ( ! $screen ) {
110 return;
111 }
112
113 $screen->add_help_tab(
114 array(
115 'id' => 'plugin-check',
116 'title' => __( 'Checks', 'plugin-check' ),
117 'content' => '',
118 'callback' => array( $this, 'render_help_tab' ),
119 )
120 );
121 }
122
123 /**
124 * Renders the plugin help tab.
125 *
126 * @since 1.1.0
127 */
128 public function render_help_tab() {
129 $check_repo = new Default_Check_Repository();
130 $collection = $check_repo->get_checks( Check_Repository::TYPE_ALL );
131
132 if ( empty( $collection ) ) {
133 return;
134 }
135
136 $category_labels = Check_Categories::get_categories();
137
138 echo '<dl>';
139
140 /**
141 * All checks to list.
142 *
143 * @var Check $check
144 */
145 foreach ( $collection as $key => $check ) {
146 $categories = array_map(
147 static function ( $category ) use ( $category_labels ) {
148 return $category_labels[ $category ] ?? $category;
149 },
150 $check->get_categories()
151 );
152 $categories = join( ', ', $categories );
153 ?>
154 <dt>
155 <code><?php echo esc_html( $key ); ?></code>
156 (<?php echo esc_html( $categories ); ?>)
157 </dt>
158 <dd>
159 <?php echo wp_kses( $check->get_description(), array( 'code' => array() ) ); ?>
160 <br>
161 <a href="<?php echo esc_url( $check->get_documentation_url() ); ?>">
162 <?php esc_html_e( 'Learn more', 'plugin-check' ); ?>
163 </a>
164 </dd>
165 <?php
166 }
167
168 echo '</dl>';
169 }
170
171 /**
172 * Loads the check's script.
173 *
174 * @since 1.0.0
175 */
176 public function enqueue_scripts() {
177 wp_enqueue_script(
178 'plugin-check-admin',
179 WP_PLUGIN_CHECK_PLUGIN_DIR_URL . 'assets/js/plugin-check-admin.js',
180 array(
181 'wp-util',
182 ),
183 WP_PLUGIN_CHECK_VERSION,
184 true
185 );
186
187 wp_enqueue_style(
188 'plugin-check-admin',
189 WP_PLUGIN_CHECK_PLUGIN_DIR_URL . 'assets/css/plugin-check-admin.css',
190 array(),
191 WP_PLUGIN_CHECK_VERSION
192 );
193
194 wp_add_inline_script(
195 'plugin-check-admin',
196 'const PLUGIN_CHECK = ' . json_encode(
197 array(
198 'nonce' => $this->admin_ajax->get_nonce(),
199 'actionGetChecksToRun' => Admin_AJAX::ACTION_GET_CHECKS_TO_RUN,
200 'actionSetUpRuntimeEnvironment' => Admin_AJAX::ACTION_SET_UP_ENVIRONMENT,
201 'actionRunChecks' => Admin_AJAX::ACTION_RUN_CHECKS,
202 'actionCleanUpRuntimeEnvironment' => Admin_AJAX::ACTION_CLEAN_UP_ENVIRONMENT,
203 'successMessage' => __( 'No errors found.', 'plugin-check' ),
204 'errorMessage' => __( 'Errors were found.', 'plugin-check' ),
205 )
206 ),
207 'before'
208 );
209 }
210
211 /**
212 * Enqueue a script in the WordPress admin on plugin-editor.php.
213 *
214 * @since 1.0.0
215 *
216 * @param string $hook_suffix The current admin page.
217 */
218 public function add_jump_to_line_code_editor( $hook_suffix ) {
219 if ( 'plugin-editor.php' !== $hook_suffix ) {
220 return;
221 }
222
223 $line = (int) ( $_GET['line'] ?? 0 );
224 if ( ! $line ) {
225 return;
226 }
227
228 wp_add_inline_script(
229 'wp-theme-plugin-editor',
230 sprintf(
231 '
232 (
233 ( originalInitCodeEditor ) => {
234 wp.themePluginEditor.initCodeEditor = function() {
235 originalInitCodeEditor.apply( this, arguments );
236 this.instance.codemirror.doc.setCursor( %d - 1 );
237 };
238 }
239 )( wp.themePluginEditor.initCodeEditor );
240 ',
241 wp_json_encode( $line )
242 )
243 );
244 }
245
246 /**
247 * Returns the list of plugins.
248 *
249 * @since 1.0.0
250 *
251 * @return array List of available plugins.
252 */
253 private function get_available_plugins() {
254 $available_plugins = get_plugins();
255
256 if ( empty( $available_plugins ) ) {
257 return array();
258 }
259
260 $plugin_check_base_name = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );
261
262 if ( isset( $available_plugins[ $plugin_check_base_name ] ) ) {
263 unset( $available_plugins[ $plugin_check_base_name ] );
264 }
265
266 return $available_plugins;
267 }
268
269 /**
270 * Renders the "Plugin Check" page.
271 *
272 * @since 1.0.0
273 *
274 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
275 */
276 public function render_page() {
277 $available_plugins = $this->get_available_plugins();
278
279 $selected_plugin_basename = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
280
281 $categories = Check_Categories::get_categories();
282
283 // Get user settings for category preferences.
284 $user_enabled_categories = get_user_setting( 'plugin_check_category_preferences', implode( '__', $this->get_default_check_categories_to_be_selected() ) );
285 $user_enabled_categories = explode( '__', $user_enabled_categories );
286
287 $check_repo = new Default_Check_Repository();
288
289 $collection = $check_repo->get_checks( Check_Repository::TYPE_ALL | Check_Repository::INCLUDE_EXPERIMENTAL )->filter(
290 static function ( Check $check ) {
291 return $check->get_stability() === Check::STABILITY_EXPERIMENTAL;
292 }
293 );
294
295 $has_experimental_checks = count( $collection ) > 0;
296
297 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php';
298 }
299
300 /**
301 * Adds "check this plugin" link in the plugins list table.
302 *
303 * @since 1.0.0
304 *
305 * @param array $actions List of actions.
306 * @param string $plugin_file Plugin main file.
307 * @param array $plugin_data An array of plugin data.
308 * @param string $context The plugin context. By default this can include 'all',
309 * 'active', 'inactive', 'recently_activated', 'upgrade',
310 * 'mustuse', 'dropins', and 'search'.
311 * @return array The modified list of actions.
312 */
313 public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
314
315 $plugin_check_base_name = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );
316 if ( in_array( $context, array( 'mustuse', 'dropins' ), true ) || $plugin_check_base_name === $plugin_file ) {
317 return $actions;
318 }
319
320 if ( current_user_can( 'activate_plugins' ) ) {
321 $actions[] = sprintf(
322 '<a href="%1$s">%2$s</a>',
323 esc_url( admin_url( "tools.php?page=plugin-check&plugin={$plugin_file}" ) ),
324 esc_html__( 'Check this plugin', 'plugin-check' )
325 );
326 }
327
328 return $actions;
329 }
330
331 /**
332 * Render the results table templates in the footer.
333 *
334 * @since 1.0.0
335 */
336 public function admin_footer() {
337 ob_start();
338 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-table.php';
339 $results_table_template = ob_get_clean();
340 wp_print_inline_script_tag(
341 $results_table_template,
342 array(
343 'id' => 'tmpl-plugin-check-results-table',
344 'type' => 'text/template',
345 )
346 );
347
348 ob_start();
349 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-row.php';
350 $results_row_template = ob_get_clean();
351 wp_print_inline_script_tag(
352 $results_row_template,
353 array(
354 'id' => 'tmpl-plugin-check-results-row',
355 'type' => 'text/template',
356 )
357 );
358
359 ob_start();
360 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-complete.php';
361 $results_row_template = ob_get_clean();
362 wp_print_inline_script_tag(
363 $results_row_template,
364 array(
365 'id' => 'tmpl-plugin-check-results-complete',
366 'type' => 'text/template',
367 )
368 );
369 ?>
370 <style>
371 #plugin-check__results .notice,
372 #plugin-check__results .notice + h4 {
373 margin-top: 20px;
374 }
375 #plugin-check__results h4:first-child {
376 margin-top: 80.5px;
377 }
378 @media ( max-width: 782px ) {
379 #plugin-check__results h4:first-child {
380 margin-top: 88.5px;
381 }
382 }
383 </style>
384 <?php
385 }
386
387 /**
388 * Gets the hook suffix under which the admin page is added.
389 *
390 * @since 1.0.0
391 *
392 * @return string Hook suffix, or empty string if admin page was not added.
393 */
394 public function get_hook_suffix() {
395 return $this->hook_suffix;
396 }
397
398 /**
399 * Gets default check categories to be selected.
400 *
401 * @since 1.0.2
402 *
403 * @return string[] An array of category slugs.
404 */
405 private function get_default_check_categories_to_be_selected() {
406 $default_check_categories = array(
407 'plugin_repo',
408 );
409
410 /**
411 * Filters the default check categories to be selected.
412 *
413 * @since 1.0.2
414 *
415 * @param string[] $default_check_categories An array of category slugs.
416 */
417 $default_categories = (array) apply_filters( 'wp_plugin_check_default_categories', $default_check_categories );
418
419 return $default_categories;
420 }
421 }
422