| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Base class for all settings |
| 5 | 8 | * |
| @@ -5,24 +8,120 @@ | ||
| 5 | 8 | * |
| 6 | 9 | * @since 1.8 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_Settings_Module { |
| 9 | - public $active_option, $configure; | |
| 10 | - public $module, $title, $description; | |
| 12 | + /** | |
| 13 | + * Stores the plugin options. | |
| 14 | + * | |
| 15 | + * @var array | |
| 16 | + */ | |
| 11 | 17 | public $options; |
| 12 | - protected $action_links, $buttons, $form = false; | |
| 13 | 18 | |
| 14 | 19 | /** |
| 20 | + * @var PLL_Model | |
| 21 | + */ | |
| 22 | + public $model; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Instance of a child class of PLL_Links_Model. | |
| 26 | + * | |
| 27 | + * @var PLL_Links_Model | |
| 28 | + */ | |
| 29 | + public $links_model; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Key to use to manage the module activation state. | |
| 33 | + * Possible values: | |
| 34 | + * - An option key for a module that can be activated/deactivated. | |
| 35 | + * - 'none' for a module that doesn't have a activation/deactivation setting. | |
| 36 | + * - 'preview' for a preview module whose functionalities are available in the Pro version. | |
| 37 | + * | |
| 38 | + * @var string | |
| 39 | + * | |
| 40 | + * @phpstan-var non-falsy-string | |
| 41 | + */ | |
| 42 | + public $active_option; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Stores the display order priority. | |
| 46 | + * | |
| 47 | + * @var int | |
| 48 | + */ | |
| 49 | + public $priority = 100; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Stores the module name. | |
| 53 | + * It must be unique. | |
| 54 | + * | |
| 55 | + * @var string | |
| 56 | + * | |
| 57 | + * @phpstan-var non-falsy-string | |
| 58 | + */ | |
| 59 | + public $module; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Stores the module title. | |
| 63 | + * | |
| 64 | + * @var string | |
| 65 | + */ | |
| 66 | + public $title; | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Stores the module description. | |
| 70 | + * | |
| 71 | + * @var string | |
| 72 | + */ | |
| 73 | + public $description; | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Stores the settings actions. | |
| 77 | + * | |
| 78 | + * @var array | |
| 79 | + */ | |
| 80 | + protected $action_links; | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Stores html fragment for the buttons. | |
| 84 | + * | |
| 85 | + * @var array | |
| 86 | + */ | |
| 87 | + protected $buttons; | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Stores html form when provided by a child class. | |
| 91 | + * | |
| 92 | + * @var string|false | |
| 93 | + */ | |
| 94 | + protected $form = false; | |
| 95 | + | |
| 96 | + /** | |
| 15 | 97 | * Constructor |
| 16 | 98 | * |
| 17 | 99 | * @since 1.8 |
| 18 | 100 | * |
| 19 | - * @param object $polylang Polylang object | |
| 20 | - * @param array $args | |
| 101 | + * @param object $polylang The Polylang object. | |
| 102 | + * @param array $args { | |
| 103 | + * @type string $module Unique module name. | |
| 104 | + * @type string $title The title of the settings module. | |
| 105 | + * @type string $description The description of the settings module. | |
| 106 | + * @type string $active_option Optional. Key to use to manage the module activation state. | |
| 107 | + * Possible values: | |
| 108 | + * - An option key for a module that can be activated/deactivated. | |
| 109 | + * - 'none' for a module that doesn't have a activation/deactivation setting. | |
| 110 | + * - 'preview' for a preview module whose functionalities are available in the Pro version. | |
| 111 | + * Default is 'none'. | |
| 112 | + * } | |
| 113 | + * | |
| 114 | + * @phpstan-param array{ | |
| 115 | + * module: non-falsy-string, | |
| 116 | + * title: string, | |
| 117 | + * description: string, | |
| 118 | + * active_option?: non-falsy-string | |
| 119 | + * } $args | |
| 21 | 120 | */ |
| 22 | 121 | public function __construct( &$polylang, $args ) { |
| 23 | - $this->options = &$polylang->options; | |
| 24 | - $this->model = &$polylang->model; | |
| 122 | + $this->options = &$polylang->options; | |
| 123 | + $this->model = &$polylang->model; | |
| 25 | 124 | $this->links_model = &$polylang->links_model; |
| 26 | 125 | |
| 27 | 126 | $args = wp_parse_args( |
| 28 | 127 | $args, |
| @@ -28,12 +127,17 @@ | ||
| 28 | 127 | $args, |
| 29 | 128 | array( |
| 30 | 129 | 'title' => '', |
| 31 | 130 | 'description' => '', |
| 32 | - 'active_option' => false, | |
| 131 | + 'active_option' => 'none', | |
| 33 | 132 | ) |
| 34 | 133 | ); |
| 35 | 134 | |
| 135 | + if ( empty( $args['active_option'] ) ) { | |
| 136 | + // Backward compatibility. | |
| 137 | + $args['active_option'] = 'none'; | |
| 138 | + } | |
| 139 | + | |
| 36 | 140 | foreach ( $args as $prop => $value ) { |
| 37 | 141 | $this->$prop = $value; |
| 38 | 142 | } |
| 39 | 143 | |
| @@ -65,14 +169,14 @@ | ||
| 65 | 169 | 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel', 'polylang' ) ), |
| 66 | 170 | 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes', 'polylang' ) ), |
| 67 | 171 | ); |
| 68 | 172 | |
| 69 | - // Ajax action to save options | |
| 173 | + // Ajax action to save options. | |
| 70 | 174 | add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) ); |
| 71 | 175 | } |
| 72 | 176 | |
| 73 | 177 | /** |
| 74 | - * Tells if the module is active | |
| 178 | + * Tells if the module is active. | |
| 75 | 179 | * |
| 76 | 180 | * @since 1.8 |
| 77 | 181 | * |
| 78 | 182 | * @return bool |
| @@ -77,18 +181,20 @@ | ||
| 77 | 181 | * |
| 78 | 182 | * @return bool |
| 79 | 183 | */ |
| 80 | 184 | public function is_active() { |
| 81 | - return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] ); | |
| 185 | + return 'none' === $this->active_option || ( 'preview' !== $this->active_option && ! empty( $this->options[ $this->active_option ] ) ); | |
| 82 | 186 | } |
| 83 | 187 | |
| 84 | 188 | /** |
| 85 | - * Activates the module | |
| 189 | + * Activates the module. | |
| 86 | 190 | * |
| 87 | 191 | * @since 1.8 |
| 192 | + * | |
| 193 | + * @return void | |
| 88 | 194 | */ |
| 89 | 195 | public function activate() { |
| 90 | - if ( ! empty( $this->active_option ) ) { | |
| 196 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 91 | 197 | $this->options[ $this->active_option ] = true; |
| 92 | 198 | update_option( 'polylang', $this->options ); |
| 93 | 199 | } |
| 94 | 200 | } |
| @@ -93,14 +199,16 @@ | ||
| 93 | 199 | } |
| 94 | 200 | } |
| 95 | 201 | |
| 96 | 202 | /** |
| 97 | - * Deactivates the module | |
| 203 | + * Deactivates the module. | |
| 98 | 204 | * |
| 99 | 205 | * @since 1.8 |
| 206 | + * | |
| 207 | + * @return void | |
| 100 | 208 | */ |
| 101 | 209 | public function deactivate() { |
| 102 | - if ( ! empty( $this->active_option ) ) { | |
| 210 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 103 | 211 | $this->options[ $this->active_option ] = false; |
| 104 | 212 | update_option( 'polylang', $this->options ); |
| 105 | 213 | } |
| 106 | 214 | } |
| @@ -105,18 +213,20 @@ | ||
| 105 | 213 | } |
| 106 | 214 | } |
| 107 | 215 | |
| 108 | 216 | /** |
| 109 | - * Protected method to display a configuration form | |
| 217 | + * Protected method to display a configuration form. | |
| 110 | 218 | * |
| 111 | 219 | * @since 1.8 |
| 220 | + * | |
| 221 | + * @return void | |
| 112 | 222 | */ |
| 113 | 223 | protected function form() { |
| 114 | - // Child classes can provide a form | |
| 224 | + // Child classes can provide a form. | |
| 115 | 225 | } |
| 116 | 226 | |
| 117 | 227 | /** |
| 118 | - * Public method returning the form if any | |
| 228 | + * Public method returning the form if any. | |
| 119 | 229 | * |
| 120 | 230 | * @since 1.8 |
| 121 | 231 | * |
| 122 | 232 | * @return string |
| @@ -121,8 +231,12 @@ | ||
| 121 | 231 | * |
| 122 | 232 | * @return string |
| 123 | 233 | */ |
| 124 | 234 | public function get_form() { |
| 235 | + if ( ! $this->is_active() ) { | |
| 236 | + return ''; | |
| 237 | + } | |
| 238 | + | |
| 125 | 239 | // Read the form only once |
| 126 | 240 | if ( false === $this->form ) { |
| 127 | 241 | ob_start(); |
| 128 | 242 | $this->form(); |
| @@ -132,23 +246,25 @@ | ||
| 132 | 246 | return $this->form; |
| 133 | 247 | } |
| 134 | 248 | |
| 135 | 249 | /** |
| 136 | - * Allows child classes to validate their options before saving | |
| 250 | + * Allows child classes to validate their options before saving. | |
| 137 | 251 | * |
| 138 | 252 | * @since 1.8 |
| 139 | 253 | * |
| 140 | - * @param array $options Raw options | |
| 254 | + * @param array $options Unsanitized options to save. | |
| 141 | 255 | * @return array Options |
| 142 | 256 | */ |
| 143 | - protected function update( $options ) { | |
| 144 | - return array(); // It's responsibility of the child class to decide what is saved | |
| 257 | + protected function update( $options ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 258 | + return array(); // It's responsibility of the child class to decide what is saved. | |
| 145 | 259 | } |
| 146 | 260 | |
| 147 | 261 | /** |
| 148 | - * Ajax method to save the options | |
| 262 | + * Ajax method to save the options. | |
| 149 | 263 | * |
| 150 | 264 | * @since 1.8 |
| 265 | + * | |
| 266 | + * @return void | |
| 151 | 267 | */ |
| 152 | 268 | public function save_options() { |
| 153 | 269 | check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 154 | 270 | if ( ! current_user_can( 'manage_options' ) ) { |
| @@ -168,19 +284,20 @@ | ||
| 168 | 284 | // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified |
| 169 | 285 | // Don't use flush_rewrite_rules as we don't have the right links model and permastruct |
| 170 | 286 | delete_option( 'rewrite_rules' ); |
| 171 | 287 | |
| 288 | + | |
| 172 | 289 | ob_start(); |
| 173 | 290 | |
| 174 | - if ( ! get_settings_errors() ) { | |
| 291 | + if ( empty( get_settings_errors( 'polylang' ) ) ) { | |
| 175 | 292 | // Send update message |
| 176 | - add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' ); | |
| 177 | - settings_errors(); | |
| 293 | + pll_add_notice( new WP_Error( 'settings_updated', __( 'Settings saved.', 'polylang' ), 'success' ) ); | |
| 294 | + settings_errors( 'polylang' ); | |
| 178 | 295 | $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 179 | 296 | $x->send(); |
| 180 | 297 | } else { |
| 181 | 298 | // Send error messages |
| 182 | - settings_errors(); | |
| 299 | + settings_errors( 'polylang' ); | |
| 183 | 300 | $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) ); |
| 184 | 301 | $x->send(); |
| 185 | 302 | } |
| 186 | 303 | } |
| @@ -186,13 +303,13 @@ | ||
| 186 | 303 | } |
| 187 | 304 | } |
| 188 | 305 | |
| 189 | 306 | /** |
| 190 | - * Get the row actions | |
| 307 | + * Get the row actions. | |
| 191 | 308 | * |
| 192 | 309 | * @since 1.8 |
| 193 | 310 | * |
| 194 | - * @return array | |
| 311 | + * @return string[] | |
| 195 | 312 | */ |
| 196 | 313 | protected function get_actions() { |
| 197 | 314 | $actions = array(); |
| 198 | 315 | |
| @@ -199,9 +316,9 @@ | ||
| 199 | 316 | if ( $this->is_active() && $this->get_form() ) { |
| 200 | 317 | $actions[] = 'configure'; |
| 201 | 318 | } |
| 202 | 319 | |
| 203 | - if ( $this->active_option ) { | |
| 320 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 204 | 321 | $actions[] = $this->is_active() ? 'deactivate' : 'activate'; |
| 205 | 322 | } |
| 206 | 323 | |
| 207 | 324 | if ( empty( $actions ) ) { |
| @@ -211,13 +328,13 @@ | ||
| 211 | 328 | return $actions; |
| 212 | 329 | } |
| 213 | 330 | |
| 214 | 331 | /** |
| 215 | - * Get the actions links | |
| 332 | + * Get the actions links. | |
| 216 | 333 | * |
| 217 | 334 | * @since 1.8 |
| 218 | 335 | * |
| 219 | - * @return array | |
| 336 | + * @return string[] Action links. | |
| 220 | 337 | */ |
| 221 | 338 | public function get_action_links() { |
| 222 | 339 | return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); |
| 223 | 340 | } |
| @@ -222,9 +339,9 @@ | ||
| 222 | 339 | return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); |
| 223 | 340 | } |
| 224 | 341 | |
| 225 | 342 | /** |
| 226 | - * Default upgrade message ( to Pro version ) | |
| 343 | + * Default upgrade message (to Pro version). | |
| 227 | 344 | * |
| 228 | 345 | * @since 1.9 |
| 229 | 346 | * |
| 230 | 347 | * @return string |
| @@ -238,9 +355,9 @@ | ||
| 238 | 355 | ); |
| 239 | 356 | } |
| 240 | 357 | |
| 241 | 358 | /** |
| 242 | - * Allows child classes to display an upgrade message | |
| 359 | + * Allows child classes to display an upgrade message. | |
| 243 | 360 | * |
| 244 | 361 | * @since 1.9 |
| 245 | 362 | * |
| 246 | 363 | * @return string |
| @@ -245,17 +362,17 @@ | ||
| 245 | 362 | * |
| 246 | 363 | * @return string |
| 247 | 364 | */ |
| 248 | 365 | public function get_upgrade_message() { |
| 249 | - return ''; | |
| 366 | + return 'preview' === $this->active_option ? $this->default_upgrade_message() : ''; | |
| 250 | 367 | } |
| 251 | 368 | |
| 252 | 369 | /** |
| 253 | - * Get the buttons | |
| 370 | + * Get the buttons. | |
| 254 | 371 | * |
| 255 | 372 | * @since 1.9 |
| 256 | 373 | * |
| 257 | - * @return array | |
| 374 | + * @return string[] An array of html fragment for the buttons. | |
| 258 | 375 | */ |
| 259 | 376 | public function get_buttons() { |
| 260 | 377 | return $this->buttons; |
| 261 | 378 | } |