MS_Addon_Redirect

Add-On controller for: Redirect control.

Defined (1)

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

/app/addon/redirect/class-ms-addon-redirect.php  
  1. class MS_Addon_Redirect extends MS_Addon { 
  2.  
  3. /** 
  4. * The Add-on ID 
  5. * @since 1.0.0 
  6. */ 
  7. const ID = 'addon_redirect'; 
  8.  
  9. // Ajax Actions 
  10. const AJAX_SAVE_SETTING = 'addon_redirect_save'; 
  11.  
  12. /** 
  13. * Checks if the current Add-on is enabled 
  14. * @since 1.0.0 
  15. * @return bool 
  16. */ 
  17. static public function is_active() { 
  18. return MS_Model_Addon::is_enabled( self::ID ); 
  19.  
  20. /** 
  21. * Returns the Add-on ID (self::ID). 
  22. * @since 1.0.1.0 
  23. * @return string 
  24. */ 
  25. public function get_id() { 
  26. return self::ID; 
  27.  
  28. /** 
  29. * Initializes the Add-on. Always executed. 
  30. * @since 1.0.0 
  31. */ 
  32. public function init() { 
  33. if ( self::is_active() ) { 
  34. // Add new settings tab 
  35. $this->add_filter( 
  36. 'ms_controller_settings_get_tabs',  
  37. 'settings_tabs',  
  38. 10, 2 
  39. ); 
  40.  
  41. $this->add_filter( 
  42. 'ms_view_settings_edit_render_callback',  
  43. 'manage_render_callback',  
  44. 10, 3 
  45. ); 
  46.  
  47. // Save settings via ajax 
  48. $this->add_ajax_action( 
  49. self::AJAX_SAVE_SETTING,  
  50. 'ajax_save_setting' 
  51. ); 
  52.  
  53. // Add filter to replace the default plugin URLs with custom URLs 
  54. $this->add_action( 
  55. 'ms_url_after_login',  
  56. 'filter_url_after_login',  
  57. 10, 2 
  58. ); 
  59.  
  60. $this->add_action( 
  61. 'ms_url_after_logout',  
  62. 'filter_url_after_logout',  
  63. 10, 2 
  64. ); 
  65.  
  66. /** 
  67. * Registers the Add-On 
  68. * @since 1.0.0 
  69. * @param array $list The Add-Ons list. 
  70. * @return array The updated Add-Ons list. 
  71. */ 
  72. public function register( $list ) { 
  73. $list[ self::ID ] = (object) array( 
  74. 'name' => __( 'Redirect Control', 'membership2' ),  
  75. 'description' => __( 'Define your individual URL to display after a user is logged-in or logged-out.', 'membership2' ),  
  76. 'icon' => 'wpmui-fa wpmui-fa-share',  
  77. 'details' => array( 
  78. array( 
  79. 'type' => MS_Helper_Html::TYPE_HTML_TEXT,  
  80. 'title' => __( 'Settings', 'membership2' ),  
  81. 'desc' => __( 'When this Add-on is enabled you will see a new section in the "Settings" page with additional options.', 'membership2' ),  
  82. ),  
  83. ),  
  84. ); 
  85.  
  86. return $list; 
  87.  
  88. /** 
  89. * Returns the Redirect-Settings model. 
  90. * @since 1.0.0 
  91. * @return MS_Addon_Redirect_Model 
  92. */ 
  93. static public function model() { 
  94. static $Model = null; 
  95.  
  96. if ( null === $Model ) { 
  97. $Model = MS_Factory::load( 'MS_Addon_Redirect_Model' ); 
  98.  
  99. return $Model; 
  100.  
  101. /** 
  102. * Add redirect settings tab in settings page. 
  103. * @since 1.0.0 
  104. * @param array $tabs The current tabs. 
  105. * @return array The filtered tabs. 
  106. */ 
  107. public function settings_tabs( $tabs ) { 
  108. $tabs[ self::ID ] = array( 
  109. 'title' => __( 'Redirect', 'membership2' ),  
  110. 'url' => MS_Controller_Plugin::get_admin_url( 
  111. 'settings',  
  112. array( 'tab' => self::ID ) 
  113. ),  
  114. ); 
  115.  
  116. return $tabs; 
  117.  
  118. /** 
  119. * Add redirect settings-view callback. 
  120. * @since 1.0.0 
  121. * @param array $callback The current function callback. 
  122. * @param string $tab The current membership rule tab. 
  123. * @param array $data The data shared to the view. 
  124. * @return array The filtered callback. 
  125. */ 
  126. public function manage_render_callback( $callback, $tab, $data ) { 
  127. if ( self::ID == $tab ) { 
  128. $view = MS_Factory::load( 'MS_Addon_Redirect_View' ); 
  129. $callback = array( $view, 'render_tab' ); 
  130.  
  131. return $callback; 
  132.  
  133. /** 
  134. * Handle Ajax update custom setting action. 
  135. * @since 1.0.0 
  136. */ 
  137. public function ajax_save_setting() { 
  138. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  139.  
  140. $isset = array( 'field', 'value' ); 
  141. if ( $this->verify_nonce() 
  142. && self::validate_required( $isset, 'POST', false ) 
  143. && $this->is_admin_user() 
  144. ) { 
  145. $model = self::model(); 
  146.  
  147. $model->set( $_POST['field'], $_POST['value'] ); 
  148. $model->save(); 
  149. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  150.  
  151. wp_die( $msg ); 
  152.  
  153. /** 
  154. * Replaces the default "After Login" URL 
  155. * @since 1.0.0 
  156. * @param string $url 
  157. * @return string 
  158. */ 
  159. public function filter_url_after_login( $url, $enforce ) { 
  160. if ( ! $enforce ) { 
  161. $model = self::model(); 
  162. $new_url = $model->get( 'redirect_login' ); 
  163.  
  164. if ( ! empty( $new_url ) ) { 
  165. $url = lib3()->net->expand_url( $new_url ); 
  166.  
  167. return $url; 
  168.  
  169. /** 
  170. * Replaces the default "After Logout" URL 
  171. * @since 1.0.0 
  172. * @param string $url 
  173. * @return string 
  174. */ 
  175. public function filter_url_after_logout( $url, $enforce ) { 
  176. if ( ! $enforce ) { 
  177. $model = self::model(); 
  178. $new_url = $model->get( 'redirect_logout' ); 
  179.  
  180. if ( ! empty( $new_url ) ) { 
  181. $url = lib3()->net->expand_url( $new_url ); 
  182.  
  183. return $url; 
  184.