PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / trunk
WPSSO Core – Complete Schema Markup and Meta Tags vtrunk
22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / script.php

script.php in WPSSO Core – Complete Schema Markup and Meta Tags trunk, at lib/script.php

499 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * License: GPLv3
4 * License URI: https://www.gnu.org/licenses/gpl.txt
5 * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/)
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9
10 die( 'These aren\'t the droids you\'re looking for.' );
11 }
12
13 if ( ! defined( 'WPSSO_PLUGINDIR' ) ) {
14
15 die( 'Do. Or do not. There is no try.' );
16 }
17
18 if ( ! class_exists( 'WpssoScript' ) ) {
19
20 class WpssoScript {
21
22 private $p; // Wpsso class object.
23 private $doing_dev = false;
24 private $file_ext = 'min.js';
25 private $version = '';
26
27 public function __construct( &$plugin ) {
28
29 $this->p =& $plugin;
30
31 if ( $this->p->debug->enabled ) {
32
33 $this->p->debug->mark();
34 }
35
36 $this->doing_dev = SucomUtilWP::doing_dev();
37 $this->file_ext = $this->doing_dev ? 'js' : 'min.js';
38 $this->version = WpssoConfig::get_version() . ( $this->doing_dev ? gmdate( '-ymd-His' ) : '' );
39
40 $doing_ajax = SucomUtilWP::doing_ajax();
41
42 if ( is_admin() ) {
43
44 $this->on_async_upload_update_toolbar_script();
45
46 if ( ! $doing_ajax ) {
47
48 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), WPSSO_BLOCK_ASSETS_PRIORITY );
49
50 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), WPSSO_ADMIN_SCRIPTS_PRIORITY );
51
52 add_action( 'admin_head', array( $this, 'on_load_update_toolbar_script' ) );
53 }
54 }
55 }
56
57 public function enqueue_block_editor_assets() {
58
59 if ( $this->p->debug->enabled ) {
60
61 $this->p->debug->mark();
62 }
63
64 if ( SucomUtilWP::is_post_page() ) {
65
66 $src = WPSSO_URLPATH . 'js/jquery-block-editor.' . $this->file_ext;
67
68 /*
69 * The 'wp-editor' dependency should not be enqueued together with the new widgets block editor.
70 */
71 $deps = array( 'wp-data', 'wp-editor', 'wp-edit-post', 'sucom-admin-page' );
72
73 /*
74 * The 'wpsso-block-editor' script, with its 'wp-edit-post' dependency, must be loaded in the
75 * footer to work around a bug in the NextGEN Gallery featured image picker. If the script is
76 * loaded in the header, with a dependency on 'wp-edit-post', the NextGEN Gallery featured image
77 * picker does not load.
78 */
79 $in_footer = true;
80
81 wp_register_script( 'wpsso-block-editor', $src, $deps, $this->version, $in_footer );
82
83 wp_enqueue_script( 'wpsso-block-editor' );
84 }
85 }
86
87 public function admin_enqueue_scripts( $hook_name ) {
88
89 if ( $this->p->debug->enabled ) {
90
91 $this->p->debug->log( 'hook name = ' . $hook_name );
92 $this->p->debug->log( 'screen base = ' . SucomUtilWP::get_screen_base() );
93 }
94
95 /*
96 * See http://qtip2.com/download.
97 */
98 wp_register_script( 'jquery-qtip', WPSSO_URLPATH . 'js/ext/jquery-qtip.' . $this->file_ext,
99 $deps = array( 'jquery' ), $this->p->cf[ 'jquery-qtip' ][ 'version' ], $in_footer = true );
100
101 /*
102 * Provides sucomInitAdminMedia().
103 */
104 wp_register_script( 'sucom-admin-media', WPSSO_URLPATH . 'js/com/jquery-admin-media.' . $this->file_ext,
105 $deps = array( 'jquery', 'jquery-ui-core' ), $this->version, $in_footer = true );
106
107 wp_localize_script( 'sucom-admin-media', 'sucomAdminMediaL10n', $this->get_admin_media_script_data() );
108
109 /*
110 * Provides sucomInitMetabox().
111 */
112 wp_register_script( 'sucom-metabox', WPSSO_URLPATH . 'js/com/jquery-metabox.' . $this->file_ext,
113 $deps = array( 'jquery', 'jquery-ui-datepicker', 'wp-color-picker', 'sucom-admin-page' ),
114 $this->version, $in_footer = true );
115
116 /*
117 * Provides sucomInitToolTips().
118 */
119 wp_register_script( 'sucom-tooltips', WPSSO_URLPATH . 'js/com/jquery-tooltips.' . $this->file_ext,
120 $deps = array( 'jquery', 'jquery-qtip' ), $this->version, $in_footer = true );
121
122 /*
123 * Provides wpssoInitMetabox().
124 */
125 wp_register_script( 'wpsso-metabox', WPSSO_URLPATH . 'js/jquery-metabox.' . $this->file_ext,
126 $deps = array( 'sucom-metabox' ), $this->version, $in_footer = true );
127
128 /*
129 * Only load scripts where we need them.
130 */
131 switch ( $hook_name ) {
132
133 /*
134 * Addons and license settings page.
135 */
136 case ( preg_match( '/_page_wpsso-.*(addons|licenses)/', $hook_name ) ? true : false ) :
137
138 if ( $this->p->debug->enabled ) {
139
140 $this->p->debug->log( 'enqueuing scripts for addons and licenses page' );
141 }
142
143 add_thickbox(); // Required for the plugin details box.
144
145 wp_enqueue_script( 'plugin-install' ); // Required for the plugin details box.
146
147 // No break.
148
149 /*
150 * Any settings page. Also matches the profile_page and users_page hooks.
151 */
152 case ( false !== strpos( $hook_name, '_page_wpsso-' ) ? true : false ):
153
154 if ( $this->p->debug->enabled ) {
155
156 $this->p->debug->log( 'enqueuing scripts for settings page' );
157 }
158
159 wp_enqueue_script( 'sucom-settings-page' );
160
161 // No break.
162
163 /*
164 * Editing page.
165 */
166 case 'post.php': // Post edit.
167 case 'post-new.php': // Post edit.
168 case 'term.php': // Term edit.
169 case 'edit-tags.php': // Term edit.
170 case 'user-edit.php': // User edit.
171 case 'profile.php': // User edit.
172 case ( SucomUtilWP::is_toplevel_edit( $hook_name ) ): // Required for event espresso plugin.
173
174 if ( $this->p->debug->enabled ) {
175
176 $this->p->debug->log( 'enqueuing scripts for editing page' );
177 }
178
179 wp_enqueue_script( 'sucom-tooltips' );
180
181 wp_enqueue_script( 'wpsso-metabox' );
182
183 if ( function_exists( 'wp_enqueue_media' ) ) {
184
185 if ( $this->p->debug->enabled ) {
186
187 $this->p->debug->log( 'wp_enqueue_media() function is available' );
188 }
189
190 if ( SucomUtilWP::is_post_page() && ( $post_id = SucomUtilWP::get_post_object( false, 'id' ) ) > 0 ) {
191
192 wp_enqueue_media( array( 'post' => $post_id ) );
193
194 } else wp_enqueue_media();
195
196 wp_enqueue_script( 'sucom-admin-media' );
197
198 } elseif ( $this->p->debug->enabled ) {
199
200 $this->p->debug->log( 'wp_enqueue_media() function not found' );
201 }
202
203 do_action( 'wpsso_admin_enqueue_scripts_editing_page', $hook_name, $this->file_ext );
204
205 break; // Stop here.
206
207 case 'plugin-install.php':
208
209 if ( isset( $_GET[ 'plugin' ] ) ) {
210
211 /*
212 * See wordpress/wp-admin/plugins.php line 21.
213 */
214 $plugin_slug = wp_unslash( $_GET[ 'plugin' ] );
215
216 if ( isset( $this->p->cf[ '*' ][ 'slug' ][ $plugin_slug ] ) ) {
217
218 if ( $this->p->debug->enabled ) {
219
220 $this->p->debug->log( 'enqueuing scripts for plugin install page' );
221 }
222
223 $this->add_plugin_install_iframe_script( $hook_name );
224 }
225 }
226
227 break;
228 }
229
230 $this->admin_register_page_scripts( $hook_name );
231
232 $this->admin_enqueue_page_scripts( $hook_name );
233 }
234
235 /*
236 * Add jQuery to update the toolbar menu item on page load.
237 *
238 * Hooked to the WordPress 'admin_footer' action.
239 */
240 public function on_load_update_toolbar_script() {
241
242 if ( $this->p->debug->enabled ) {
243
244 $this->p->debug->mark();
245 }
246
247 $toolbar_notice_types = $this->p->notice->get_toolbar_types();
248
249 if ( empty( $toolbar_notice_types ) || ! is_array( $toolbar_notice_types ) ) {
250
251 if ( ! empty( $this->p->debug->enabled ) ) {
252
253 $this->p->debug->log( 'exiting early: no toolbar notice types defined' );
254 }
255
256 return;
257 }
258
259 /*
260 * Exit early if this is a block editor page.
261 *
262 * Notices will be retrieved using an ajax call during editor page load and post save.
263 */
264 if ( SucomUtilWP::doing_block_editor() ) {
265
266 if ( ! empty( $this->p->debug->enabled ) ) {
267
268 $this->p->debug->log( 'exiting early: doing block editor is true' );
269 }
270
271 return;
272 }
273
274 if ( ! empty( $this->p->debug->enabled ) ) {
275
276 $this->p->debug->log( 'doing block editor is false' );
277 }
278
279 /*
280 * jQuery() or jQuery( document ).on( 'ready' ) executes when HTML-Document is loaded and DOM is ready.
281 *
282 * jQuery( window ).on( 'load' ) executes when page is fully loaded, including all frames, objects and images.
283 *
284 * The type="text/javascript" attribute is unnecessary for JavaScript resources and creates warnings in the W3C validator.
285 */
286 $admin_l10n = $this->p->cf[ 'plugin' ][ 'wpsso' ][ 'admin_l10n' ];
287
288 echo '<script>';
289 echo 'jQuery( window ).on( \'load\', function(){';
290 echo ' if ( \'function\' === typeof sucomToolbarNotices ) {';
291 echo ' sucomToolbarNotices( \'wpsso\', \'' . $admin_l10n . '\' );';
292 echo ' }';
293 echo '});';
294 echo '</script>' . "\n";
295 }
296
297 public function on_async_upload_update_toolbar_script() {
298
299 if ( 'async-upload' === basename( $_SERVER[ 'PHP_SELF' ], '.php' ) ) {
300
301 /*
302 * See wordpress/wp-admin/async-upload.php line 42.
303 */
304 if ( ! empty( $_REQUEST[ 'fetch' ] ) && ! empty( $_REQUEST[ 'attachment_id' ] ) && is_numeric( $_REQUEST[ 'attachment_id' ] ) ) {
305
306 $admin_l10n = $this->p->cf[ 'plugin' ][ 'wpsso' ][ 'admin_l10n' ];
307
308 echo '<script>';
309 echo 'if ( \'function\' === typeof sucomToolbarNotices ) { ';
310 echo 'sucomToolbarNotices( \'wpsso\', \'' . $admin_l10n . '\' );';
311 echo ' }';
312 echo '</script>' . "\n";
313 }
314 }
315 }
316
317 /*
318 * Add jQuery to correctly follow the Install / Update link when clicked (WordPress bug). Also adds the parent URL
319 * and settings page title as query arguments, which are then used by WpssoAdmin class filters to return the user
320 * back to the settings page after installing / activating / updating the plugin.
321 */
322 private function add_plugin_install_iframe_script( $hook_name ) { // $hook_name = plugin-install.php
323
324 if ( $this->p->debug->enabled ) {
325
326 $this->p->debug->mark();
327 }
328
329 wp_enqueue_script( 'plugin-install' ); // Required for the plugin details box.
330
331 /*
332 * Fix the update / install button to load the href when clicked.
333 *
334 * jQuery() or jQuery( document ).on( 'ready' ) executes when HTML-Document is loaded and DOM is ready.
335 */
336 $custom_script_js = <<<EOF
337 jQuery( function(){
338
339 jQuery( 'body#plugin-information.iframe a[id$=_from_iframe]' ).on( 'click', function(){
340
341 if ( window.top.location.href.indexOf( 'page=wpsso-' ) ){
342
343 var plugin_url = jQuery( this ).attr( 'href' );
344 var pageref_url_arg = '&wpsso_pageref_url=' + encodeURIComponent( window.top.location.href );
345 var pageref_title_arg = '&wpsso_pageref_title=' + encodeURIComponent( jQuery( 'h1', window.parent.document ).text() );
346
347 window.top.location.href = plugin_url + pageref_url_arg + pageref_title_arg;
348 }
349 });
350 });
351 EOF;
352
353 wp_add_inline_script( 'plugin-install', $custom_script_js );
354 }
355
356 /*
357 * sucomAdminMediaL10n array for the 'sucom-admin-media' script located in js/com/jquery-admin-media.js.
358 */
359 public function get_admin_media_script_data() {
360
361 /*
362 * var sucomAdminMediaL10n array.
363 */
364 return array(
365 '_select_image' => __( 'Select Image', 'wpsso' ),
366 );
367 }
368
369 public function admin_register_page_scripts( $hook_name ) {
370
371 if ( $this->p->debug->enabled ) {
372
373 $this->p->debug->log( 'registering script sucom-admin-page' );
374 }
375
376 $admin_l10n = $this->p->cf[ 'plugin' ][ 'wpsso' ][ 'admin_l10n' ];
377
378 /*
379 * The version number should match the version in js/com/jquery-admin-page.js.
380 */
381 wp_register_script( 'sucom-admin-page', WPSSO_URLPATH . 'js/com/jquery-admin-page.' . $this->file_ext,
382 $deps = array( 'jquery' ), '20240810', $in_footer = true );
383
384 wp_localize_script( 'sucom-admin-page', $admin_l10n, $this->get_admin_page_script_data() );
385 }
386
387 /*
388 * This method is run a second time by the 'admin_enqueue_scripts' action with a priority of PHP_INT_MAX to make
389 * sure another plugin (like Squirrly SEO) has not cleared our admin page scripts from the queue.
390 */
391 public function admin_enqueue_page_scripts( $hook_name ) {
392
393 $script_handles = array( 'sucom-admin-page', 'jquery' );
394
395 static $is_enqueued = null; // Default value for first execution.
396
397 if ( ! $is_enqueued ) { // Re-check the $wp_scripts queue at priority PHP_INT_MAX.
398
399 add_action( 'admin_enqueue_scripts', array( $this, __FUNCTION__ ), PHP_INT_MAX );
400 }
401
402 global $wp_scripts;
403
404 foreach ( $script_handles as $handle ) {
405
406 if ( ! $is_enqueued || ! isset( $wp_scripts->queue ) || ! in_array( $handle, $wp_scripts->queue ) ) {
407
408 if ( $this->p->debug->enabled ) {
409
410 $this->p->debug->log( 'enqueueing script ' . $handle );
411 }
412
413 wp_enqueue_script( $handle );
414
415 } elseif ( $this->p->debug->enabled ) {
416
417 $this->p->debug->log( $handle . ' script already enqueued' );
418 }
419 }
420
421 $is_enqueued = true; // Signal that we've already run once.
422 }
423
424 /*
425 * The config array for the 'sucom-admin-page' script located in js/com/jquery-admin-page.js.
426 */
427 public function get_admin_page_script_data() {
428
429 $admin_l10n = $this->p->cf[ 'plugin' ][ 'wpsso' ][ 'admin_l10n' ];
430
431 $option_labels = apply_filters( 'wpsso_admin_page_script_data_option_labels', array(
432 'schema_type' => _x( 'Schema Type', 'option label', 'wpsso' ),
433 ) );
434
435 /*
436 * noticeTime = -1 to skip automatically showing notifications.
437 * noticeTime = 0 to automatically show notifications until click or hover.
438 * noticeTime = milliseconds to automatically show notifications.
439 */
440 $toolbar_notice_timeout = array(
441 'err' => WPSSO_TB_NOTICE_TIME_ERR,
442 'warn' => WPSSO_TB_NOTICE_TIME_WARN,
443 'inf' => WPSSO_TB_NOTICE_TIME_INF,
444 'upd' => WPSSO_TB_NOTICE_TIME_UPD,
445 );
446
447 $toolbar_notice_types = $this->p->notice->get_toolbar_types();
448 $notice_text_uniqid = 'wpsso_' . uniqid(); // CSS id of hidden notice text container.
449 $no_notices_transl = sprintf( __( 'No %s notifications.', 'wpsso' ), $this->p->cf[ 'menu' ][ 'title' ] );
450 $no_notices_html = '<div class="ab-item ab-empty-item">' . $no_notices_transl . '</div>';
451 $copy_notices_transl = __( 'Copy notifications to clipboard.', 'wpsso' );
452
453 /*
454 * Add an 'inline' class to toolbar notices to prevent WordPress from moving the notice.
455 *
456 * See wordpress/wp-admin/js/common.js:1083
457 */
458 $copy_notices_html = '<div class="wpsso-notice notice notice-alt inline notice-copy" style="display:block !important;">' .
459 '<div class="notice-message">' .
460 '<a href="" onClick="return sucomCopyById( \'' . $notice_text_uniqid . '\', \'' . $admin_l10n . '\' );">' . $copy_notices_transl . '</a>' .
461 '</div><!-- .notice-message -->' .
462 '</div><!-- .notice-copy -->';
463
464 /*
465 * The config array.
466 */
467 $metabox_id = $this->p->cf[ 'meta' ][ 'id' ];
468
469 return array(
470 '_ajax_nonce' => wp_create_nonce( WPSSO_NONCE_NAME ),
471 '_ajax_actions' => array(
472 'schema_type_og_type' => 'wpsso_schema_type_og_type',
473 'get_notices_json' => 'wpsso_get_notices_json',
474 'get_validate_submenu' => 'wpsso_get_validate_submenu',
475 'metabox_postboxes' => array(
476 'wpsso_' . $metabox_id => 'wpsso_get_metabox_postbox_id_' . $metabox_id . '_inside',
477 ),
478 ),
479 '_option_labels' => $option_labels,
480 '_toolbar_notice_timeout' => $toolbar_notice_timeout,
481 '_toolbar_notice_types' => $toolbar_notice_types, // Maybe null, true, false, or array.
482 '_notice_text_id' => $notice_text_uniqid, // CSS id of hidden notice text container.
483 '_no_notices_html' => $no_notices_html,
484 '_copy_notices_html' => $copy_notices_html,
485 '_copy_clipboard_transl' => __( 'Copied to clipboard.', 'wpsso' ),
486 '_linked_to_transl' => __( 'Value linked to {0} option', 'wpsso' ),
487 '_count_msgs_transl' => array(
488 'error' => sprintf( __( 'There are {0} important error messages under the %s notification icon.', 'wpsso' ),
489 $this->p->cf[ 'notice' ][ 'title' ] ),
490 ),
491 '_min_len_transl' => __( '{0} of {1} characters minimum', 'wpsso' ),
492 '_req_len_transl' => __( '{0} of {1} characters recommended', 'wpsso' ),
493 '_max_len_transl' => __( '{0} of {1} characters maximum', 'wpsso' ),
494 '_len_transl' => __( '{0} characters', 'wpsso' ),
495 );
496 }
497 }
498 }
499