PluginProbe
Polylang / 3.1
Polylang v3.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | modules/wizard/wizard.php +115 -49 2.93.1 View file →
@@ -9,39 +9,46 @@
9 9 * @since 2.7
10 10 */
11 11 class PLL_Wizard {
12 12 /**
13 - * Reference to PLL_Model object
13 + * Reference to the model object
14 14 *
15 - * @var object $model
15 + * @var PLL_Admin_Model
16 16 */
17 17 protected $model;
18 18
19 19 /**
20 - * Reference to Polylang options array
20 + * Reference to the Polylang options array.
21 21 *
22 - * @var array $options
22 + * @var array
23 23 */
24 24 protected $options;
25 25
26 26 /**
27 - * List of steps
27 + * List of steps.
28 28 *
29 29 * @var array $steps {
30 - * @type string $name i18n string which names the step.
30 + * @type string $name I18n string which names the step.
31 31 * @type callable $view The callback function use to display the step content.
32 32 * @type callable $handler The callback function use to process the step after it is submitted.
33 - * @type array $scripts List of scripts handle needed by the step.
34 - * @type array $styles The list of styles handle needed by the step.
33 + * @type array $scripts List of scripts handle needed by the step.
34 + * @type array $styles The list of styles handle needed by the step.
35 35 * }
36 36 */
37 37 protected $steps = array();
38 38
39 39 /**
40 - * List of WordPress CSS file handles
40 + * The current step.
41 41 *
42 - * @var array $styles
42 + * @var string
43 43 */
44 + protected $step;
45 +
46 + /**
47 + * List of WordPress CSS file handles.
48 + *
49 + * @var string[]
50 + */
44 51 protected $styles = array();
45 52
46 53 /**
47 54 * Constructor
@@ -72,10 +79,12 @@
72 79
73 80 /**
74 81 * Save an activation transient when Polylang is activating to redirect to the wizard
75 82 *
83 + * @since 2.7
84 + *
76 85 * @param bool $network_wide if activated for all sites in the network.
77 - * @since 2.7
86 + * @return void
78 87 */
79 88 public static function start_wizard( $network_wide ) {
80 89 $options = get_option( 'polylang' );
81 90
@@ -88,8 +97,10 @@
88 97 /**
89 98 * Redirect to the wizard depending on the context
90 99 *
91 100 * @since 2.7
101 + *
102 + * @return void
92 103 */
93 104 public function redirect_to_wizard() {
94 105 if ( get_transient( 'pll_activation_redirect' ) ) {
95 106 $do_redirect = true;
@@ -116,11 +127,12 @@
116 127
117 128 /**
118 129 * Add an admin Polylang submenu to access the wizard
119 130 *
120 - * @param array $tabs Submenus list.
121 - * @return array Submenus list updated.
122 131 * @since 2.7
132 + *
133 + * @param string[] $tabs Submenus list.
134 + * @return string[] Submenus list updated.
123 135 */
124 136 public function settings_tabs( $tabs ) {
125 137 $tabs['wizard'] = esc_html__( 'Setup', 'polylang' );
126 138 return $tabs;
@@ -126,13 +138,14 @@
126 138 return $tabs;
127 139 }
128 140
129 141 /**
130 - * Return if the media step is displayable
142 + * Returns true if the media step is displayable, false otherwise.
131 143 *
132 - * @param array $languages List of language objects.
144 + * @since 2.7
145 + *
146 + * @param PLL_Language[] $languages List of language objects.
133 147 * @return bool
134 - * @since 2.7
135 148 */
136 149 public function is_media_step_displayable( $languages ) {
137 150 $media = array();
138 151 // If there is no language or only one the media step is displayable.
@@ -153,10 +166,11 @@
153 166
154 167 /**
155 168 * Check if the licenses step is displayable
156 169 *
170 + * @since 2.7
171 + *
157 172 * @return bool
158 - * @since 2.7
159 173 */
160 174 public function is_licenses_step_displayable() {
161 175 $licenses = apply_filters( 'pll_settings_licenses', array() );
162 176 return count( $licenses ) > 0;
@@ -165,8 +179,10 @@
165 179 /**
166 180 * Setup the wizard page
167 181 *
168 182 * @since 2.7
183 + *
184 + * @return void
169 185 */
170 186 public function setup_wizard_page() {
171 187
172 188 PLL_Admin_Notices::add_notice( 'wizard', $this->wizard_notice() );
@@ -212,17 +228,18 @@
212 228
213 229 /**
214 230 * Adds some admin screens where to display the wizard notice
215 231 *
232 + * @since 2.7
233 + *
216 234 * @param bool $can_display_notice Whether the notice can be displayed.
217 235 * @param string $notice The notice name.
218 236 * @return bool
219 - * @since 2.7
220 237 */
221 238 public function can_display_notice( $can_display_notice, $notice ) {
222 239 if ( ! $can_display_notice && 'wizard' === $notice ) {
223 240 $screen = get_current_screen();
224 - $can_display_notice = in_array(
241 + $can_display_notice = ! empty( $screen ) && in_array(
225 242 $screen->base,
226 243 array(
227 244 'edit',
228 245 'upload',
@@ -236,8 +253,10 @@
236 253 /**
237 254 * Return html code of the wizard notice
238 255 *
239 256 * @since 2.7
257 + *
258 + * @return string
240 259 */
241 260 public function wizard_notice() {
242 261 ob_start();
243 262 include __DIR__ . '/html-wizard-notice.php';
@@ -247,8 +266,10 @@
247 266 /**
248 267 * Display the wizard page
249 268 *
250 269 * @since 2.7
270 + *
271 + * @return void
251 272 */
252 273 public function display_wizard_page() {
253 274 set_current_screen();
254 275 include __DIR__ . '/view-wizard-page.php';
@@ -257,12 +278,14 @@
257 278 /**
258 279 * Enqueue scripts and styles for the wizard
259 280 *
260 281 * @since 2.7
282 + *
283 + * @return void
261 284 */
262 285 public function enqueue_scripts() {
263 - wp_enqueue_style( 'polylang_admin', plugins_url( '/css/admin' . $this->get_suffix() . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION );
264 - wp_enqueue_style( 'pll-wizard', plugins_url( '/modules/wizard/css/wizard' . $this->get_suffix() . '.css', POLYLANG_FILE ), array( 'dashicons', 'install', 'common', 'forms' ), POLYLANG_VERSION );
286 + wp_enqueue_style( 'polylang_admin', plugins_url( '/css/build/admin' . $this->get_suffix() . '.css', POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION );
287 + wp_enqueue_style( 'pll-wizard', plugins_url( '/css/build/wizard' . $this->get_suffix() . '.css', POLYLANG_ROOT_FILE ), array( 'dashicons', 'install', 'common', 'forms' ), POLYLANG_VERSION );
265 288
266 289 $this->styles = array( 'polylang_admin', 'pll-wizard' );
267 290 }
268 291
@@ -268,11 +291,12 @@
268 291
269 292 /**
270 293 * Get the suffix to enqueue non minified files in a Debug context
271 294 *
295 + * @since 2.7
296 + *
272 297 * @return string Empty when SCRIPT_DEBUG equal to true
273 298 * otherwise .min
274 - * @since 2.7
275 299 */
276 300 public function get_suffix() {
277 301 return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
278 302 }
@@ -279,12 +303,13 @@
279 303
280 304 /**
281 305 * Get the URL for the step's screen.
282 306 *
307 + * @since 2.7
308 + *
283 309 * @param string $step slug (default: current step).
284 310 * @return string URL for the step if it exists.
285 311 * Empty string on failure.
286 - * @since 2.7
287 312 */
288 313 public function get_step_link( $step = '' ) {
289 314 if ( ! $step ) {
290 315 $step = $this->step;
@@ -302,13 +327,14 @@
302 327
303 328 /**
304 329 * Get the URL for the next step's screen.
305 330 *
331 + * @since 2.7
332 + *
306 333 * @param string $step slug (default: current step).
307 334 * @return string URL for next step if a next step exists.
308 335 * Admin URL if it's the last step.
309 336 * Empty string on failure.
310 - * @since 2.7
311 337 */
312 338 public function get_next_step_link( $step = '' ) {
313 339 if ( ! $step ) {
314 340 $step = $this->step;
@@ -329,18 +355,22 @@
329 355
330 356 /**
331 357 * Add licenses step to the wizard
332 358 *
359 + * @since 2.7
360 + *
333 361 * @param array $steps List of steps.
334 362 * @return array List of steps updated.
335 - * @since 2.7
336 363 */
337 364 public function add_step_licenses( $steps ) {
338 365 // Add ajax action on deactivate button in licenses step.
339 366 add_action( 'wp_ajax_pll_deactivate_license', array( $this, 'deactivate_license' ) );
340 367
341 - wp_enqueue_script( 'pll_admin', plugins_url( '/js/admin' . $this->get_suffix() . '.js', POLYLANG_FILE ), array( 'jquery', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true );
342 - wp_localize_script( 'pll_admin', 'pll_dismiss_notice', esc_html__( 'Dismiss this notice.', 'polylang' ) );
368 + // Be careful pll_admin script is enqueued here without depedency except jquery because only code useful for deactivate license button is needed.
369 + // To be really loaded the script need to be passed to the $steps['licenses']['scripts'] array below with the same handle than in wp_enqueue_script().
370 + wp_enqueue_script( 'pll_admin', plugins_url( '/js/build/admin' . $this->get_suffix() . '.js', POLYLANG_ROOT_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
371 + wp_localize_script( 'pll_admin', 'pll_admin', array( 'dismiss_notice' => esc_html__( 'Dismiss this notice.', 'polylang' ) ) );
372 +
343 373 if ( $this->is_licenses_step_displayable() ) {
344 374 $steps['licenses'] = array(
345 375 'name' => esc_html__( 'Licenses', 'polylang' ),
346 376 'view' => array( $this, 'display_step_licenses' ),
@@ -355,8 +385,10 @@
355 385 /**
356 386 * Display the languages step form
357 387 *
358 388 * @since 2.7
389 + *
390 + * @return void
359 391 */
360 392 public function display_step_licenses() {
361 393 include __DIR__ . '/view-wizard-step-licenses.php';
362 394 }
@@ -364,8 +396,10 @@
364 396 /**
365 397 * Execute the languages step
366 398 *
367 399 * @since 2.7
400 + *
401 + * @return void
368 402 */
369 403 public function save_step_licenses() {
370 404 check_admin_referer( 'pll-wizard', '_pll_nonce' );
371 405
@@ -394,8 +428,10 @@
394 428 /**
395 429 * Ajax method to deactivate a license
396 430 *
397 431 * @since 2.7
432 + *
433 + * @return void
398 434 */
399 435 public function deactivate_license() {
400 436 check_ajax_referer( 'pll-wizard', '_pll_nonce' );
401 437
@@ -422,16 +458,20 @@
422 458
423 459 /**
424 460 * Add languages step to the wizard
425 461 *
462 + * @since 2.7
463 + *
426 464 * @param array $steps List of steps.
427 465 * @return array List of steps updated.
428 - * @since 2.7
429 466 */
430 467 public function add_step_languages( $steps ) {
431 - wp_enqueue_script( 'pll-wizard-language-choice', plugins_url( '/js/admin' . $this->get_suffix() . '.js', POLYLANG_FILE ), array( 'jquery', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true );
432 - wp_localize_script( 'pll-wizard-language-choice', 'pll_dismiss_notice', esc_html__( 'Dismiss this notice.', 'polylang' ) );
433 - wp_register_script( 'pll-wizard-languages', plugins_url( '/modules/wizard/js/languages-step' . $this->get_suffix() . '.js', POLYLANG_FILE ), array( 'jquery', 'jquery-ui-dialog' ), POLYLANG_VERSION, true );
468 + wp_deregister_script( 'pll_admin' ); // Deregister after the licenses step enqueue to update jquery-ui-selectmenu dependency.
469 + // The wp-ajax-response and postbox dependencies is useless in wizard steps espacially postbox which triggers a javascript error otherwise.
470 + // To be really loaded the script need to be passed to the $steps['languages']['scripts'] array below with the same handle than in wp_enqueue_script().
471 + wp_enqueue_script( 'pll_admin', plugins_url( '/js/build/admin' . $this->get_suffix() . '.js', POLYLANG_ROOT_FILE ), array( 'jquery', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true );
472 + wp_localize_script( 'pll_admin', 'pll_admin', array( 'dismiss_notice' => esc_html__( 'Dismiss this notice.', 'polylang' ) ) );
473 + wp_register_script( 'pll-wizard-languages', plugins_url( '/js/build/languages-step' . $this->get_suffix() . '.js', POLYLANG_ROOT_FILE ), array( 'jquery', 'jquery-ui-dialog' ), POLYLANG_VERSION, true );
434 474 wp_localize_script(
435 475 'pll-wizard-languages',
436 476 'pll_wizard_params',
437 477 array(
@@ -452,14 +492,14 @@
452 492 'i18n_remove_language_icon' => esc_html__( 'Remove this language', 'polylang' ),
453 493 )
454 494 );
455 495 wp_enqueue_script( 'pll-wizard-languages' );
456 - wp_enqueue_style( 'pll-wizard-selectmenu', plugins_url( '/css/selectmenu' . $this->get_suffix() . '.css', POLYLANG_FILE ), array( 'dashicons', 'install', 'common', 'wp-jquery-ui-dialog' ), POLYLANG_VERSION );
496 + wp_enqueue_style( 'pll-wizard-selectmenu', plugins_url( '/css/build/selectmenu' . $this->get_suffix() . '.css', POLYLANG_ROOT_FILE ), array( 'dashicons', 'install', 'common', 'wp-jquery-ui-dialog' ), POLYLANG_VERSION );
457 497 $steps['languages'] = array(
458 498 'name' => esc_html__( 'Languages', 'polylang' ),
459 499 'view' => array( $this, 'display_step_languages' ),
460 500 'handler' => array( $this, 'save_step_languages' ),
461 - 'scripts' => array( 'pll-wizard-languages', 'pll-wizard-language-choice' ),
501 + 'scripts' => array( 'pll-wizard-languages', 'pll_admin' ),
462 502 'styles' => array( 'pll-wizard-selectmenu' ),
463 503 );
464 504 return $steps;
465 505 }
@@ -467,8 +507,10 @@
467 507 /**
468 508 * Display the languages step form
469 509 *
470 510 * @since 2.7
511 + *
512 + * @return void
471 513 */
472 514 public function display_step_languages() {
473 515 include __DIR__ . '/view-wizard-step-languages.php';
474 516 }
@@ -476,8 +518,10 @@
476 518 /**
477 519 * Execute the languages step
478 520 *
479 521 * @since 2.7
522 + *
523 + * @return void
480 524 */
481 525 public function save_step_languages() {
482 526 check_admin_referer( 'pll-wizard', '_pll_nonce' );
483 527
@@ -483,9 +527,9 @@
483 527
484 528 $existing_languages = $this->model->get_languages_list();
485 529
486 530 $all_languages = include POLYLANG_DIR . '/settings/languages.php';
487 - $languages = isset( $_POST['languages'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['languages'] ) ) : false;
531 + $languages = isset( $_POST['languages'] ) && is_array( $_POST['languages'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['languages'] ) ) : false;
488 532 $saved_languages = array();
489 533
490 534 // If there is no language added or defined.
491 535 if ( empty( $languages ) && empty( $existing_languages ) ) {
@@ -549,14 +593,14 @@
549 593 exit;
550 594 }
551 595
552 596 /**
553 - * Add media step to the wizard
554 - * Add media step to the wizard
597 + * Add the media step to the wizard.
555 598 *
599 + * @since 2.7
600 + *
556 601 * @param array $steps List of steps.
557 602 * @return array List of steps updated.
558 - * @since 2.7
559 603 */
560 604 public function add_step_media( $steps ) {
561 605 $languages = $this->model->get_languages_list();
562 606
@@ -575,8 +619,10 @@
575 619 /**
576 620 * Display the media step form
577 621 *
578 622 * @since 2.7
623 + *
624 + * @return void
579 625 */
580 626 public function display_step_media() {
581 627 include __DIR__ . '/view-wizard-step-media.php';
582 628 }
@@ -584,8 +630,10 @@
584 630 /**
585 631 * Execute the media step
586 632 *
587 633 * @since 2.7
634 + *
635 + * @return void
588 636 */
589 637 public function save_step_media() {
590 638 check_admin_referer( 'pll-wizard', '_pll_nonce' );
591 639
@@ -601,22 +649,25 @@
601 649
602 650 /**
603 651 * Add untranslated contents step to the wizard
604 652 *
653 + * @since 2.7
654 + *
605 655 * @param array $steps List of steps.
606 656 * @return array List of steps updated.
607 - * @since 2.7
608 657 */
609 658 public function add_step_untranslated_contents( $steps ) {
610 659 if ( ! $this->model->get_languages_list() || $this->model->get_objects_with_no_lang( 1 ) ) {
611 - wp_enqueue_script( 'pll-wizard-language-choice', plugins_url( '/js/admin' . $this->get_suffix() . '.js', POLYLANG_FILE ), array( 'jquery', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true );
612 - wp_localize_script( 'pll-wizard-language-choice', 'pll_dismiss_notice', esc_html__( 'Dismiss this notice.', 'polylang' ) );
613 - wp_enqueue_style( 'pll-wizard-selectmenu', plugins_url( '/css/selectmenu' . $this->get_suffix() . '.css', POLYLANG_FILE ), array( 'dashicons', 'install', 'common' ), POLYLANG_VERSION );
660 + // Even if pll_admin is already enqueued with the same dependencies by the languages step, it is interesting to keep that it's also useful for the untranslated-contents step.
661 + // To be really loaded the script need to be passed to the $steps['untranslated-contents']['scripts'] array below with the same handle than in wp_enqueue_script().
662 + wp_enqueue_script( 'pll_admin', plugins_url( '/js/build/admin' . $this->get_suffix() . '.js', POLYLANG_ROOT_FILE ), array( 'jquery', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true );
663 + wp_localize_script( 'pll_admin', 'pll_admin', array( 'dismiss_notice' => esc_html__( 'Dismiss this notice.', 'polylang' ) ) );
664 + wp_enqueue_style( 'pll-wizard-selectmenu', plugins_url( '/css/build/selectmenu' . $this->get_suffix() . '.css', POLYLANG_ROOT_FILE ), array( 'dashicons', 'install', 'common' ), POLYLANG_VERSION );
614 665 $steps['untranslated-contents'] = array(
615 666 'name' => esc_html__( 'Content', 'polylang' ),
616 667 'view' => array( $this, 'display_step_untranslated_contents' ),
617 668 'handler' => array( $this, 'save_step_untranslated_contents' ),
618 - 'scripts' => array( 'pll-wizard-language-choice' ),
669 + 'scripts' => array( 'pll_admin' ),
619 670 'styles' => array( 'pll-wizard-selectmenu' ),
620 671 );
621 672 }
622 673 return $steps;
@@ -625,8 +676,10 @@
625 676 /**
626 677 * Display the untranslated contents step form
627 678 *
628 679 * @since 2.7
680 + *
681 + * @return void
629 682 */
630 683 public function display_step_untranslated_contents() {
631 684 include __DIR__ . '/view-wizard-step-untranslated-contents.php';
632 685 }
@@ -634,8 +687,10 @@
634 687 /**
635 688 * Execute the untranslated contents step
636 689 *
637 690 * @since 2.7
691 + *
692 + * @return void
638 693 */
639 694 public function save_step_untranslated_contents() {
640 695 check_admin_referer( 'pll-wizard', '_pll_nonce' );
641 696
@@ -662,11 +717,12 @@
662 717
663 718 /**
664 719 * Add home page step to the wizard
665 720 *
721 + * @since 2.7
722 + *
666 723 * @param array $steps List of steps.
667 724 * @return array List of steps updated.
668 - * @since 2.7
669 725 */
670 726 public function add_step_home_page( $steps ) {
671 727 $languages = $this->model->get_languages_list();
672 728 $home_page_id = get_option( 'page_on_front' );
@@ -688,8 +744,10 @@
688 744 /**
689 745 * Display the home page step form
690 746 *
691 747 * @since 2.7
748 + *
749 + * @return void
692 750 */
693 751 public function display_step_home_page() {
694 752 include __DIR__ . '/view-wizard-step-home-page.php';
695 753 }
@@ -697,8 +755,10 @@
697 755 /**
698 756 * Execute the home page step
699 757 *
700 758 * @since 2.7
759 + *
760 + * @return void
701 761 */
702 762 public function save_step_home_page() {
703 763 check_admin_referer( 'pll-wizard', '_pll_nonce' );
704 764
@@ -730,13 +790,14 @@
730 790 * Create home page translations for each language defined.
731 791 *
732 792 * @since 2.7
733 793 *
734 - * @param string $default_language slug of the default language; null if no default language is defined.
735 - * @param int $home_page post_id of the home page if it's defined, false otherwise.
736 - * @param string $home_page_title home page title if it's defined, 'Homepage' otherwise.
737 - * @param string $home_page_language slug of the home page if it's defined, false otherwise.
738 - * @param array $untranslated_languages array of languages which needs to have a home page translated.
794 + * @param string $default_language Slug of the default language; null if no default language is defined.
795 + * @param int $home_page Post ID of the home page if it's defined, false otherwise.
796 + * @param string $home_page_title Home page title if it's defined, 'Homepage' otherwise.
797 + * @param string $home_page_language Slug of the home page if it's defined, false otherwise.
798 + * @param string[] $untranslated_languages Array of languages which needs to have a home page translated.
799 + * @return void
739 800 */
740 801 public function create_home_page_translations( $default_language, $home_page, $home_page_title, $home_page_language, $untranslated_languages ) {
741 802 $translations = $this->model->post->get_translations( $home_page );
742 803
@@ -757,11 +818,12 @@
757 818
758 819 /**
759 820 * Add last step to the wizard
760 821 *
822 + * @since 2.7
823 + *
761 824 * @param array $steps List of steps.
762 825 * @return array List of steps updated.
763 - * @since 2.7
764 826 */
765 827 public function add_step_last( $steps ) {
766 828 $steps['last'] = array(
767 829 'name' => esc_html__( 'Ready!', 'polylang' ),
@@ -776,8 +838,10 @@
776 838 /**
777 839 * Display the last step form
778 840 *
779 841 * @since 2.7
842 + *
843 + * @return void
780 844 */
781 845 public function display_step_last() {
782 846 // We ran the wizard once. So we can dismiss its notice.
783 847 PLL_Admin_Notices::dismiss( 'wizard' );
@@ -787,8 +851,10 @@
787 851 /**
788 852 * Execute the last step
789 853 *
790 854 * @since 2.7
855 + *
856 + * @return void
791 857 */
792 858 public function save_step_last() {
793 859 check_admin_referer( 'pll-wizard', '_pll_nonce' );
794 860