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 / api / api.php

api.php in WP-Members Membership Plugin trunk, at includes/api/api.php

790 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-Members API Functions
4 *
5 * This file is part of the WP-Members plugin by Chad Butler
6 * You can find out more about this plugin at https://rocketgeek.com
7 * Copyright (c) 2006-2026 Chad Butler
8 * WP-Members(tm) is a trademark of butlerblog.com
9 *
10 * @package WP-Members
11 * @subpackage WP-Members API Functions
12 * @author Chad Butler
13 * @copyright 2006-2026
14 */
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit();
19 }
20
21 /**
22 * Redirects a user to defined login page with return redirect.
23 *
24 * While a specific URL can be passed as an argument, the default will
25 * redirect the user back to the original page using wpmem_current_url().
26 *
27 * @since 3.0.2
28 * @since 3.1.1 Moved to API.
29 * @since 3.1.3 Added $redirect_to argument.
30 *
31 * @param string $redirect_to URL to redirect to (default: false).
32 */
33 function wpmem_redirect_to_login( $redirect_to = false ) {
34 if ( ! is_user_logged_in() ) {
35 $redirect_to = ( $redirect_to ) ? $redirect_to : wpmem_current_url();
36 wp_safe_redirect( wpmem_login_url( esc_url_raw( $redirect_to ) ) );
37 exit();
38 }
39 return;
40 }
41
42 /**
43 * Checks if content is blocked (replaces wpmem_block()).
44 *
45 * @since 3.1.1
46 * @since 3.3.0 Added $post_id
47 *
48 * @global object $wpmem The WP-Members object class.
49 * @param int $post_id
50 * @return bool $block True if content is blocked, otherwise false.
51 */
52 function wpmem_is_blocked( $post_id = false ) {
53 global $wpmem;
54 return $wpmem->is_blocked( $post_id );
55 }
56
57 /**
58 * Checks if specific post is marked as hidden.
59 *
60 * @since 3.3.2
61 *
62 * @param int $post_id
63 * @return bool $block True if content is hidden, otherwise false.
64 */
65 function wpmem_is_hidden( $post_id = false ) {
66 return ( 2 == get_post_meta( $post_id, '_wpmem_block', true ) ) ? true : false;
67 }
68
69 /**
70 * Returns the block setting for a post.
71 *
72 * @since 3.3.0
73 *
74 * @global object $wpmem
75 *
76 * @param int $post_id
77 * @return int $block_value
78 */
79 function wpmem_get_block_setting( $post_id ) {
80 return get_post_meta( $post_id, '_wpmem_block', true );
81 }
82
83 /**
84 * Gets a link to the login page with a return link redirect_to value.
85 *
86 * @since 3.4.6
87 *
88 * @param array $args {
89 * Additional attributes to be merged with defaults.
90 *
91 * @type string $tag The HTML tag to be used (default: a)
92 * @type array $attributes {
93 * Any valid attributes for the HTML <a> tag.
94 *
95 * @type string $id
96 * @type string $class
97 * @type string $href
98 * }
99 * @type string $content The text used with the <a href> tag
100 * }
101 */
102 function wpmem_get_login_link( $args = array() ) {
103 $defaults = array(
104 'tag' => 'a',
105 'attributes' => array(
106 'id' => 'wpmem_login_link',
107 'class' => 'wpmem-login-link',
108 'href' => wpmem_login_url( wpmem_current_url() ),
109 ),
110 'content' => esc_html__( 'Log In' )
111 );
112 $args = rktgk_wp_parse_args( $args, $defaults );
113 return rktgk_build_html_tag( $args );
114 }
115
116 /**
117 * Echos a login link using wpmem_get_login_link().
118 *
119 * @since 3.5.6
120 */
121 function wpmem_login_link( $args = array() ) {
122 echo wpmem_get_login_link( $args );
123 }
124
125 /**
126 * Gets a link to the register page with a return link redirect_to value.
127 *
128 * @since 3.4.6
129 *
130 * @param array $args {
131 * Additional attributes to be merged with defaults.
132 *
133 * @type string $tag The HTML tag to be used (default: a)
134 * @type array $attributes {
135 * Any valid attributes for the HTML <a> tag.
136 *
137 * @type string $id
138 * @type string $class
139 * @type string $href
140 * }
141 * @type string $content The text used with the <a href> tag
142 * }
143 */
144 function wpmem_get_reg_link( $args = array() ) {
145 $defaults = array(
146 'tag' => 'a',
147 'attributes' => array(
148 'id' => 'wpmem_reg_link',
149 'class' => 'wpmem-reg-link',
150 'href' => add_query_arg( 'redirect_to', wpmem_current_url(), wpmem_register_url() ),
151 ),
152 'content' => esc_html__( 'Register' )
153 );
154 $args = rktgk_wp_parse_args( $args, $defaults );
155 return rktgk_build_html_tag( $args );
156 }
157
158 /**
159 * Echos a login link using wpmem_get_reg_link().
160 *
161 * @since 3.5.6
162 */
163 function wpmem_reg_link( $args = array() ) {
164 echo wpmem_get_reg_link( $args );
165 }
166
167 /**
168 * Wrapper to get the login page location.
169 *
170 * @since 3.1.1
171 * @since 3.1.2 Added redirect_to parameter.
172 * @since 3.4.0 If no login page is set, return the wp_login_url().
173 *
174 * @global object $wpmem The WP_Members object.
175 * @param string $redirect_to URL to return to (optional).
176 * @return string $url The login page url.
177 */
178 function wpmem_login_url( $redirect_to = false ) {
179 global $wpmem;
180 // If no login page is set, get WP login url.
181 $login_url = ( isset( $wpmem->user_pages['login'] ) ) ? $wpmem->user_pages['login'] : wp_login_url();
182 /**
183 * Filter the login url.
184 *
185 * @since 3.5.0
186 *
187 * @param string $login_url
188 */
189 $login_url = apply_filters( 'wpmem_login_url', $login_url );
190 if ( $redirect_to ) {
191 $url = add_query_arg( 'redirect_to', urlencode( $redirect_to ), $login_url );
192 } else {
193 $url = $login_url;
194 }
195 return esc_url_raw( $url );
196 }
197
198 /**
199 * Wrapper to get the register page location.
200 *
201 * @since 3.1.1
202 *
203 * @global object $wpmem The WP_Members object.
204 * @return string The register page url.
205 */
206 function wpmem_register_url() {
207 global $wpmem;
208 $reg_url = $wpmem->user_pages['register'];
209 /**
210 * Filter the register url.
211 *
212 * @since 3.5.0
213 *
214 * @param string $reg_url
215 */
216 return esc_url_raw( apply_filters( 'wpmem_register_url', $reg_url ) );
217 }
218
219 /**
220 * Wrapper to get the profile page location.
221 *
222 * @since 3.1.1
223 * @since 3.1.2 Added $a parameter.
224 *
225 * @global object $wpmem The WP_Members object.
226 * @param string $a Action (optional).
227 * @return string The profile page url.
228 */
229 function wpmem_profile_url( $a = false ) {
230 global $wpmem;
231 return ( $a ) ? add_query_arg( 'a', esc_attr( $a ), trailingslashit( $wpmem->user_pages['profile'] ) ) : $wpmem->user_pages['profile'];
232 }
233
234 /**
235 * Alias of wpmem_profile_url() to return the password reset URL.
236 *
237 * @since 3.4.5
238 *
239 * @return string The password reset url.
240 */
241 function wpmem_pwd_reset_url() {
242 return wpmem_profile_url( 'pwdreset' );
243 }
244
245 /**
246 * Alias of wpmem_profile_url() to return the forgot username URL.
247 *
248 * @since 3.4.5
249 *
250 * @return string The forgot username url.
251 */
252 function wpmem_forgot_username_url() {
253 return wpmem_profile_url( 'getusername' );
254 }
255
256 /**
257 * Alias of wpmem_profile_url() to return the forgot username URL.
258 *
259 * @since 3.5.0
260 *
261 * @return string The resend confirmation url.
262 */
263 function wpmem_reconfirm_url() {
264 return wpmem_profile_url( 'reconfirm' );
265 }
266
267 /**
268 * Returns an array of user pages.
269 *
270 * @since 3.1.2
271 * @since 3.1.3 Added array keys.
272 *
273 * @return array $pages {
274 * The URLs of login, register, and user profile pages.
275 *
276 * @type string $login
277 * @type string $register
278 * @type string $profile
279 * }
280 */
281 function wpmem_user_pages() {
282 $pages = array(
283 'login' => trailingslashit( wpmem_login_url() ),
284 'register' => trailingslashit( wpmem_register_url() ),
285 'profile' => trailingslashit( wpmem_profile_url() ),
286 );
287 return $pages;
288 }
289
290 /**
291 * Returns the current full url.
292 *
293 * @since 3.1.1
294 * @since 3.1.7 Added check for query string.
295 *
296 * @global object $wp
297 * @param boolean $slash Trailing slash the end of the url (default:true).
298 * @param boolean $getq Toggles getting the query string (default:true).
299 * @return string $url The current page full url path.
300 */
301 function wpmem_current_url( $slash = true, $getq = true ) {
302 global $wp;
303 $url = home_url( add_query_arg( array(), $wp->request ) );
304 $url = ( $slash ) ? trailingslashit( $url ) : $url;
305 $url = ( $getq && count( $_GET ) > 0 ) ? $url . '?' . $_SERVER['QUERY_STRING'] : $url;
306 return esc_url_raw( $url );
307 }
308
309 /**
310 * Checks if the current page is the login page.
311 *
312 * @since 3.5.0
313 *
314 * @return boolean
315 */
316 function wpmem_is_login() {
317 return ( wpmem_login_url() == wpmem_current_url( true, false ) ) ? true : false;
318 }
319
320 /**
321 * Checks if the current page is the register page.
322 *
323 * @since 3.5.0
324 *
325 * @return boolean
326 */
327 function wpmem_is_register() {
328 return ( wpmem_register_url() == wpmem_current_url( true, false ) ) ? true : false;
329 }
330
331 /**
332 * Checks if the current page is the user profile page.
333 *
334 * @since 3.5.0
335 *
336 * @return boolean
337 */
338 function wpmem_is_profile() {
339 return ( wpmem_profile_url() == wpmem_current_url( true, false ) ) ? true : false;
340 }
341
342 /**
343 * Gets post ID of current URL.
344 *
345 * @since 3.1.7
346 *
347 * @return int Post ID.
348 */
349 function wpmem_current_post_id() {
350 return url_to_postid( wpmem_current_url() );
351 }
352
353 /**
354 * Returns or displays the user's login status.
355 *
356 * @since 2.0.0
357 * @since 3.1.2 Moved to api.php, no longer pluggable.
358 * @since 3.1.6 Dependencies now loaded by object.
359 * @since 3.4.0 Added $tag for id'ing useage, to be passed through filter.
360 *
361 * @global string $user_login
362 * @param boolean $echo Determines whether function should print result or not (default: true).
363 * @return string $status The user status string produced by wpmem_inc_memberlinks().
364 */
365 function wpmem_login_status( $echo = true, $tag = false ) {
366 if ( is_user_logged_in() ) {
367
368 global $user_login;
369
370 $args = array(
371 'wrapper_before' => '<p>',
372 'wrapper_after' => '</p>',
373 'user_login' => $user_login,
374 'welcome' => wpmem_get_text( 'status_welcome' ),
375 'logout_text' => wpmem_get_text( 'status_logout' ),
376 'logout_link' => '<a href="' . esc_url( wpmem_logout_link() ) . '">%s</a>',
377 'separator' => ' | ',
378 );
379 /**
380 * Filter the status message parts.
381 *
382 * @since 2.9.9
383 * @since 3.4.0 Added $tag as a parameter (most often will be false).
384 *
385 * @param array $args {
386 * The components of the links.
387 *
388 * @type string $wrapper_before The wrapper opening tag (default: <p>).
389 * @type string $wrapper_after The wrapper closing tag (default: </p>).
390 * @type string $user_login
391 * @type string $welcome
392 * @type string $logout_text
393 * @type string $logout_link
394 * @type string $separator
395 * }
396 * @param string $tag
397 */
398 $args = apply_filters( 'wpmem_status_msg_args', $args, $tag );
399
400 // Assemble the message string.
401 $status = $args['wrapper_before']
402 . sprintf( $args['welcome'], $args['user_login'] )
403 . $args['separator']
404 . sprintf( $args['logout_link'], $args['logout_text'] )
405 . $args['wrapper_after'];
406 }
407
408 if ( $echo ) {
409 echo $status;
410 } else {
411 return $status;
412 }
413 }
414
415 /**
416 * Utility function to validate $_POST, $_GET, and $_REQUEST.
417 *
418 * While this function retrieves data, remember that the data should generally be
419 * sanitized or escaped depending on how it is used.
420 *
421 * @since 3.1.3
422 * @since 3.4.0 Now an alias for rktgk_get().
423 *
424 * @param string $tag The form field or query string.
425 * @param string $default The default value (optional).
426 * @param string $type post|get|request (optional).
427 * @return string
428 */
429 function wpmem_get( $tag, $default = '', $type = 'post' ) {
430 return rktgk_get( $tag, $default, $type );
431 }
432
433 /**
434 * Utility function to check if $_POST, $_GET, or $_REQUEST is valid, and sanitizes the result.
435 *
436 * @since 3.5.0
437 *
438 * @param string $tag Form field or query string param.
439 * @param string $default Default value (optional, default: null).
440 * @param string $request_type Request type (post|get|request) (optional, default:post).
441 * @param string $field_type Type of sanitization (using rktgk_sanitize_field()) (optional, default:text).
442 * @return mixed The sanitized result (string|array|integer|boolean).
443 */
444 function wpmem_get_sanitized( $tag, $default = '', $request_type = 'post', $field_type = 'text' ) {
445 return rktgk_get_sanitized( $tag, $default, $request_type, $field_type );
446 }
447
448 /**
449 * Compares wpmem_reg_page value with the register page URL.
450 *
451 * @since 3.1.4
452 * @since 3.1.7 Added default of current page ID.
453 *
454 * @param string|int $check_page
455 * @return bool
456 */
457 function wpmem_is_reg_page( $check = false ) {
458 if ( ! $check ) {
459 $check = get_the_ID();
460 } else {
461 if ( ! is_int( $check ) ) {
462 global $wpdb;
463 $sql = 'SELECT ID FROM $wpdb->posts WHERE post_name = "' . esc_sql( $check ) . '" AND post_status = "publish" LIMIT 1';
464 $arr = $wpdb->get_results( $sql, ARRAY_A );
465 $check = $arr[0]['ID'];
466 }
467 }
468 $reg_page = wpmem_get( 'wpmem_reg_page' );
469 $check_page = get_permalink( $check );
470 return ( $check_page == $reg_page ) ? true : false;
471 }
472
473 /**
474 * Creates a login/logout link.
475 *
476 * @since 3.1.6
477 *
478 * @param array $args {
479 * Array of arguments to customize output.
480 *
481 * @type string $login_redirect_to The url to redirect to after login (optional).
482 * @type string $logout_redirect_to The url to redirect to after logout (optional).
483 * @type string $login_text Text for the login link (optional).
484 * @type string $logout_text Text for the logout link (optional).
485 * }
486 * @param boolean $echo (default: false)
487 * @return string $link
488 */
489 function wpmem_loginout( $args = array(), $echo = false ) {
490 global $wpmem;
491 return $wpmem->loginout_args( $args );
492 }
493
494 /**
495 * Returns a URL to log a user out.
496 *
497 * @since 3.4.0
498 *
499 * @return string Logout link.
500 */
501 function wpmem_logout_link() {
502 /**
503 * Filter the log out link.
504 *
505 * @since 2.8.3
506 *
507 * @param string The default logout link.
508 */
509 return apply_filters( 'wpmem_logout_link', add_query_arg( 'a', 'logout' ) );
510 }
511
512 /**
513 * Wrapper to return a string from the get_text function.
514 *
515 * @since 3.4.0
516 *
517 * @global object $wpmem The WP_Members object.
518 * @param string $str The string to retrieve.
519 * @param bool $echo Print the string (default: false).
520 * @return string $str The localized string.
521 */
522 function wpmem_get_text( $str, $echo = false ) {
523 global $wpmem;
524 if ( $echo ) {
525 echo $wpmem->dialogs->get_text( $str );
526 } else {
527 return $wpmem->dialogs->get_text( $str );
528 }
529 }
530
531 /**
532 * Gets requested dialog.
533 *
534 * @since 3.4.0
535 *
536 * @note It is being relased now as tentative.
537 * There may be some changes to how this is applied.
538 *
539 * @todo What about wpmem_use_custom_dialog()?
540 *
541 * @global stdClass $wpmem
542 * @param string $tag
543 * @param string $custom
544 * @return
545 */
546 function wpmem_get_display_message( $tag, $custom = false ) {
547 global $wpmem;
548 return $wpmem->dialogs->get_message( $tag, $custom );
549 }
550
551 /**
552 * Dispalays requested dialog.
553 *
554 * @note It is being relased now as tentative.
555 * There may be some changes to how this is applied.
556 *
557 * @since 3.2.0
558 * @since 3.4.0 Now echos the message. Added $custom argument
559 *
560 * @todo What about wpmem_use_custom_dialog()?
561 *
562 * @global stdClass $wpmem
563 * @param string $tag
564 * @param string $custom
565 * @return
566 */
567 function wpmem_display_message( $tag, $custom = false ) {
568 echo wpmem_get_display_message( $tag, $custom );
569 }
570
571 /**
572 * Wrapper to use custom dialog.
573 *
574 * @since 3.1.1
575 *
576 * @param array $defaults Dialog message defaults from the wpmem_msg_defaults filter.
577 * @param string $tag The dialog tag/name.
578 * @param array $dialogs The dialog settings array (passed through filter).
579 * @return array $dialogs The dialog settings array (filtered).
580 */
581 function wpmem_use_custom_dialog( $defaults, $tag, $dialogs ) {
582 $msg_string = ( is_array( $dialogs[ $tag ] ) ) ? $dialogs[ $tag ]['value'] : $dialogs[ $tag ];
583 $defaults['msg'] = esc_html__( $msg_string, 'wp-members' );
584 return $defaults;
585 }
586
587 /**
588 * Wrapper function for adding custom dialogs.
589 *
590 * @since 3.1.1
591 * @since 3.3.0 Moved to main API.
592 *
593 * @param array $dialogs Dialog settings array.
594 * @param string $tag Slug for dialog to be added.
595 * @param string $msg The dialog message.
596 * @param string $label Label for admin panel.
597 * @return array $dialogs Dialog settings array with prepped custom dialog added.
598 */
599 function wpmem_add_custom_dialog( $dialogs, $tag, $msg, $label ) {
600 $msg = ( ! isset( $dialogs[ $tag ] ) ) ? $msg : $dialogs[ $tag ];
601 $dialogs[ $tag ] = array(
602 'name' => $tag,
603 'label' => $label,
604 'value' => $msg,
605 );
606 return $dialogs;
607 }
608
609 /**
610 * Gets an array of hidden post IDs.
611 *
612 * @since 3.3.1
613 *
614 * @global stdClass $wpmem
615 * @return array
616 */
617 function wpmem_get_hidden_posts() {
618 global $wpmem;
619 return $wpmem->get_hidden_posts();
620 }
621
622 /**
623 * Updates the hiddent posts array.
624 *
625 * @since 3.3.5
626 *
627 * @global stdClass $wpmem
628 */
629 function wpmem_update_hidden_posts() {
630 global $wpmem;
631 $wpmem->update_hidden_posts();
632 }
633
634 /**
635 * Conditional if REST request.
636 *
637 * @since 3.3.2
638 *
639 * @global stdClass $wpmem
640 * @return boolean
641 */
642 function wpmem_is_rest() {
643 global $wpmem;
644 return $wpmem->is_rest;
645 }
646
647 /**
648 * Gets registration type.
649 *
650 * @since 3.3.5
651 * @since 3.5.1 Checks is_reg_type().
652 *
653 * @global stdClass $wpmem
654 * @param string $type (wpmem|native|add_new|woo|woo_checkout)
655 * @return boolean
656 */
657 function wpmem_is_reg_type( $type ) {
658 global $wpmem;
659 return $wpmem->user->is_reg_type( $type );
660 }
661
662 /**
663 * Displays the post restricted message.
664 *
665 * @since 3.4.0
666 *
667 * @return string
668 */
669 function wpmem_restricted_message() {
670 global $wpmem;
671 return $wpmem->forms->add_restricted_msg();
672 }
673
674 /**
675 * Gets plugin url.
676 *
677 * @since 3.4.1
678 *
679 * @global stdClass $wpmem
680 * @return string $wpmem->url
681 */
682 function wpmem_get_plugin_url() {
683 global $wpmem;
684 return $wpmem->url;
685 }
686
687 /**
688 * Gets plugin version.
689 *
690 * @since 3.4.1
691 *
692 * @global stdClass $wpmem
693 * @return string $wpmem->version
694 */
695 function wpmem_get_plugin_version() {
696 global $wpmem;
697 return $wpmem->version;
698 }
699
700 /**
701 * Checks whether a WooCommerce product is purchasable.
702 *
703 * If a product is blocked or requires a membership, this function
704 * will return false if the user lacks the wpmem criteria to access.
705 *
706 * @since 3.4.7
707 *
708 * @param boolean $is_purchasable
709 * @param object $product The WooCommerce product object.
710 */
711 function wpmem_woo_is_purchasable( $is_purchasable, $product ) {
712
713 // Is the product restricted?
714 if ( wpmem_is_blocked( $product->get_id() ) ) {
715
716 // If it's restricted and the user is not logged in, return false (not purchasable by this user).
717 if ( ! is_user_logged_in() ) {
718 return false;
719 }
720
721 // Does the product have a membership requirement?
722 $membership_required = wpmem_get_post_memberships( $product->get_id() );
723 // If not, it's purchasable.
724 if ( false == $membership_required ) {
725 return true;
726 } else {
727 // If a membership is required, does the user have access? If not, return false (not purchasable by this user).
728 if ( ! wpmem_user_has_access( $membership_required ) ) {
729 return false;
730 }
731 }
732 }
733
734 return true;
735 }
736
737 /**
738 * Display an icon or text indicating the restriction value of a post for logged out users.
739 *
740 * @note Make sure dashicons are loaded if $icon is true.
741 *
742 * @since 3.5.0
743 *
744 * @param int $post_id
745 * @param bool $icon
746 * @param bool $echo
747 * @return bool
748 */
749 function wpmem_show_post_restriction( $post_id, $icon = false, $echo = false ) {
750
751 $defaults = array(
752 'open_icon' => '<span class="dashicons dashicons-lock"></span>',
753 'open_text' => '[ members only ]',
754 'restricted_icon' => '<span class="dashicons dashicons-unlock"></span>',
755 'restricted_text' => '[ free ]',
756 );
757 $filtered = apply_filters( 'wpmem_show_post_restriction_args', $defaults, $post_id );
758 $args = wp_parse_args( $filtered, $defaults );
759
760 if ( ! is_user_logged_in() ) {
761
762 if ( wpmem_is_blocked( $post_id ) ) {
763
764 $result = ( $icon ) ? $args['restricted_icon'] : $args['restricted_text'];
765
766 } else {
767
768 $result = ( $icon ) ? $args['open_icon'] : $args['open_text'];
769
770 }
771
772 if ( $echo ) {
773 echo $result;
774 } else {
775 return $result;
776 }
777 }
778 }
779
780 /**
781 * Checks if the WP-Members PayPal extension is activated.
782 *
783 * @since 3.5.5
784 *
785 * @return boolean
786 */
787 function wpmem_is_exp_enabled() {
788 return ( function_exists( 'wpmem_exp_init' ) || function_exists( 'wpmem_set_exp' ) ) ? true : false;
789 }
790 // End of file.