| @@ -1,51 +1,42 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * A class for the Polylang settings pages, accessible from @see PLL(). | |
| 4 | + * A class for the Polylang settings pages | |
| 5 | + * accessible in $polylang global object | |
| 8 | 6 | * |
| 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 | + * | |
| 9 | 17 | * @since 1.2 |
| 10 | 18 | */ |
| 11 | 19 | class PLL_Settings extends PLL_Admin_Base { |
| 20 | + protected $active_tab, $modules; | |
| 12 | 21 | |
| 13 | 22 | /** |
| 14 | - * @var PLL_Admin_Model | |
| 15 | - */ | |
| 16 | - public $model; | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * Name of the active module. | |
| 20 | - * | |
| 21 | - * @var string | |
| 22 | - */ | |
| 23 | - protected $active_tab; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Array of modules classes. | |
| 27 | - * | |
| 28 | - * @var PLL_Settings_Module[] | |
| 29 | - */ | |
| 30 | - protected $modules; | |
| 31 | - | |
| 32 | - /** | |
| 33 | 23 | * Constructor |
| 34 | 24 | * |
| 35 | 25 | * @since 1.2 |
| 36 | 26 | * |
| 37 | - * @param PLL_Links_Model $links_model Reference to the links model. | |
| 27 | + * @param object $links_model | |
| 38 | 28 | */ |
| 39 | 29 | public function __construct( &$links_model ) { |
| 40 | 30 | parent::__construct( $links_model ); |
| 41 | 31 | |
| 42 | - if ( isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 43 | - $this->active_tab = 'mlang' === $_GET['page'] ? 'lang' : substr( sanitize_key( $_GET['page'] ), 6 ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 32 | + if ( isset( $_GET['page'] ) ) { | |
| 33 | + $this->active_tab = 'mlang' === $_GET['page'] ? 'lang' : substr( $_GET['page'], 6 ); | |
| 44 | 34 | } |
| 45 | 35 | |
| 46 | 36 | PLL_Admin_Strings::init(); |
| 47 | 37 | |
| 38 | + // FIXME put this as late as possible | |
| 48 | 39 | add_action( 'admin_init', array( $this, 'register_settings_modules' ) ); |
| 49 | 40 | |
| 50 | 41 | // Adds screen options and the about box in the languages admin panel |
| 51 | 42 | add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) ); |
| @@ -58,25 +49,28 @@ | ||
| 58 | 49 | /** |
| 59 | 50 | * Initializes the modules |
| 60 | 51 | * |
| 61 | 52 | * @since 1.8 |
| 62 | - * | |
| 63 | - * @return void | |
| 64 | 53 | */ |
| 65 | 54 | public function register_settings_modules() { |
| 66 | - $modules = array(); | |
| 55 | + $modules = array( | |
| 56 | + 'PLL_Settings_Tools', | |
| 57 | + 'PLL_Settings_Licenses', | |
| 58 | + ); | |
| 67 | 59 | |
| 68 | 60 | if ( $this->model->get_languages_list() ) { |
| 69 | - $modules = array( | |
| 61 | + $modules = array_merge( array( | |
| 70 | 62 | 'PLL_Settings_Url', |
| 71 | 63 | 'PLL_Settings_Browser', |
| 72 | 64 | 'PLL_Settings_Media', |
| 73 | 65 | 'PLL_Settings_CPT', |
| 74 | - ); | |
| 66 | + 'PLL_Settings_Sync', | |
| 67 | + 'PLL_Settings_WPML', | |
| 68 | + 'PLL_Settings_Share_Slug', | |
| 69 | + 'PLL_Settings_Translate_Slugs', | |
| 70 | + ), $modules ); | |
| 75 | 71 | } |
| 76 | 72 | |
| 77 | - $modules[] = 'PLL_Settings_Licenses'; | |
| 78 | - | |
| 79 | 73 | /** |
| 80 | 74 | * Filter the list of setting modules |
| 81 | 75 | * |
| 82 | 76 | * @since 1.8 |
| @@ -94,13 +88,11 @@ | ||
| 94 | 88 | /** |
| 95 | 89 | * Loads the about metabox |
| 96 | 90 | * |
| 97 | 91 | * @since 0.8 |
| 98 | - * | |
| 99 | - * @return void | |
| 100 | 92 | */ |
| 101 | 93 | public function metabox_about() { |
| 102 | - include __DIR__ . '/view-about.php'; | |
| 94 | + include PLL_SETTINGS_INC . '/view-about.php'; | |
| 103 | 95 | } |
| 104 | 96 | |
| 105 | 97 | /** |
| 106 | 98 | * Adds screen options and the about box in the languages admin panel |
| @@ -105,10 +97,8 @@ | ||
| 105 | 97 | /** |
| 106 | 98 | * Adds screen options and the about box in the languages admin panel |
| 107 | 99 | * |
| 108 | 100 | * @since 0.9.5 |
| 109 | - * | |
| 110 | - * @return void | |
| 111 | 101 | */ |
| 112 | 102 | public function load_page() { |
| 113 | 103 | if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) { |
| 114 | 104 | add_meta_box( |
| @@ -114,21 +104,18 @@ | ||
| 114 | 104 | add_meta_box( |
| 115 | 105 | 'pll-about-box', |
| 116 | 106 | __( 'About Polylang', 'polylang' ), |
| 117 | 107 | array( $this, 'metabox_about' ), |
| 118 | - 'toplevel_page_mlang', | |
| 108 | + 'settings_page_mlang', // FIXME not shown in screen options | |
| 119 | 109 | 'normal' |
| 120 | 110 | ); |
| 121 | 111 | } |
| 122 | 112 | |
| 123 | - add_screen_option( | |
| 124 | - 'per_page', | |
| 125 | - array( | |
| 126 | - 'label' => __( 'Languages', 'polylang' ), | |
| 127 | - 'default' => 10, | |
| 128 | - 'option' => 'pll_lang_per_page', | |
| 129 | - ) | |
| 130 | - ); | |
| 113 | + add_screen_option( 'per_page', array( | |
| 114 | + 'label' => __( 'Languages', 'polylang' ), | |
| 115 | + 'default' => 10, | |
| 116 | + 'option' => 'pll_lang_per_page', | |
| 117 | + ) ); | |
| 131 | 118 | |
| 132 | 119 | add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) ); |
| 133 | 120 | } |
| 134 | 121 | |
| @@ -135,20 +122,15 @@ | ||
| 135 | 122 | /** |
| 136 | 123 | * Adds screen options in the strings translations admin panel |
| 137 | 124 | * |
| 138 | 125 | * @since 2.1 |
| 139 | - * | |
| 140 | - * @return void | |
| 141 | 126 | */ |
| 142 | 127 | public function load_page_strings() { |
| 143 | - add_screen_option( | |
| 144 | - 'per_page', | |
| 145 | - array( | |
| 146 | - 'label' => __( 'Strings translations', 'polylang' ), | |
| 147 | - 'default' => 10, | |
| 148 | - 'option' => 'pll_strings_per_page', | |
| 149 | - ) | |
| 150 | - ); | |
| 128 | + add_screen_option( 'per_page', array( | |
| 129 | + 'label' => __( 'Strings translations', 'polylang' ), | |
| 130 | + 'default' => 10, | |
| 131 | + 'option' => 'pll_strings_per_page', | |
| 132 | + ) ); | |
| 151 | 133 | } |
| 152 | 134 | |
| 153 | 135 | /** |
| 154 | 136 | * Save the "Views/Uploads per page" option set by this user |
| @@ -170,35 +152,24 @@ | ||
| 170 | 152 | * |
| 171 | 153 | * @since 1.9 |
| 172 | 154 | * |
| 173 | 155 | * @param string $action |
| 174 | - * @return void | |
| 175 | 156 | */ |
| 176 | 157 | public function handle_actions( $action ) { |
| 177 | 158 | switch ( $action ) { |
| 178 | 159 | case 'add': |
| 179 | 160 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 180 | - $errors = $this->model->add_language( $_POST ); | |
| 181 | 161 | |
| 182 | - if ( is_wp_error( $errors ) ) { | |
| 183 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 184 | - add_settings_error( 'general', 'pll_add_language', $message ); | |
| 162 | + if ( $this->model->add_language( $_POST ) && 'en_US' !== $_POST['locale'] ) { | |
| 163 | + // Attempts to install the language pack | |
| 164 | + require_once ABSPATH . 'wp-admin/includes/translation-install.php'; | |
| 165 | + if ( ! wp_download_language_pack( $_POST['locale'] ) ) { | |
| 166 | + add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) ); | |
| 185 | 167 | } |
| 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 | |
| 189 | 168 | |
| 190 | - if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) { | |
| 191 | - // Attempts to install the language pack | |
| 192 | - require_once ABSPATH . 'wp-admin/includes/translation-install.php'; | |
| 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' ) ); | |
| 195 | - } | |
| 196 | - | |
| 197 | - // Force checking for themes and plugins translations updates | |
| 198 | - wp_clean_themes_cache(); | |
| 199 | - wp_clean_plugins_cache(); | |
| 200 | - } | |
| 169 | + // Force checking for themes and plugins translations updates | |
| 170 | + wp_clean_themes_cache(); | |
| 171 | + wp_clean_plugins_cache(); | |
| 201 | 172 | } |
| 202 | 173 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 203 | 174 | break; |
| 204 | 175 | |
| @@ -204,10 +175,10 @@ | ||
| 204 | 175 | |
| 205 | 176 | case 'delete': |
| 206 | 177 | check_admin_referer( 'delete-lang' ); |
| 207 | 178 | |
| 208 | - if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) { | |
| 209 | - add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' ); | |
| 179 | + if ( ! empty( $_GET['lang'] ) ) { | |
| 180 | + $this->model->delete_language( (int) $_GET['lang'] ); | |
| 210 | 181 | } |
| 211 | 182 | |
| 212 | 183 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 213 | 184 | break; |
| @@ -213,18 +184,9 @@ | ||
| 213 | 184 | break; |
| 214 | 185 | |
| 215 | 186 | case 'update': |
| 216 | 187 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 217 | - $errors = $this->model->update_language( $_POST ); | |
| 218 | - | |
| 219 | - if ( is_wp_error( $errors ) ) { | |
| 220 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 221 | - add_settings_error( 'general', 'pll_update_language', $message ); | |
| 222 | - } | |
| 223 | - } else { | |
| 224 | - add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' ); | |
| 225 | - } | |
| 226 | - | |
| 188 | + $error = $this->model->update_language( $_POST ); | |
| 227 | 189 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 228 | 190 | break; |
| 229 | 191 | |
| 230 | 192 | case 'default-lang': |
| @@ -253,25 +215,15 @@ | ||
| 253 | 215 | break; |
| 254 | 216 | |
| 255 | 217 | case 'activate': |
| 256 | 218 | check_admin_referer( 'pll_activate' ); |
| 257 | - if ( isset( $_GET['module'] ) ) { | |
| 258 | - $module = sanitize_key( $_GET['module'] ); | |
| 259 | - if ( isset( $this->modules[ $module ] ) ) { | |
| 260 | - $this->modules[ $module ]->activate(); | |
| 261 | - } | |
| 262 | - } | |
| 219 | + $this->modules[ $_GET['module'] ]->activate(); | |
| 263 | 220 | self::redirect(); |
| 264 | 221 | break; |
| 265 | 222 | |
| 266 | 223 | case 'deactivate': |
| 267 | 224 | check_admin_referer( 'pll_deactivate' ); |
| 268 | - if ( isset( $_GET['module'] ) ) { | |
| 269 | - $module = sanitize_key( $_GET['module'] ); | |
| 270 | - if ( isset( $this->modules[ $module ] ) ) { | |
| 271 | - $this->modules[ $module ]->deactivate(); | |
| 272 | - } | |
| 273 | - } | |
| 225 | + $this->modules[ $_GET['module'] ]->deactivate(); | |
| 274 | 226 | self::redirect(); |
| 275 | 227 | break; |
| 276 | 228 | |
| 277 | 229 | default: |
| @@ -289,10 +241,8 @@ | ||
| 289 | 241 | * Displays the 3 tabs pages: languages, strings translations, settings |
| 290 | 242 | * Also manages user input for these pages |
| 291 | 243 | * |
| 292 | 244 | * @since 0.1 |
| 293 | - * | |
| 294 | - * @return void | |
| 295 | 245 | */ |
| 296 | 246 | public function languages_page() { |
| 297 | 247 | switch ( $this->active_tab ) { |
| 298 | 248 | case 'lang': |
| @@ -307,11 +257,10 @@ | ||
| 307 | 257 | break; |
| 308 | 258 | } |
| 309 | 259 | |
| 310 | 260 | // Handle user input |
| 311 | - $action = isset( $_REQUEST['pll_action'] ) ? sanitize_key( $_REQUEST['pll_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification | |
| 312 | - if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 313 | - // phpcs:ignore WordPress.Security.NonceVerification, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 261 | + $action = isset( $_REQUEST['pll_action'] ) ? $_REQUEST['pll_action'] : ''; | |
| 262 | + if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { | |
| 314 | 263 | $edit_lang = $this->model->get_language( (int) $_GET['lang'] ); |
| 315 | 264 | } else { |
| 316 | 265 | $this->handle_actions( $action ); |
| 317 | 266 | } |
| @@ -316,15 +265,13 @@ | ||
| 316 | 265 | $this->handle_actions( $action ); |
| 317 | 266 | } |
| 318 | 267 | |
| 319 | 268 | // Displays the page |
| 320 | - include __DIR__ . '/view-languages.php'; | |
| 269 | + include PLL_SETTINGS_INC . '/view-languages.php'; | |
| 321 | 270 | } |
| 322 | 271 | |
| 323 | 272 | /** |
| 324 | 273 | * Enqueues scripts and styles |
| 325 | - * | |
| 326 | - * @return void | |
| 327 | 274 | */ |
| 328 | 275 | public function admin_enqueue_scripts() { |
| 329 | 276 | parent::admin_enqueue_scripts(); |
| 330 | 277 | |
| @@ -329,12 +276,12 @@ | ||
| 329 | 276 | parent::admin_enqueue_scripts(); |
| 330 | 277 | |
| 331 | 278 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 332 | 279 | |
| 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' ) ) ); | |
| 280 | + wp_enqueue_script( 'pll_admin', plugins_url( '/js/admin' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION ); | |
| 281 | + wp_localize_script( 'pll_admin', 'pll_flag_base_url', plugins_url( '/flags/', POLYLANG_FILE ) ); | |
| 335 | 282 | |
| 336 | - wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/build/selectmenu' . $suffix . '.css', POLYLANG_BASENAME ), array(), POLYLANG_VERSION ); | |
| 283 | + wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/selectmenu' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION ); | |
| 337 | 284 | } |
| 338 | 285 | |
| 339 | 286 | /** |
| 340 | 287 | * Displays a notice when there are objects with no language assigned |
| @@ -339,10 +286,8 @@ | ||
| 339 | 286 | /** |
| 340 | 287 | * Displays a notice when there are objects with no language assigned |
| 341 | 288 | * |
| 342 | 289 | * @since 1.8 |
| 343 | - * | |
| 344 | - * @return void | |
| 345 | 290 | */ |
| 346 | 291 | public function notice_objects_with_no_lang() { |
| 347 | 292 | if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) { |
| 348 | 293 | printf( |
| @@ -347,9 +292,9 @@ | ||
| 347 | 292 | if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) { |
| 348 | 293 | printf( |
| 349 | 294 | '<div class="error"><p>%s <a href="%s">%s</a></p></div>', |
| 350 | 295 | esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ), |
| 351 | - esc_url( wp_nonce_url( '?page=mlang&pll_action=content-default-lang&noheader=true', 'content-default-lang' ) ), | |
| 296 | + wp_nonce_url( '?page=mlang&pll_action=content-default-lang&noheader=true', 'content-default-lang' ), | |
| 352 | 297 | esc_html__( 'You can set them all to the default language.', 'polylang' ) |
| 353 | 298 | ); |
| 354 | 299 | } |
| 355 | 300 | } |
| @@ -360,11 +305,10 @@ | ||
| 360 | 305 | * |
| 361 | 306 | * @since 1.5 |
| 362 | 307 | * |
| 363 | 308 | * @param array $args query arguments to add to the url |
| 364 | - * @return void | |
| 365 | 309 | */ |
| 366 | - public static function redirect( $args = array() ) { | |
| 310 | + static public function redirect( $args = array() ) { | |
| 367 | 311 | if ( $errors = get_settings_errors() ) { |
| 368 | 312 | set_transient( 'settings_errors', $errors, 30 ); |
| 369 | 313 | $args['settings-updated'] = 1; |
| 370 | 314 | } |
| @@ -376,23 +320,13 @@ | ||
| 376 | 320 | /** |
| 377 | 321 | * Get the list of predefined languages |
| 378 | 322 | * |
| 379 | 323 | * @since 2.3 |
| 380 | - * | |
| 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. | |
| 389 | - * } | |
| 390 | 324 | */ |
| 391 | - public static function get_predefined_languages() { | |
| 392 | - require_once ABSPATH . 'wp-admin/includes/translation-install.php'; | |
| 325 | + public function get_predefined_languages() { | |
| 326 | + require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); | |
| 327 | + include PLL_SETTINGS_INC . '/languages.php'; | |
| 393 | 328 | |
| 394 | - $languages = include __DIR__ . '/languages.php'; | |
| 395 | 329 | $translations = wp_get_available_translations(); |
| 396 | 330 | |
| 397 | 331 | // Keep only languages with existing WP language pack |
| 398 | 332 | // Unless the transient has expired and we don't have an internet connection to refresh it |
| @@ -405,9 +339,9 @@ | ||
| 405 | 339 | * Filter the list of predefined languages |
| 406 | 340 | * |
| 407 | 341 | * @since 1.7.10 |
| 408 | 342 | * @since 2.3 The languages arrays use associative keys instead of numerical keys |
| 409 | - * @see https://github.com/polylang/polylang/blob/2.8.2/settings/languages.php the list of predefined languages | |
| 343 | + * @see settings/languages.php | |
| 410 | 344 | * |
| 411 | 345 | * @param array $languages |
| 412 | 346 | */ |
| 413 | 347 | $languages = apply_filters( 'pll_predefined_languages', $languages ); |