PluginProbe
Polylang / 2.3.1
Polylang v2.3.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / settings / settings.php

settings.php in Polylang 2.3.1, at settings/settings.php

355 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class for the Polylang settings pages
5 * accessible in $polylang global object
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 *
17 * @since 1.2
18 */
19 class PLL_Settings extends PLL_Admin_Base {
20 protected $active_tab, $modules;
21
22 /**
23 * Constructor
24 *
25 * @since 1.2
26 *
27 * @param object $links_model
28 */
29 public function __construct( &$links_model ) {
30 parent::__construct( $links_model );
31
32 if ( isset( $_GET['page'] ) ) {
33 $this->active_tab = 'mlang' === $_GET['page'] ? 'lang' : substr( $_GET['page'], 6 );
34 }
35
36 PLL_Admin_Strings::init();
37
38 // FIXME put this as late as possible
39 add_action( 'admin_init', array( $this, 'register_settings_modules' ) );
40
41 // Adds screen options and the about box in the languages admin panel
42 add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) );
43 add_action( 'load-languages_page_mlang_strings', array( $this, 'load_page_strings' ) );
44
45 // Saves per-page value in screen option
46 add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
47 }
48
49 /**
50 * Initializes the modules
51 *
52 * @since 1.8
53 */
54 public function register_settings_modules() {
55 $modules = array(
56 'PLL_Settings_Tools',
57 'PLL_Settings_Licenses',
58 );
59
60 if ( $this->model->get_languages_list() ) {
61 $modules = array_merge( array(
62 'PLL_Settings_Url',
63 'PLL_Settings_Browser',
64 'PLL_Settings_Media',
65 'PLL_Settings_CPT',
66 'PLL_Settings_Sync',
67 'PLL_Settings_WPML',
68 'PLL_Settings_Share_Slug',
69 'PLL_Settings_Translate_Slugs',
70 ), $modules );
71 }
72
73 /**
74 * Filter the list of setting modules
75 *
76 * @since 1.8
77 *
78 * @param array $modules the list of module classes
79 */
80 $modules = apply_filters( 'pll_settings_modules', $modules );
81
82 foreach ( $modules as $key => $class ) {
83 $key = is_numeric( $key ) ? strtolower( str_replace( 'PLL_Settings_', '', $class ) ) : $key;
84 $this->modules[ $key ] = new $class( $this );
85 }
86 }
87
88 /**
89 * Loads the about metabox
90 *
91 * @since 0.8
92 */
93 public function metabox_about() {
94 include PLL_SETTINGS_INC . '/view-about.php';
95 }
96
97 /**
98 * Adds screen options and the about box in the languages admin panel
99 *
100 * @since 0.9.5
101 */
102 public function load_page() {
103 if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) {
104 add_meta_box(
105 'pll-about-box',
106 __( 'About Polylang', 'polylang' ),
107 array( $this, 'metabox_about' ),
108 'settings_page_mlang', // FIXME not shown in screen options
109 'normal'
110 );
111 }
112
113 add_screen_option( 'per_page', array(
114 'label' => __( 'Languages', 'polylang' ),
115 'default' => 10,
116 'option' => 'pll_lang_per_page',
117 ) );
118
119 add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) );
120 }
121
122 /**
123 * Adds screen options in the strings translations admin panel
124 *
125 * @since 2.1
126 */
127 public function load_page_strings() {
128 add_screen_option( 'per_page', array(
129 'label' => __( 'Strings translations', 'polylang' ),
130 'default' => 10,
131 'option' => 'pll_strings_per_page',
132 ) );
133 }
134
135 /**
136 * Save the "Views/Uploads per page" option set by this user
137 *
138 * @since 0.9.5
139 *
140 * @param mixed $status false or value returned by previous filter
141 * @param string $option Name of the option being changed
142 * @param string $value Value of the option
143 *
144 * @return string New value if this is our option, otherwise nothing
145 */
146 public function set_screen_option( $status, $option, $value ) {
147 return 'pll_lang_per_page' === $option || 'pll_strings_per_page' === $option ? $value : $status;
148 }
149
150 /**
151 * Manages the user input for the languages pages
152 *
153 * @since 1.9
154 *
155 * @param string $action
156 */
157 public function handle_actions( $action ) {
158 switch ( $action ) {
159 case 'add':
160 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
161
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' ) );
167 }
168
169 // Force checking for themes and plugins translations updates
170 wp_clean_themes_cache();
171 wp_clean_plugins_cache();
172 }
173 self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
174 break;
175
176 case 'delete':
177 check_admin_referer( 'delete-lang' );
178
179 if ( ! empty( $_GET['lang'] ) ) {
180 $this->model->delete_language( (int) $_GET['lang'] );
181 }
182
183 self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
184 break;
185
186 case 'update':
187 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
188 $error = $this->model->update_language( $_POST );
189 self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
190 break;
191
192 case 'default-lang':
193 check_admin_referer( 'default-lang' );
194
195 if ( $lang = $this->model->get_language( (int) $_GET['lang'] ) ) {
196 $this->model->update_default_lang( $lang->slug );
197 }
198
199 self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
200 break;
201
202 case 'content-default-lang':
203 check_admin_referer( 'content-default-lang' );
204
205 if ( $nolang = $this->model->get_objects_with_no_lang() ) {
206 if ( ! empty( $nolang['posts'] ) ) {
207 $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] );
208 }
209 if ( ! empty( $nolang['terms'] ) ) {
210 $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] );
211 }
212 }
213
214 self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
215 break;
216
217 case 'activate':
218 check_admin_referer( 'pll_activate' );
219 $this->modules[ $_GET['module'] ]->activate();
220 self::redirect();
221 break;
222
223 case 'deactivate':
224 check_admin_referer( 'pll_deactivate' );
225 $this->modules[ $_GET['module'] ]->deactivate();
226 self::redirect();
227 break;
228
229 default:
230 /**
231 * Fires when a non default action has been sent to Polylang settings
232 *
233 * @since 1.8
234 */
235 do_action( "mlang_action_$action" );
236 break;
237 }
238 }
239
240 /**
241 * Displays the 3 tabs pages: languages, strings translations, settings
242 * Also manages user input for these pages
243 *
244 * @since 0.1
245 */
246 public function languages_page() {
247 switch ( $this->active_tab ) {
248 case 'lang':
249 // Prepare the list table of languages
250 $list_table = new PLL_Table_Languages();
251 $list_table->prepare_items( $this->model->get_languages_list() );
252 break;
253
254 case 'strings':
255 $string_table = new PLL_Table_String( $this->model->get_languages_list() );
256 $string_table->prepare_items();
257 break;
258 }
259
260 // Handle user input
261 $action = isset( $_REQUEST['pll_action'] ) ? $_REQUEST['pll_action'] : '';
262 if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) {
263 $edit_lang = $this->model->get_language( (int) $_GET['lang'] );
264 } else {
265 $this->handle_actions( $action );
266 }
267
268 // Displays the page
269 include PLL_SETTINGS_INC . '/view-languages.php';
270 }
271
272 /**
273 * Enqueues scripts and styles
274 */
275 public function admin_enqueue_scripts() {
276 parent::admin_enqueue_scripts();
277
278 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
279
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 ) );
282
283 wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/selectmenu' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION );
284 }
285
286 /**
287 * Displays a notice when there are objects with no language assigned
288 *
289 * @since 1.8
290 */
291 public function notice_objects_with_no_lang() {
292 if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) {
293 printf(
294 '<div class="error"><p>%s <a href="%s">%s</a></p></div>',
295 esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ),
296 wp_nonce_url( '?page=mlang&amp;pll_action=content-default-lang&amp;noheader=true', 'content-default-lang' ),
297 esc_html__( 'You can set them all to the default language.', 'polylang' )
298 );
299 }
300 }
301
302 /**
303 * Redirects to language page ( current active tab )
304 * saves error messages in a transient for reuse in redirected page
305 *
306 * @since 1.5
307 *
308 * @param array $args query arguments to add to the url
309 */
310 static public function redirect( $args = array() ) {
311 if ( $errors = get_settings_errors() ) {
312 set_transient( 'settings_errors', $errors, 30 );
313 $args['settings-updated'] = 1;
314 }
315 // Remove possible 'pll_action' and 'lang' query args from the referer before redirecting
316 wp_safe_redirect( add_query_arg( $args, remove_query_arg( array( 'pll_action', 'lang' ), wp_get_referer() ) ) );
317 exit;
318 }
319
320 /**
321 * Get the list of predefined languages
322 *
323 * @since 2.3
324 */
325 public function get_predefined_languages() {
326 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
327 include PLL_SETTINGS_INC . '/languages.php';
328
329 // Keep only languages with existing WP language pack
330 $translations = wp_get_available_translations();
331 $translations['en_US'] = ''; // Languages packs don't include en_US
332 $languages = array_intersect_key( $languages, $translations );
333
334 /**
335 * Filter the list of predefined languages
336 *
337 * @since 1.7.10
338 * @since 2.3 The languages arrays use associative keys instead of numerical keys
339 * @see settings/languages.php
340 *
341 * @param array $languages
342 */
343 $languages = apply_filters( 'pll_predefined_languages', $languages );
344
345 // Keep only languages with all necessary informations
346 foreach ( $languages as $k => $lang ) {
347 if ( ! isset( $lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'] ) ) {
348 unset( $languages[ $k ] );
349 }
350 }
351
352 return $languages;
353 }
354 }
355