PluginProbe
Polylang / 2.2.6
Polylang v2.2.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-module.php

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

264 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;
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 static $form = false;
127
128 // Read the form only once
129 if ( false === $form ) {
130 ob_start();
131 $this->form();
132 $form = ob_get_clean();
133 }
134
135 return $form;
136 }
137
138 /**
139 * Allows child classes to validate their options before saving
140 *
141 * @since 1.8
142 *
143 * @param array $options Raw options
144 * @return array Options
145 */
146 protected function update( $options ) {
147 return array(); // It's responsibility of the child class to decide what is saved
148 }
149
150 /**
151 * Ajax method to save the options
152 *
153 * @since 1.8
154 */
155 public function save_options() {
156 check_ajax_referer( 'pll_options', '_pll_nonce' );
157 if ( ! current_user_can( 'manage_options' ) ) {
158 wp_die( -1 );
159 }
160
161 if ( $this->module == $_POST['module'] ) {
162 // It's up to the child class to decide which options are saved, whether there are errors or not
163 $post = array_diff_key( $_POST, array_flip( array( 'action', 'module', 'pll_ajax_backend', '_pll_nonce' ) ) );
164 $options = $this->update( $post );
165 $this->options = array_merge( $this->options, $options );
166 update_option( 'polylang', $this->options );
167
168 // Refresh language cache in case home urls have been modified
169 $this->model->clean_languages_cache();
170
171 // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified
172 // Don't use flush_rewrite_rules as we don't have the right links model and permastruct
173 delete_option( 'rewrite_rules' );
174
175 ob_start();
176
177 if ( ! get_settings_errors() ) {
178 // Send update message
179 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' );
180 settings_errors();
181 $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) );
182 $x->send();
183 } else {
184 // Send error messages
185 settings_errors();
186 $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) );
187 $x->send();
188 }
189 }
190 }
191
192 /**
193 * Get the row actions
194 *
195 * @since 1.8
196 *
197 * @return array
198 */
199 protected function get_actions() {
200 if ( $this->is_active() && $this->get_form() ) {
201 $actions[] = 'configure';
202 }
203
204 if ( $this->active_option ) {
205 $actions[] = $this->is_active() ? 'deactivate' : 'activate';
206 }
207
208 if ( empty( $actions ) ) {
209 $actions[] = $this->is_active() ? 'activated' : 'deactivated';
210 }
211
212 return $actions;
213 }
214
215 /**
216 * Get the actions links
217 *
218 * @since 1.8
219 *
220 * @return array
221 */
222 public function get_action_links() {
223 return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) );
224 }
225
226 /**
227 * Default upgrade message ( to Pro version )
228 *
229 * @since 1.9
230 *
231 * @return string
232 */
233 protected function default_upgrade_message() {
234 return sprintf(
235 '%s <a href="%s">%s</a>',
236 __( 'You need Polylang Pro to enable this feature.', 'polylang' ),
237 'https://polylang.pro',
238 __( 'Upgrade now.', 'polylang' )
239 );
240 }
241
242 /**
243 * Allows child classes to display an upgrade message
244 *
245 * @since 1.9
246 *
247 * @return string
248 */
249 public function get_upgrade_message() {
250 return '';
251 }
252
253 /**
254 * Get the buttons
255 *
256 * @since 1.9
257 *
258 * @return array
259 */
260 public function get_buttons() {
261 return $this->buttons;
262 }
263 }
264