| @@ -1,100 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * Base class for all settings | |
| 4 | + * base class for all settings | |
| 8 | 5 | * |
| 9 | 6 | * @since 1.8 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Settings_Module { |
| 12 | - /** | |
| 13 | - * Stores the plugin options. | |
| 14 | - * | |
| 15 | - * @var array | |
| 16 | - */ | |
| 9 | + public $active_option, $configure; | |
| 10 | + public $module, $title, $description; | |
| 17 | 11 | public $options; |
| 12 | + protected $action_links, $buttons; | |
| 18 | 13 | |
| 19 | 14 | /** |
| 20 | - * Instance of PLL_Model. | |
| 15 | + * constructor | |
| 21 | 16 | * |
| 22 | - * @var PLL_Model | |
| 23 | - */ | |
| 24 | - public $model; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Instance of a child class of PLL_Links_Model. | |
| 28 | - * | |
| 29 | - * @var PLL_Links_Model | |
| 30 | - */ | |
| 31 | - public $links_model; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Stores if the module is active. | |
| 35 | - * | |
| 36 | - * @var bool | |
| 37 | - */ | |
| 38 | - public $active_option; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Stores the display order priority. | |
| 42 | - * | |
| 43 | - * @var int | |
| 44 | - */ | |
| 45 | - public $priority = 100; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Stores the module name. | |
| 49 | - * It must be unique. | |
| 50 | - * | |
| 51 | - * @var string | |
| 52 | - */ | |
| 53 | - public $module; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Stores the module title. | |
| 57 | - * | |
| 58 | - * @var string | |
| 59 | - */ | |
| 60 | - public $title; | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * Stores the module description. | |
| 64 | - * | |
| 65 | - * @var string | |
| 66 | - */ | |
| 67 | - public $description; | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Stores the settings actions. | |
| 71 | - * | |
| 72 | - * @var array | |
| 73 | - */ | |
| 74 | - protected $action_links; | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * Stores html fragment for the buttons. | |
| 78 | - * | |
| 79 | - * @var array | |
| 80 | - */ | |
| 81 | - protected $buttons; | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Stores html form when provided by a child class. | |
| 85 | - * | |
| 86 | - * @var bool|string | |
| 87 | - */ | |
| 88 | - protected $form = false; | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Constructor | |
| 92 | - * | |
| 93 | 17 | * @since 1.8 |
| 94 | 18 | * |
| 95 | - * @param object $polylang Polylang object | |
| 96 | - * @param array $args | |
| 19 | + * @param object $polylang polylang object | |
| 20 | + * @param array $args | |
| 97 | 21 | */ |
| 98 | 22 | public function __construct( &$polylang, $args ) { |
| 99 | 23 | $this->options = &$polylang->options; |
| 100 | 24 | $this->model = &$polylang->model; |
| @@ -99,56 +23,57 @@ | ||
| 99 | 23 | $this->options = &$polylang->options; |
| 100 | 24 | $this->model = &$polylang->model; |
| 101 | 25 | $this->links_model = &$polylang->links_model; |
| 102 | 26 | |
| 103 | - $args = wp_parse_args( | |
| 104 | - $args, | |
| 105 | - array( | |
| 106 | - 'title' => '', | |
| 107 | - 'description' => '', | |
| 108 | - 'active_option' => false, | |
| 109 | - ) | |
| 110 | - ); | |
| 27 | + $args = wp_parse_args( $args, array( | |
| 28 | + 'title' => '', | |
| 29 | + 'description' => '', | |
| 30 | + 'active_option' => false, | |
| 31 | + ) ); | |
| 111 | 32 | |
| 112 | 33 | foreach ( $args as $prop => $value ) { |
| 113 | 34 | $this->$prop = $value; |
| 114 | 35 | } |
| 115 | 36 | |
| 116 | - // All possible action links, even if not always a link ;-) | |
| 37 | + // all possible action links, even if not always a link ;- ) | |
| 117 | 38 | $this->action_links = array( |
| 118 | - 'configure' => sprintf( | |
| 39 | + 'configure' => sprintf( | |
| 119 | 40 | '<a title="%s" href="%s">%s</a>', |
| 120 | 41 | esc_attr__( 'Configure this module', 'polylang' ), |
| 121 | 42 | '#', |
| 122 | 43 | esc_html__( 'Settings', 'polylang' ) |
| 123 | 44 | ), |
| 124 | - 'deactivate' => sprintf( | |
| 45 | + | |
| 46 | + 'deactivate' => sprintf( | |
| 125 | 47 | '<a title="%s" href="%s">%s</a>', |
| 126 | 48 | esc_attr__( 'Deactivate this module', 'polylang' ), |
| 127 | - esc_url( wp_nonce_url( '?page=mlang&tab=modules&pll_action=deactivate&noheader=true&module=' . $this->module, 'pll_deactivate' ) ), | |
| 49 | + wp_nonce_url( '?page=mlang&tab=modules&pll_action=deactivate&noheader=true&module=' . $this->module, 'pll_deactivate' ), | |
| 128 | 50 | esc_html__( 'Deactivate', 'polylang' ) |
| 129 | 51 | ), |
| 130 | - 'activate' => sprintf( | |
| 52 | + | |
| 53 | + 'activate' => sprintf( | |
| 131 | 54 | '<a title="%s" href="%s">%s</a>', |
| 132 | 55 | esc_attr__( 'Activate this module', 'polylang' ), |
| 133 | - esc_url( wp_nonce_url( '?page=mlang&tab=modules&pll_action=activate&noheader=true&module=' . $this->module, 'pll_activate' ) ), | |
| 56 | + wp_nonce_url( '?page=mlang&tab=modules&pll_action=activate&noheader=true&module=' . $this->module, 'pll_activate' ), | |
| 134 | 57 | esc_html__( 'Activate', 'polylang' ) |
| 135 | 58 | ), |
| 136 | - 'activated' => esc_html__( 'Activated', 'polylang' ), | |
| 59 | + | |
| 60 | + 'activated' => esc_html__( 'Activated', 'polylang' ), | |
| 61 | + | |
| 137 | 62 | 'deactivated' => esc_html__( 'Deactivated', 'polylang' ), |
| 138 | 63 | ); |
| 139 | 64 | |
| 140 | 65 | $this->buttons = array( |
| 141 | - 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel', 'polylang' ) ), | |
| 142 | - 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes', 'polylang' ) ), | |
| 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' ) ), | |
| 143 | 68 | ); |
| 144 | 69 | |
| 145 | - // Ajax action to save options | |
| 70 | + // ajax action to save options | |
| 146 | 71 | add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) ); |
| 147 | 72 | } |
| 148 | 73 | |
| 149 | 74 | /** |
| 150 | - * Tells if the module is active | |
| 75 | + * tells if the module is active | |
| 151 | 76 | * |
| 152 | 77 | * @since 1.8 |
| 153 | 78 | * |
| 154 | 79 | * @return bool |
| @@ -157,9 +82,9 @@ | ||
| 157 | 82 | return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] ); |
| 158 | 83 | } |
| 159 | 84 | |
| 160 | 85 | /** |
| 161 | - * Activates the module | |
| 86 | + * activates the module | |
| 162 | 87 | * |
| 163 | 88 | * @since 1.8 |
| 164 | 89 | */ |
| 165 | 90 | public function activate() { |
| @@ -169,9 +94,9 @@ | ||
| 169 | 94 | } |
| 170 | 95 | } |
| 171 | 96 | |
| 172 | 97 | /** |
| 173 | - * Deactivates the module | |
| 98 | + * deactivates the module | |
| 174 | 99 | * |
| 175 | 100 | * @since 1.8 |
| 176 | 101 | */ |
| 177 | 102 | public function deactivate() { |
| @@ -181,18 +106,19 @@ | ||
| 181 | 106 | } |
| 182 | 107 | } |
| 183 | 108 | |
| 184 | 109 | /** |
| 185 | - * Protected method to display a configuration form | |
| 110 | + * protected method to display a configuration form | |
| 186 | 111 | * |
| 187 | 112 | * @since 1.8 |
| 113 | + * | |
| 188 | 114 | */ |
| 189 | 115 | protected function form() { |
| 190 | - // Child classes can provide a form | |
| 116 | + // child classes can provide a form | |
| 191 | 117 | } |
| 192 | 118 | |
| 193 | 119 | /** |
| 194 | - * Public method returning the form if any | |
| 120 | + * public method returning the form if any | |
| 195 | 121 | * |
| 196 | 122 | * @since 1.8 |
| 197 | 123 | * |
| 198 | 124 | * @return string |
| @@ -197,32 +123,34 @@ | ||
| 197 | 123 | * |
| 198 | 124 | * @return string |
| 199 | 125 | */ |
| 200 | 126 | public function get_form() { |
| 201 | - // Read the form only once | |
| 202 | - if ( false === $this->form ) { | |
| 127 | + static $form = false; | |
| 128 | + | |
| 129 | + // read the form only once | |
| 130 | + if ( false === $form ) { | |
| 203 | 131 | ob_start(); |
| 204 | 132 | $this->form(); |
| 205 | - $this->form = ob_get_clean(); | |
| 133 | + $form = ob_get_clean(); | |
| 206 | 134 | } |
| 207 | 135 | |
| 208 | - return $this->form; | |
| 136 | + return $form; | |
| 209 | 137 | } |
| 210 | 138 | |
| 211 | 139 | /** |
| 212 | - * Allows child classes to validate their options before saving | |
| 140 | + * allows child classes to validate their options before saving | |
| 213 | 141 | * |
| 214 | 142 | * @since 1.8 |
| 215 | 143 | * |
| 216 | - * @param array $options Raw options | |
| 217 | - * @return array Options | |
| 144 | + * @param array $options raw options | |
| 145 | + * @param array options | |
| 218 | 146 | */ |
| 219 | - protected function update( $options ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 220 | - return array(); // It's responsibility of the child class to decide what is saved | |
| 147 | + protected function update( $options ) { | |
| 148 | + return array(); // it's responsibility of the child class to decide what is saved | |
| 221 | 149 | } |
| 222 | 150 | |
| 223 | 151 | /** |
| 224 | - * Ajax method to save the options | |
| 152 | + * ajax method to save the options | |
| 225 | 153 | * |
| 226 | 154 | * @since 1.8 |
| 227 | 155 | */ |
| 228 | 156 | public function save_options() { |
| @@ -230,32 +158,32 @@ | ||
| 230 | 158 | if ( ! current_user_can( 'manage_options' ) ) { |
| 231 | 159 | wp_die( -1 ); |
| 232 | 160 | } |
| 233 | 161 | |
| 234 | - if ( isset( $_POST['module'] ) && $this->module === $_POST['module'] ) { | |
| 235 | - // It's up to the child class to decide which options are saved, whether there are errors or not | |
| 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 | |
| 236 | 164 | $post = array_diff_key( $_POST, array_flip( array( 'action', 'module', 'pll_ajax_backend', '_pll_nonce' ) ) ); |
| 237 | 165 | $options = $this->update( $post ); |
| 238 | 166 | $this->options = array_merge( $this->options, $options ); |
| 239 | 167 | update_option( 'polylang', $this->options ); |
| 240 | 168 | |
| 241 | - // Refresh language cache in case home urls have been modified | |
| 169 | + // refresh language cache in case home urls have been modified | |
| 242 | 170 | $this->model->clean_languages_cache(); |
| 243 | 171 | |
| 244 | - // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified | |
| 245 | - // Don't use flush_rewrite_rules as we don't have the right links model and permastruct | |
| 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 | |
| 246 | 174 | delete_option( 'rewrite_rules' ); |
| 247 | 175 | |
| 248 | 176 | ob_start(); |
| 249 | 177 | |
| 250 | 178 | if ( ! get_settings_errors() ) { |
| 251 | - // Send update message | |
| 252 | - add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' ); | |
| 179 | + // send update message | |
| 180 | + add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); | |
| 253 | 181 | settings_errors(); |
| 254 | 182 | $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 255 | 183 | $x->send(); |
| 256 | 184 | } else { |
| 257 | - // Send error messages | |
| 185 | + // send error messages | |
| 258 | 186 | settings_errors(); |
| 259 | 187 | $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) ); |
| 260 | 188 | $x->send(); |
| 261 | 189 | } |
| @@ -262,9 +190,9 @@ | ||
| 262 | 190 | } |
| 263 | 191 | } |
| 264 | 192 | |
| 265 | 193 | /** |
| 266 | - * Get the row actions | |
| 194 | + * get the row actions | |
| 267 | 195 | * |
| 268 | 196 | * @since 1.8 |
| 269 | 197 | * |
| 270 | 198 | * @return array |
| @@ -269,10 +197,8 @@ | ||
| 269 | 197 | * |
| 270 | 198 | * @return array |
| 271 | 199 | */ |
| 272 | 200 | protected function get_actions() { |
| 273 | - $actions = array(); | |
| 274 | - | |
| 275 | 201 | if ( $this->is_active() && $this->get_form() ) { |
| 276 | 202 | $actions[] = 'configure'; |
| 277 | 203 | } |
| 278 | 204 | |
| @@ -287,9 +213,9 @@ | ||
| 287 | 213 | return $actions; |
| 288 | 214 | } |
| 289 | 215 | |
| 290 | 216 | /** |
| 291 | - * Get the actions links | |
| 217 | + * get the actions links | |
| 292 | 218 | * |
| 293 | 219 | * @since 1.8 |
| 294 | 220 | * |
| 295 | 221 | * @return array |
| @@ -298,9 +224,9 @@ | ||
| 298 | 224 | return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); |
| 299 | 225 | } |
| 300 | 226 | |
| 301 | 227 | /** |
| 302 | - * Default upgrade message ( to Pro version ) | |
| 228 | + * default upgrade message ( to pro version ) | |
| 303 | 229 | * |
| 304 | 230 | * @since 1.9 |
| 305 | 231 | * |
| 306 | 232 | * @return string |
| @@ -307,9 +233,9 @@ | ||
| 307 | 233 | */ |
| 308 | 234 | protected function default_upgrade_message() { |
| 309 | 235 | return sprintf( |
| 310 | 236 | '%s <a href="%s">%s</a>', |
| 311 | - __( 'To enable this feature, you need Polylang Pro.', 'polylang' ), | |
| 237 | + __( 'You need Polylang Pro to enable this feature.', 'polylang' ), | |
| 312 | 238 | 'https://polylang.pro', |
| 313 | 239 | __( 'Upgrade now.', 'polylang' ) |
| 314 | 240 | ); |
| 315 | 241 | } |
| @@ -314,9 +240,9 @@ | ||
| 314 | 240 | ); |
| 315 | 241 | } |
| 316 | 242 | |
| 317 | 243 | /** |
| 318 | - * Allows child classes to display an upgrade message | |
| 244 | + * allows child classes to display an upgrade message | |
| 319 | 245 | * |
| 320 | 246 | * @since 1.9 |
| 321 | 247 | * |
| 322 | 248 | * @return string |
| @@ -325,9 +251,9 @@ | ||
| 325 | 251 | return ''; |
| 326 | 252 | } |
| 327 | 253 | |
| 328 | 254 | /** |
| 329 | - * Get the buttons | |
| 255 | + * get the buttons | |
| 330 | 256 | * |
| 331 | 257 | * @since 1.9 |
| 332 | 258 | * |
| 333 | 259 | * @return array |