| @@ -1,44 +1,36 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 6 | +defined( 'ABSPATH' ) || exit; | |
| 7 | + | |
| 3 | 8 | /** |
| 4 | - * A class for the Polylang settings pages | |
| 5 | - * accessible in $polylang global object | |
| 9 | + * A class for the Polylang settings pages, accessible from @see PLL(). | |
| 6 | 10 | * |
| 7 | - * Properties: | |
| 8 | - * options => inherited, reference to Polylang options array | |
| 9 | - * model => inherited, reference to PLL_Model object | |
| 10 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 11 | - * links => inherited, reference to PLL_Admin_Links object | |
| 12 | - * static_pages => inherited, reference to PLL_Admin_Static_Pages object | |
| 13 | - * filters_links => inherited, reference to PLL_Filters_Links object | |
| 14 | - * curlang => inherited, optional, current language used to filter admin content | |
| 15 | - * pref_lang => inherited, preferred language used as default when saving posts or terms | |
| 16 | - * | |
| 17 | 11 | * @since 1.2 |
| 18 | 12 | */ |
| 19 | 13 | class PLL_Settings extends PLL_Admin_Base { |
| 20 | 14 | |
| 21 | 15 | /** |
| 22 | - * Name of the active module | |
| 23 | - * | |
| 24 | - * @var string $active_tab | |
| 16 | + * @var PLL_Admin_Model | |
| 25 | 17 | */ |
| 26 | - protected $active_tab; | |
| 18 | + public $model; | |
| 27 | 19 | |
| 28 | 20 | /** |
| 29 | - * Array of modules classes | |
| 21 | + * Name of the active module. | |
| 30 | 22 | * |
| 31 | - * @var array $modules | |
| 23 | + * @var string|null | |
| 32 | 24 | */ |
| 33 | - protected $modules; | |
| 25 | + protected $active_tab; | |
| 34 | 26 | |
| 35 | 27 | /** |
| 36 | - * Reference to PLL_Import_Export | |
| 28 | + * Array of modules classes. | |
| 37 | 29 | * |
| 38 | - * @var PLL_Import_Export $import_export | |
| 30 | + * @var PLL_Settings_Module[]|null | |
| 39 | 31 | */ |
| 40 | - protected $import_export; | |
| 32 | + protected $modules; | |
| 41 | 33 | |
| 42 | 34 | /** |
| 43 | 35 | * Constructor |
| 44 | 36 | * |
| @@ -43,9 +35,9 @@ | ||
| 43 | 35 | * Constructor |
| 44 | 36 | * |
| 45 | 37 | * @since 1.2 |
| 46 | 38 | * |
| 47 | - * @param object $links_model | |
| 39 | + * @param PLL_Links_Model $links_model Reference to the links model. | |
| 48 | 40 | */ |
| 49 | 41 | public function __construct( &$links_model ) { |
| 50 | 42 | parent::__construct( $links_model ); |
| 51 | 43 | |
| @@ -54,21 +46,17 @@ | ||
| 54 | 46 | } |
| 55 | 47 | |
| 56 | 48 | PLL_Admin_Strings::init(); |
| 57 | 49 | |
| 58 | - if ( class_exists( 'PLL_Import_Export' ) ) { | |
| 59 | - $this->import_export = new PLL_Import_Export( $this ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - // FIXME put this as late as possible | |
| 63 | 50 | add_action( 'admin_init', array( $this, 'register_settings_modules' ) ); |
| 64 | 51 | |
| 65 | - // 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. | |
| 66 | 53 | add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) ); |
| 67 | 54 | add_action( 'load-languages_page_mlang_strings', array( $this, 'load_page_strings' ) ); |
| 68 | 55 | |
| 69 | - // Saves per-page value in screen option | |
| 70 | - 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 ); | |
| 71 | 59 | } |
| 72 | 60 | |
| 73 | 61 | /** |
| 74 | 62 | * Initializes the modules |
| @@ -73,30 +61,25 @@ | ||
| 73 | 61 | /** |
| 74 | 62 | * Initializes the modules |
| 75 | 63 | * |
| 76 | 64 | * @since 1.8 |
| 65 | + * | |
| 66 | + * @return void | |
| 77 | 67 | */ |
| 78 | 68 | public function register_settings_modules() { |
| 79 | - $modules = array( | |
| 80 | - 'PLL_Settings_Licenses', | |
| 81 | - ); | |
| 69 | + $modules = array(); | |
| 82 | 70 | |
| 83 | - if ( $this->model->get_languages_list() ) { | |
| 84 | - $modules = array_merge( | |
| 85 | - array( | |
| 86 | - 'PLL_Settings_Url', | |
| 87 | - 'PLL_Settings_Browser', | |
| 88 | - 'PLL_Settings_Media', | |
| 89 | - 'PLL_Settings_CPT', | |
| 90 | - 'PLL_Settings_Sync', | |
| 91 | - 'PLL_Settings_WPML', | |
| 92 | - 'PLL_Settings_Share_Slug', | |
| 93 | - 'PLL_Settings_Translate_Slugs', | |
| 94 | - ), | |
| 95 | - $modules | |
| 71 | + if ( $this->model->has_languages() ) { | |
| 72 | + $modules = array( | |
| 73 | + 'PLL_Settings_Url', | |
| 74 | + 'PLL_Settings_Browser', | |
| 75 | + 'PLL_Settings_Media', | |
| 76 | + 'PLL_Settings_CPT', | |
| 96 | 77 | ); |
| 97 | 78 | } |
| 98 | 79 | |
| 80 | + $modules[] = 'PLL_Settings_Licenses'; | |
| 81 | + | |
| 99 | 82 | /** |
| 100 | 83 | * Filter the list of setting modules |
| 101 | 84 | * |
| 102 | 85 | * @since 1.8 |
| @@ -114,11 +97,13 @@ | ||
| 114 | 97 | /** |
| 115 | 98 | * Loads the about metabox |
| 116 | 99 | * |
| 117 | 100 | * @since 0.8 |
| 101 | + * | |
| 102 | + * @return void | |
| 118 | 103 | */ |
| 119 | 104 | public function metabox_about() { |
| 120 | - include PLL_SETTINGS_INC . '/view-about.php'; | |
| 105 | + include __DIR__ . '/view-about.php'; | |
| 121 | 106 | } |
| 122 | 107 | |
| 123 | 108 | /** |
| 124 | 109 | * Adds screen options and the about box in the languages admin panel |
| @@ -123,8 +108,10 @@ | ||
| 123 | 108 | /** |
| 124 | 109 | * Adds screen options and the about box in the languages admin panel |
| 125 | 110 | * |
| 126 | 111 | * @since 0.9.5 |
| 112 | + * | |
| 113 | + * @return void | |
| 127 | 114 | */ |
| 128 | 115 | public function load_page() { |
| 129 | 116 | if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) { |
| 130 | 117 | add_meta_box( |
| @@ -151,8 +138,10 @@ | ||
| 151 | 138 | /** |
| 152 | 139 | * Adds screen options in the strings translations admin panel |
| 153 | 140 | * |
| 154 | 141 | * @since 2.1 |
| 142 | + * | |
| 143 | + * @return void | |
| 155 | 144 | */ |
| 156 | 145 | public function load_page_strings() { |
| 157 | 146 | add_screen_option( |
| 158 | 147 | 'per_page', |
| @@ -164,28 +153,28 @@ | ||
| 164 | 153 | ); |
| 165 | 154 | } |
| 166 | 155 | |
| 167 | 156 | /** |
| 168 | - * 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. | |
| 169 | 158 | * |
| 170 | 159 | * @since 0.9.5 |
| 171 | 160 | * |
| 172 | - * @param mixed $status false or value returned by previous filter | |
| 173 | - * @param string $option Name of the option being changed | |
| 174 | - * @param string $value Value of the option | |
| 175 | - * | |
| 176 | - * @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. | |
| 177 | 165 | */ |
| 178 | - public function set_screen_option( $status, $option, $value ) { | |
| 179 | - 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; | |
| 180 | 168 | } |
| 181 | 169 | |
| 182 | 170 | /** |
| 183 | - * Manages the user input for the languages pages | |
| 171 | + * Manages the user input for the languages pages. | |
| 184 | 172 | * |
| 185 | 173 | * @since 1.9 |
| 186 | 174 | * |
| 187 | - * @param string $action | |
| 175 | + * @param string $action The action name. | |
| 176 | + * @return void | |
| 188 | 177 | */ |
| 189 | 178 | public function handle_actions( $action ) { |
| 190 | 179 | switch ( $action ) { |
| 191 | 180 | case 'add': |
| @@ -192,20 +181,18 @@ | ||
| 192 | 181 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 193 | 182 | $errors = $this->model->add_language( $_POST ); |
| 194 | 183 | |
| 195 | 184 | if ( is_wp_error( $errors ) ) { |
| 196 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 197 | - add_settings_error( 'general', 'pll_add_language', $message ); | |
| 198 | - } | |
| 185 | + pll_add_notice( $errors ); | |
| 199 | 186 | } else { |
| 200 | - add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' ); | |
| 201 | - $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 | |
| 202 | 189 | |
| 203 | - if ( 'en_US' !== $locale ) { | |
| 190 | + if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) { | |
| 204 | 191 | // Attempts to install the language pack |
| 205 | 192 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 206 | 193 | if ( ! wp_download_language_pack( $locale ) ) { |
| 207 | - 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' ) ); | |
| 208 | 195 | } |
| 209 | 196 | |
| 210 | 197 | // Force checking for themes and plugins translations updates |
| 211 | 198 | wp_clean_themes_cache(); |
| @@ -218,9 +205,9 @@ | ||
| 218 | 205 | case 'delete': |
| 219 | 206 | check_admin_referer( 'delete-lang' ); |
| 220 | 207 | |
| 221 | 208 | if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) { |
| 222 | - 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' ) ); | |
| 223 | 210 | } |
| 224 | 211 | |
| 225 | 212 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 226 | 213 | break; |
| @@ -229,13 +216,11 @@ | ||
| 229 | 216 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 230 | 217 | $errors = $this->model->update_language( $_POST ); |
| 231 | 218 | |
| 232 | 219 | if ( is_wp_error( $errors ) ) { |
| 233 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 234 | - add_settings_error( 'general', 'pll_update_language', $message ); | |
| 235 | - } | |
| 220 | + pll_add_notice( $errors ); | |
| 236 | 221 | } else { |
| 237 | - 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' ) ); | |
| 238 | 223 | } |
| 239 | 224 | |
| 240 | 225 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 241 | 226 | break; |
| @@ -252,16 +237,9 @@ | ||
| 252 | 237 | |
| 253 | 238 | case 'content-default-lang': |
| 254 | 239 | check_admin_referer( 'content-default-lang' ); |
| 255 | 240 | |
| 256 | - if ( $nolang = $this->model->get_objects_with_no_lang() ) { | |
| 257 | - if ( ! empty( $nolang['posts'] ) ) { | |
| 258 | - $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] ); | |
| 259 | - } | |
| 260 | - if ( ! empty( $nolang['terms'] ) ) { | |
| 261 | - $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] ); | |
| 262 | - } | |
| 263 | - } | |
| 241 | + $this->model->set_language_in_mass(); | |
| 264 | 242 | |
| 265 | 243 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 266 | 244 | break; |
| 267 | 245 | |
| @@ -302,8 +280,10 @@ | ||
| 302 | 280 | * Displays the 3 tabs pages: languages, strings translations, settings |
| 303 | 281 | * Also manages user input for these pages |
| 304 | 282 | * |
| 305 | 283 | * @since 0.1 |
| 284 | + * | |
| 285 | + * @return void | |
| 306 | 286 | */ |
| 307 | 287 | public function languages_page() { |
| 308 | 288 | switch ( $this->active_tab ) { |
| 309 | 289 | case 'lang': |
| @@ -320,9 +300,9 @@ | ||
| 320 | 300 | |
| 321 | 301 | // Handle user input |
| 322 | 302 | $action = isset( $_REQUEST['pll_action'] ) ? sanitize_key( $_REQUEST['pll_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 323 | 303 | if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 324 | - // phpcs:ignore WordPress.Security.NonceVerification, WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 304 | + // phpcs:ignore WordPress.Security.NonceVerification, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 325 | 305 | $edit_lang = $this->model->get_language( (int) $_GET['lang'] ); |
| 326 | 306 | } else { |
| 327 | 307 | $this->handle_actions( $action ); |
| 328 | 308 | } |
| @@ -327,13 +307,17 @@ | ||
| 327 | 307 | $this->handle_actions( $action ); |
| 328 | 308 | } |
| 329 | 309 | |
| 330 | 310 | // Displays the page |
| 331 | - include PLL_SETTINGS_INC . '/view-languages.php'; | |
| 311 | + $modules = $this->modules; | |
| 312 | + $active_tab = $this->active_tab; | |
| 313 | + include __DIR__ . '/view-languages.php'; | |
| 332 | 314 | } |
| 333 | 315 | |
| 334 | 316 | /** |
| 335 | 317 | * Enqueues scripts and styles |
| 318 | + * | |
| 319 | + * @return void | |
| 336 | 320 | */ |
| 337 | 321 | public function admin_enqueue_scripts() { |
| 338 | 322 | parent::admin_enqueue_scripts(); |
| 339 | 323 | |
| @@ -338,12 +322,12 @@ | ||
| 338 | 322 | parent::admin_enqueue_scripts(); |
| 339 | 323 | |
| 340 | 324 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 341 | 325 | |
| 342 | - wp_enqueue_script( 'pll_admin', plugins_url( '/js/admin' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION ); | |
| 343 | - wp_localize_script( 'pll_admin', 'pll_flag_base_url', plugins_url( '/flags/', POLYLANG_FILE ) ); | |
| 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' ) ) ); | |
| 344 | 328 | |
| 345 | - 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 ); | |
| 346 | 330 | } |
| 347 | 331 | |
| 348 | 332 | /** |
| 349 | 333 | * Displays a notice when there are objects with no language assigned |
| @@ -348,8 +332,10 @@ | ||
| 348 | 332 | /** |
| 349 | 333 | * Displays a notice when there are objects with no language assigned |
| 350 | 334 | * |
| 351 | 335 | * @since 1.8 |
| 336 | + * | |
| 337 | + * @return void | |
| 352 | 338 | */ |
| 353 | 339 | public function notice_objects_with_no_lang() { |
| 354 | 340 | if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) { |
| 355 | 341 | printf( |
| @@ -367,11 +353,13 @@ | ||
| 367 | 353 | * |
| 368 | 354 | * @since 1.5 |
| 369 | 355 | * |
| 370 | 356 | * @param array $args query arguments to add to the url |
| 357 | + * @return void | |
| 371 | 358 | */ |
| 372 | 359 | public static function redirect( $args = array() ) { |
| 373 | - if ( $errors = get_settings_errors() ) { | |
| 360 | + $errors = get_settings_errors( 'polylang' ); | |
| 361 | + if ( ! empty( $errors ) ) { | |
| 374 | 362 | set_transient( 'settings_errors', $errors, 30 ); |
| 375 | 363 | $args['settings-updated'] = 1; |
| 376 | 364 | } |
| 377 | 365 | // Remove possible 'pll_action' and 'lang' query args from the referer before redirecting |
| @@ -382,13 +370,27 @@ | ||
| 382 | 370 | /** |
| 383 | 371 | * Get the list of predefined languages |
| 384 | 372 | * |
| 385 | 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 | + * } | |
| 386 | 388 | */ |
| 387 | 389 | public static function get_predefined_languages() { |
| 388 | 390 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 389 | 391 | |
| 390 | - $languages = include PLL_SETTINGS_INC . '/languages.php'; | |
| 392 | + $languages = include __DIR__ . '/languages.php'; | |
| 391 | 393 | $translations = wp_get_available_translations(); |
| 392 | 394 | |
| 393 | 395 | // Keep only languages with existing WP language pack |
| 394 | 396 | // Unless the transient has expired and we don't have an internet connection to refresh it |
| @@ -401,15 +403,15 @@ | ||
| 401 | 403 | * Filter the list of predefined languages |
| 402 | 404 | * |
| 403 | 405 | * @since 1.7.10 |
| 404 | 406 | * @since 2.3 The languages arrays use associative keys instead of numerical keys |
| 405 | - * @see settings/languages.php | |
| 407 | + * @see https://github.com/polylang/polylang/blob/2.8.2/settings/languages.php the list of predefined languages | |
| 406 | 408 | * |
| 407 | 409 | * @param array $languages |
| 408 | 410 | */ |
| 409 | 411 | $languages = apply_filters( 'pll_predefined_languages', $languages ); |
| 410 | 412 | |
| 411 | - // Keep only languages with all necessary informations | |
| 413 | + // Keep only languages with all necessary information | |
| 412 | 414 | foreach ( $languages as $k => $lang ) { |
| 413 | 415 | if ( ! isset( $lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'] ) ) { |
| 414 | 416 | unset( $languages[ $k ] ); |
| 415 | 417 | } |