| @@ -28,11 +28,17 @@ | ||
| 28 | 28 | */ |
| 29 | 29 | public $links_model; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | - * Stores if the module is active. | |
| 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. | |
| 33 | 37 | * |
| 34 | - * @var bool | |
| 38 | + * @var string | |
| 39 | + * | |
| 40 | + * @phpstan-var non-falsy-string | |
| 35 | 41 | */ |
| 36 | 42 | public $active_option; |
| 37 | 43 | |
| 38 | 44 | /** |
| @@ -46,8 +52,10 @@ | ||
| 46 | 52 | * Stores the module name. |
| 47 | 53 | * It must be unique. |
| 48 | 54 | * |
| 49 | 55 | * @var string |
| 56 | + * | |
| 57 | + * @phpstan-var non-falsy-string | |
| 50 | 58 | */ |
| 51 | 59 | public $module; |
| 52 | 60 | |
| 53 | 61 | /** |
| @@ -80,9 +88,9 @@ | ||
| 80 | 88 | |
| 81 | 89 | /** |
| 82 | 90 | * Stores html form when provided by a child class. |
| 83 | 91 | * |
| 84 | - * @var bool|string | |
| 92 | + * @var string|false | |
| 85 | 93 | */ |
| 86 | 94 | protected $form = false; |
| 87 | 95 | |
| 88 | 96 | /** |
| @@ -89,14 +97,31 @@ | ||
| 89 | 97 | * Constructor |
| 90 | 98 | * |
| 91 | 99 | * @since 1.8 |
| 92 | 100 | * |
| 93 | - * @param object $polylang Polylang object | |
| 94 | - * @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 | |
| 95 | 120 | */ |
| 96 | 121 | public function __construct( &$polylang, $args ) { |
| 97 | - $this->options = &$polylang->options; | |
| 98 | - $this->model = &$polylang->model; | |
| 122 | + $this->options = &$polylang->options; | |
| 123 | + $this->model = &$polylang->model; | |
| 99 | 124 | $this->links_model = &$polylang->links_model; |
| 100 | 125 | |
| 101 | 126 | $args = wp_parse_args( |
| 102 | 127 | $args, |
| @@ -102,12 +127,17 @@ | ||
| 102 | 127 | $args, |
| 103 | 128 | array( |
| 104 | 129 | 'title' => '', |
| 105 | 130 | 'description' => '', |
| 106 | - 'active_option' => false, | |
| 131 | + 'active_option' => 'none', | |
| 107 | 132 | ) |
| 108 | 133 | ); |
| 109 | 134 | |
| 135 | + if ( empty( $args['active_option'] ) ) { | |
| 136 | + // Backward compatibility. | |
| 137 | + $args['active_option'] = 'none'; | |
| 138 | + } | |
| 139 | + | |
| 110 | 140 | foreach ( $args as $prop => $value ) { |
| 111 | 141 | $this->$prop = $value; |
| 112 | 142 | } |
| 113 | 143 | |
| @@ -139,14 +169,14 @@ | ||
| 139 | 169 | 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel', 'polylang' ) ), |
| 140 | 170 | 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes', 'polylang' ) ), |
| 141 | 171 | ); |
| 142 | 172 | |
| 143 | - // Ajax action to save options | |
| 173 | + // Ajax action to save options. | |
| 144 | 174 | add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) ); |
| 145 | 175 | } |
| 146 | 176 | |
| 147 | 177 | /** |
| 148 | - * Tells if the module is active | |
| 178 | + * Tells if the module is active. | |
| 149 | 179 | * |
| 150 | 180 | * @since 1.8 |
| 151 | 181 | * |
| 152 | 182 | * @return bool |
| @@ -151,13 +181,13 @@ | ||
| 151 | 181 | * |
| 152 | 182 | * @return bool |
| 153 | 183 | */ |
| 154 | 184 | public function is_active() { |
| 155 | - 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 ] ) ); | |
| 156 | 186 | } |
| 157 | 187 | |
| 158 | 188 | /** |
| 159 | - * Activates the module | |
| 189 | + * Activates the module. | |
| 160 | 190 | * |
| 161 | 191 | * @since 1.8 |
| 162 | 192 | * |
| 163 | 193 | * @return void |
| @@ -162,9 +192,9 @@ | ||
| 162 | 192 | * |
| 163 | 193 | * @return void |
| 164 | 194 | */ |
| 165 | 195 | public function activate() { |
| 166 | - if ( ! empty( $this->active_option ) ) { | |
| 196 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 167 | 197 | $this->options[ $this->active_option ] = true; |
| 168 | 198 | update_option( 'polylang', $this->options ); |
| 169 | 199 | } |
| 170 | 200 | } |
| @@ -169,9 +199,9 @@ | ||
| 169 | 199 | } |
| 170 | 200 | } |
| 171 | 201 | |
| 172 | 202 | /** |
| 173 | - * Deactivates the module | |
| 203 | + * Deactivates the module. | |
| 174 | 204 | * |
| 175 | 205 | * @since 1.8 |
| 176 | 206 | * |
| 177 | 207 | * @return void |
| @@ -176,9 +206,9 @@ | ||
| 176 | 206 | * |
| 177 | 207 | * @return void |
| 178 | 208 | */ |
| 179 | 209 | public function deactivate() { |
| 180 | - if ( ! empty( $this->active_option ) ) { | |
| 210 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 181 | 211 | $this->options[ $this->active_option ] = false; |
| 182 | 212 | update_option( 'polylang', $this->options ); |
| 183 | 213 | } |
| 184 | 214 | } |
| @@ -183,9 +213,9 @@ | ||
| 183 | 213 | } |
| 184 | 214 | } |
| 185 | 215 | |
| 186 | 216 | /** |
| 187 | - * Protected method to display a configuration form | |
| 217 | + * Protected method to display a configuration form. | |
| 188 | 218 | * |
| 189 | 219 | * @since 1.8 |
| 190 | 220 | * |
| 191 | 221 | * @return void |
| @@ -190,13 +220,13 @@ | ||
| 190 | 220 | * |
| 191 | 221 | * @return void |
| 192 | 222 | */ |
| 193 | 223 | protected function form() { |
| 194 | - // Child classes can provide a form | |
| 224 | + // Child classes can provide a form. | |
| 195 | 225 | } |
| 196 | 226 | |
| 197 | 227 | /** |
| 198 | - * Public method returning the form if any | |
| 228 | + * Public method returning the form if any. | |
| 199 | 229 | * |
| 200 | 230 | * @since 1.8 |
| 201 | 231 | * |
| 202 | 232 | * @return string |
| @@ -201,8 +231,12 @@ | ||
| 201 | 231 | * |
| 202 | 232 | * @return string |
| 203 | 233 | */ |
| 204 | 234 | public function get_form() { |
| 235 | + if ( ! $this->is_active() ) { | |
| 236 | + return ''; | |
| 237 | + } | |
| 238 | + | |
| 205 | 239 | // Read the form only once |
| 206 | 240 | if ( false === $this->form ) { |
| 207 | 241 | ob_start(); |
| 208 | 242 | $this->form(); |
| @@ -212,21 +246,21 @@ | ||
| 212 | 246 | return $this->form; |
| 213 | 247 | } |
| 214 | 248 | |
| 215 | 249 | /** |
| 216 | - * Allows child classes to validate their options before saving | |
| 250 | + * Allows child classes to validate their options before saving. | |
| 217 | 251 | * |
| 218 | 252 | * @since 1.8 |
| 219 | 253 | * |
| 220 | - * @param array $options Raw options | |
| 254 | + * @param array $options Unsanitized options to save. | |
| 221 | 255 | * @return array Options |
| 222 | 256 | */ |
| 223 | 257 | 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 | |
| 258 | + return array(); // It's responsibility of the child class to decide what is saved. | |
| 225 | 259 | } |
| 226 | 260 | |
| 227 | 261 | /** |
| 228 | - * Ajax method to save the options | |
| 262 | + * Ajax method to save the options. | |
| 229 | 263 | * |
| 230 | 264 | * @since 1.8 |
| 231 | 265 | * |
| 232 | 266 | * @return void |
| @@ -250,19 +284,20 @@ | ||
| 250 | 284 | // Refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified |
| 251 | 285 | // Don't use flush_rewrite_rules as we don't have the right links model and permastruct |
| 252 | 286 | delete_option( 'rewrite_rules' ); |
| 253 | 287 | |
| 288 | + | |
| 254 | 289 | ob_start(); |
| 255 | 290 | |
| 256 | - if ( ! get_settings_errors() ) { | |
| 291 | + if ( empty( get_settings_errors( 'polylang' ) ) ) { | |
| 257 | 292 | // Send update message |
| 258 | - add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' ); | |
| 259 | - settings_errors(); | |
| 293 | + pll_add_notice( new WP_Error( 'settings_updated', __( 'Settings saved.', 'polylang' ), 'success' ) ); | |
| 294 | + settings_errors( 'polylang' ); | |
| 260 | 295 | $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 261 | 296 | $x->send(); |
| 262 | 297 | } else { |
| 263 | 298 | // Send error messages |
| 264 | - settings_errors(); | |
| 299 | + settings_errors( 'polylang' ); | |
| 265 | 300 | $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) ); |
| 266 | 301 | $x->send(); |
| 267 | 302 | } |
| 268 | 303 | } |
| @@ -281,9 +316,9 @@ | ||
| 281 | 316 | if ( $this->is_active() && $this->get_form() ) { |
| 282 | 317 | $actions[] = 'configure'; |
| 283 | 318 | } |
| 284 | 319 | |
| 285 | - if ( $this->active_option ) { | |
| 320 | + if ( 'none' !== $this->active_option && 'preview' !== $this->active_option ) { | |
| 286 | 321 | $actions[] = $this->is_active() ? 'deactivate' : 'activate'; |
| 287 | 322 | } |
| 288 | 323 | |
| 289 | 324 | if ( empty( $actions ) ) { |
| @@ -304,9 +339,9 @@ | ||
| 304 | 339 | return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); |
| 305 | 340 | } |
| 306 | 341 | |
| 307 | 342 | /** |
| 308 | - * Default upgrade message ( to Pro version ) | |
| 343 | + * Default upgrade message (to Pro version). | |
| 309 | 344 | * |
| 310 | 345 | * @since 1.9 |
| 311 | 346 | * |
| 312 | 347 | * @return string |
| @@ -320,9 +355,9 @@ | ||
| 320 | 355 | ); |
| 321 | 356 | } |
| 322 | 357 | |
| 323 | 358 | /** |
| 324 | - * Allows child classes to display an upgrade message | |
| 359 | + * Allows child classes to display an upgrade message. | |
| 325 | 360 | * |
| 326 | 361 | * @since 1.9 |
| 327 | 362 | * |
| 328 | 363 | * @return string |
| @@ -327,9 +362,9 @@ | ||
| 327 | 362 | * |
| 328 | 363 | * @return string |
| 329 | 364 | */ |
| 330 | 365 | public function get_upgrade_message() { |
| 331 | - return ''; | |
| 366 | + return 'preview' === $this->active_option ? $this->default_upgrade_message() : ''; | |
| 332 | 367 | } |
| 333 | 368 | |
| 334 | 369 | /** |
| 335 | 370 | * Get the buttons. |