PluginProbe
Polylang / 3.3
Polylang v3.3
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-module.php

settings-module.php in Polylang 3.3, at settings/settings-module.php

346 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Base class for all settings
8 *
9 * @since 1.8
10 */
11 class PLL_Settings_Module {
12 /**
13 * Stores the plugin options.
14 *
15 * @var array
16 */
17 public $options;
18
19 /**
20 * @var PLL_Model
21 */
22 public $model;
23
24 /**
25 * Instance of a child class of PLL_Links_Model.
26 *
27 * @var PLL_Links_Model
28 */
29 public $links_model;
30
31 /**
32 * Stores if the module is active.
33 *
34 * @var bool
35 */
36 public $active_option;
37
38 /**
39 * Stores the display order priority.
40 *
41 * @var int
42 */
43 public $priority = 100;
44
45 /**
46 * Stores the module name.
47 * It must be unique.
48 *
49 * @var string|null
50 */
51 public $module;
52
53 /**
54 * Stores the module title.
55 *
56 * @var string
57 */
58 public $title;
59
60 /**
61 * Stores the module description.
62 *
63 * @var string
64 */
65 public $description;
66
67 /**
68 * Stores the settings actions.
69 *
70 * @var array
71 */
72 protected $action_links;
73
74 /**
75 * Stores html fragment for the buttons.
76 *
77 * @var array
78 */
79 protected $buttons;
80
81 /**
82 * Stores html form when provided by a child class.
83 *
84 * @var string|false
85 */
86 protected $form = false;
87
88 /**
89 * Constructor
90 *
91 * @since 1.8
92 *
93 * @param object $polylang Polylang object
94 * @param array $args
95 */
96 public function __construct( &$polylang, $args ) {
97 $this->options = &$polylang->options;
98 $this->model = &$polylang->model;
99 $this->links_model = &$polylang->links_model;
100
101 $args = wp_parse_args(
102 $args,
103 array(
104 'title' => '',
105 'description' => '',
106 'active_option' => false,
107 )
108 );
109
110 foreach ( $args as $prop => $value ) {
111 $this->$prop = $value;
112 }
113
114 // All possible action links, even if not always a link ;-)
115 $this->action_links = array(
116 'configure' => sprintf(
117 '<a title="%s" href="%s">%s</a>',
118 esc_attr__( 'Configure this module', 'polylang' ),
119 '#',
120 esc_html__( 'Settings', 'polylang' )
121 ),
122 'deactivate' => sprintf(
123 '<a title="%s" href="%s">%s</a>',
124 esc_attr__( 'Deactivate this module', 'polylang' ),
125 esc_url( wp_nonce_url( '?page=mlang&tab=modules&pll_action=deactivate&noheader=true&module=' . $this->module, 'pll_deactivate' ) ),
126 esc_html__( 'Deactivate', 'polylang' )
127 ),
128 'activate' => sprintf(
129 '<a title="%s" href="%s">%s</a>',
130 esc_attr__( 'Activate this module', 'polylang' ),
131 esc_url( wp_nonce_url( '?page=mlang&tab=modules&pll_action=activate&noheader=true&module=' . $this->module, 'pll_activate' ) ),
132 esc_html__( 'Activate', 'polylang' )
133 ),
134 'activated' => esc_html__( 'Activated', 'polylang' ),
135 'deactivated' => esc_html__( 'Deactivated', 'polylang' ),
136 );
137
138 $this->buttons = array(
139 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel', 'polylang' ) ),
140 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes', 'polylang' ) ),
141 );
142
143 // Ajax action to save options
144 add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) );
145 }
146
147 /**
148 * Tells if the module is active
149 *
150 * @since 1.8
151 *
152 * @return bool
153 */
154 public function is_active() {
155 return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] );
156 }
157
158 /**
159 * Activates the module
160 *
161 * @since 1.8
162 *
163 * @return void
164 */
165 public function activate() {
166 if ( ! empty( $this->active_option ) ) {
167 $this->options[ $this->active_option ] = true;
168 update_option( 'polylang', $this->options );
169 }
170 }
171
172 /**
173 * Deactivates the module
174 *
175 * @since 1.8
176 *
177 * @return void
178 */
179 public function deactivate() {
180 if ( ! empty( $this->active_option ) ) {
181 $this->options[ $this->active_option ] = false;
182 update_option( 'polylang', $this->options );
183 }
184 }
185
186 /**
187 * Protected method to display a configuration form
188 *
189 * @since 1.8
190 *
191 * @return void
192 */
193 protected function form() {
194 // Child classes can provide a form
195 }
196
197 /**
198 * Public method returning the form if any
199 *
200 * @since 1.8
201 *
202 * @return string
203 */
204 public function get_form() {
205 // Read the form only once
206 if ( false === $this->form ) {
207 ob_start();
208 $this->form();
209 $this->form = ob_get_clean();
210 }
211
212 return $this->form;
213 }
214
215 /**
216 * Allows child classes to validate their options before saving
217 *
218 * @since 1.8
219 *
220 * @param array $options Raw options
221 * @return array Options
222 */
223 protected function update( $options ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
224 return array(); // It's responsibility of the child class to decide what is saved
225 }
226
227 /**
228 * Ajax method to save the options
229 *
230 * @since 1.8
231 *
232 * @return void
233 */
234 public function save_options() {
235 check_ajax_referer( 'pll_options', '_pll_nonce' );
236 if ( ! current_user_can( 'manage_options' ) ) {
237 wp_die( -1 );
238 }
239
240 if ( isset( $_POST['module'] ) && $this->module === $_POST['module'] ) {
241 // It's up to the child class to decide which options are saved, whether there are errors or not
242 $post = array_diff_key( $_POST, array_flip( array( 'action', 'module', 'pll_ajax_backend', '_pll_nonce' ) ) );
243 $options = $this->update( $post );
244 $this->options = array_merge( $this->options, $options );
245 update_option( 'polylang', $this->options );
246
247 // Refresh language cache in case home urls have been modified
248 $this->model->clean_languages_cache();
249
250 // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified
251 // Don't use flush_rewrite_rules as we don't have the right links model and permastruct
252 delete_option( 'rewrite_rules' );
253
254
255 ob_start();
256
257 if ( empty( get_settings_errors() ) ) {
258 // Send update message
259 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' );
260 settings_errors();
261 $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) );
262 $x->send();
263 } else {
264 // Send error messages
265 settings_errors();
266 $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) );
267 $x->send();
268 }
269 }
270 }
271
272 /**
273 * Get the row actions.
274 *
275 * @since 1.8
276 *
277 * @return string[]
278 */
279 protected function get_actions() {
280 $actions = array();
281
282 if ( $this->is_active() && $this->get_form() ) {
283 $actions[] = 'configure';
284 }
285
286 if ( $this->active_option ) {
287 $actions[] = $this->is_active() ? 'deactivate' : 'activate';
288 }
289
290 if ( empty( $actions ) ) {
291 $actions[] = $this->is_active() ? 'activated' : 'deactivated';
292 }
293
294 return $actions;
295 }
296
297 /**
298 * Get the actions links.
299 *
300 * @since 1.8
301 *
302 * @return string[] Action links.
303 */
304 public function get_action_links() {
305 return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) );
306 }
307
308 /**
309 * Default upgrade message ( to Pro version )
310 *
311 * @since 1.9
312 *
313 * @return string
314 */
315 protected function default_upgrade_message() {
316 return sprintf(
317 '%s <a href="%s">%s</a>',
318 __( 'To enable this feature, you need Polylang Pro.', 'polylang' ),
319 'https://polylang.pro',
320 __( 'Upgrade now.', 'polylang' )
321 );
322 }
323
324 /**
325 * Allows child classes to display an upgrade message
326 *
327 * @since 1.9
328 *
329 * @return string
330 */
331 public function get_upgrade_message() {
332 return '';
333 }
334
335 /**
336 * Get the buttons.
337 *
338 * @since 1.9
339 *
340 * @return string[] An array of html fragment for the buttons.
341 */
342 public function get_buttons() {
343 return $this->buttons;
344 }
345 }
346