PluginProbe
Polylang / 2.0.6
Polylang v2.0.6
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.0.6, at settings/settings.php

345 lines 10.1 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 $this->active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'lang';
33
34 PLL_Admin_Strings::init();
35
36 // FIXME put this as late as possible
37 add_action( 'admin_init', array( $this, 'register_settings_modules' ) );
38
39 // adds screen options and the about box in the languages admin panel
40 add_action( 'load-settings_page_mlang', array( $this, 'load_page' ) );
41
42 // saves per-page value in screen option
43 add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
44 }
45
46 /**
47 * Initializes the modules
48 *
49 * @since 1.8
50 */
51 public function register_settings_modules() {
52 $modules = array(
53 'PLL_Settings_Tools',
54 'PLL_Settings_Licenses',
55 );
56
57 if ( $this->model->get_languages_list() ) {
58 $modules = array_merge( array(
59 'PLL_Settings_Url',
60 'PLL_Settings_Browser',
61 'PLL_Settings_Media',
62 'PLL_Settings_CPT',
63 'PLL_Settings_Sync',
64 'PLL_Settings_WPML',
65 'PLL_Settings_Share_Slug',
66 'PLL_Settings_Translate_Slugs',
67 ), $modules );
68 }
69
70 /**
71 * Filter the list of setting modules
72 *
73 * @since 1.8
74 *
75 * @param array $modules the list of module classes
76 */
77 $modules = apply_filters( 'pll_settings_modules', $modules );
78
79 foreach ( $modules as $key => $class ) {
80 $key = is_numeric( $key ) ? strtolower( str_replace( 'PLL_Settings_', '', $class ) ) : $key;
81 $this->modules[ $key ] = new $class( $this );
82 }
83 }
84
85 /**
86 * Adds the link to the languages panel in the WordPress admin menu
87 *
88 * @since 0.1
89 */
90 public function add_menus() {
91 add_submenu_page( 'options-general.php', $title = __( 'Languages', 'polylang' ), $title, 'manage_options', 'mlang', array( $this, 'languages_page' ) );
92 }
93
94 /**
95 * Loads the about metabox
96 *
97 * @since 0.8
98 */
99 public function metabox_about() {
100 include( PLL_SETTINGS_INC.'/view-about.php' );
101 }
102
103 /**
104 * Adds screen options and the about box in the languages admin panel
105 *
106 * @since 0.9.5
107 */
108 public function load_page() {
109 // test of $this->active_tab avoids displaying the automatically generated screen options on other tabs
110 switch ( $this->active_tab ) {
111 case 'lang':
112 if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) {
113 add_meta_box(
114 'pll-about-box',
115 __( 'About Polylang', 'polylang' ),
116 array( $this, 'metabox_about' ),
117 'settings_page_mlang',
118 'normal'
119 );
120 }
121
122 add_screen_option( 'per_page', array(
123 'label' => __( 'Languages', 'polylang' ),
124 'default' => 10,
125 'option' => 'pll_lang_per_page',
126 ) );
127
128 add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) );
129 break;
130
131 case 'strings':
132 add_screen_option( 'per_page', array(
133 'label' => __( 'Strings translations', 'polylang' ),
134 'default' => 10,
135 'option' => 'pll_strings_per_page',
136 ) );
137 break;
138 }
139 }
140
141 /**
142 * Save the "Views/Uploads per page" option set by this user
143 *
144 * @since 0.9.5
145 *
146 * @param mixed $status false or value returned by previous filter
147 * @param string $option Name of the option being changed
148 * @param string $value Value of the option
149 *
150 * @return string New value if this is our option, otherwise nothing
151 */
152 public function set_screen_option( $status, $option, $value ) {
153 return 'pll_lang_per_page' === $option || 'pll_strings_per_page' === $option ? $value : $status;
154 }
155
156 /**
157 * Manages the user input for the languages pages
158 *
159 * @since 1.9
160 *
161 * @param string $action
162 */
163 public function handle_actions( $action ) {
164 switch ( $action ) {
165 case 'add':
166 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
167
168 if ( $this->model->add_language( $_POST ) && 'en_US' !== $_POST['locale'] ) {
169 // attempts to install the language pack
170 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
171 if ( ! wp_download_language_pack( $_POST['locale'] ) ) {
172 add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) );
173 }
174
175 // force checking for themes and plugins translations updates
176 wp_clean_themes_cache();
177 wp_clean_plugins_cache();
178 }
179 self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
180 break;
181
182 case 'delete':
183 check_admin_referer( 'delete-lang' );
184
185 if ( ! empty( $_GET['lang'] ) ) {
186 $this->model->delete_language( (int) $_GET['lang'] );
187 }
188
189 self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
190 break;
191
192 case 'update':
193 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
194 $error = $this->model->update_language( $_POST );
195 self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
196 break;
197
198 case 'default-lang':
199 check_admin_referer( 'default-lang' );
200
201 if ( $lang = $this->model->get_language( (int) $_GET['lang'] ) ) {
202 $this->model->update_default_lang( $lang->slug );
203 }
204
205 self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
206 break;
207
208 case 'content-default-lang':
209 check_admin_referer( 'content-default-lang' );
210
211 if ( $nolang = $this->model->get_objects_with_no_lang() ) {
212 if ( ! empty( $nolang['posts'] ) ) {
213 $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] );
214 }
215 if ( ! empty( $nolang['terms'] ) ) {
216 $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] );
217 }
218 }
219
220 self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
221 break;
222
223 case 'activate':
224 check_admin_referer( 'pll_activate' );
225 $this->modules[ $_GET['module'] ]->activate();
226 self::redirect();
227 break;
228
229 case 'deactivate':
230 check_admin_referer( 'pll_deactivate' );
231 $this->modules[ $_GET['module'] ]->deactivate();
232 self::redirect();
233 break;
234
235 default:
236 /**
237 * Fires when a non default action has been sent to Polylang settings
238 *
239 * @since 1.8
240 */
241 do_action( "mlang_action_$action" );
242 break;
243 }
244 }
245
246 /**
247 * Displays the 3 tabs pages: languages, strings translations, settings
248 * also manages user input for these pages
249 *
250 * @since 0.1
251 */
252 public function languages_page() {
253 // prepare the list of tabs
254 $tabs = array( 'lang' => __( 'Languages','polylang' ) );
255
256 // only if at least one language has been created
257 if ( $listlanguages = $this->model->get_languages_list() ) {
258 $tabs['strings'] = __( 'Strings translations', 'polylang' );
259 }
260
261 $tabs['settings'] = __( 'Settings', 'polylang' );
262
263 /**
264 * Filter the list of tabs in Polylang settings
265 *
266 * @since 1.5.1
267 *
268 * @param array $tabs list of tab names
269 */
270 $tabs = apply_filters( 'pll_settings_tabs', $tabs );
271
272 switch ( $this->active_tab ) {
273 case 'lang':
274 // prepare the list table of languages
275 $list_table = new PLL_Table_Languages();
276 $list_table->prepare_items( $listlanguages );
277 break;
278
279 case 'strings':
280 $string_table = new PLL_Table_String( $listlanguages );
281 $string_table->prepare_items();
282 break;
283 }
284
285 // handle user input
286 $action = isset( $_REQUEST['pll_action'] ) ? $_REQUEST['pll_action'] : '';
287 if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) {
288 $edit_lang = $this->model->get_language( (int) $_GET['lang'] );
289 } else {
290 $this->handle_actions( $action );
291 }
292
293 // displays the page
294 include( PLL_SETTINGS_INC.'/view-languages.php' );
295 }
296
297 /**
298 * Enqueues scripts and styles
299 */
300 public function admin_enqueue_scripts() {
301 parent::admin_enqueue_scripts();
302
303 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
304
305 wp_enqueue_script( 'pll_admin', POLYLANG_URL .'/js/admin'.$suffix.'.js', array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION );
306 wp_localize_script( 'pll_admin', 'pll_flag_base_url', POLYLANG_URL . '/flags/' );
307
308 wp_enqueue_style( 'pll_selectmenu', POLYLANG_URL .'/css/selectmenu'.$suffix.'.css', array(), POLYLANG_VERSION );
309 }
310
311 /**
312 * Displays a notice when there are objects with no language assigned
313 *
314 * @since 1.8
315 */
316 public function notice_objects_with_no_lang() {
317 if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang() ) {
318 printf(
319 '<div class="error"><p>%s <a href="%s">%s</a></p></div>',
320 esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ),
321 wp_nonce_url( '?page=mlang&amp;pll_action=content-default-lang&amp;noheader=true', 'content-default-lang' ),
322 esc_html__( 'You can set them all to the default language.', 'polylang' )
323 );
324 }
325 }
326
327 /**
328 * Redirects to language page ( current active tab )
329 * saves error messages in a transient for reuse in redirected page
330 *
331 * @since 1.5
332 *
333 * @param array $args query arguments to add to the url
334 */
335 static public function redirect( $args = array() ) {
336 if ( $errors = get_settings_errors() ) {
337 set_transient( 'settings_errors', $errors, 30 );
338 $args['settings-updated'] = 1;
339 }
340 // remove possible 'pll_action' and 'lang' query args from the referer before redirecting
341 wp_safe_redirect( add_query_arg( $args, remove_query_arg( array( 'pll_action', 'lang' ), wp_get_referer() ) ) );
342 exit;
343 }
344 }
345