/app/widget/class-ms-widget-login.php

  1. <?php 
  2.  
  3. class MS_Widget_Login extends WP_Widget { 
  4.  
  5. /** 
  6. * Constructor. 
  7. * Sets up the widgets name etc. 
  8. * 
  9. * @since 1.0.0 
  10. */ 
  11. public function __construct() { 
  12. parent::__construct( 
  13. 'ms_widget_login',  
  14. __( '[Membership2] Login', 'membership2' ),  
  15. array( 
  16. 'description' => __( 'Display a Login Form to all guests. Logged-in users will see a Logout link.', 'membership2' ),  
  17. ); 
  18.  
  19. /** 
  20. * Outputs the content of the widget. 
  21. * 
  22. * @since 1.0.0 
  23. * 
  24. * @param array $args 
  25. * @param array $instance 
  26. */ 
  27. public function widget( $args, $instance ) { 
  28. $redirect_login = false; 
  29. $redirect_logout = false; 
  30. $shortcode_args = ''; 
  31.  
  32. if ( ! empty( $instance['redirect_login'] ) ) { 
  33. $redirect_login = lib3()->net->expand_url( $instance['redirect_login'] ); 
  34.  
  35. if ( ! empty( $instance['redirect_logout'] ) ) { 
  36. $redirect_logout = lib3()->net->expand_url( $instance['redirect_logout'] ); 
  37.  
  38. if ( ! empty( $instance['shortcode_args'] ) ) { 
  39. $shortcode_args = $instance['shortcode_args']; 
  40.  
  41. echo $args['before_widget']; 
  42.  
  43. if ( ! empty( $instance['title'] ) ) { 
  44. echo $args['before_title']; 
  45. echo apply_filters( 'widget_title', $instance['title'] ); 
  46. echo $args['after_title']; 
  47.  
  48. $scode = sprintf( 
  49. '[%1$s header="no" %2$s %3$s %4$s]',  
  50. MS_Helper_Shortcode::SCODE_LOGIN,  
  51. $redirect_login ? 'redirect_login="' . $redirect_login . '"' : '',  
  52. $redirect_logout ? 'redirect_logout="' . $redirect_logout . '"' : '',  
  53. $shortcode_args 
  54. ); 
  55. echo do_shortcode( $scode ); 
  56.  
  57. echo $args['after_widget']; 
  58.  
  59. /** 
  60. * Outputs the options form on admin 
  61. * 
  62. * @since 1.0.0 
  63. * 
  64. * @param array $instance The widget options 
  65. */ 
  66. public function form( $instance ) { 
  67. $title = __( 'Login', 'membership2' ); 
  68. $redirect_login = ''; 
  69. $redirect_logout = ''; 
  70. $shortcode_args = ''; 
  71.  
  72. if ( isset( $instance['title'] ) ) { 
  73. $title = $instance['title']; 
  74.  
  75. if ( isset( $instance['redirect_login'] ) ) { 
  76. $redirect_login = $instance['redirect_login']; 
  77.  
  78. if ( isset( $instance['redirect_logout'] ) ) { 
  79. $redirect_logout = $instance['redirect_logout']; 
  80.  
  81. if ( isset( $instance['shortcode_args'] ) ) { 
  82. $shortcode_args = $instance['shortcode_args']; 
  83.  
  84. $placeholder_login = MS_Model_Pages::get_url_after_login(); 
  85. if ( strlen( $placeholder_login ) > 55 ) { 
  86. $parts = explode( '://', $placeholder_login ); 
  87. $placeholder_login = $parts[0] . '://' . substr( $parts[1], 0, 5 ) . '…' . substr( $parts[1], -38 ); 
  88. $placeholder_logout = MS_Model_Pages::get_url_after_logout(); 
  89. if ( strlen( $placeholder_logout ) > 55 ) { 
  90. $parts = explode( '://', $placeholder_logout ); 
  91. $placeholder_logout = $parts[0] . '://' . substr( $parts[1], 0, 5 ) . '…' . substr( $parts[1], -38 ); 
  92.  
  93. $field_title = array( 
  94. 'id' => $this->get_field_id( 'title' ),  
  95. 'name' => $this->get_field_name( 'title' ),  
  96. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  97. 'title' => __( 'Title:', 'membership2' ),  
  98. 'value' => $title,  
  99. 'class' => 'widefat',  
  100. ); 
  101.  
  102. $field_redirect_login = array( 
  103. 'id' => $this->get_field_id( 'redirect_login' ),  
  104. 'name' => $this->get_field_name( 'redirect_login' ),  
  105. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  106. 'title' => __( 'Show this page after login:', 'membership2' ),  
  107. 'value' => $redirect_login,  
  108. 'placeholder' => $placeholder_login,  
  109. 'class' => 'widefat',  
  110. ); 
  111.  
  112. $field_redirect_logout = array( 
  113. 'id' => $this->get_field_id( 'redirect_logout' ),  
  114. 'name' => $this->get_field_name( 'redirect_logout' ),  
  115. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  116. 'title' => __( 'Show this page after logout:', 'membership2' ),  
  117. 'value' => $redirect_logout,  
  118. 'placeholder' => $placeholder_logout,  
  119. 'class' => 'widefat',  
  120. ); 
  121.  
  122. $field_shortcode_args = array( 
  123. 'id' => $this->get_field_id( 'shortcode_args' ),  
  124. 'name' => $this->get_field_name( 'shortcode_args' ),  
  125. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  126. 'title' => __( 'Shortcode Options:', 'membership2' ),  
  127. 'desc' => sprintf( 
  128. __( 'Arguments to pass to the %slogin shortcode%s', 'membership2' ),  
  129. sprintf( 
  130. '<a href="%s#ms-membership-login" target="_blank">',  
  131. MS_Controller_Plugin::get_admin_url( 
  132. 'help',  
  133. array( 'tab' => 'shortcodes' ) 
  134. ),  
  135. '</a>' 
  136. ),  
  137. 'value' => $shortcode_args,  
  138. 'placeholder' => 'header="no"',  
  139. 'class' => 'widefat',  
  140. ); 
  141.  
  142. MS_Helper_Html::html_element( $field_title ); 
  143. MS_Helper_Html::html_element( $field_redirect_login ); 
  144. MS_Helper_Html::html_element( $field_redirect_logout ); 
  145. MS_Helper_Html::html_element( $field_shortcode_args ); 
  146.  
  147. /** 
  148. * Processing widget options on save 
  149. * 
  150. * @since 1.0.0 
  151. * 
  152. * @param array $new_instance The new options 
  153. * @param array $old_instance The previous options 
  154. * @return array Updated safe values to be saved. 
  155. */ 
  156. public function update( $new_instance, $old_instance ) { 
  157. $instance = array(); 
  158. $instance['title'] = ''; 
  159. $instance['redirect_login'] = ''; 
  160. $instance['redirect_logout'] = ''; 
  161. $instance['shortcode_args'] = ''; 
  162.  
  163. if ( isset( $new_instance['title'] ) ) { 
  164. $instance['title'] = strip_tags( $new_instance['title'] ); 
  165.  
  166. if ( isset( $new_instance['redirect_login'] ) ) { 
  167. $instance['redirect_login'] = strip_tags( $new_instance['redirect_login'] ); 
  168.  
  169. if ( isset( $new_instance['redirect_logout'] ) ) { 
  170. $instance['redirect_logout'] = strip_tags( $new_instance['redirect_logout'] ); 
  171.  
  172. if ( isset( $new_instance['shortcode_args'] ) ) { 
  173. $instance['shortcode_args'] = strip_tags( $new_instance['shortcode_args'] ); 
  174.  
  175. return $instance; 
.