| @@ -2,37 +2,33 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | +defined( 'ABSPATH' ) || exit; | |
| 7 | + | |
| 6 | 8 | /** |
| 7 | - * A class for the Polylang settings pages | |
| 8 | - * accessible in $polylang global object | |
| 9 | + * A class for the Polylang settings pages, accessible from @see PLL(). | |
| 9 | 10 | * |
| 10 | - * Properties: | |
| 11 | - * options => inherited, reference to Polylang options array | |
| 12 | - * model => inherited, reference to PLL_Model object | |
| 13 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 14 | - * links => inherited, reference to PLL_Admin_Links object | |
| 15 | - * static_pages => inherited, reference to PLL_Admin_Static_Pages object | |
| 16 | - * filters_links => inherited, reference to PLL_Filters_Links object | |
| 17 | - * curlang => inherited, optional, current language used to filter admin content | |
| 18 | - * pref_lang => inherited, preferred language used as default when saving posts or terms | |
| 19 | - * | |
| 20 | 11 | * @since 1.2 |
| 21 | 12 | */ |
| 22 | 13 | class PLL_Settings extends PLL_Admin_Base { |
| 23 | 14 | |
| 24 | 15 | /** |
| 25 | - * Name of the active module | |
| 16 | + * @var PLL_Admin_Model | |
| 17 | + */ | |
| 18 | + public $model; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Name of the active module. | |
| 26 | 22 | * |
| 27 | - * @var string $active_tab | |
| 23 | + * @var string|null | |
| 28 | 24 | */ |
| 29 | 25 | protected $active_tab; |
| 30 | 26 | |
| 31 | 27 | /** |
| 32 | - * Array of modules classes | |
| 28 | + * Array of modules classes. | |
| 33 | 29 | * |
| 34 | - * @var array $modules | |
| 30 | + * @var PLL_Settings_Module[]|null | |
| 35 | 31 | */ |
| 36 | 32 | protected $modules; |
| 37 | 33 | |
| 38 | 34 | /** |
| @@ -39,9 +35,9 @@ | ||
| 39 | 35 | * Constructor |
| 40 | 36 | * |
| 41 | 37 | * @since 1.2 |
| 42 | 38 | * |
| 43 | - * @param object $links_model | |
| 39 | + * @param PLL_Links_Model $links_model Reference to the links model. | |
| 44 | 40 | */ |
| 45 | 41 | public function __construct( &$links_model ) { |
| 46 | 42 | parent::__construct( $links_model ); |
| 47 | 43 | |
| @@ -52,14 +48,15 @@ | ||
| 52 | 48 | PLL_Admin_Strings::init(); |
| 53 | 49 | |
| 54 | 50 | add_action( 'admin_init', array( $this, 'register_settings_modules' ) ); |
| 55 | 51 | |
| 56 | - // Adds screen options and the about box in the languages admin panel | |
| 52 | + // Adds screen options and the about box in the languages admin panel. | |
| 57 | 53 | add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) ); |
| 58 | 54 | add_action( 'load-languages_page_mlang_strings', array( $this, 'load_page_strings' ) ); |
| 59 | 55 | |
| 60 | - // Saves per-page value in screen option | |
| 61 | - add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 ); | |
| 56 | + // Saves the per-page value in screen options. | |
| 57 | + add_filter( 'set_screen_option_pll_lang_per_page', array( $this, 'set_screen_option' ), 10, 3 ); | |
| 58 | + add_filter( 'set_screen_option_pll_strings_per_page', array( $this, 'set_screen_option' ), 10, 3 ); | |
| 62 | 59 | } |
| 63 | 60 | |
| 64 | 61 | /** |
| 65 | 62 | * Initializes the modules |
| @@ -64,13 +61,15 @@ | ||
| 64 | 61 | /** |
| 65 | 62 | * Initializes the modules |
| 66 | 63 | * |
| 67 | 64 | * @since 1.8 |
| 65 | + * | |
| 66 | + * @return void | |
| 68 | 67 | */ |
| 69 | 68 | public function register_settings_modules() { |
| 70 | 69 | $modules = array(); |
| 71 | 70 | |
| 72 | - if ( $this->model->get_languages_list() ) { | |
| 71 | + if ( $this->model->has_languages() ) { | |
| 73 | 72 | $modules = array( |
| 74 | 73 | 'PLL_Settings_Url', |
| 75 | 74 | 'PLL_Settings_Browser', |
| 76 | 75 | 'PLL_Settings_Media', |
| @@ -98,8 +97,10 @@ | ||
| 98 | 97 | /** |
| 99 | 98 | * Loads the about metabox |
| 100 | 99 | * |
| 101 | 100 | * @since 0.8 |
| 101 | + * | |
| 102 | + * @return void | |
| 102 | 103 | */ |
| 103 | 104 | public function metabox_about() { |
| 104 | 105 | include __DIR__ . '/view-about.php'; |
| 105 | 106 | } |
| @@ -107,8 +108,10 @@ | ||
| 107 | 108 | /** |
| 108 | 109 | * Adds screen options and the about box in the languages admin panel |
| 109 | 110 | * |
| 110 | 111 | * @since 0.9.5 |
| 112 | + * | |
| 113 | + * @return void | |
| 111 | 114 | */ |
| 112 | 115 | public function load_page() { |
| 113 | 116 | if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) { |
| 114 | 117 | add_meta_box( |
| @@ -135,8 +138,10 @@ | ||
| 135 | 138 | /** |
| 136 | 139 | * Adds screen options in the strings translations admin panel |
| 137 | 140 | * |
| 138 | 141 | * @since 2.1 |
| 142 | + * | |
| 143 | + * @return void | |
| 139 | 144 | */ |
| 140 | 145 | public function load_page_strings() { |
| 141 | 146 | add_screen_option( |
| 142 | 147 | 'per_page', |
| @@ -148,28 +153,28 @@ | ||
| 148 | 153 | ); |
| 149 | 154 | } |
| 150 | 155 | |
| 151 | 156 | /** |
| 152 | - * Save the "Views/Uploads per page" option set by this user | |
| 157 | + * Saves the number of rows in the languages or strings table set by this user. | |
| 153 | 158 | * |
| 154 | 159 | * @since 0.9.5 |
| 155 | 160 | * |
| 156 | - * @param mixed $status false or value returned by previous filter | |
| 157 | - * @param string $option Name of the option being changed | |
| 158 | - * @param string $value Value of the option | |
| 159 | - * | |
| 160 | - * @return string New value if this is our option, otherwise nothing | |
| 161 | + * @param mixed $screen_option False or value returned by a previous filter, not used. | |
| 162 | + * @param string $option The name of the option, not used. | |
| 163 | + * @param int $value The new value of the option to save. | |
| 164 | + * @return int The new value of the option. | |
| 161 | 165 | */ |
| 162 | - public function set_screen_option( $status, $option, $value ) { | |
| 163 | - return 'pll_lang_per_page' === $option || 'pll_strings_per_page' === $option ? $value : $status; | |
| 166 | + public function set_screen_option( $screen_option, $option, $value ) { | |
| 167 | + return (int) $value; | |
| 164 | 168 | } |
| 165 | 169 | |
| 166 | 170 | /** |
| 167 | - * Manages the user input for the languages pages | |
| 171 | + * Manages the user input for the languages pages. | |
| 168 | 172 | * |
| 169 | 173 | * @since 1.9 |
| 170 | 174 | * |
| 171 | - * @param string $action | |
| 175 | + * @param string $action The action name. | |
| 176 | + * @return void | |
| 172 | 177 | */ |
| 173 | 178 | public function handle_actions( $action ) { |
| 174 | 179 | switch ( $action ) { |
| 175 | 180 | case 'add': |
| @@ -176,20 +181,18 @@ | ||
| 176 | 181 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 177 | 182 | $errors = $this->model->add_language( $_POST ); |
| 178 | 183 | |
| 179 | 184 | if ( is_wp_error( $errors ) ) { |
| 180 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 181 | - add_settings_error( 'general', 'pll_add_language', $message ); | |
| 182 | - } | |
| 185 | + pll_add_notice( $errors ); | |
| 183 | 186 | } else { |
| 184 | - add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' ); | |
| 185 | - $locale = sanitize_text_field( wp_unslash( $_POST['locale'] ) ); // phpcs:ignore WordPress.Security | |
| 187 | + pll_add_notice( new WP_Error( 'pll_languages_created', __( 'Language added.', 'polylang' ), 'success' ) ); | |
| 188 | + $locale = sanitize_locale_name( $_POST['locale'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated | |
| 186 | 189 | |
| 187 | 190 | if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) { |
| 188 | 191 | // Attempts to install the language pack |
| 189 | 192 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 190 | 193 | if ( ! wp_download_language_pack( $locale ) ) { |
| 191 | - add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) ); | |
| 194 | + pll_add_notice( new WP_Error( 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ), 'warning' ) ); | |
| 192 | 195 | } |
| 193 | 196 | |
| 194 | 197 | // Force checking for themes and plugins translations updates |
| 195 | 198 | wp_clean_themes_cache(); |
| @@ -202,9 +205,9 @@ | ||
| 202 | 205 | case 'delete': |
| 203 | 206 | check_admin_referer( 'delete-lang' ); |
| 204 | 207 | |
| 205 | 208 | if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) { |
| 206 | - add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' ); | |
| 209 | + pll_add_notice( new WP_Error( 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'success' ) ); | |
| 207 | 210 | } |
| 208 | 211 | |
| 209 | 212 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 210 | 213 | break; |
| @@ -213,13 +216,11 @@ | ||
| 213 | 216 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 214 | 217 | $errors = $this->model->update_language( $_POST ); |
| 215 | 218 | |
| 216 | 219 | if ( is_wp_error( $errors ) ) { |
| 217 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 218 | - add_settings_error( 'general', 'pll_update_language', $message ); | |
| 219 | - } | |
| 220 | + pll_add_notice( $errors ); | |
| 220 | 221 | } else { |
| 221 | - add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' ); | |
| 222 | + pll_add_notice( new WP_Error( 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'success' ) ); | |
| 222 | 223 | } |
| 223 | 224 | |
| 224 | 225 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 225 | 226 | break; |
| @@ -236,16 +237,9 @@ | ||
| 236 | 237 | |
| 237 | 238 | case 'content-default-lang': |
| 238 | 239 | check_admin_referer( 'content-default-lang' ); |
| 239 | 240 | |
| 240 | - if ( $nolang = $this->model->get_objects_with_no_lang() ) { | |
| 241 | - if ( ! empty( $nolang['posts'] ) ) { | |
| 242 | - $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] ); | |
| 243 | - } | |
| 244 | - if ( ! empty( $nolang['terms'] ) ) { | |
| 245 | - $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] ); | |
| 246 | - } | |
| 247 | - } | |
| 241 | + $this->model->set_language_in_mass(); | |
| 248 | 242 | |
| 249 | 243 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 250 | 244 | break; |
| 251 | 245 | |
| @@ -286,8 +280,10 @@ | ||
| 286 | 280 | * Displays the 3 tabs pages: languages, strings translations, settings |
| 287 | 281 | * Also manages user input for these pages |
| 288 | 282 | * |
| 289 | 283 | * @since 0.1 |
| 284 | + * | |
| 285 | + * @return void | |
| 290 | 286 | */ |
| 291 | 287 | public function languages_page() { |
| 292 | 288 | switch ( $this->active_tab ) { |
| 293 | 289 | case 'lang': |
| @@ -304,9 +300,9 @@ | ||
| 304 | 300 | |
| 305 | 301 | // Handle user input |
| 306 | 302 | $action = isset( $_REQUEST['pll_action'] ) ? sanitize_key( $_REQUEST['pll_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 307 | 303 | if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 308 | - // phpcs:ignore WordPress.Security.NonceVerification, WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 304 | + // phpcs:ignore WordPress.Security.NonceVerification, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 309 | 305 | $edit_lang = $this->model->get_language( (int) $_GET['lang'] ); |
| 310 | 306 | } else { |
| 311 | 307 | $this->handle_actions( $action ); |
| 312 | 308 | } |
| @@ -311,13 +307,17 @@ | ||
| 311 | 307 | $this->handle_actions( $action ); |
| 312 | 308 | } |
| 313 | 309 | |
| 314 | 310 | // Displays the page |
| 311 | + $modules = $this->modules; | |
| 312 | + $active_tab = $this->active_tab; | |
| 315 | 313 | include __DIR__ . '/view-languages.php'; |
| 316 | 314 | } |
| 317 | 315 | |
| 318 | 316 | /** |
| 319 | 317 | * Enqueues scripts and styles |
| 318 | + * | |
| 319 | + * @return void | |
| 320 | 320 | */ |
| 321 | 321 | public function admin_enqueue_scripts() { |
| 322 | 322 | parent::admin_enqueue_scripts(); |
| 323 | 323 | |
| @@ -322,13 +322,12 @@ | ||
| 322 | 322 | parent::admin_enqueue_scripts(); |
| 323 | 323 | |
| 324 | 324 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 325 | 325 | |
| 326 | - wp_enqueue_script( 'pll_admin', plugins_url( '/js/admin' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION ); | |
| 327 | - wp_localize_script( 'pll_admin', 'pll_flag_base_url', plugins_url( '/flags/', POLYLANG_FILE ) ); | |
| 328 | - wp_localize_script( 'pll_admin', 'pll_dismiss_notice', esc_html__( 'Dismiss this notice.', 'polylang' ) ); | |
| 326 | + wp_enqueue_script( 'pll_settings', plugins_url( '/js/build/settings' . $suffix . '.js', POLYLANG_ROOT_FILE ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu', 'wp-hooks' ), POLYLANG_VERSION, true ); | |
| 327 | + wp_localize_script( 'pll_settings', 'pll_settings', array( 'dismiss_notice' => esc_html__( 'Dismiss this notice.', 'polylang' ) ) ); | |
| 329 | 328 | |
| 330 | - wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/selectmenu' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION ); | |
| 329 | + wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/build/selectmenu' . $suffix . '.css', POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION ); | |
| 331 | 330 | } |
| 332 | 331 | |
| 333 | 332 | /** |
| 334 | 333 | * Displays a notice when there are objects with no language assigned |
| @@ -333,8 +332,10 @@ | ||
| 333 | 332 | /** |
| 334 | 333 | * Displays a notice when there are objects with no language assigned |
| 335 | 334 | * |
| 336 | 335 | * @since 1.8 |
| 336 | + * | |
| 337 | + * @return void | |
| 337 | 338 | */ |
| 338 | 339 | public function notice_objects_with_no_lang() { |
| 339 | 340 | if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) { |
| 340 | 341 | printf( |
| @@ -352,11 +353,13 @@ | ||
| 352 | 353 | * |
| 353 | 354 | * @since 1.5 |
| 354 | 355 | * |
| 355 | 356 | * @param array $args query arguments to add to the url |
| 357 | + * @return void | |
| 356 | 358 | */ |
| 357 | 359 | public static function redirect( $args = array() ) { |
| 358 | - if ( $errors = get_settings_errors() ) { | |
| 360 | + $errors = get_settings_errors( 'polylang' ); | |
| 361 | + if ( ! empty( $errors ) ) { | |
| 359 | 362 | set_transient( 'settings_errors', $errors, 30 ); |
| 360 | 363 | $args['settings-updated'] = 1; |
| 361 | 364 | } |
| 362 | 365 | // Remove possible 'pll_action' and 'lang' query args from the referer before redirecting |
| @@ -367,8 +370,22 @@ | ||
| 367 | 370 | /** |
| 368 | 371 | * Get the list of predefined languages |
| 369 | 372 | * |
| 370 | 373 | * @since 2.3 |
| 374 | + * | |
| 375 | + * @return string[][] { | |
| 376 | + * An array of array of language properties. | |
| 377 | + * | |
| 378 | + * @type string[] { | |
| 379 | + * @type string $code ISO 639-1 language code. | |
| 380 | + * @type string $locale WordPress locale. | |
| 381 | + * @type string $name Native language name. | |
| 382 | + * @type string $dir Text direction: 'ltr' or 'rtl'. | |
| 383 | + * @type string $flag Flag code, generally the country code. | |
| 384 | + * @type string $w3c W3C locale. | |
| 385 | + * @type string $facebook Facebook locale. | |
| 386 | + * } | |
| 387 | + * } | |
| 371 | 388 | */ |
| 372 | 389 | public static function get_predefined_languages() { |
| 373 | 390 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 374 | 391 | |
| @@ -386,15 +403,15 @@ | ||
| 386 | 403 | * Filter the list of predefined languages |
| 387 | 404 | * |
| 388 | 405 | * @since 1.7.10 |
| 389 | 406 | * @since 2.3 The languages arrays use associative keys instead of numerical keys |
| 390 | - * @see settings/languages.php | |
| 407 | + * @see https://github.com/polylang/polylang/blob/2.8.2/settings/languages.php the list of predefined languages | |
| 391 | 408 | * |
| 392 | 409 | * @param array $languages |
| 393 | 410 | */ |
| 394 | 411 | $languages = apply_filters( 'pll_predefined_languages', $languages ); |
| 395 | 412 | |
| 396 | - // Keep only languages with all necessary informations | |
| 413 | + // Keep only languages with all necessary information | |
| 397 | 414 | foreach ( $languages as $k => $lang ) { |
| 398 | 415 | if ( ! isset( $lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'] ) ) { |
| 399 | 416 | unset( $languages[ $k ] ); |
| 400 | 417 | } |