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 / cli / class-wp-members-cli-memberships.php

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

274 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( defined( 'WP_CLI' ) && WP_CLI ) {
3
4 class WP_Members_CLI_Memberships {
5
6 function __construct() {
7 // Need the admin api for some CLI commands.
8 global $wpmem;
9 require_once $wpmem->path . 'includes/admin/api.php';
10 }
11
12 /**
13 * CLI command to list memberships on the site.
14 * @since 3.5.3
15 */
16 public function list( $args, $assoc_args ) {
17 $memberships = wpmem_get_memberships();
18 if ( empty( $memberships ) ) {
19 WP_CLI::line( 'There are no memberships created for this site' );
20 } else {
21 foreach ( $memberships as $membership ) {
22 $list[] = array(
23 'name' => $membership['title'],
24 'meta (slug)' => $membership['name'],
25 );
26 }
27
28 WP_CLI::line( 'WP-Members memberships:' );
29 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'name', 'meta (slug)' ) );
30 $formatter->display_items( $list );
31 }
32 }
33
34 /**
35 * Get membership counts.
36 *
37 * ## OPTIONS
38 *
39 * [<meta>]
40 * : The meta key (slug) of the membership to get data for (gets all memberships by default)
41 *
42 * [--type=<active|expired>]
43 * : Get only active or expired membership count.
44 *
45 * [--all]
46 * : Gets all membership count.
47 *
48 * @since 3.5.3
49 * @subcommand list-count
50 */
51 public function list_count( $args, $assoc_args ) {
52
53 if ( empty( $args ) && ! isset( $assoc_args['all'] ) && ! isset( $assoc_args['type'] ) ) {
54 $count_type = "all";
55 } else {
56 if ( isset( $assoc_args['type'] ) ) {
57 $count_type = $assoc_args['type'];
58 } else {
59 $count_type = "all";
60 }
61 }
62
63 switch ( $count_type ) {
64
65 case "all":
66
67 $memberships = wpmem_get_memberships();
68 if ( empty( $memberships ) ) {
69 WP_CLI::error( 'No memberships exist for this site' );
70 }
71
72 // Is this "all" as in "all memberships" or "all" as in "all counts of an individual membership"?
73 if ( isset( $args[0] ) ) {
74
75 $membership = $memberships[ $args[0] ];
76
77 $active_count = wpmem_get_membership_count( $membership['name'], 'active' );
78 $expired_count = wpmem_get_membership_count( $membership['name'], 'expired' );
79 $total_count = wpmem_get_membership_count( $membership['name'] );
80
81 WP_CLI::line( sprintf( 'Counts for "%s" membership:', $membership['title'] ) );
82 WP_CLI::line( 'Active: ' . $active_count );
83 WP_CLI::line( 'Expired: ' . $expired_count );
84 WP_CLI::line( 'Total: ' . $total_count );
85
86 } else {
87
88 foreach ( $memberships as $membership ) {
89
90 $active_count = wpmem_get_membership_count( $membership['name'], 'active' );
91 $expired_count = wpmem_get_membership_count( $membership['name'], 'expired' );
92 $total_count = wpmem_get_membership_count( $membership['name'] );
93
94 $list[] = array(
95 'Membership' => $membership['title'],
96 'Active' => $active_count,
97 'Expired' => $expired_count,
98 'Total' => $total_count
99 );
100 }
101
102 WP_CLI::line( 'WP-Members membership counts:' );
103 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'Membership', 'Active', 'Expired', 'Total' ) );
104 $formatter->display_items( $list );
105 }
106 break;
107
108 case "active":
109 case "expired":
110
111 $memberships = wpmem_get_memberships();
112
113 if ( empty( $memberships ) ) {
114 WP_CLI::error( 'No memberships exist for this site' );
115 }
116
117 $membership = $args[0];
118 $type = ( "active" == $count_type ) ? "active" : "expired";
119 $label = ( "active" == $count_type ) ? "Active" : "Expired";
120 $count = wpmem_get_membership_count( $memberships[ $membership ]['name'], $type );
121 WP_CLI::line( $label . ' "' . $memberships[ $membership ]['name'] . '" memberships: ' . $count );
122 break;
123
124 }
125 }
126
127 /**
128 * Adds a membership to a user.
129 *
130 * ## OPTIONS
131 *
132 * <membership_meta_key>
133 * : The meta key (slug) of the membership to add to the user.
134 *
135 * [--id=<user>]
136 * : The ID of the user to add the membership to.
137 *
138 * [--login=<user>]
139 * : The login of the user to add the membership to.
140 *
141 * [--email=<user>]
142 * : The email of the user to add the membership to.
143 *
144 * [--date=<YYYY-MM-DD>]
145 * : The expiration date (optional, if excluded, the default time period will be set).
146 *
147 * @since 3.5.3
148 * @since 3.5.4 Added expiration date.
149 */
150 public function add( $args, $assoc_args ) {
151 $date = ( isset( $assoc_args['date'] ) ) ? $assoc_args['date'] : false;
152 $user = wpmem_cli_get_user( $assoc_args );
153 wpmem_set_user_membership( $args[0], $user->ID, $date );
154 WP_CLI::success( sprintf( '%s membership added to %s', $args[0], $user->user_email ) );
155 }
156
157 /**
158 * Updates a membership for a user.
159 *
160 * ## OPTIONS
161 *
162 * <membership_meta_key>
163 * : The meta key (slug) of the membership to update.
164 *
165 * [--date=<YYYY-MM-DD>]
166 * : The expiration date (optional, if excluded, the default time period will be set).
167 *
168 * [--id=<user>]
169 * : The ID of the user to update.
170 *
171 * [--login=<user>]
172 * : The login of the user to udpate.
173 *
174 * [--email=<user>]
175 * : The email of the user to update.
176 *
177 * @since 3.5.3
178 */
179 public function update( $args, $assoc_args ) {
180 $user = wpmem_cli_get_user( $assoc_args );
181 wpmem_set_user_membership( $args[0], $user->ID, $assoc_args['date'] );
182 WP_CLI::success( sprintf( '%s membership update for %s', $args[0], $user->user_email ) );
183 }
184
185 /**
186 * Removes a membership to a user.
187 *
188 * ## OPTIONS
189 *
190 * <membership_meta_key>
191 * : The meta key (slug) of the membership to remove from the user.
192 *
193 * [--id=<user>]
194 * : The ID of the user to remove the membership from.
195 *
196 * [--login=<user>]
197 * : The login of the user to remove the membership from.
198 *
199 * [--email=<user>]
200 * : The email of the user to remove the membership from.
201 *
202 * @since 3.5.3
203 */
204 public function remove( $args, $assoc_args ) {
205 $user = wpmem_cli_get_user( $assoc_args );
206 wpmem_remove_user_membership( $args[0], $user );
207 WP_CLI::success( sprintf( '%s membership removed from %s', $args[0], $user->user_email ) );
208 }
209
210 /**
211 * Create a new membership.
212 *
213 * ## OPTIONS
214 *
215 * --title=<title>
216 * : The membership title (readable), use quotes if title has whitespace ("The Title").
217 *
218 * --name=<meta>
219 * : The membership meta key.
220 *
221 * [--status=<status>]
222 * : Published status.
223 * ---
224 * default: publish
225 * options:
226 * - publish
227 * - draft
228 * ---
229 *
230 * [--author=<user_id>]
231 * : User ID of the author (defaults to site admin user ID).
232 *
233 */
234 public function create( $args, $assoc_args ) {
235
236 /**
237 * wpmem_create_membership( $args ): {
238 * @type string $title User readable name of membership.
239 * @type string $name Sanitized title of the membership to be used as the meta key.
240 * @type string $status Published status: publish|draft (default: publish)
241 * @type int $author User ID of membership author, Optional, defaults to site admin.
242 * @type array $meta_input
243 * Meta fields for membership CPT (not all are required).
244 *
245 * @type string $name The sanitized title of the membership.
246 * @type string $default
247 * @type string $role Roles if a role is required.
248 * @type string $expires Expiration period if used (num|per).
249 * @type int $no_gap If renewal is "no gap" renewal.
250 * @type string $fixed_period (start|end|grace_num|grace_per)
251 * @type int $set_default_{$key}
252 * @type string $message Custom message for restriction.
253 * @type int $child_access If membership hierarchy is used.
254 * }
255 */
256
257 $top_level_args = [ 'title', 'name', 'status', 'author' ];
258 // @todo Will need some validation here so we don't blow things up.
259 foreach ( $assoc_args as $key => $arg ) {
260 if ( in_array( $key, $top_level_args ) ) {
261 $mem_args[ $key ] = $arg;
262 }
263 }
264
265 // @todo Assemble meta input
266
267 $result = wpmem_create_membership( $mem_args );
268 if ( is_wp_error( $result ) ) {
269 WP_CLI::error( 'There was an error creating the membership.' );
270 }
271 }
272 }
273 }
274 WP_CLI::add_command( 'mem membership', 'WP_Members_CLI_Memberships' );