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

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