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

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