PluginProbe
Polylang / 3.6.7
Polylang v3.6.7
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
← All changes | settings/settings-module.php +80 -39 2.83.6.7 View file →
@@ -16,10 +16,8 @@
16 16 */
17 17 public $options;
18 18
19 19 /**
20 - * Instance of PLL_Model.
21 - *
22 20 * @var PLL_Model
23 21 */
24 22 public $model;
25 23
@@ -30,11 +28,17 @@
30 28 */
31 29 public $links_model;
32 30
33 31 /**
34 - * Stores if the module is active.
32 + * Key to use to manage the module activation state.
33 + * Possible values:
34 + * - An option key for a module that can be activated/deactivated.
35 + * - 'none' for a module that doesn't have a activation/deactivation setting.
36 + * - 'preview' for a preview module whose functionalities are available in the Pro version.
35 37 *
36 - * @var bool
38 + * @var string
39 + *
40 + * @phpstan-var non-falsy-string
37 41 */
38 42 public $active_option;
39 43
40 44 /**
@@ -48,8 +52,10 @@
48 52 * Stores the module name.
49 53 * It must be unique.
50 54 *
51 55 * @var string
56 + *
57 + * @phpstan-var non-falsy-string
52 58 */
53 59 public $module;
54 60
55 61 /**
@@ -82,9 +88,9 @@
82 88
83 89 /**
84 90 * Stores html form when provided by a child class.
85 91 *
86 - * @var bool|string
92 + * @var string|false
87 93 */
88 94 protected $form = false;
89 95
90 96 /**
@@ -91,14 +97,31 @@
91 97 * Constructor
92 98 *
93 99 * @since 1.8
94 100 *
95 - * @param object $polylang Polylang object
96 - * @param array $args
101 + * @param object $polylang The Polylang object.
102 + * @param array $args {
103 + * @type string $module Unique module name.
104 + * @type string $title The title of the settings module.
105 + * @type string $description The description of the settings module.
106 + * @type string $active_option Optional. Key to use to manage the module activation state.
107 + * Possible values:
108 + * - An option key for a module that can be activated/deactivated.
109 + * - 'none' for a module that doesn't have a activation/deactivation setting.
110 + * - 'preview' for a preview module whose functionalities are available in the Pro version.
111 + * Default is 'none'.
112 + * }
113 + *
114 + * @phpstan-param array{
115 + * module: non-falsy-string,
116 + * title: string,
117 + * description: string,
118 + * active_option?: non-falsy-string
119 + * } $args
97 120 */
98 121 public function __construct( &$polylang, $args ) {
99 - $this->options = &$polylang->options;
100 - $this->model = &$polylang->model;
122 + $this->options = &$polylang->options;
123 + $this->model = &$polylang->model;
101 124 $this->links_model = &$polylang->links_model;
102 125
103 126 $args = wp_parse_args(
104 127 $args,
@@ -104,12 +127,17 @@
104 127 $args,
105 128 array(
106 129 'title' => '',
107 130 'description' => '',
108 - 'active_option' => false,
131 + 'active_option' => 'none',
109 132 )
110 133 );
111 134
135 + if ( empty( $args['active_option'] ) ) {
136 + // Backward compatibility.
137 + $args['active_option'] = 'none';
138 + }
139 +
112 140 foreach ( $args as $prop => $value ) {
113 141 $this->$prop = $value;
114 142 }
115 143
@@ -141,14 +169,14 @@
141 169 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel', 'polylang' ) ),
142 170 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes', 'polylang' ) ),
143 171 );
144 172
145 - // Ajax action to save options
173 + // Ajax action to save options.
146 174 add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) );
147 175 }
148 176
149 177 /**
150 - * Tells if the module is active
178 + * Tells if the module is active.
151 179 *
152 180 * @since 1.8
153 181 *
154 182 * @return bool
@@ -153,18 +181,20 @@
153 181 *
154 182 * @return bool
155 183 */
156 184 public function is_active() {
157 - return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] );
185 + return 'none' === $this->active_option || ( 'preview' !== $this->active_option && ! empty( $this->options[ $this->active_option ] ) );
158 186 }
159 187
160 188 /**
161 - * Activates the module
189 + * Activates the module.
162 190 *
163 191 * @since 1.8
192 + *
193 + * @return void
164 194 */
165 195 public function activate() {
166 - if ( ! empty( $this->active_option ) ) {
196 + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) {
167 197 $this->options[ $this->active_option ] = true;
168 198 update_option( 'polylang', $this->options );
169 199 }
170 200 }
@@ -169,14 +199,16 @@
169 199 }
170 200 }
171 201
172 202 /**
173 - * Deactivates the module
203 + * Deactivates the module.
174 204 *
175 205 * @since 1.8
206 + *
207 + * @return void
176 208 */
177 209 public function deactivate() {
178 - if ( ! empty( $this->active_option ) ) {
210 + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) {
179 211 $this->options[ $this->active_option ] = false;
180 212 update_option( 'polylang', $this->options );
181 213 }
182 214 }
@@ -181,18 +213,20 @@
181 213 }
182 214 }
183 215
184 216 /**
185 - * Protected method to display a configuration form
217 + * Protected method to display a configuration form.
186 218 *
187 219 * @since 1.8
220 + *
221 + * @return void
188 222 */
189 223 protected function form() {
190 - // Child classes can provide a form
224 + // Child classes can provide a form.
191 225 }
192 226
193 227 /**
194 - * Public method returning the form if any
228 + * Public method returning the form if any.
195 229 *
196 230 * @since 1.8
197 231 *
198 232 * @return string
@@ -197,8 +231,12 @@
197 231 *
198 232 * @return string
199 233 */
200 234 public function get_form() {
235 + if ( ! $this->is_active() ) {
236 + return '';
237 + }
238 +
201 239 // Read the form only once
202 240 if ( false === $this->form ) {
203 241 ob_start();
204 242 $this->form();
@@ -208,23 +246,25 @@
208 246 return $this->form;
209 247 }
210 248
211 249 /**
212 - * Allows child classes to validate their options before saving
250 + * Allows child classes to validate their options before saving.
213 251 *
214 252 * @since 1.8
215 253 *
216 - * @param array $options Raw options
254 + * @param array $options Unsanitized options to save.
217 255 * @return array Options
218 256 */
219 - protected function update( $options ) {
220 - return array(); // It's responsibility of the child class to decide what is saved
257 + protected function update( $options ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
258 + return array(); // It's responsibility of the child class to decide what is saved.
221 259 }
222 260
223 261 /**
224 - * Ajax method to save the options
262 + * Ajax method to save the options.
225 263 *
226 264 * @since 1.8
265 + *
266 + * @return void
227 267 */
228 268 public function save_options() {
229 269 check_ajax_referer( 'pll_options', '_pll_nonce' );
230 270 if ( ! current_user_can( 'manage_options' ) ) {
@@ -244,19 +284,20 @@
244 284 // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified
245 285 // Don't use flush_rewrite_rules as we don't have the right links model and permastruct
246 286 delete_option( 'rewrite_rules' );
247 287
288 +
248 289 ob_start();
249 290
250 - if ( ! get_settings_errors() ) {
291 + if ( empty( get_settings_errors( 'polylang' ) ) ) {
251 292 // Send update message
252 - add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' );
253 - settings_errors();
293 + pll_add_notice( new WP_Error( 'settings_updated', __( 'Settings saved.', 'polylang' ), 'success' ) );
294 + settings_errors( 'polylang' );
254 295 $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) );
255 296 $x->send();
256 297 } else {
257 298 // Send error messages
258 - settings_errors();
299 + settings_errors( 'polylang' );
259 300 $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) );
260 301 $x->send();
261 302 }
262 303 }
@@ -262,13 +303,13 @@
262 303 }
263 304 }
264 305
265 306 /**
266 - * Get the row actions
307 + * Get the row actions.
267 308 *
268 309 * @since 1.8
269 310 *
270 - * @return array
311 + * @return string[]
271 312 */
272 313 protected function get_actions() {
273 314 $actions = array();
274 315
@@ -275,9 +316,9 @@
275 316 if ( $this->is_active() && $this->get_form() ) {
276 317 $actions[] = 'configure';
277 318 }
278 319
279 - if ( $this->active_option ) {
320 + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) {
280 321 $actions[] = $this->is_active() ? 'deactivate' : 'activate';
281 322 }
282 323
283 324 if ( empty( $actions ) ) {
@@ -287,13 +328,13 @@
287 328 return $actions;
288 329 }
289 330
290 331 /**
291 - * Get the actions links
332 + * Get the actions links.
292 333 *
293 334 * @since 1.8
294 335 *
295 - * @return array
336 + * @return string[] Action links.
296 337 */
297 338 public function get_action_links() {
298 339 return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) );
299 340 }
@@ -298,9 +339,9 @@
298 339 return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) );
299 340 }
300 341
301 342 /**
302 - * Default upgrade message ( to Pro version )
343 + * Default upgrade message (to Pro version).
303 344 *
304 345 * @since 1.9
305 346 *
306 347 * @return string
@@ -314,9 +355,9 @@
314 355 );
315 356 }
316 357
317 358 /**
318 - * Allows child classes to display an upgrade message
359 + * Allows child classes to display an upgrade message.
319 360 *
320 361 * @since 1.9
321 362 *
322 363 * @return string
@@ -321,17 +362,17 @@
321 362 *
322 363 * @return string
323 364 */
324 365 public function get_upgrade_message() {
325 - return '';
366 + return 'preview' === $this->active_option ? $this->default_upgrade_message() : '';
326 367 }
327 368
328 369 /**
329 - * Get the buttons
370 + * Get the buttons.
330 371 *
331 372 * @since 1.9
332 373 *
333 - * @return array
374 + * @return string[] An array of html fragment for the buttons.
334 375 */
335 376 public function get_buttons() {
336 377 return $this->buttons;
337 378 }