PluginProbe
Polylang / 2.3.10
Polylang v2.3.10
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 2.3.10, at settings/settings-module.php

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