PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp/wp-content/plugins/sidebar-login/includes/class-sidebar-login-widget.php

https://bitbucket.org/akeda/bmw-id-hris
PHP | 408 lines | 259 code | 75 blank | 74 comment | 43 complexity | c932ee4e48977c7f03c5c310f434c8ea MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. /**
  4. * Sidebar_Login_Widget class.
  5. *
  6. * @extends WP_Widget
  7. */
  8. class Sidebar_Login_Widget extends WP_Widget {
  9. private $instance = '';
  10. private $user = null;
  11. private $options = array();
  12. /**
  13. * Sidebar_Login_Widget function.
  14. *
  15. * @access public
  16. * @return void
  17. */
  18. public function Sidebar_Login_Widget() {
  19. /* Widget settings. */
  20. $widget_ops = array( 'description' => __( 'Displays a login area in the sidebar.', 'sidebar_login' ) );
  21. /* Create the widget. */
  22. $this->WP_Widget( 'wp_sidebarlogin', __( 'Sidebar Login', 'sidebar_login' ), $widget_ops );
  23. }
  24. /**
  25. * define_options function.
  26. *
  27. * @access public
  28. * @return void
  29. */
  30. public function define_options() {
  31. // Define options for widget
  32. $this->options = array(
  33. 'logged_out_title' => array(
  34. 'label' => __( 'Logged-out title', 'sidebar_login' ),
  35. 'default' => __( 'Login', 'sidebar_login' ),
  36. 'type' => 'text'
  37. ),
  38. 'logged_out_links' => array(
  39. 'label' => __( 'Links', 'sidebar_login' ) . ' (' . __( '<code>Text | HREF</code>', 'sidebar_login' ) . ')',
  40. 'default' => '',
  41. 'type' => 'textarea'
  42. ),
  43. 'show_lost_password_link' => array(
  44. 'label' => __( 'Show lost password link', 'sidebar_login' ),
  45. 'default' => 1,
  46. 'type' => 'checkbox'
  47. ),
  48. 'show_register_link' => array(
  49. 'label' => __( 'Show register link', 'sidebar_login' ),
  50. 'default' => 1,
  51. 'description' => sprintf( __( '<a href="%s">Anyone can register</a> must be enabled.', 'sidebar_login' ), admin_url('options-general.php') ),
  52. 'type' => 'checkbox'
  53. ),
  54. 'login_redirect_url' => array(
  55. 'label' => __( 'Login Redirect URL', 'sidebar_login' ),
  56. 'default' => '',
  57. 'type' => 'text',
  58. 'placeholder' => 'Current page URL'
  59. ),
  60. 'break-1' => array(
  61. 'type' => 'break'
  62. ),
  63. 'logged_in_title' => array(
  64. 'label' => __( 'Logged-in title', 'sidebar_login' ),
  65. 'default' => __( 'Welcome %username%', 'sidebar_login' ),
  66. 'type' => 'text'
  67. ),
  68. 'logged_in_links' => array(
  69. 'label' => __( 'Links', 'sidebar_login' ) . ' (' . __( '<code>Text | HREF | Capability</code>', 'sidebar_login' ) . ')',
  70. 'description' => sprintf( __( '<a href="%s">Capability</a> (optional) refers to the type of user who can view the link.', 'sidebar_login' ), 'http://codex.wordpress.org/Roles_and_Capabilities' ),
  71. 'default' => "Dashboard | %admin_url%\nProfile | %admin_url%/profile.php\nLogout | %logout_url%",
  72. 'type' => 'textarea'
  73. ),
  74. 'show_avatar' => array(
  75. 'label' => __( 'Show logged-in user avatar', 'sidebar_login' ),
  76. 'default' => 1,
  77. 'type' => 'checkbox'
  78. ),
  79. 'logout_redirect_url' => array(
  80. 'label' => __( 'Logout Redirect URL', 'sidebar_login' ),
  81. 'default' => '',
  82. 'type' => 'text',
  83. 'placeholder' => 'Current page URL'
  84. )
  85. );
  86. }
  87. /**
  88. * replace_tags function.
  89. *
  90. * @access public
  91. * @param mixed $text
  92. * @return void
  93. */
  94. public function replace_tags( $text ) {
  95. if ( $this->user ) {
  96. $text = str_replace(
  97. array( '%username%', '%userid%' ),
  98. array( ucwords( $this->user->display_name ), $this->user->ID ),
  99. $text
  100. );
  101. // Buddypress
  102. if ( function_exists( 'bp_loggedin_user_domain' ) ) {
  103. $text = str_replace(
  104. array( '%buddypress_profile_url%' ),
  105. array( bp_loggedin_user_domain() ),
  106. $text
  107. );
  108. }
  109. // BBpress
  110. if ( function_exists( 'bbp_get_user_profile_url' ) ) {
  111. $text = str_replace(
  112. array( '%bbpress_profile_url%' ),
  113. array( bbp_get_user_profile_url( $this->user->ID ) ),
  114. $text
  115. );
  116. }
  117. }
  118. $logout_redirect = wp_logout_url( empty( $this->instance['logout_redirect_url'] ) ? $this->current_url( 'nologout' ) : $this->instance['logout_redirect_url'] );
  119. $text = str_replace(
  120. array( '%admin_url%', '%logout_url%' ),
  121. array( untrailingslashit( admin_url() ), apply_filters( 'sidebar_login_widget_logout_redirect', $logout_redirect ) ),
  122. $text
  123. );
  124. $text = do_shortcode( $text );
  125. return $text;
  126. }
  127. /**
  128. * show_links function.
  129. *
  130. * @access public
  131. * @param string $show (default: 'logged_in')
  132. * @return void
  133. */
  134. public function show_links( $show = 'logged_in', $links = array() ) {
  135. do_action( 'sidebar_login_widget_before_' . $show . '_links' );
  136. if ( ! is_array( $links ) ) {
  137. $raw_links = array_map( 'trim', explode( "\n", $links ) );
  138. $links = array();
  139. foreach ( $raw_links as $link ) {
  140. $link = array_map( 'trim', explode( '|', $link ) );
  141. $link_cap = '';
  142. if ( sizeof( $link ) == 3 )
  143. list( $link_text, $link_href, $link_cap ) = $link;
  144. elseif ( sizeof( $link ) == 2 )
  145. list( $link_text, $link_href ) = $link;
  146. else
  147. continue;
  148. // Check capability
  149. if ( ! empty( $link_cap ) )
  150. if ( ! current_user_can( strtolower( $link_cap ) ) )
  151. continue;
  152. $links[ sanitize_title( $link_text ) ] = array(
  153. 'text' => $link_text,
  154. 'href' => $link_href
  155. );
  156. }
  157. }
  158. if ( $show == 'logged_out' ) {
  159. if ( get_option('users_can_register') && ! empty( $this->instance['show_register_link'] ) && $this->instance['show_register_link'] == 1 ) {
  160. if ( ! is_multisite() ) {
  161. $links['register'] = array(
  162. 'text' => __( 'Register', 'sidebar_login' ),
  163. 'href' => apply_filters( 'sidebar_login_widget_register_url', site_url( 'wp-login.php?action=register', 'login' ) )
  164. );
  165. } else {
  166. $links['register'] = array(
  167. 'text' => __( 'Register', 'sidebar_login' ),
  168. 'href' => apply_filters( 'sidebar_login_widget_register_url', site_url('wp-signup.php', 'login') )
  169. );
  170. }
  171. }
  172. if ( ! empty( $this->instance['show_lost_password_link'] ) && $this->instance['show_lost_password_link'] == 1 ) {
  173. $links['lost_password'] = array(
  174. 'text' => __( 'Lost Password', 'sidebar_login' ),
  175. 'href' => apply_filters( 'sidebar_login_widget_lost_password_url', wp_lostpassword_url() )
  176. );
  177. }
  178. }
  179. $links = apply_filters( 'sidebar_login_widget_' . $show . '_links', $links );
  180. if ( ! empty( $links ) && is_array( $links ) && sizeof( $links > 0 ) ) {
  181. echo '<ul class="pagenav sidebar_login_links">';
  182. foreach ( $links as $id => $link )
  183. echo '<li class="' . esc_attr( $id ) . '-link"><a href="' . esc_url( $this->replace_tags( $link['href'] ) ) . '">' . wp_kses_post( $this->replace_tags( $link['text'] ) ) . '</a></li>';
  184. echo '</ul>';
  185. }
  186. do_action( 'sidebar_login_widget_after_' . $show . '_links' );
  187. }
  188. /**
  189. * widget function.
  190. *
  191. * @access public
  192. * @param mixed $args
  193. * @param mixed $instance
  194. * @return void
  195. */
  196. public function widget( $args, $instance ) {
  197. // Filter can be used to conditonally hide the widget
  198. if ( ! apply_filters( 'sidebar_login_widget_display', true ) )
  199. return;
  200. // Record $instance
  201. $this->instance = $instance;
  202. // Get user
  203. if ( is_user_logged_in() )
  204. $this->user = get_user_by( 'id', get_current_user_id() );
  205. $defaults = array(
  206. 'logged_in_title' => ! empty( $instance['logged_in_title'] ) ? $instance['logged_in_title'] : __( 'Welcome %username%', 'sidebar_login' ),
  207. 'logged_out_title' => ! empty( $instance['logged_out_title'] ) ? $instance['logged_out_title'] : __( 'Login', 'sidebar_login' ),
  208. 'show_avatar' => isset( $instance['show_avatar'] ) ? $instance['show_avatar'] : 1,
  209. 'logged_in_links' => ! empty( $instance['logged_in_links'] ) ? $instance['logged_in_links'] : array(),
  210. 'logged_out_links' => ! empty( $instance['logged_out_links'] ) ? $instance['logged_out_links'] : array()
  211. );
  212. $args = array_merge( $defaults, $args );
  213. extract( $args );
  214. echo $before_widget;
  215. do_action( 'sidebar_login_widget_start' );
  216. // Logged in user
  217. if ( is_user_logged_in() ) {
  218. $logged_in_title = $this->replace_tags( apply_filters( 'sidebar_login_widget_logged_in_title', $logged_in_title ) );
  219. if ( $logged_in_title )
  220. echo $before_title . $logged_in_title . $after_title;
  221. do_action( 'sidebar_login_widget_logged_in_content_start' );
  222. if ( $show_avatar == 1 )
  223. echo '<div class="avatar_container">' . get_avatar( $this->user->ID, apply_filters( 'sidebar_login_widget_avatar_size', 38 ) ) . '</div>';
  224. $this->show_links( 'logged_in', $logged_in_links );
  225. do_action( 'sidebar_login_widget_logged_in_content_end' );
  226. // Logged out user
  227. } else {
  228. $logged_out_title = $this->replace_tags( apply_filters( 'sidebar_login_widget_logged_out_title', $logged_out_title ) );
  229. if ( $logged_out_title )
  230. echo $before_title . $logged_out_title . $after_title;
  231. do_action( 'sidebar_login_widget_logged_out_content_start' );
  232. $redirect = empty( $instance['login_redirect_url'] ) ? $this->current_url( 'nologout' ) : $instance['login_redirect_url'];
  233. $login_form_args = apply_filters( 'sidebar_login_widget_form_args', array(
  234. 'echo' => true,
  235. 'redirect' => esc_url( apply_filters( 'sidebar_login_widget_login_redirect', $redirect ) ),
  236. 'label_username' => __( 'Username', 'sidebar_login' ),
  237. 'label_password' => __( 'Password', 'sidebar_login' ),
  238. 'label_remember' => __( 'Remember Me', 'sidebar_login' ),
  239. 'label_log_in' => __( 'Login &rarr;', 'sidebar_login' ),
  240. 'remember' => true,
  241. 'value_remember' => true
  242. ) );
  243. wp_login_form( $login_form_args );
  244. $this->show_links( 'logged_out', $logged_out_links );
  245. do_action( 'sidebar_login_widget_logged_out_content_end' );
  246. }
  247. do_action( 'sidebar_login_widget_end' );
  248. echo $after_widget;
  249. }
  250. /**
  251. * current_url function.
  252. *
  253. * @access public
  254. * @param string $url (default: '')
  255. * @return void
  256. */
  257. private function current_url( $url = '' ) {
  258. $pageURL = force_ssl_admin() ? 'https://' : 'http://';
  259. $pageURL .= esc_attr( $_SERVER['HTTP_HOST'] );
  260. $pageURL .= esc_attr( $_SERVER['REQUEST_URI'] );
  261. if ( $url != "nologout" ) {
  262. if ( ! strpos( $pageURL, '_login=' ) ) {
  263. $rand_string = md5( uniqid( rand(), true ) );
  264. $rand_string = substr( $rand_string, 0, 10 );
  265. $pageURL = add_query_arg( '_login', $rand_string, $pageURL );
  266. }
  267. }
  268. return esc_url_raw( $pageURL );
  269. }
  270. /**
  271. * update function.
  272. *
  273. * @see WP_Widget->update
  274. * @access public
  275. * @param array $new_instance
  276. * @param array $old_instance
  277. * @return array
  278. */
  279. function update( $new_instance, $old_instance ) {
  280. $this->define_options();
  281. foreach ( $this->options as $name => $option ) {
  282. if ( $option['type'] == 'break' )
  283. continue;
  284. $instance[ $name ] = strip_tags( stripslashes( $new_instance[ $name ] ) );
  285. }
  286. return $instance;
  287. }
  288. /**
  289. * form function.
  290. *
  291. * @see WP_Widget->form
  292. * @access public
  293. * @param array $instance
  294. * @return void
  295. */
  296. function form( $instance ) {
  297. $this->define_options();
  298. foreach ( $this->options as $name => $option ) {
  299. if ( $option['type'] == 'break' ) {
  300. echo '<hr style="border: 1px solid #ddd; margin: 1em 0" />';
  301. continue;
  302. }
  303. if ( ! isset( $instance[ $name ] ) )
  304. $instance[ $name ] = $option['default'];
  305. if ( empty( $option['placeholder'] ) )
  306. $option['placeholder'] = '';
  307. echo '<p>';
  308. switch ( $option['type'] ) {
  309. case "text" :
  310. ?>
  311. <label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"><?php echo wp_kses_post( $option['label'] ) ?>:</label>
  312. <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>" placeholder="<?php echo esc_attr( $option['placeholder'] ); ?>" value="<?php echo esc_attr( $instance[ $name ] ); ?>" />
  313. <?php
  314. break;
  315. case "checkbox" :
  316. ?>
  317. <label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>" <?php checked( $instance[ $name ], 1 ) ?> value="1" /> <?php echo wp_kses_post( $option['label'] ) ?></label>
  318. <?php
  319. break;
  320. case "textarea" :
  321. ?>
  322. <label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"><?php echo wp_kses_post( $option['label'] ) ?>:</label>
  323. <textarea class="widefat" cols="20" rows="3" id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>" placeholder="<?php echo esc_attr( $option['placeholder'] ); ?>"><?php echo esc_textarea( $instance[ $name ] ); ?></textarea>
  324. <?php
  325. break;
  326. }
  327. if ( ! empty( $option['description'] ) )
  328. echo '<span class="description" style="display:block; padding-top:.25em">' . wp_kses_post( $option['description'] ) . '</span>';
  329. echo '</p>';
  330. }
  331. }
  332. }
  333. register_widget( 'Sidebar_Login_Widget' );