PLL_Settings_Module

Base class for all settings.

Defined (1)

The class is defined in the following location(s).

/settings/settings-module.php  
  1. class PLL_Settings_Module { 
  2. public $active_option, $configure; 
  3. public $module, $title, $description; 
  4. public $options; 
  5. protected $action_links, $buttons; 
  6.  
  7. /** 
  8. * constructor 
  9. * @since 1.8 
  10. * @param object $polylang polylang object 
  11. * @param array $args 
  12. */ 
  13. public function __construct( &$polylang, $args ) { 
  14. $this->options = &$polylang->options; 
  15. $this->model = &$polylang->model; 
  16. $this->links_model = &$polylang->links_model; 
  17.  
  18. $args = wp_parse_args( $args, array( 
  19. 'title' => '',  
  20. 'description' => '',  
  21. 'active_option' => false,  
  22. ) ); 
  23.  
  24. foreach ( $args as $prop => $value ) { 
  25. $this->$prop = $value; 
  26.  
  27. // all possible action links, even if not always a link ;- ) 
  28. $this->action_links = array( 
  29. 'configure' => sprintf( 
  30. '<a title="%s" href="%s">%s</a>',  
  31. esc_attr__( 'Configure this module', 'polylang' ),  
  32. '#',  
  33. esc_html__( 'Settings', 'polylang' ) 
  34. ),  
  35.  
  36. 'deactivate' => sprintf( 
  37. '<a title="%s" href="%s">%s</a>',  
  38. esc_attr__( 'Deactivate this module', 'polylang' ),  
  39. wp_nonce_url( '?page=mlang&tab=modules&pll_action=deactivate&noheader=true&module=' . $this->module, 'pll_deactivate' ),  
  40. esc_html__( 'Deactivate', 'polylang' ) 
  41. ),  
  42.  
  43. 'activate' => sprintf( 
  44. '<a title="%s" href="%s">%s</a>',  
  45. esc_attr__( 'Activate this module', 'polylang' ),  
  46. wp_nonce_url( '?page=mlang&tab=modules&pll_action=activate&noheader=true&module=' . $this->module, 'pll_activate' ),  
  47. esc_html__( 'Activate', 'polylang' ) 
  48. ),  
  49.  
  50. 'activated' => esc_html__( 'Activated', 'polylang' ),  
  51.  
  52. 'deactivated' => esc_html__( 'Deactivated', 'polylang' ),  
  53. ); 
  54.  
  55. $this->buttons = array( 
  56. 'cancel' => sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', esc_html__( 'Cancel' ) ),  
  57. 'save' => sprintf( '<button type="button" class="button button-primary save">%s</button>', esc_html__( 'Save Changes' ) ),  
  58. ); 
  59.  
  60. // ajax action to save options 
  61. add_action( 'wp_ajax_pll_save_options', array( $this, 'save_options' ) ); 
  62.  
  63. /** 
  64. * tells if the module is active 
  65. * @since 1.8 
  66. * @return bool 
  67. */ 
  68. public function is_active() { 
  69. return empty( $this->active_option ) || ! empty( $this->options[ $this->active_option ] ); 
  70.  
  71. /** 
  72. * activates the module 
  73. * @since 1.8 
  74. */ 
  75. public function activate() { 
  76. if ( ! empty( $this->active_option ) ) { 
  77. $this->options[ $this->active_option ] = true; 
  78. update_option( 'polylang', $this->options ); 
  79.  
  80. /** 
  81. * deactivates the module 
  82. * @since 1.8 
  83. */ 
  84. public function deactivate() { 
  85. if ( ! empty( $this->active_option ) ) { 
  86. $this->options[ $this->active_option ] = false; 
  87. update_option( 'polylang', $this->options ); 
  88.  
  89. /** 
  90. * protected method to display a configuration form 
  91. * @since 1.8 
  92. */ 
  93. protected function form() { 
  94. // child classes can provide a form 
  95.  
  96. /** 
  97. * public method returning the form if any 
  98. * @since 1.8 
  99. * @return string 
  100. */ 
  101. public function get_form() { 
  102. static $form = false; 
  103.  
  104. // read the form only once 
  105. if ( false === $form ) { 
  106. ob_start(); 
  107. $this->form(); 
  108. $form = ob_get_clean(); 
  109.  
  110. return $form; 
  111.  
  112. /** 
  113. * allows child classes to validate their options before saving 
  114. * @since 1.8 
  115. * @param array $options raw options 
  116. * @param array options 
  117. */ 
  118. protected function update( $options ) { 
  119. return array(); // it's responsibility of the child class to decide what is saved 
  120.  
  121. /** 
  122. * ajax method to save the options 
  123. * @since 1.8 
  124. */ 
  125. public function save_options() { 
  126. check_ajax_referer( 'pll_options', '_pll_nonce' ); 
  127. if ( ! current_user_can( 'manage_options' ) ) { 
  128. wp_die( -1 ); 
  129.  
  130. if ( $this->module == $_POST['module'] ) { 
  131. // it's up to the child class to decide which options are saved, whether there are errors or not 
  132. $post = array_diff_key( $_POST, array_flip( array( 'action', 'module', 'pll_ajax_backend', '_pll_nonce' ) ) ); 
  133. $options = $this->update( $post ); 
  134. $this->options = array_merge( $this->options, $options ); 
  135. update_option( 'polylang', $this->options ); 
  136.  
  137. // refresh language cache in case home urls have been modified 
  138. $this->model->clean_languages_cache(); 
  139.  
  140. // refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified 
  141. // don't use flush_rewrite_rules as we don't have the right links model and permastruct 
  142. delete_option( 'rewrite_rules' ); 
  143.  
  144. ob_start(); 
  145.  
  146. if ( ! get_settings_errors() ) { 
  147. // send update message 
  148. add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); 
  149. settings_errors(); 
  150. $x = new WP_Ajax_Response( array( 'what' => 'success', 'data' => ob_get_clean() ) ); 
  151. $x->send(); 
  152. } else { 
  153. // send error messages 
  154. settings_errors(); 
  155. $x = new WP_Ajax_Response( array( 'what' => 'error', 'data' => ob_get_clean() ) ); 
  156. $x->send(); 
  157.  
  158. /** 
  159. * get the row actions 
  160. * @since 1.8 
  161. * @return array 
  162. */ 
  163. protected function get_actions() { 
  164. if ( $this->is_active() && $this->get_form() ) { 
  165. $actions[] = 'configure'; 
  166.  
  167. if ( $this->active_option ) { 
  168. $actions[] = $this->is_active() ? 'deactivate' : 'activate'; 
  169.  
  170. if ( empty( $actions ) ) { 
  171. $actions[] = $this->is_active() ? 'activated' : 'deactivated'; 
  172.  
  173. return $actions; 
  174.  
  175. /** 
  176. * get the actions links 
  177. * @since 1.8 
  178. * @return array 
  179. */ 
  180. public function get_action_links() { 
  181. return array_intersect_key( $this->action_links, array_flip( $this->get_actions() ) ); 
  182.  
  183. /** 
  184. * default upgrade message ( to pro version ) 
  185. * @since 1.9 
  186. * @return string 
  187. */ 
  188. protected function default_upgrade_message() { 
  189. return sprintf( 
  190. '%s <a href="%s">%s</a>',  
  191. __( 'You need Polylang Pro to enable this feature.', 'polylang' ),  
  192. 'https://polylang.pro',  
  193. __( 'Upgrade now.', 'polylang' ) 
  194. ); 
  195.  
  196. /** 
  197. * allows child classes to display an upgrade message 
  198. * @since 1.9 
  199. * @return string 
  200. */ 
  201. public function get_upgrade_message() { 
  202. return ''; 
  203.  
  204. /** 
  205. * get the buttons 
  206. * @since 1.9 
  207. * @return array 
  208. */ 
  209. public function get_buttons() { 
  210. return $this->buttons;