PluginProbe
Polylang / 3.1.1
Polylang v3.1.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-module.php

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

345 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
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 bool|string
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 ob_start();
255
256 if ( ! get_settings_errors() ) {
257 // Send update message
258 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' );
259 settings_errors();
260 $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) );
261 $x->send();
262 } else {
263 // Send error messages
264 settings_errors();
265 $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) );
266 $x->send();
267 }
268 }
269 }
270
271 /**
272 * Get the row actions.
273 *
274 * @since 1.8
275 *
276 * @return string[]
277 */
278 protected function get_actions() {
279 $actions = array();
280
281 if ( $this->is_active() && $this->get_form() ) {
282 $actions[] = 'configure';
283 }
284
285 if ( $this->active_option ) {
286 $actions[] = $this->is_active() ? 'deactivate' : 'activate';
287 }
288
289 if ( empty( $actions ) ) {
290 $actions[] = $this->is_active() ? 'activated' : 'deactivated';
291 }
292
293 return $actions;
294 }
295
296 /**
297 * Get the actions links.
298 *
299 * @since 1.8
300 *
301 * @return string[] Action links.
302 */
303 public function get_action_links() {
304 return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) );
305 }
306
307 /**
308 * Default upgrade message ( to Pro version )
309 *
310 * @since 1.9
311 *
312 * @return string
313 */
314 protected function default_upgrade_message() {
315 return sprintf(
316 '%s <a href="%s">%s</a>',
317 __( 'To enable this feature, you need Polylang Pro.', 'polylang' ),
318 'https://polylang.pro',
319 __( 'Upgrade now.', 'polylang' )
320 );
321 }
322
323 /**
324 * Allows child classes to display an upgrade message
325 *
326 * @since 1.9
327 *
328 * @return string
329 */
330 public function get_upgrade_message() {
331 return '';
332 }
333
334 /**
335 * Get the buttons.
336 *
337 * @since 1.9
338 *
339 * @return string[] An array of html fragment for the buttons.
340 */
341 public function get_buttons() {
342 return $this->buttons;
343 }
344 }
345