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

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

149 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( defined( 'WP_CLI' ) && WP_CLI ) {
4
5 /**
6 * Manage WP-Members via WP-CLI
7 */
8 class WP_Members_CLI {
9
10 /**
11 * Gets that status or block value of a post or post setting.
12 *
13 * ## OPTIONS
14 *
15 * <block_val|status|hidden>
16 * : What to get?
17 *
18 * --id=<post_id|post_slug>
19 * : Post ID
20 */
21 public function get( $args, $assoc_args ) {
22
23 if ( is_numeric( $assoc_args['id'] ) ) {
24 $post_id = $assoc_args['id'];
25 } else {
26 $post = get_page_by_path( $assoc_args['id'], OBJECT );
27 if ( $post ) {
28 $post_id = $post->ID;
29 } else {
30 WP_CLI::error( $assoc_args['id'] . ' is not a valid post' );
31 }
32 }
33
34 switch ( $args[0] ) {
35
36 case "block_val":
37 $block_setting = wpmem_get_block_setting( $post_id );
38 switch( $block_setting ) {
39 case '1':
40 $block_text = 'restricted';
41 break;
42 case '2':
43 $block_text = 'hidden';
44 break;
45 case '0':
46 case false:
47 $block_text = 'unrestricted';
48 break;
49 }
50 WP_CLI::line( 'post restriction setting:' . ' ' . $block_text );
51 break;
52
53 case "status":
54 if ( false == get_post_status( $post_id ) ) {
55 WP_CLI::error( sprintf( 'No post id %d exists. Try wp post list', $post_id ) );
56 }
57 if ( true == wpmem_is_hidden( $post_id ) ) {
58 $line = sprintf( 'post %s is hidden', $post_id );
59 } else {
60 $line = ( wpmem_is_blocked( $post_id ) ) ? sprintf( 'post %s is restricted', $post_id ) : sprintf( 'post %s is not restricted', $post_id );
61 }
62 WP_CLI::line( $line );
63 break;
64
65 case "hidden":
66 $hidden_posts = wpmem_get_hidden_posts();
67
68 if ( empty( $hidden_posts ) ) {
69 WP_CLI::line( 'There are no hidden posts' );
70 } else {
71 foreach ( $hidden_posts as $post_id ) {
72 $list[] = array(
73 'id' => $post_id,
74 'title' => get_the_title( $post_id ),
75 'url' => get_permalink( $post_id ),
76 );
77 }
78
79 WP_CLI::line( 'WP-Members hidden posts:' );
80 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'id', 'title', 'url' ) );
81 $formatter->display_items( $list );
82 }
83 break;
84 }
85 }
86
87 /**
88 * Sets post properites.
89 *
90 * ## OPTIONS
91 *
92 * --id=<post_id>
93 * : Post ID to set property for.
94 *
95 * --status=<unblock|unrestrict|hide|block|restrict>
96 * : The status to set.
97 * ---
98 * options:
99 * - restrict
100 * - block
101 * - unrestrict
102 * - unblock
103 * - hide
104 *
105 * @since 3.3.5
106 */
107 public function set( $args, $assoc_args ) {
108
109 $post_id = $assoc_args['post_id'];
110 switch( $assoc_args['status'] ) {
111 case 'unblock':
112 case 'unrestrict':
113 $val = 0; $line = 'unrestricted';
114 break;
115 case 'hide':
116 $val = 2; $line = 'hidden';
117 break;
118 case 'block':
119 case 'restrict':
120 default;
121 $val = 1; $line = 'restricted';
122 break;
123 }
124 update_post_meta( $post_id, '_wpmem_block', $val );
125 WP_CLI::line( sprintf( 'Set post id %s as %s', $post_id, $line ) );
126 }
127
128 /**
129 * Refreshes the hidden post array.
130 *
131 * @since 3.3.5
132 *
133 * @alias refresh-hidden
134 */
135 public function refresh_hidden() {
136 wpmem_update_hidden_posts();
137 WP_CLI::success( 'Hidden posts refreshed' );
138 }
139 }
140
141 WP_CLI::add_command( 'mem', 'WP_Members_CLI' );
142
143 // Load all subcommands
144 require_once 'class-wp-members-cli-filesystem.php';
145 require_once 'class-wp-members-cli-import.php';
146 require_once 'class-wp-members-cli-memberships.php';
147 require_once 'class-wp-members-cli-settings.php';
148 require_once 'class-wp-members-cli-user.php';
149 }