PluginProbe
Plugin Check (PCP) / 1.0.2
Plugin Check (PCP) v1.0.2
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.0.2, at includes/Admin/Admin_Page.php

330 lines 8.0 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_Categories;
11
12 /**
13 * Class is handling admin tools page functionality.
14 *
15 * @since 1.0.0
16 *
17 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
18 */
19 final class Admin_Page {
20
21 /**
22 * Admin AJAX class instance.
23 *
24 * @since 1.0.0
25 * @var Admin_AJAX
26 */
27 protected $admin_ajax;
28
29 /**
30 * Admin page hook suffix.
31 *
32 * @since 1.0.0
33 * @var string
34 */
35 protected $hook_suffix = '';
36
37 /**
38 * Constructor.
39 *
40 * @since 1.0.0
41 *
42 * @param Admin_AJAX $admin_ajax Instance of Admin_AJAX.
43 */
44 public function __construct( Admin_AJAX $admin_ajax ) {
45 $this->admin_ajax = $admin_ajax;
46 }
47
48 /**
49 * Registers WordPress hooks for the admin page.
50 *
51 * @since 1.0.0
52 */
53 public function add_hooks() {
54 add_action( 'admin_menu', array( $this, 'add_and_initialize_page' ) );
55 add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 4 );
56 add_action( 'admin_enqueue_scripts', array( $this, 'add_jump_to_line_code_editor' ) );
57
58 $this->admin_ajax->add_hooks();
59 }
60
61 /**
62 * Adds the admin page under the tools menu.
63 *
64 * @since 1.0.0
65 */
66 public function add_page() {
67 $this->hook_suffix = add_management_page(
68 __( 'Plugin Check', 'plugin-check' ),
69 __( 'Plugin Check', 'plugin-check' ),
70 'activate_plugins',
71 'plugin-check',
72 array( $this, 'render_page' )
73 );
74 }
75
76 /**
77 * Adds and initializes the admin page under the tools menu.
78 *
79 * @since 1.0.0
80 */
81 public function add_and_initialize_page() {
82 $this->add_page();
83 add_action( 'load-' . $this->get_hook_suffix(), array( $this, 'initialize_page' ) );
84 }
85
86 /**
87 * Initializes page hooks.
88 *
89 * @since 1.0.0
90 */
91 public function initialize_page() {
92 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
93 add_action( 'admin_footer', array( $this, 'admin_footer' ) );
94 }
95
96 /**
97 * Loads the check's script.
98 *
99 * @since 1.0.0
100 */
101 public function enqueue_scripts() {
102 wp_enqueue_script(
103 'plugin-check-admin',
104 WP_PLUGIN_CHECK_PLUGIN_DIR_URL . 'assets/js/plugin-check-admin.js',
105 array(
106 'wp-util',
107 ),
108 WP_PLUGIN_CHECK_VERSION,
109 true
110 );
111
112 wp_add_inline_script(
113 'plugin-check-admin',
114 'const PLUGIN_CHECK = ' . json_encode(
115 array(
116 'nonce' => $this->admin_ajax->get_nonce(),
117 'actionGetChecksToRun' => Admin_AJAX::ACTION_GET_CHECKS_TO_RUN,
118 'actionSetUpRuntimeEnvironment' => Admin_AJAX::ACTION_SET_UP_ENVIRONMENT,
119 'actionRunChecks' => Admin_AJAX::ACTION_RUN_CHECKS,
120 'actionCleanUpRuntimeEnvironment' => Admin_AJAX::ACTION_CLEAN_UP_ENVIRONMENT,
121 'successMessage' => __( 'No errors found.', 'plugin-check' ),
122 'errorMessage' => __( 'Errors were found.', 'plugin-check' ),
123 )
124 ),
125 'before'
126 );
127 }
128
129 /**
130 * Enqueue a script in the WordPress admin on plugin-editor.php.
131 *
132 * @since 1.0.0
133 *
134 * @param string $hook_suffix The current admin page.
135 */
136 public function add_jump_to_line_code_editor( $hook_suffix ) {
137 if ( 'plugin-editor.php' !== $hook_suffix ) {
138 return;
139 }
140
141 $line = (int) ( $_GET['line'] ?? 0 );
142 if ( ! $line ) {
143 return;
144 }
145
146 wp_add_inline_script(
147 'wp-theme-plugin-editor',
148 sprintf(
149 '
150 (
151 ( originalInitCodeEditor ) => {
152 wp.themePluginEditor.initCodeEditor = function() {
153 originalInitCodeEditor.apply( this, arguments );
154 this.instance.codemirror.doc.setCursor( %d - 1 );
155 };
156 }
157 )( wp.themePluginEditor.initCodeEditor );
158 ',
159 wp_json_encode( $line )
160 )
161 );
162 }
163
164 /**
165 * Returns the list of plugins.
166 *
167 * @since 1.0.0
168 *
169 * @return array List of available plugins.
170 */
171 private function get_available_plugins() {
172 $available_plugins = get_plugins();
173
174 if ( empty( $available_plugins ) ) {
175 return array();
176 }
177
178 $plugin_check_base_name = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );
179
180 if ( isset( $available_plugins[ $plugin_check_base_name ] ) ) {
181 unset( $available_plugins[ $plugin_check_base_name ] );
182 }
183
184 return $available_plugins;
185 }
186
187 /**
188 * Renders the "Plugin Check" page.
189 *
190 * @since 1.0.0
191 *
192 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
193 */
194 public function render_page() {
195 $available_plugins = $this->get_available_plugins();
196
197 $selected_plugin_basename = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
198
199 $categories = Check_Categories::get_categories();
200
201 // Get user settings for category preferences.
202 $user_enabled_categories = get_user_setting( 'plugin_check_category_preferences', implode( '__', $this->get_default_check_categories_to_be_selected() ) );
203 $user_enabled_categories = explode( '__', $user_enabled_categories );
204
205 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php';
206 }
207
208 /**
209 * Adds "check this plugin" link in the plugins list table.
210 *
211 * @since 1.0.0
212 *
213 * @param array $actions List of actions.
214 * @param string $plugin_file Plugin main file.
215 * @param array $plugin_data An array of plugin data.
216 * @param string $context The plugin context. By default this can include 'all',
217 * 'active', 'inactive', 'recently_activated', 'upgrade',
218 * 'mustuse', 'dropins', and 'search'.
219 * @return array The modified list of actions.
220 */
221 public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
222
223 $plugin_check_base_name = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );
224 if ( in_array( $context, array( 'mustuse', 'dropins' ), true ) || $plugin_check_base_name === $plugin_file ) {
225 return $actions;
226 }
227
228 if ( current_user_can( 'activate_plugins' ) ) {
229 $actions[] = sprintf(
230 '<a href="%1$s">%2$s</a>',
231 esc_url( admin_url( "tools.php?page=plugin-check&plugin={$plugin_file}" ) ),
232 esc_html__( 'Check this plugin', 'plugin-check' )
233 );
234 }
235
236 return $actions;
237 }
238
239 /**
240 * Render the results table templates in the footer.
241 *
242 * @since 1.0.0
243 */
244 public function admin_footer() {
245 ob_start();
246 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-table.php';
247 $results_table_template = ob_get_clean();
248 wp_print_inline_script_tag(
249 $results_table_template,
250 array(
251 'id' => 'tmpl-plugin-check-results-table',
252 'type' => 'text/template',
253 )
254 );
255
256 ob_start();
257 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-row.php';
258 $results_row_template = ob_get_clean();
259 wp_print_inline_script_tag(
260 $results_row_template,
261 array(
262 'id' => 'tmpl-plugin-check-results-row',
263 'type' => 'text/template',
264 )
265 );
266
267 ob_start();
268 require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-complete.php';
269 $results_row_template = ob_get_clean();
270 wp_print_inline_script_tag(
271 $results_row_template,
272 array(
273 'id' => 'tmpl-plugin-check-results-complete',
274 'type' => 'text/template',
275 )
276 );
277 ?>
278 <style>
279 #plugin-check__results .notice,
280 #plugin-check__results .notice + h4 {
281 margin-top: 20px;
282 }
283 #plugin-check__results h4:first-child {
284 margin-top: 80.5px;
285 }
286 @media ( max-width: 782px ) {
287 #plugin-check__results h4:first-child {
288 margin-top: 88.5px;
289 }
290 }
291 </style>
292 <?php
293 }
294
295 /**
296 * Gets the hook suffix under which the admin page is added.
297 *
298 * @since 1.0.0
299 *
300 * @return string Hook suffix, or empty string if admin page was not added.
301 */
302 public function get_hook_suffix() {
303 return $this->hook_suffix;
304 }
305
306 /**
307 * Gets default check categories to be selected.
308 *
309 * @since 1.0.2
310 *
311 * @return string[] An array of category slugs.
312 */
313 private function get_default_check_categories_to_be_selected() {
314 $default_check_categories = array(
315 'plugin_repo',
316 );
317
318 /**
319 * Filters the default check categories to be selected.
320 *
321 * @since 1.0.2
322 *
323 * @param string[] $default_check_categories An array of category slugs.
324 */
325 $default_categories = (array) apply_filters( 'wp_plugin_check_default_categories', $default_check_categories );
326
327 return $default_categories;
328 }
329 }
330