| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A class for the Polylang settings pages |
| 8 | 5 | * accessible in $polylang global object |
| @@ -19,24 +16,11 @@ | ||
| 19 | 16 | * |
| 20 | 17 | * @since 1.2 |
| 21 | 18 | */ |
| 22 | 19 | class PLL_Settings extends PLL_Admin_Base { |
| 20 | + protected $active_tab, $modules; | |
| 23 | 21 | |
| 24 | 22 | /** |
| 25 | - * Name of the active module | |
| 26 | - * | |
| 27 | - * @var string $active_tab | |
| 28 | - */ | |
| 29 | - protected $active_tab; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Array of modules classes | |
| 33 | - * | |
| 34 | - * @var array $modules | |
| 35 | - */ | |
| 36 | - protected $modules; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 23 | * Constructor |
| 40 | 24 | * |
| 41 | 25 | * @since 1.2 |
| 42 | 26 | * |
| @@ -44,14 +28,15 @@ | ||
| 44 | 28 | */ |
| 45 | 29 | public function __construct( &$links_model ) { |
| 46 | 30 | parent::__construct( $links_model ); |
| 47 | 31 | |
| 48 | - if ( isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 49 | - $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 ); | |
| 50 | 34 | } |
| 51 | 35 | |
| 52 | 36 | PLL_Admin_Strings::init(); |
| 53 | 37 | |
| 38 | + // FIXME put this as late as possible | |
| 54 | 39 | add_action( 'admin_init', array( $this, 'register_settings_modules' ) ); |
| 55 | 40 | |
| 56 | 41 | // Adds screen options and the about box in the languages admin panel |
| 57 | 42 | add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) ); |
| @@ -66,21 +51,26 @@ | ||
| 66 | 51 | * |
| 67 | 52 | * @since 1.8 |
| 68 | 53 | */ |
| 69 | 54 | public function register_settings_modules() { |
| 70 | - $modules = array(); | |
| 55 | + $modules = array( | |
| 56 | + 'PLL_Settings_Tools', | |
| 57 | + 'PLL_Settings_Licenses', | |
| 58 | + ); | |
| 71 | 59 | |
| 72 | 60 | if ( $this->model->get_languages_list() ) { |
| 73 | - $modules = array( | |
| 61 | + $modules = array_merge( array( | |
| 74 | 62 | 'PLL_Settings_Url', |
| 75 | 63 | 'PLL_Settings_Browser', |
| 76 | 64 | 'PLL_Settings_Media', |
| 77 | 65 | 'PLL_Settings_CPT', |
| 78 | - ); | |
| 66 | + 'PLL_Settings_Sync', | |
| 67 | + 'PLL_Settings_WPML', | |
| 68 | + 'PLL_Settings_Share_Slug', | |
| 69 | + 'PLL_Settings_Translate_Slugs', | |
| 70 | + ), $modules ); | |
| 79 | 71 | } |
| 80 | 72 | |
| 81 | - $modules[] = 'PLL_Settings_Licenses'; | |
| 82 | - | |
| 83 | 73 | /** |
| 84 | 74 | * Filter the list of setting modules |
| 85 | 75 | * |
| 86 | 76 | * @since 1.8 |
| @@ -100,9 +90,9 @@ | ||
| 100 | 90 | * |
| 101 | 91 | * @since 0.8 |
| 102 | 92 | */ |
| 103 | 93 | public function metabox_about() { |
| 104 | - include __DIR__ . '/view-about.php'; | |
| 94 | + include PLL_SETTINGS_INC . '/view-about.php'; | |
| 105 | 95 | } |
| 106 | 96 | |
| 107 | 97 | /** |
| 108 | 98 | * Adds screen options and the about box in the languages admin panel |
| @@ -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 | |
| @@ -137,16 +124,13 @@ | ||
| 137 | 124 | * |
| 138 | 125 | * @since 2.1 |
| 139 | 126 | */ |
| 140 | 127 | public function load_page_strings() { |
| 141 | - add_screen_option( | |
| 142 | - 'per_page', | |
| 143 | - array( | |
| 144 | - 'label' => __( 'Strings translations', 'polylang' ), | |
| 145 | - 'default' => 10, | |
| 146 | - 'option' => 'pll_strings_per_page', | |
| 147 | - ) | |
| 148 | - ); | |
| 128 | + add_screen_option( 'per_page', array( | |
| 129 | + 'label' => __( 'Strings translations', 'polylang' ), | |
| 130 | + 'default' => 10, | |
| 131 | + 'option' => 'pll_strings_per_page', | |
| 132 | + ) ); | |
| 149 | 133 | } |
| 150 | 134 | |
| 151 | 135 | /** |
| 152 | 136 | * Save the "Views/Uploads per page" option set by this user |
| @@ -173,29 +157,19 @@ | ||
| 173 | 157 | public function handle_actions( $action ) { |
| 174 | 158 | switch ( $action ) { |
| 175 | 159 | case 'add': |
| 176 | 160 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 177 | - $errors = $this->model->add_language( $_POST ); | |
| 178 | 161 | |
| 179 | - if ( is_wp_error( $errors ) ) { | |
| 180 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 181 | - 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' ) ); | |
| 182 | 167 | } |
| 183 | - } 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 | |
| 186 | 168 | |
| 187 | - if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) { | |
| 188 | - // Attempts to install the language pack | |
| 189 | - require_once ABSPATH . 'wp-admin/includes/translation-install.php'; | |
| 190 | - 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' ) ); | |
| 192 | - } | |
| 193 | - | |
| 194 | - // Force checking for themes and plugins translations updates | |
| 195 | - wp_clean_themes_cache(); | |
| 196 | - wp_clean_plugins_cache(); | |
| 197 | - } | |
| 169 | + // Force checking for themes and plugins translations updates | |
| 170 | + wp_clean_themes_cache(); | |
| 171 | + wp_clean_plugins_cache(); | |
| 198 | 172 | } |
| 199 | 173 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 200 | 174 | break; |
| 201 | 175 | |
| @@ -201,10 +175,10 @@ | ||
| 201 | 175 | |
| 202 | 176 | case 'delete': |
| 203 | 177 | check_admin_referer( 'delete-lang' ); |
| 204 | 178 | |
| 205 | - if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) { | |
| 206 | - add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' ); | |
| 179 | + if ( ! empty( $_GET['lang'] ) ) { | |
| 180 | + $this->model->delete_language( (int) $_GET['lang'] ); | |
| 207 | 181 | } |
| 208 | 182 | |
| 209 | 183 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 210 | 184 | break; |
| @@ -210,18 +184,9 @@ | ||
| 210 | 184 | break; |
| 211 | 185 | |
| 212 | 186 | case 'update': |
| 213 | 187 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 214 | - $errors = $this->model->update_language( $_POST ); | |
| 215 | - | |
| 216 | - if ( is_wp_error( $errors ) ) { | |
| 217 | - foreach ( $errors->get_error_messages() as $message ) { | |
| 218 | - add_settings_error( 'general', 'pll_update_language', $message ); | |
| 219 | - } | |
| 220 | - } else { | |
| 221 | - add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' ); | |
| 222 | - } | |
| 223 | - | |
| 188 | + $error = $this->model->update_language( $_POST ); | |
| 224 | 189 | self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true ) |
| 225 | 190 | break; |
| 226 | 191 | |
| 227 | 192 | case 'default-lang': |
| @@ -250,25 +215,15 @@ | ||
| 250 | 215 | break; |
| 251 | 216 | |
| 252 | 217 | case 'activate': |
| 253 | 218 | check_admin_referer( 'pll_activate' ); |
| 254 | - if ( isset( $_GET['module'] ) ) { | |
| 255 | - $module = sanitize_key( $_GET['module'] ); | |
| 256 | - if ( isset( $this->modules[ $module ] ) ) { | |
| 257 | - $this->modules[ $module ]->activate(); | |
| 258 | - } | |
| 259 | - } | |
| 219 | + $this->modules[ $_GET['module'] ]->activate(); | |
| 260 | 220 | self::redirect(); |
| 261 | 221 | break; |
| 262 | 222 | |
| 263 | 223 | case 'deactivate': |
| 264 | 224 | check_admin_referer( 'pll_deactivate' ); |
| 265 | - if ( isset( $_GET['module'] ) ) { | |
| 266 | - $module = sanitize_key( $_GET['module'] ); | |
| 267 | - if ( isset( $this->modules[ $module ] ) ) { | |
| 268 | - $this->modules[ $module ]->deactivate(); | |
| 269 | - } | |
| 270 | - } | |
| 225 | + $this->modules[ $_GET['module'] ]->deactivate(); | |
| 271 | 226 | self::redirect(); |
| 272 | 227 | break; |
| 273 | 228 | |
| 274 | 229 | default: |
| @@ -302,11 +257,10 @@ | ||
| 302 | 257 | break; |
| 303 | 258 | } |
| 304 | 259 | |
| 305 | 260 | // Handle user input |
| 306 | - $action = isset( $_REQUEST['pll_action'] ) ? sanitize_key( $_REQUEST['pll_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification | |
| 307 | - if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 308 | - // 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'] ) ) { | |
| 309 | 263 | $edit_lang = $this->model->get_language( (int) $_GET['lang'] ); |
| 310 | 264 | } else { |
| 311 | 265 | $this->handle_actions( $action ); |
| 312 | 266 | } |
| @@ -311,9 +265,9 @@ | ||
| 311 | 265 | $this->handle_actions( $action ); |
| 312 | 266 | } |
| 313 | 267 | |
| 314 | 268 | // Displays the page |
| 315 | - include __DIR__ . '/view-languages.php'; | |
| 269 | + include PLL_SETTINGS_INC . '/view-languages.php'; | |
| 316 | 270 | } |
| 317 | 271 | |
| 318 | 272 | /** |
| 319 | 273 | * Enqueues scripts and styles |
| @@ -324,9 +278,8 @@ | ||
| 324 | 278 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 325 | 279 | |
| 326 | 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 ); |
| 327 | 281 | 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' ) ); | |
| 329 | 282 | |
| 330 | 283 | wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/selectmenu' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION ); |
| 331 | 284 | } |
| 332 | 285 | |
| @@ -339,9 +292,9 @@ | ||
| 339 | 292 | if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) { |
| 340 | 293 | printf( |
| 341 | 294 | '<div class="error"><p>%s <a href="%s">%s</a></p></div>', |
| 342 | 295 | esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ), |
| 343 | - 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' ), | |
| 344 | 297 | esc_html__( 'You can set them all to the default language.', 'polylang' ) |
| 345 | 298 | ); |
| 346 | 299 | } |
| 347 | 300 | } |
| @@ -353,9 +306,9 @@ | ||
| 353 | 306 | * @since 1.5 |
| 354 | 307 | * |
| 355 | 308 | * @param array $args query arguments to add to the url |
| 356 | 309 | */ |
| 357 | - public static function redirect( $args = array() ) { | |
| 310 | + static public function redirect( $args = array() ) { | |
| 358 | 311 | if ( $errors = get_settings_errors() ) { |
| 359 | 312 | set_transient( 'settings_errors', $errors, 30 ); |
| 360 | 313 | $args['settings-updated'] = 1; |
| 361 | 314 | } |
| @@ -368,12 +321,12 @@ | ||
| 368 | 321 | * Get the list of predefined languages |
| 369 | 322 | * |
| 370 | 323 | * @since 2.3 |
| 371 | 324 | */ |
| 372 | - public static function get_predefined_languages() { | |
| 373 | - 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'; | |
| 374 | 328 | |
| 375 | - $languages = include __DIR__ . '/languages.php'; | |
| 376 | 329 | $translations = wp_get_available_translations(); |
| 377 | 330 | |
| 378 | 331 | // Keep only languages with existing WP language pack |
| 379 | 332 | // Unless the transient has expired and we don't have an internet connection to refresh it |
| @@ -386,9 +339,9 @@ | ||
| 386 | 339 | * Filter the list of predefined languages |
| 387 | 340 | * |
| 388 | 341 | * @since 1.7.10 |
| 389 | 342 | * @since 2.3 The languages arrays use associative keys instead of numerical keys |
| 390 | - * @see https://github.com/polylang/polylang/blob/2.8.2/settings/languages.php the list of predefined languages | |
| 343 | + * @see settings/languages.php | |
| 391 | 344 | * |
| 392 | 345 | * @param array $languages |
| 393 | 346 | */ |
| 394 | 347 | $languages = apply_filters( 'pll_predefined_languages', $languages ); |