PluginProbe
Siteimprove / 2.1.4
Siteimprove v2.1.4
2.1.4 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3
siteimprove / admin / class-siteimprove-admin.php

class-siteimprove-admin.php in Siteimprove 2.1.4, at admin/class-siteimprove-admin.php

546 lines 18.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the plugin.
4 *
5 * @package Siteimprove
6 * @subpackage Siteimprove/admin
7 */
8
9 /**
10 * The admin-specific functionality of the plugin.
11 *
12 * Defines the plugin name, version, and two examples hooks for how to
13 * enqueue the admin-specific stylesheet and JavaScript.
14 *
15 * @package Siteimprove
16 * @subpackage Siteimprove/admin
17 */
18 class Siteimprove_Admin {
19
20 /**
21 * The ID of this plugin.
22 *
23 * @access private
24 * @var string $plugin_name The ID of this plugin.
25 */
26 private $plugin_name;
27
28 /**
29 * The version of this plugin.
30 *
31 * @access private
32 * @var string $version The current version of this plugin.
33 */
34 private $version;
35
36 /**
37 * Instance of Siteimprove_Admin_Settings class used inside the Admin class to load dependencies
38 *
39 * @var Siteimprove_Admin_Settings
40 */
41 private $settings;
42
43 /**
44 * Initialize the class and set its properties.
45 *
46 * @param string $plugin_name The name of this plugin.
47 * @param string $version The version of this plugin.
48 */
49 public function __construct( $plugin_name, $version ) {
50
51 $this->plugin_name = $plugin_name;
52 $this->version = $version;
53
54 $this->load_dependencies();
55
56 }
57
58 /**
59 * Load the required dependencies for this plugin.
60 *
61 * Include the following files that make up the plugin:
62 *
63 * - Siteimprove_Loader. Orchestrates the hooks of the plugin.
64 * - Siteimprove_I18n. Defines internationalization functionality.
65 * - Siteimprove_Admin. Defines all hooks for the admin area.
66 * - Siteimprove_Public. Defines all hooks for the public side of the site.
67 *
68 * Create an instance of the loader which will be used to register the hooks
69 * with WordPress.
70 *
71 * @access private
72 */
73 private function load_dependencies() {
74 require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/class-siteimprove-admin-settings.php';
75 $this->settings = new Siteimprove_Admin_Settings();
76 }
77
78 /**
79 * Register the stylesheets for the admin area.
80 */
81 public function enqueue_styles() {
82 wp_enqueue_style( 'siteimprove_admin_css', plugin_dir_url( __FILE__ ) . 'css/siteimprove-admin.css', array(), $this->version, 'all' );
83 }
84
85 /**
86 * Register the stylesheets for the preview area.
87 */
88 public function enqueue_preview_styles() {
89 global $wp_query;
90 $prepublish_allowed = intval( get_option( 'siteimprove_prepublish_allowed', 0 ) );
91 $prepublish_enabled = intval( get_option( 'siteimprove_prepublish_enabled', 0 ) );
92
93 if ( ( $wp_query->is_preview() || $wp_query->is_singular() || is_front_page() ) && 1 === $prepublish_allowed && 1 === $prepublish_enabled ) {
94 wp_enqueue_style( 'siteimprove_preview_css', plugin_dir_url( __FILE__ ) . 'css/siteimprove-preview.css', array(), $this->version, 'all' );
95 }
96 }
97
98 /**
99 * Register the JavaScript for the admin area.
100 */
101 public function enqueue_scripts() {
102 wp_enqueue_script( 'siteimprove_admin_js', plugin_dir_url( __FILE__ ) . 'js/siteimprove-admin.js', array( 'jquery' ), $this->version, false );
103 }
104
105 /**
106 * Gutenberg script for adding buttons to its editor such as Recheck
107 */
108 public function gutenberg_siteimprove_plugin() {
109 global $post;
110
111 if ( $post && $post->ID ) {
112 $permalink = get_permalink( $post->ID );
113 if ( $permalink ) {
114 $this->siteimprove_add_js( $permalink, 'siteimprove_input' );
115
116 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
117 error_log( 'Siteimprove Debug: Gutenberg - Post ID: ' . $post->ID );
118 error_log( 'Siteimprove Debug: Gutenberg - Permalink: ' . $permalink );
119 }
120 }
121 }
122
123 wp_enqueue_script(
124 'gutenberg-siteimprove-plugin',
125 plugin_dir_url( __FILE__ ) . 'js/siteimprove-gutenberg.js',
126 array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'siteimprove' ),
127 $this->version,
128 false
129 );
130
131 $post_id = $post ? $post->ID : 0;
132 $url = $post_id ? get_permalink( $post_id ) : '';
133
134 $si_js_args = array(
135 'token' => get_option( 'siteimprove_token' ),
136 'text' => __( 'Siteimprove Recheck', 'siteimprove' ),
137 'url' => $url,
138 );
139 wp_localize_script(
140 'gutenberg-siteimprove-plugin',
141 'siteimprove_gutenberg_recheck',
142 $si_js_args
143 );
144 }
145
146 /**
147 * Initial actions.
148 */
149 public function siteimprove_init() {
150 global $pagenow;
151
152 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
153 error_log( 'Siteimprove Debug: Current page: ' . $pagenow );
154 error_log( 'Siteimprove Debug: GET params: ' . print_r( $_GET, true ) );
155 }
156
157 $urls = get_transient( 'siteimprove_url_' . get_current_user_id() );
158
159 if ( ! wp_doing_ajax() && ! empty( $urls ) ) {
160 if ( is_array( $urls ) && count( $urls ) > 1 ) {
161 $url = esc_url( home_url() );
162 $method = 'siteimprove_recrawl';
163 } else {
164 $url = array_pop( $urls );
165 $method = 'siteimprove_recheck';
166 }
167 delete_transient( 'siteimprove_url_' . get_current_user_id() );
168 $this->siteimprove_add_js( $url, $method );
169 }
170
171 switch ( $pagenow ) {
172 case 'post.php':
173 $post_id = ! empty( $_GET['post'] ) ? (int) $_GET['post'] : 0;
174 $permalink = $post_id ? get_permalink( $post_id ) : false;
175
176 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
177 error_log( 'Siteimprove Debug: Post ID: ' . $post_id );
178 error_log( 'Siteimprove Debug: Permalink: ' . $permalink );
179 error_log( 'Siteimprove Debug: Token: ' . get_option( 'siteimprove_token', 'NOT_SET' ) );
180 error_log( 'Siteimprove Debug: User can edit posts: ' . ( current_user_can( 'edit_posts' ) ? 'yes' : 'no' ) );
181 }
182
183 if ( $permalink && current_user_can( 'edit_posts' ) ) {
184 $this->siteimprove_add_js( get_permalink( $post_id ), 'siteimprove_input' );
185 // Only display recheck button in published posts.
186 if ( get_post_status( $post_id ) === 'publish' ) {
187 $this->siteimprove_add_js( get_permalink( $post_id ), 'siteimprove_recheck_button' );
188 }
189 }
190 break;
191
192 case 'term.php':
193 case 'edit-tags.php':
194 $tag_id = ! empty( $_GET['tag_ID'] ) ? (int) $_GET['tag_ID'] : 0;
195 $taxonomy = ! empty( $_GET['taxonomy'] ) ? sanitize_key( $_GET['taxonomy'] ) : '';
196
197 if ( ( 'term.php' === $pagenow || ( 'edit-tags.php' === $pagenow && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ) && current_user_can( 'manage_categories' ) ) {
198 $term_link = get_term_link( (int) $tag_id, $taxonomy );
199 if ( ! is_wp_error( $term_link ) ) {
200 $this->siteimprove_add_js( $term_link, 'siteimprove_input' );
201 $this->siteimprove_add_js( $term_link, 'siteimprove_recheck_button' );
202 }
203 }
204 break;
205
206 default:
207 $host = isset( $_SERVER['HTTP_HOST'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
208 $request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
209 $this->siteimprove_add_js( $host . $request, 'siteimprove_domain' );
210 }
211
212 }
213
214 /**
215 * Include siteimprove js.
216 *
217 * @param string $url Url of the included js file.
218 * @param string $type Type/Handle of resource being included to localize the script correctly.
219 * @return void
220 */
221 private function siteimprove_add_js( $url, $type ) {
222 $file_name = get_option( 'siteimprove_overlayjs_file', 'overlay-v2-dev.js' );
223 $disabled_new_version = get_option( 'siteimprove_disable_new_version' );
224 $pattern = '/^[a-zA-Z_\d-]+.js/';
225 $nonce = wp_create_nonce( 'siteimprove_nonce' );
226
227 if ( ! empty( $file_name ) ) {
228 if ( preg_match( $pattern, $file_name ) ) {
229 $overlay_path = Siteimprove::JS_LIBRARY_URL . $file_name;
230 } else {
231 $overlay_path = $file_name;
232 }
233 } else {
234 if ( $disabled_new_version ) {
235 $overlay_path = Siteimprove::JS_LIBRARY_URL . 'overlay-latest.js';
236 } else {
237 $overlay_path = Siteimprove::JS_LIBRARY_URL . 'overlay-v1.js';
238 }
239 }
240
241 if ( isset( $_GET['si_preview_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['si_preview_nonce'] ) ), 'siteimprove_nonce' ) ) {
242 return;
243 } else {
244 wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/siteimprove.js', array( 'jquery' ), $this->version, false );
245
246 /*
247 Pass any server-side vars down to JS. These will be exposed as
248 php_vars.variablename for example
249 */
250 $jsarray = array(
251 'prepublish_allowed' => intval( get_option( 'siteimprove_prepublish_allowed', 0 ) ),
252 'prepublish_enabled' => intval( get_option( 'siteimprove_prepublish_enabled', 0 ) ),
253 'has_api_key' => intval( strlen( get_option( 'siteimprove_api_key', 0 ) ) > 0 ),
254 );
255 wp_localize_script( $this->plugin_name, 'php_vars', $jsarray );
256 wp_enqueue_script( 'siteimprove_overlay', $overlay_path, array(), $this->version, true );
257 }
258 $public_url = get_option( 'siteimprove_public_url' );
259 $ignore_segments = get_option( 'siteimprove_ignore_path_segments' );
260
261 // Parse the URL to get its components.
262 $parsed_url = wp_parse_url( $url );
263 $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
264
265 // Apply path segment filtering if configured.
266 if ( ! empty( $ignore_segments ) && ! empty( $path ) ) {
267 // Split the ignore segments by comma and trim whitespace.
268 $segments_to_ignore = array_map( 'trim', explode( ',', $ignore_segments ) );
269
270 // Split the path into segments.
271 $path_segments = array_filter( explode( '/', $path ) );
272
273 // Remove segments that should be ignored.
274 $filtered_segments = array();
275 foreach ( $path_segments as $segment ) {
276 if ( ! in_array( $segment, $segments_to_ignore, true ) ) {
277 $filtered_segments[] = $segment;
278 }
279 }
280
281 // Reconstruct the path.
282 $path = '/' . implode( '/', $filtered_segments );
283 }
284
285 // Apply public URL transformation if configured.
286 if ( ! empty( $public_url ) ) {
287 $public_url = rtrim( $public_url, '/' );
288 $normalized_path = '/' . ltrim( $path, '/' );
289 $query_string = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '';
290 $url = $public_url . $normalized_path . $query_string;
291 } else {
292 // If no public URL is set, reconstruct the URL with the filtered path.
293 $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '';
294 $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : '';
295 $port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
296 $query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '';
297 $fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '';
298
299 $url = $scheme . $host . $port . $path . $query . $fragment;
300 }
301
302 $is_content_page = is_preview() || is_singular() || is_front_page();
303
304 $si_js_args = array(
305 'token' => get_option( 'siteimprove_token' ),
306 'txt' => __( 'Siteimprove Recheck', 'siteimprove' ),
307 'url' => $url,
308 'version' => $disabled_new_version,
309 'is_content_page' => $is_content_page,
310 'nonce' => $nonce,
311 );
312
313 wp_localize_script(
314 $this->plugin_name,
315 esc_js( $type ),
316 $si_js_args
317 );
318
319 // Adding translation strings.
320 wp_localize_script(
321 $this->plugin_name,
322 'siteimprove_plugin_text',
323 array(
324 'loading' => __( 'Loading... Please wait.', 'siteimprove' ),
325 'prepublish_activate_running' => __( 'We are now activating prepublish for your website... Please keep the current page open while the process is running.', 'siteimprove' ),
326 'prepublish_feature_ready' => __( 'Prepublish feature is already enabled for the current website. To use it please go to the preview of any page/post or content that you want to check and click the button <strong>Siteimprove Prepublish Check</strong> located on the top bar of the admin panel.', 'siteimprove' ),
327 'prepublish_activation_error' => __( 'Error activating prepublish. Please contact support team.', 'siteimprove' ),
328 )
329 );
330 }
331
332 /**
333 * Register settings form section.
334 */
335 public function siteimprove_settings() {
336 $this->settings->register_section();
337 }
338
339 /**
340 * Register menu page.
341 */
342 public function siteimprove_settings_page() {
343 $this->settings->register_menu();
344 }
345
346 /**
347 * Register action for token requests.
348 */
349 public function siteimprove_request_token() {
350 $this->settings->request_token();
351 }
352
353 /**
354 * Register action for prepublish feature check after manual activation on the admin panel side
355 *
356 * @return void
357 */
358 public function siteimprove_check_prepublish_activation() {
359 $this->settings->check_prepublish_activation();
360 }
361
362 /**
363 * Register action for prepublish feature manual activation made on the admin panel side
364 *
365 * @return void
366 */
367 public function siteimprove_prepublish_manual_activation() {
368 $this->settings->prepublish_manual_activation();
369 }
370
371
372 /**
373 * Save in session post url.
374 *
375 * @param integer $post_ID WordPress Post ID.
376 * @return void
377 */
378 public function siteimprove_save_session_url_post( $post_ID ) {
379 if ( ! wp_is_post_revision( $post_ID ) && ! wp_is_post_autosave( $post_ID ) ) {
380 $urls = get_transient( 'siteimprove_url_' . get_current_user_id() );
381 $urls[] = get_permalink( $post_ID );
382 set_transient( 'siteimprove_url_' . get_current_user_id(), $urls, 900 );
383 }
384 }
385
386 /**
387 * Save in session term url.
388 *
389 * @param integer $term_id WordPress Term ID.
390 * @param mixed $tt_id WordPress parameter added for hook compatibility.
391 * @param mixed $taxonomy WordPress taxonomy.
392 * @return void
393 */
394 public function siteimprove_save_session_url_term( $term_id, $tt_id, $taxonomy ) {
395 $urls = get_transient( 'siteimprove_url_' . get_current_user_id() );
396 $urls[] = get_term_link( (int) $term_id, $taxonomy );
397 set_transient( 'siteimprove_url_' . get_current_user_id(), $urls, 900 );
398 }
399
400 /**
401 * Save in session product url.
402 *
403 * @param string $new_status WordPress post status.
404 * @param string $old_status WordPress post status.
405 * @param object $post WordPress Post Object.
406 * @return void
407 */
408 public function siteimprove_save_session_url_product( $new_status, $old_status, $post ) {
409 if (
410 'publish' === $new_status
411 && ! empty( $post->ID )
412 && in_array(
413 $post->post_type,
414 array( 'product' ),
415 true
416 )
417 ) {
418 $urls = get_transient( 'siteimprove_url_' . get_current_user_id() );
419 $urls[] = get_permalink( $post->ID );
420 set_transient( 'siteimprove_url_' . get_current_user_id(), $urls, 900 );
421 }
422 }
423
424 /**
425 * Include js in frontend pages.
426 */
427 public function siteimprove_wp_head() {
428
429 if ( current_user_can( 'edit_posts' ) ) {
430 $type = $this->get_current_page_type();
431 switch ( $type ) {
432 case 'front':
433 case 'home':
434 case 'page':
435 case 'single':
436 case 'category':
437 case 'tag':
438 case 'tax':
439 $this->siteimprove_add_js( get_permalink(), 'siteimprove_input' );
440 break;
441
442 default:
443 $host = isset( $_SERVER['HTTP_HOST'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
444 $request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
445 $this->siteimprove_add_js( $host . $request, 'siteimprove_domain' );
446 }
447 }
448 }
449
450 /**
451 * Return current page type.
452 */
453 protected function get_current_page_type() {
454 global $wp_query;
455 $loop = 'notfound';
456
457 if ( $wp_query->is_page ) {
458 $loop = is_front_page() ? 'front' : 'page';
459 } elseif ( $wp_query->is_home ) {
460 $loop = 'home';
461 } elseif ( $wp_query->is_single ) {
462 $loop = ( $wp_query->is_attachment ) ? 'attachment' : 'single';
463 } elseif ( $wp_query->is_category ) {
464 $loop = 'category';
465 } elseif ( $wp_query->is_tag ) {
466 $loop = 'tag';
467 } elseif ( $wp_query->is_tax ) {
468 $loop = 'tax';
469 } elseif ( $wp_query->is_archive ) {
470 if ( $wp_query->is_day ) {
471 $loop = 'day';
472 } elseif ( $wp_query->is_month ) {
473 $loop = 'month';
474 } elseif ( $wp_query->is_year ) {
475 $loop = 'year';
476 } elseif ( $wp_query->is_author ) {
477 $loop = 'author';
478 } else {
479 $loop = 'archive';
480 }
481 } elseif ( $wp_query->is_search ) {
482 $loop = 'search';
483 } elseif ( $wp_query->is_404 ) {
484 $loop = 'notfound';
485 }
486
487 return $loop;
488 }
489
490 /**
491 * Adds the prepublish menu item on the top bar when user is
492 * in preview mode so he can send the content to prepublish.
493 *
494 * @param WP_Admin_Bar $admin_bar WordPress Admin Bar Object.
495 * @return void
496 */
497 public function add_prepublish_toolbar_item( WP_Admin_Bar $admin_bar ) {
498 global $pagenow;
499 $prepublish_allowed = intval( get_option( 'siteimprove_prepublish_allowed', 0 ) );
500 $prepublish_enabled = intval( get_option( 'siteimprove_prepublish_enabled', 0 ) );
501 $has_api_key = intval( strlen( get_option( 'siteimprove_api_key', 0 ) ) > 0 );
502 if ( ( is_preview() || is_singular() || is_front_page() ) && 1 === $prepublish_allowed && 1 === $prepublish_enabled && 1 === $has_api_key ) {
503 $prepublish_button =
504 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" data-name="Layer 1" viewBox="0 0 300 300" height="28px" width="28px">
505 <defs>
506 <style>
507 .cls-1 {
508 fill: url(#linear-gradient);
509 }
510
511 .cls-2 {
512 fill: #fff;
513 }
514 </style>
515 <linearGradient id="linear-gradient" x1="0" y1="150" x2="300" y2="150" gradientUnits="userSpaceOnUse">
516 <stop offset="0" stop-color="#9d0077"/>
517 <stop offset=".13" stop-color="#97027a"/>
518 <stop offset=".3" stop-color="#880984"/>
519 <stop offset=".49" stop-color="#6e1695"/>
520 <stop offset=".69" stop-color="#4b27ad"/>
521 <stop offset=".89" stop-color="#1d3cca"/>
522 <stop offset="1" stop-color="#0449dc"/>
523 </linearGradient>
524 </defs>
525 <circle id="Circle_Background" data-name="Circle Background" class="cls-1" cx="150" cy="150" r="150"/>
526 <g id="Icon_S" data-name="Icon S">
527 <path class="cls-2" d="M167.9,135.3l-14.3-4.2c-6.5-2-11.6-3.8-15.4-5.4-3.8-1.6-6.5-3.4-8.1-5.2-1.6-1.8-2.4-4.1-2.4-6.9,0-3.4,1.1-6.3,3.2-8.7,2.1-2.5,5.1-4.4,9-5.7,3.8-1.3,8.3-2,13.5-2s10.4.7,15.6,2c5.2,1.3,9.9,3.2,14.3,5.5,4.4,2.3,8,5.1,11,8.2l19-28.1c-6.5-6.3-15.1-11.2-25.7-14.8-10.7-3.6-22.2-5.5-34.6-5.5-9,0-17.3,1.3-24.9,3.8-7.6,2.5-14.2,6.1-19.9,10.7-5.6,4.6-10,10-13.1,16.2-3.1,6.2-4.7,13-4.7,20.3,0,11.4,3.7,21.2,11.1,29.3s19.6,14.5,36.5,19.2l14.1,4c9.9,2.7,16.7,5.4,20.3,8.3,3.6,2.9,5.5,6.4,5.5,10.7,0,5.2-2.5,9.1-7.6,11.8-5,2.7-11.6,4-19.6,4-5.7,0-11.6-.7-17.6-2.2-6.1-1.4-11.7-3.5-17.1-6.2-5.3-2.7-9.7-5.8-13.1-9.5l-18.3,29.1c8.1,7,18.1,12.3,29.8,15.9,11.7,3.7,23.8,5.5,36.2,5.5,13.4,0,24.9-2.2,34.6-6.7s17.2-10.6,22.6-18.6c5.3-7.9,8-17.1,8-27.5,0-11.9-3.8-21.5-11.3-29-7.7-7.2-19.8-13.4-36.6-18.3Z"/>
528 </g>
529 </svg>';
530 $admin_bar->add_menu(
531 array(
532 'id' => 'siteimprove-trigger-contentcheck',
533 'title' => $prepublish_button . __( 'Prepublish', 'siteimprove' ),
534 'group' => null,
535 'href' => '#',
536 'meta' => array(
537 'title' => __( 'Siteimprove Prepublish', 'siteimprove' ),
538 'class' => 'siteimprove-trigger-contentcheck',
539 ),
540 )
541 );
542 }
543 }
544
545 }
546