| @@ -2,8 +2,10 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | +defined( 'ABSPATH' ) || exit; | |
| 7 | + | |
| 6 | 8 | /** |
| 7 | 9 | * A class for the Polylang settings pages, accessible from @see PLL(). |
| 8 | 10 | * |
| 9 | 11 | * @since 1.2 |
| @@ -17,9 +19,9 @@ | ||
| 17 | 19 | |
| 18 | 20 | /** |
| 19 | 21 | * Name of the active module. |
| 20 | 22 | * |
| 21 | - * @var string | |
| 23 | + * @var string|null | |
| 22 | 24 | */ |
| 23 | 25 | protected $active_tab; |
| 24 | 26 | |
| 25 | 27 | /** |
| @@ -24,9 +26,9 @@ | ||
| 24 | 26 | |
| 25 | 27 | /** |
| 26 | 28 | * Array of modules classes. |
| 27 | 29 | * |
| 28 | - * @var PLL_Settings_Module[] | |
| 30 | + * @var PLL_Settings_Module[]|null | |
| 29 | 31 | */ |
| 30 | 32 | protected $modules; |
| 31 | 33 | |
| 32 | 34 | /** |
| @@ -46,14 +48,15 @@ | ||
| 46 | 48 | PLL_Admin_Strings::init(); |
| 47 | 49 | |
| 48 | 50 | add_action( 'admin_init', array( $this, 'register_settings_modules' ) ); |
| 49 | 51 | |
| 50 | - // 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. | |
| 51 | 53 | add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) ); |
| 52 | 54 | add_action( 'load-languages_page_mlang_strings', array( $this, 'load_page_strings' ) ); |
| 53 | 55 | |
| 54 | - // Saves per-page value in screen option | |
| 55 | - 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 ); | |
| 56 | 59 | } |
| 57 | 60 | |
| 58 | 61 | /** |
| 59 | 62 | * Initializes the modules |
| @@ -64,9 +67,9 @@ | ||
| 64 | 67 | */ |
| 65 | 68 | public function register_settings_modules() { |
| 66 | 69 | $modules = array(); |
| 67 | 70 | |
| 68 | - if ( $this->model->get_languages_list() ) { | |
| 71 | + if ( $this->model->has_languages() ) { | |
| 69 | 72 | $modules = array( |
| 70 | 73 | 'PLL_Settings_Url', |
| 71 | 74 | 'PLL_Settings_Browser', |
| 72 | 75 | 'PLL_Settings_Media', |
| @@ -150,28 +153,27 @@ | ||
| 150 | 153 | ); |
| 151 | 154 | } |
| 152 | 155 | |
| 153 | 156 | /** |
| 154 | - * 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. | |
| 155 | 158 | * |
| 156 | 159 | * @since 0.9.5 |
| 157 | 160 | * |
| 158 | - * @param mixed $status false or value returned by previous filter | |
| 159 | - * @param string $option Name of the option being changed | |
| 160 | - * @param string $value Value of the option | |
| 161 | - * | |
| 162 | - * @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. | |
| 163 | 165 | */ |
| 164 | - public function set_screen_option( $status, $option, $value ) { | |
| 165 | - 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; | |
| 166 | 168 | } |
| 167 | 169 | |
| 168 | 170 | /** |
| 169 | - * Manages the user input for the languages pages | |
| 171 | + * Manages the user input for the languages pages. | |
| 170 | 172 | * |
| 171 | 173 | * @since 1.9 |
| 172 | 174 | * |
| 173 | - * @param string $action | |
| 175 | + * @param string $action The action name. | |
| 174 | 176 | * @return void |
| 175 | 177 | */ |
| 176 | 178 | public function handle_actions( $action ) { |
| 177 | 179 | switch ( $action ) { |
| @@ -179,20 +181,18 @@ | ||
| 179 | 181 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 180 | 182 | $errors = $this->model->add_language( $_POST ); |
| 181 | 183 | |
| 182 | 184 | if ( is_wp_error( $errors ) ) { |
| 183 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 184 | - add_settings_error( 'general', 'pll_add_language', $message ); | |
| 185 | - } | |
| 185 | + pll_add_notice( $errors ); | |
| 186 | 186 | } else { |
| 187 | - add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' ); | |
| 188 | - $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 | |
| 189 | 189 | |
| 190 | 190 | if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) { |
| 191 | 191 | // Attempts to install the language pack |
| 192 | 192 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 193 | 193 | if ( ! wp_download_language_pack( $locale ) ) { |
| 194 | - 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' ) ); | |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | // Force checking for themes and plugins translations updates |
| 198 | 198 | wp_clean_themes_cache(); |
| @@ -205,9 +205,9 @@ | ||
| 205 | 205 | case 'delete': |
| 206 | 206 | check_admin_referer( 'delete-lang' ); |
| 207 | 207 | |
| 208 | 208 | if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) { |
| 209 | - 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' ) ); | |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 213 | 213 | break; |
| @@ -216,13 +216,11 @@ | ||
| 216 | 216 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 217 | 217 | $errors = $this->model->update_language( $_POST ); |
| 218 | 218 | |
| 219 | 219 | if ( is_wp_error( $errors ) ) { |
| 220 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 221 | - add_settings_error( 'general', 'pll_update_language', $message ); | |
| 222 | - } | |
| 220 | + pll_add_notice( $errors ); | |
| 223 | 221 | } else { |
| 224 | - 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' ) ); | |
| 225 | 223 | } |
| 226 | 224 | |
| 227 | 225 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 228 | 226 | break; |
| @@ -239,16 +237,9 @@ | ||
| 239 | 237 | |
| 240 | 238 | case 'content-default-lang': |
| 241 | 239 | check_admin_referer( 'content-default-lang' ); |
| 242 | 240 | |
| 243 | - if ( $nolang = $this->model->get_objects_with_no_lang() ) { | |
| 244 | - if ( ! empty( $nolang['posts'] ) ) { | |
| 245 | - $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] ); | |
| 246 | - } | |
| 247 | - if ( ! empty( $nolang['terms'] ) ) { | |
| 248 | - $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] ); | |
| 249 | - } | |
| 250 | - } | |
| 241 | + $this->model->set_language_in_mass(); | |
| 251 | 242 | |
| 252 | 243 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 253 | 244 | break; |
| 254 | 245 | |
| @@ -316,8 +307,10 @@ | ||
| 316 | 307 | $this->handle_actions( $action ); |
| 317 | 308 | } |
| 318 | 309 | |
| 319 | 310 | // Displays the page |
| 311 | + $modules = $this->modules; | |
| 312 | + $active_tab = $this->active_tab; | |
| 320 | 313 | include __DIR__ . '/view-languages.php'; |
| 321 | 314 | } |
| 322 | 315 | |
| 323 | 316 | /** |
| @@ -329,12 +322,12 @@ | ||
| 329 | 322 | parent::admin_enqueue_scripts(); |
| 330 | 323 | |
| 331 | 324 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 332 | 325 | |
| 333 | - wp_enqueue_script( 'pll_admin', plugins_url( '/js/build/admin' . $suffix . '.js', POLYLANG_BASENAME ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION, true ); | |
| 334 | - wp_localize_script( 'pll_admin', 'pll_admin', array( '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' ) ) ); | |
| 335 | 328 | |
| 336 | - wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/build/selectmenu' . $suffix . '.css', POLYLANG_BASENAME ), array(), POLYLANG_VERSION ); | |
| 329 | + wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/build/selectmenu' . $suffix . '.css', POLYLANG_ROOT_FILE ), array(), POLYLANG_VERSION ); | |
| 337 | 330 | } |
| 338 | 331 | |
| 339 | 332 | /** |
| 340 | 333 | * Displays a notice when there are objects with no language assigned |
| @@ -363,9 +356,10 @@ | ||
| 363 | 356 | * @param array $args query arguments to add to the url |
| 364 | 357 | * @return void |
| 365 | 358 | */ |
| 366 | 359 | public static function redirect( $args = array() ) { |
| 367 | - if ( $errors = get_settings_errors() ) { | |
| 360 | + $errors = get_settings_errors( 'polylang' ); | |
| 361 | + if ( ! empty( $errors ) ) { | |
| 368 | 362 | set_transient( 'settings_errors', $errors, 30 ); |
| 369 | 363 | $args['settings-updated'] = 1; |
| 370 | 364 | } |
| 371 | 365 | // Remove possible 'pll_action' and 'lang' query args from the referer before redirecting |
| @@ -377,16 +371,20 @@ | ||
| 377 | 371 | * Get the list of predefined languages |
| 378 | 372 | * |
| 379 | 373 | * @since 2.3 |
| 380 | 374 | * |
| 381 | - * @return string[] { | |
| 382 | - * @type string $code ISO 639-1 language code. | |
| 383 | - * @type string $locale WordPress locale. | |
| 384 | - * @type string $name Native language name. | |
| 385 | - * @type string $dir Text direction: 'ltr' or 'rtl'. | |
| 386 | - * @type string $flag Flag code, generally the country code. | |
| 387 | - * @type string $w3c W3C locale. | |
| 388 | - * @type string $facebook Facebook locale. | |
| 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 | + * } | |
| 389 | 387 | * } |
| 390 | 388 | */ |
| 391 | 389 | public static function get_predefined_languages() { |
| 392 | 390 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| @@ -411,9 +409,9 @@ | ||
| 411 | 409 | * @param array $languages |
| 412 | 410 | */ |
| 413 | 411 | $languages = apply_filters( 'pll_predefined_languages', $languages ); |
| 414 | 412 | |
| 415 | - // Keep only languages with all necessary informations | |
| 413 | + // Keep only languages with all necessary information | |
| 416 | 414 | foreach ( $languages as $k => $lang ) { |
| 417 | 415 | if ( ! isset( $lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'] ) ) { |
| 418 | 416 | unset( $languages[ $k ] ); |
| 419 | 417 | } |