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