| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Base class for all settings |
| 8 |
* |
| 9 |
* @since 1.8 |
| 10 |
*/ |
| 11 |
class PLL_Settings_Module { |
| 12 |
/** |
| 13 |
* Stores the plugin options. |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
public $options; |
| 18 |
|
| 19 |
/** |
| 20 |
* Instance of PLL_Model. |
| 21 |
* |
| 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 |
* @since 1.8 |
| 94 |
* |
| 95 |
* @param object $polylang Polylang object |
| 96 |
* @param array $args |
| 97 |
*/ |
| 98 |
public function __construct( &$polylang, $args ) { |
| 99 |
$this->options = &$polylang->options; |
| 100 |
$this->model = &$polylang->model; |
| 101 |
$this->links_model = &$polylang->links_model; |
| 102 |
|
| 103 |
$args = wp_parse_args( |
| 104 |
$args, |
| 105 |
array( |
| 106 |
'title' => '', |
| 107 |
'description' => '', |
| 108 |
'active_option' => false, |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
foreach ( $args as $prop => $value ) { |
| 113 |
$this->$prop = $value; |
| 114 |
} |
| 115 |
|
| 116 |
// All possible action links, even if not always a link ;-) |
| 117 |
$this->action_links = array( |
| 118 |
'configure' => sprintf( |
| 119 |
'<a title="%s" href="%s">%s</a>', |
| 120 |
esc_attr__( 'Configure this module', 'polylang' ), |
| 121 |
'#', |
| 122 |
esc_html__( 'Settings', 'polylang' ) |
| 123 |
), |
| 124 |
'deactivate' => sprintf( |
| 125 |
'<a title="%s" href="%s">%s</a>', |
| 126 |
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' ) ), |
| 128 |
esc_html__( 'Deactivate', 'polylang' ) |
| 129 |
), |
| 130 |
'activate' => sprintf( |
| 131 |
'<a title="%s" href="%s">%s</a>', |
| 132 |
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' ) ), |
| 134 |
esc_html__( 'Activate', 'polylang' ) |
| 135 |
), |
| 136 |
'activated' => esc_html__( 'Activated', 'polylang' ), |
| 137 |
'deactivated' => esc_html__( 'Deactivated', 'polylang' ), |
| 138 |
); |
| 139 |
|
| 140 |
$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' ) ), |
| 143 |
); |
| 144 |
|
| 145 |
// Ajax action to save options |
| 146 |
add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Tells if the module is active |
| 151 |
* |
| 152 |
* @since 1.8 |
| 153 |
* |
| 154 |
* @return bool |
| 155 |
*/ |
| 156 |
public function is_active() { |
| 157 |
return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Activates the module |
| 162 |
* |
| 163 |
* @since 1.8 |
| 164 |
*/ |
| 165 |
public function activate() { |
| 166 |
if ( ! empty( $this->active_option ) ) { |
| 167 |
$this->options[ $this->active_option ] = true; |
| 168 |
update_option( 'polylang', $this->options ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Deactivates the module |
| 174 |
* |
| 175 |
* @since 1.8 |
| 176 |
*/ |
| 177 |
public function deactivate() { |
| 178 |
if ( ! empty( $this->active_option ) ) { |
| 179 |
$this->options[ $this->active_option ] = false; |
| 180 |
update_option( 'polylang', $this->options ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Protected method to display a configuration form |
| 186 |
* |
| 187 |
* @since 1.8 |
| 188 |
*/ |
| 189 |
protected function form() { |
| 190 |
// Child classes can provide a form |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Public method returning the form if any |
| 195 |
* |
| 196 |
* @since 1.8 |
| 197 |
* |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function get_form() { |
| 201 |
// Read the form only once |
| 202 |
if ( false === $this->form ) { |
| 203 |
ob_start(); |
| 204 |
$this->form(); |
| 205 |
$this->form = ob_get_clean(); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->form; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Allows child classes to validate their options before saving |
| 213 |
* |
| 214 |
* @since 1.8 |
| 215 |
* |
| 216 |
* @param array $options Raw options |
| 217 |
* @return array Options |
| 218 |
*/ |
| 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 |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Ajax method to save the options |
| 225 |
* |
| 226 |
* @since 1.8 |
| 227 |
*/ |
| 228 |
public function save_options() { |
| 229 |
check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 230 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 231 |
wp_die( -1 ); |
| 232 |
} |
| 233 |
|
| 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 |
| 236 |
$post = array_diff_key( $_POST, array_flip( array( 'action', 'module', 'pll_ajax_backend', '_pll_nonce' ) ) ); |
| 237 |
$options = $this->update( $post ); |
| 238 |
$this->options = array_merge( $this->options, $options ); |
| 239 |
update_option( 'polylang', $this->options ); |
| 240 |
|
| 241 |
// Refresh language cache in case home urls have been modified |
| 242 |
$this->model->clean_languages_cache(); |
| 243 |
|
| 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 |
| 246 |
delete_option( 'rewrite_rules' ); |
| 247 |
|
| 248 |
ob_start(); |
| 249 |
|
| 250 |
if ( ! get_settings_errors() ) { |
| 251 |
// Send update message |
| 252 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' ); |
| 253 |
settings_errors(); |
| 254 |
$x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 255 |
$x->send(); |
| 256 |
} else { |
| 257 |
// Send error messages |
| 258 |
settings_errors(); |
| 259 |
$x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) ); |
| 260 |
$x->send(); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get the row actions |
| 267 |
* |
| 268 |
* @since 1.8 |
| 269 |
* |
| 270 |
* @return array |
| 271 |
*/ |
| 272 |
protected function get_actions() { |
| 273 |
$actions = array(); |
| 274 |
|
| 275 |
if ( $this->is_active() && $this->get_form() ) { |
| 276 |
$actions[] = 'configure'; |
| 277 |
} |
| 278 |
|
| 279 |
if ( $this->active_option ) { |
| 280 |
$actions[] = $this->is_active() ? 'deactivate' : 'activate'; |
| 281 |
} |
| 282 |
|
| 283 |
if ( empty( $actions ) ) { |
| 284 |
$actions[] = $this->is_active() ? 'activated' : 'deactivated'; |
| 285 |
} |
| 286 |
|
| 287 |
return $actions; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get the actions links |
| 292 |
* |
| 293 |
* @since 1.8 |
| 294 |
* |
| 295 |
* @return array |
| 296 |
*/ |
| 297 |
public function get_action_links() { |
| 298 |
return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Default upgrade message ( to Pro version ) |
| 303 |
* |
| 304 |
* @since 1.9 |
| 305 |
* |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
protected function default_upgrade_message() { |
| 309 |
return sprintf( |
| 310 |
'%s <a href="%s">%s</a>', |
| 311 |
__( 'To enable this feature, you need Polylang Pro.', 'polylang' ), |
| 312 |
'https://polylang.pro', |
| 313 |
__( 'Upgrade now.', 'polylang' ) |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Allows child classes to display an upgrade message |
| 319 |
* |
| 320 |
* @since 1.9 |
| 321 |
* |
| 322 |
* @return string |
| 323 |
*/ |
| 324 |
public function get_upgrade_message() { |
| 325 |
return ''; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Get the buttons |
| 330 |
* |
| 331 |
* @since 1.9 |
| 332 |
* |
| 333 |
* @return array |
| 334 |
*/ |
| 335 |
public function get_buttons() { |
| 336 |
return $this->buttons; |
| 337 |
} |
| 338 |
} |
| 339 |
|