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

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

251 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-Members Admin 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 * @author Chad Butler
12 * @copyright 2006-2026
13 *
14 * Functions included:
15 * - wpmem_add_custom_email
16 * - wpmem_add_custom_dialog
17 * - wpmem_is_tab
18 */
19
20 // Exit if accessed directly.
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit();
23 }
24
25 /**
26 * Wrapper function for adding custom emails.
27 *
28 * @since 3.1.1
29 *
30 * @global object $wpmem The WP_Members object class.
31 * @param string $tag Slug for the custom email.
32 * @param string $heading Heading to display in admin panel.
33 * @param string $subject_input Slug for the subject.
34 * @param string $message_input Slug for the message body.
35 */
36 function wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input ) {
37 global $wpmem;
38 $args = array(
39 'name' => $tag,
40 'heading' => $heading,
41 'subject_input' => $subject_input,
42 'body_input' => $message_input,
43 );
44 $wpmem->admin->add_email( $args );
45 }
46
47 /**
48 * Checks the current tab being displayed in the admin panel.
49 *
50 * @since 3.1.4
51 *
52 * @param string $tab The tab slug.
53 * @return bool
54 */
55 function wpmem_is_tab( $tab ) {
56 return ( $tab == wpmem_get( 'tab', false, 'get' ) ) ? true : false;
57 }
58
59 /**
60 * Utility function generates link to user profile.
61 *
62 * @since 3.1.7
63 *
64 * @param int $user_id
65 * @return string user profile URL.
66 */
67 function wpmem_admin_user_profile( $user_id ) {
68 return add_query_arg( 'user_id', $user_id, admin_url( 'user-edit.php' ) );
69 }
70
71 /**
72 * Wrapper for form_post_url().
73 *
74 * @since 3.1.8
75 *
76 * @global object $wpmem The WP_Members Object.
77 * @param string $tab The plugin tab being displayed.
78 * @param mixed $args Array of additional arguments|boolean. Default: false.
79 * @return string $url The escaped admin URL for posting forms.
80 */
81 function wpmem_admin_form_post_url( $args = false ) {
82 global $wpmem;
83 return $wpmem->admin->form_post_url( $args );
84 }
85
86 /**
87 * Returns an array of WordPress reserved terms.
88 *
89 * @since 3.0.2
90 * @since 3.2.3 Moved to Admin API as wrapper for WP_Members_Admin_API::wp_reserved_terms().
91 *
92 * @global object $wpmem
93 * @return array An array of WordPress reserved terms.
94 */
95 function wpmem_wp_reserved_terms() {
96 global $wpmem;
97 return $wpmem->admin->wp_reserved_terms();
98 }
99
100 /**
101 * Updates a single plugin option.
102 *
103 * @since 3.3.6
104 * @since 3.5.2 Added $autoload
105 *
106 * @param string $option Name of the option to update.
107 * @param string $key Which key to update. Update a subkey as primary_key/subkey.
108 * @param string $value New value.
109 * @param bool $autoload True|false to autoload, null to use default.
110 * @return bool True if the value was updated, otherwise false.
111 */
112 function wpmem_update_option( $option, $key, $value, $autoload = null ) {
113 $settings = get_option( $option );
114 if ( strpos( $key, '/' ) ) {
115 $keys = explode( '/', $key );
116 $settings[ $keys[0] ][ $keys[1] ] = $value;
117 } else {
118 $settings[ $key ] = $value;
119 }
120 return update_option( $option, $settings, $autoload );
121 }
122
123 /**
124 * Returns a custom "query_where" if the current view is selected.
125 *
126 * @since 3.4.5
127 *
128 * @param string $query_where $query_where value from the filter (required)
129 * @param string $view Custom view slug
130 * @param string $meta_key Meta key the view is filtered by (needed for count)
131 * @param string $meta_value Value of the meta key for the view (needed for count)
132 * @param string $compare Comparison operator (optional, default "=")
133 */
134 function wpmem_add_query_where( $query_where, $view, $meta_key, $meta_value, $compare = '=' ) {
135 $show = sanitize_text_field( wpmem_get( 'show', false, 'get' ) );
136 if ( $view == $show ) {
137 $query_where = wpmem_get_query_where( $meta_key, $meta_value, $compare );
138 }
139 return $query_where;
140 }
141
142 /**
143 * Builds a "query_where" for custom user views in Users > All Users.
144 *
145 * @since 3.4.5
146 *
147 * @param string $meta_key Meta key the view is filtered by (needed for count)
148 * @param string $meta_value Value of the meta key for the view (needed for count)
149 * @param string $compare Comparison operator (optional, default "=")
150 */
151 function wpmem_get_query_where( $meta_key, $meta_value, $compare = '=' ) {
152 global $wpdb;
153 $query_where = 'WHERE 1=1 AND ' . $wpdb->users . '.ID IN (
154 SELECT ' . $wpdb->usermeta . '.user_id FROM ' . $wpdb->usermeta . '
155 WHERE ' . $wpdb->usermeta . '.meta_key = "' . esc_sql( $meta_key ) . '"
156 AND ' . $wpdb->usermeta . '.meta_value ' . $compare . ' "' . esc_sql( $meta_value ) . '" )';
157 return $query_where;
158 }
159
160 /**
161 * Adds a custom user view link for Users > All Users to the existing views array.
162 *
163 * @since 3.4.5
164 *
165 * @param array $views The $views value from the "wpmem_views_users" filter (required)
166 * @param string $name Text for the view link
167 * @param string $view Custom view slug
168 * @param string $meta_key Meta key the view is filtered by (needed for count)
169 * @param string $meta_value Value of the meta key for the view (needed for count)
170 * @param string $compare Comparison operator (optional, default "=")
171 * @param int $expires Expiration of the count transient in seconds (optional, default = 60)
172 */
173 function wpmem_add_user_view_link( $views, $link_text, $view_slug, $meta_key, $meta_value, $compare = "=", $expires = 60 ) {
174 $views[ $view_slug ] = wpmem_get_user_view_link( $link_text, $view_slug, $meta_key, $meta_value, $compare, $expires );
175 return $views;
176 }
177
178 /**
179 * Returns a custom user view link for Users > All Users.
180 *
181 * @since 3.4.5
182 *
183 * @param string $name Text for the view link
184 * @param string $view Custom view slug
185 * @param string $meta_key Meta key the view is filtered by (needed for count)
186 * @param string $meta_value Value of the meta key for the view (needed for count)
187 * @param string $compare Comparison operator (optional, default "=")
188 * @param int $expires Expiration of the count transient in seconds (optional, default = 60)
189 */
190 function wpmem_get_user_view_link( $name, $view, $meta_key, $meta_value, $compare = "=", $expires = 60 ) {
191 $show = sanitize_text_field( wpmem_get( 'show', '', 'get' ) );
192 $url = 'users.php?action=show&show=' . $view;
193 $class = ( $show == $view ) ? ' class="current"' : '';
194
195 // Count is stored in a transient (see "if" condition below).
196 $count = get_transient( 'wpmem_user_counts_' . $view );
197
198 // If the transient is not already set.
199 if ( false === $count ) {
200
201 // Get the count
202 $count = wpmem_get_user_count_by_meta( $meta_key, $meta_value, $compare, $expires );
203
204 // Save it in a transient
205 $transient_expires = $expires; // Value in seconds, 1 day: ( 60 * 60 * 24 );
206 set_transient( 'wpmem_user_counts_' . $view, $count, $transient_expires );
207 }
208
209 /**
210 * Filters the user view link args.
211 *
212 * @since 3.5.4
213 *
214 * @param array $args
215 * @param string $name
216 */
217 $args = apply_filters( 'wpmem_user_view_link_args', array(
218 'url' => $url,
219 'class' => $class,
220 'name' => $name,
221 'count' => $count,
222 ), $name );
223
224 return sprintf(
225 '<a href="%s" %s>%s <span class="count">(%d)</span></a>',
226 esc_url( $args['url'] ),
227 esc_attr( $args['class'] ),
228 esc_html( $args['name'] ),
229 intval( $args['count'] )
230 );
231 }
232
233 /**
234 * @todo could use some additional error messaging and testing.
235 */
236 function wpmem_cli_get_user( $assoc_args ) {
237
238 if ( isset( $assoc_args['id'] ) ) {
239 $user_id = $assoc_args['id'];
240 $user = get_user_by( 'ID', $assoc_args['id'] );
241 } elseif ( isset( $assoc_args['login'] ) ) {
242 $user = get_user_by( 'login', $assoc_args['login'] );
243 } elseif ( isset( $assoc_args['email'] ) ) {
244 $user = get_user_by( 'email', $assoc_args['email'] );
245 }
246 if ( $user ) {
247 return $user;
248 }
249 WP_CLI::error( __( 'No valid user data from inputs given. Try [wp user list] to find a valid user.', 'wp-members' ) );
250
251 }