PluginProbe
WP-Members Membership Plugin / trunk
WP-Members Membership Plugin vtrunk
trunk 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.4.2 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.9.1 3.4.9.2 3.4.9.3 3.4.9.4 3.4.9.5 3.4.9.6 3.4.9.7 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 All 34 releases
wp-members / includes / class-wp-members-widget.php

class-wp-members-widget.php in WP-Members Membership Plugin trunk, at includes/class-wp-members-widget.php

487 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for the sidebar login widget.
4 *
5 * @since 2.7
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit();
11 }
12
13 class widget_wpmemwidget extends WP_Widget {
14
15 /**
16 * Sets up the WP-Members login widget.
17 */
18 function __construct() {
19 parent::__construct(
20 'widget_wpmemwidget',
21 'WP-Members Login',
22 array(
23 'classname' => 'wp-members',
24 'description' => esc_html__( 'Displays the WP-Members sidebar login.', 'wp-members' ),
25 )
26 );
27 }
28
29 /**
30 * Displays the WP-Members login widget settings
31 * controls on the widget panel.
32 *
33 * @param array $instance
34 */
35 function form( $instance ) {
36
37 // Default widget settings.
38 $defaults = array(
39 'title' => esc_html__( 'Login Status', 'wp-members' ),
40 'redirect_to' => '',
41 );
42 $instance = wp_parse_args( ( array ) $instance, $defaults );
43
44 // Title input. ?>
45 <p>
46 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wp-members' ); ?></label>
47 <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
48 </p>
49 <p>
50 <label for="<?php echo esc_attr( $this->get_field_id( 'redirect_to' ) ); ?>"><?php _e( 'Redirect to (optional):', 'wp-members' ); ?></label>
51 <input id="<?php echo esc_attr( $this->get_field_id( 'redirect_to' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'redirect_to' ) ); ?>" value="<?php echo esc_attr( $instance['redirect_to'] ); ?>" style="width:95%;" />
52 </p>
53 <?php
54 }
55
56 /**
57 * Update the WP-Members login widget settings.
58 *
59 * @param array $new_instance
60 * @param array $old_instance
61 * @return array $instance
62 */
63 function update( $new_instance, $old_instance ) {
64 $instance = $old_instance;
65
66 // Strip tags for title to remove HTML.
67 $instance['title'] = strip_tags( $new_instance['title'] );
68 $instance['redirect_to'] = strip_tags( $new_instance['redirect_to'] );
69
70 return $instance;
71 }
72
73 /**
74 * Displays the WP-Members login widget.
75 *
76 * @param array $args
77 * @param array $instance
78 */
79 function widget( $args, $instance ) {
80
81 $redirect_to = ( array_key_exists( 'redirect_to', $instance ) ) ? $instance['redirect_to'] : '';
82 $title = ( array_key_exists( 'title', $instance ) ) ? $instance['title'] : esc_html__( 'Login Status', 'wp-members' );
83 $customizer = ( is_customize_preview() ) ? get_theme_mod( 'wpmem_show_logged_out_state', false ) : false;
84
85 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
86 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
87 /**
88 * Filter the widget title.
89 *
90 * @since Unknown
91 * @since 3.2.0 Added instance and id_base params.
92 *
93 * @param string $title The widget title.
94 * @param array $instance Array of settings for the current widget.
95 * @param mixed $id_base The widget ID.
96 */
97 $title = apply_filters( 'wpmem_widget_title', $title, $instance, $this->id_base );
98
99 /**
100 * Filter the widget ID.
101 *
102 * @since Unknown
103 * @since 3.2.0 Added instance and id_base params.
104 *
105 * @param string The ID for the sidebar widget.
106 * @param array $instance Array of settings for the current widget.
107 * @param mixed $id_base The widget ID.
108 */
109 $id = apply_filters( 'wpmem_widget_id', 'wp-members', $instance, $this->id_base );
110
111 echo $args['before_widget'];
112 echo '<div id="' . esc_attr( $id ) . '">';
113 echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
114 // The Widget
115 $this->do_sidebar( $redirect_to, $customizer );
116 echo '</div>';
117 echo $args['after_widget'];
118 }
119
120 /**
121 * Creates the sidebar login form and status.
122 *
123 * This function determines if the user is logged in and displays either
124 * a login form, or the user's login status. Typically used for a sidebar.
125 * You can call this directly, or with the widget.
126 *
127 * @since 2.4.0
128 * @since 3.0.0 Added $post_to argument.
129 * @since 3.1.0 Changed $post_to to $redirect_to.
130 * @since 3.2.0 Moved to widget_wpmemwidget class as do_sidebar().
131 * @since 3.4.0 Revise form for consitency with main body form.
132 *
133 * @param string $redirect_to A URL to redirect to upon login, default null.
134 * @param bool $customizer Whether to show the form for the customizer.
135 * @global string $wpmem_regchk
136 * @global string $user_login
137 */
138 function do_sidebar( $redirect_to = null, $customizer = false ) {
139
140 global $wpmem, $wpmem_regchk;
141
142 // Used here and in the logout.
143 $url = get_bloginfo('url');
144
145 if ( isset( $_REQUEST['redirect_to'] ) ) {
146 $post_to = $_REQUEST['redirect_to'];
147 } elseif ( is_home() || is_front_page() ) {
148 $post_to = $_SERVER['REQUEST_URI'];
149 } elseif ( is_single() || is_page() ) {
150 $post_to = get_permalink();
151 } elseif ( is_category() ) {
152 global $wp_query;
153 $cat_id = get_query_var( 'cat' );
154 $post_to = get_category_link( $cat_id );
155 } elseif ( is_search() ) {
156 $post_to = add_query_arg( 's', get_search_query(), $url );
157 } else {
158 $post_to = $_SERVER['REQUEST_URI'];
159 }
160
161 // Clean whatever the url is.
162 $esc_post_to = esc_url( $post_to );
163
164 if ( ! is_user_logged_in() || ( '1' == $customizer && is_customize_preview() ) ) {
165
166 // If the user is not logged in, we need the form.
167
168 // Defaults.
169 $defaults = array(
170 // Wrappers.
171 'error_before' => '<p class="err">',
172 'error_after' => '</p>',
173 'fieldset_before' => '<fieldset>',
174 'fieldset_after' => '</fieldset>',
175 'row_before' => '',
176 'row_after' => '',
177 'inputs_before' => '<div class="div_texbox">',
178 'inputs_after' => '</div>',
179 'buttons_before' => '<div class="button_div">',
180 'buttons_after' => '</div>',
181
182 // Messages.
183 'error_msg' => wpmem_get_text( 'widget_login_failed' ),
184 'status_msg' => wpmem_get_text( 'widget_not_logged_in' ) . '<br />',
185
186 'form_id' => 'wpmem_login_widget_form',
187 'form_class' => 'widget_form',
188 'button_id' => '',
189 'button_class' => 'buttons',
190
191 // Other.
192 'strip_breaks' => true,
193 'wrap_inputs' => true,
194 'n' => "\n",
195 't' => "\t",
196 'login_form_action' => true,
197 );
198
199 /**
200 * Filter arguments for the sidebar defaults.
201 *
202 * @since 2.9.0
203 * @deprecated 3.3.0 Use wpmem_login_widget_args instead.
204 *
205 * @param array An array of the defaults to be changed.
206 */
207 $args = apply_filters_deprecated( 'wpmem_sb_login_args', array(''), '3.3.0', 'wpmem_login_widget_args' );
208
209 // Merge $args with defaults.
210 $args = wp_parse_args( $args, $defaults );
211
212 /**
213 * Filter the defaults for the login widget.
214 *
215 * @since 3.3.0
216 *
217 * @param array And array of the defaults to be changed.
218 */
219 $args = apply_filters( 'wpmem_login_widget_args', $args );
220
221 $inputs = array(
222 array(
223 'name' => wpmem_get_text( 'widget_login_username' ),
224 'type' => 'text',
225 'tag' => 'log',
226 'class' => 'username',
227 'div' => 'div_text'
228 ),
229 array(
230 'name' => wpmem_get_text( 'widget_login_password' ),
231 'type' => 'password',
232 'tag' => 'pwd',
233 'class' => 'password',
234 'div' => 'div_text',
235 ),
236 );
237
238 // Build the input rows.
239 foreach ( $inputs as $input ) {
240 $label = '<label for="' . esc_attr( $input['tag'] ) . '">' . esc_html( $input['name'] ) . '</label>';
241 $field = wpmem_form_field( array(
242 'name' => $input['tag'],
243 'type' => $input['type'],
244 'class' => $input['class'],
245 'required' => true,
246 ) );
247 $field_before = ( $args['wrap_inputs'] ) ? '<div class="' . wpmem_sanitize_class( $input['div'] ) . '">' : '';
248 $field_after = ( $args['wrap_inputs'] ) ? '</div>' : '';
249 $rows[] = array(
250 'row_before' => $args['row_before'],
251 'label' => $label,
252 'field_before' => $field_before,
253 'field' => $field,
254 'field_after' => $field_after,
255 'row_after' => $args['row_after'],
256 );
257 }
258
259 /**
260 * Filter the array of form rows. Works like wpmem_login_form_rows.
261 *
262 * @since 3.4.0
263 *
264 * @param array $rows An array containing the form rows.
265 */
266 $rows = apply_filters( 'wpmem_login_widget_form_rows', $rows );
267
268 // Put the rows from the array into $form.
269 $form = '';
270 foreach ( $rows as $row_item ) {
271 $row = ( $row_item['row_before'] != '' ) ? $row_item['row_before'] . $args['n'] . $row_item['label'] . $args['n'] : $row_item['label'] . $args['n'];
272 $row .= ( $row_item['field_before'] != '' ) ? $row_item['field_before'] . $args['n'] . $args['t'] . $row_item['field'] . $args['n'] . $row_item['field_after'] . $args['n'] : $row_item['field'] . $args['n'];
273 $row .= ( $row_item['row_after'] != '' ) ? $row_item['row_after'] . $args['n'] : '';
274 $form.= $row;
275 }
276
277 // Handle outside elements added to the login form (currently ONLY for login).
278 if ( $args['login_form_action'] ) {
279 ob_start();
280 do_action( 'login_form' );
281 $add_to_form = ob_get_contents();
282 ob_end_clean();
283 $form.= $add_to_form;
284 }
285
286 $hidden = '<input type="hidden" name="rememberme" value="forever" />' . $args['n'] .
287 '<input type="hidden" name="redirect_to" value="' . ( ( $redirect_to ) ? esc_url( $redirect_to ) : $esc_post_to ) . '" />' . $args['n'] .
288 '<input type="hidden" name="a" value="login" />' . $args['n'] .
289 '<input type="hidden" name="slog" value="true" />';
290 /**
291 * Filter sidebar login form hidden fields.
292 *
293 * @since 2.9.0
294 * @deprecated 3.4.0 Use wpmem_login_widget_hidden_fields instead.
295 *
296 * @param string $hidden The HTML for the hidden fields.
297 */
298 $hidden = apply_filters_deprecated( 'wpmem_sb_hidden_fields', array( $hidden ), '3.4.0', 'wpmem_login_widget_hidden_fields' );
299 /**
300 * Filter sidebar login form hidden fields.
301 *
302 * @since 3.4.0
303 *
304 * @param string $hidden The HTML for the hidden fields.
305 */
306 $form = $form . apply_filters( 'wpmem_login_widget_hidden_fields', $hidden );
307
308 $buttons = '<input type="submit" name="Submit" class="' . wpmem_sanitize_class( $args['button_class'] ) . '" value="' . wpmem_get_text( 'widget_login_button' ) . '" />';
309
310 if ( $wpmem->user_pages['profile'] != null ) {
311 /** This filter is documented in wp-members/includes/class-wp-members-forms.php */
312 $link = apply_filters( 'wpmem_forgot_link', wpmem_profile_url( 'pwdreset' ) );
313 $link_html = ' <a href="' . esc_url_raw( $link ) . '">' . wpmem_get_text( 'widget_login_forgot' ) . '</a>&nbsp;';
314 /**
315 * Filter the sidebar forgot password.
316 *
317 * @since 3.0.9
318 * @deprecated 3.4.0 Use wpmem_login_widget_forgot_link_str instead.
319 *
320 * @param string $link_html
321 * @param string $link
322 */
323 $link_html = apply_filters_deprecated( 'wpmem_sb_forgot_link_str', array( $link_html, $link ), '3.4.0', 'wpmem_login_widget_forgot_link_str' );
324 /**
325 * Filter the sidebar forgot password.
326 *
327 * @since 3.4.0
328 *
329 * @param string $link_html
330 * @param string $link
331 */
332 $link_html = apply_filters( 'wpmem_login_widget_forgot_link_str', $link_html, $link );
333 $buttons.= $link_html;
334 }
335
336 if ( $wpmem->user_pages['register'] != null ) {
337 /** This filter is documented in wp-members/includes/class-wp-members-forms.php */
338 $link = apply_filters( 'wpmem_reg_link', $wpmem->user_pages['register'] );
339 $link_html = ' <a href="' . esc_url_raw( $link ) . '">' . wpmem_get_text( 'widget_login_register' ) . '</a>';
340 /**
341 * Filter the sidebar register link.
342 *
343 * @since 3.0.9
344 * @deprecated 3.4.0 Use wpmem_login_widget_reg_link_str instead.
345 *
346 * @param string $link_html
347 * @param string $link
348 */
349 $link_html = apply_filters_deprecated( 'wpmem_sb_reg_link_str', array( $link_html, $link ), '3.4.0', 'wpmem_login_widget_reg_link_str' );
350 /**
351 * Filter the sidebar register link.
352 *
353 * @since 3.4.0
354 *
355 * @param string $link_html
356 * @param string $link
357 */
358 $link_html = apply_filters( 'wpmem_login_widget_reg_link_str', $link_html, $link );
359 $buttons.= $link_html;
360 }
361
362 $form = $form . $args['n'] . $args['buttons_before'] . $buttons . $args['n'] . $args['buttons_after'];
363
364 $form = $args['fieldset_before'] . $args['n'] . $form . $args['n'] . $args['fieldset_after'];
365
366 $form = '<form name="form" method="post" action="' . $esc_post_to . '" id="' . wpmem_sanitize_class( $args['form_id'] ) . '" class="' . wpmem_sanitize_class( $args['form_class'] ) . '">' . $args['n'] . $form . $args['n'] . '</form>';
367
368 // Add status message, if one exists.
369 if ( '' == $args['status_msg'] ) {
370 $args['status_msg'] . $args['n'] . $form;
371 }
372
373 // Strip breaks.
374 $form = ( $args['strip_breaks'] ) ? str_replace( array( "\n", "\r", "\t" ), array( '','','' ), $form ) : $form;
375
376 /**
377 * Filter the sidebar form.
378 *
379 * @since unknown
380 * @deprecated 3.3.9 Use wpmem_login_widget_form instead.
381 *
382 * @param string $form The HTML for the sidebar login form.
383 */
384 $form = apply_filters_deprecated( 'wpmem_sidebar_form', array( $form ), '3.3.9', 'wpmem_login_widget_form' );
385 /**
386 * Filter the sidebar form.
387 *
388 * @since 3.3.9
389 *
390 * @param string $form The HTML for the sidebar login form.
391 */
392 $form = apply_filters( 'wpmem_login_widget_form', $form );
393
394 $do_error_msg = '';
395 $error_msg = $args['error_before'] . $args['error_msg'] . $args['error_after'];
396
397 if ( isset( $_POST['slog'] ) && $wpmem_regchk == 'loginfailed' ) {
398 $do_error_msg = true;
399 } elseif( is_customize_preview() && get_theme_mod( 'wpmem_show_form_message_dialog', false ) ) {
400 $do_error_msg = true;
401 }
402
403 if ( $do_error_msg ) {
404 /**
405 * Filter the sidebar login failed message.
406 *
407 * @since unknown
408 * @deprecated 3.4.0 Use wpmem_login_widget_login_failed instead.
409 *
410 * @param string $error_msg The error message.
411 */
412 $error_msg = apply_filters_deprecated( 'wpmem_login_failed_sb', array( $error_msg ), '3.4.0', 'wpmem_login_widget_login_failed' );
413 /**
414 * Filter the sidebar login failed message.
415 *
416 * @since 3.4.0
417 *
418 * @param string $error_msg The error message.
419 */
420 $error_msg = apply_filters( 'wpmem_login_widget_login_failed', $error_msg );
421 $form = $error_msg . $form;
422 }
423
424 echo $form;
425
426 } else {
427
428 global $user_login;
429
430 // Defaults.
431 $defaults = array(
432 'user_login' => $user_login,
433 'wrapper_before' => '<p class="login_widget_status">',
434 'status_text' => sprintf( wpmem_get_text( 'widget_status' ), $user_login ) . '<br />',
435 'link_text' => wpmem_get_text( 'widget_logout' ),
436 'wrapper_after' => '</p>',
437 );
438
439 /**
440 * Filter sidebar login status arguments.
441 *
442 * @since 3.1.0
443 * @since 3.1.2 Pass default args.
444 * @deprecated 3.4.0 Use wpmem_login_widget_status_args instead.
445 *
446 * @param array $defaults
447 * @return array
448 */
449 $args = apply_filters_deprecated( 'wpmem_sidebar_status_args', array( $defaults ), '3.4.0', 'wpmem_login_widget_status_args' );
450 /**
451 * Filter sidebar login status arguments.
452 *
453 * @since 3.4.0
454 *
455 * @param array $defaults
456 * @return array
457 */
458 $args = apply_filters( 'wpmem_login_widget_status_args', $defaults );
459
460 // Merge $args with $defaults.
461 $args = wp_parse_args( $args, $defaults );
462
463 // Generate the message string.
464 $str = $args['wrapper_before'] . $args['status_text'] . '<a href="' . esc_url_raw( wpmem_logout_link() ) . '">' . esc_html( $args['link_text'] ) . '</a>' . $args['wrapper_after'];
465
466 /**
467 * Filter the sidebar user login status.
468 *
469 * @since unknown
470 * @deprecated 3.4.0 Use wpmem_login_widget_status instead.
471 *
472 * @param string $str The login status for the user.
473 */
474 $str = apply_filters_deprecated( 'wpmem_sidebar_status', array( $str ), '3.4.0', 'wpmem_login_widget_status' );
475 /**
476 * Filter the sidebar user login status.
477 *
478 * @since 3.4.0
479 *
480 * @param string $str The login status for the user.
481 */
482 $str = apply_filters( 'wpmem_login_widget_status', $str );
483
484 echo $str;
485 }
486 }
487 }