Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | class WPCOM_JSON_API_List_Invites_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 3 | var $blog_id; |
||
| 4 | var $is_wpcom; |
||
| 5 | |||
| 6 | function callback( $path = '', $blog_id = 0 ) { |
||
| 7 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | if ( is_wp_error( $blog_id ) ) { |
||
| 9 | return $blog_id; |
||
| 10 | } |
||
| 11 | |||
| 12 | if ( ! is_multisite() ) { |
||
| 13 | return new WP_Error( 'forbidden', 'To query invites, site must be on a multisite installation.', 403 ); |
||
| 14 | } |
||
| 15 | |||
| 16 | if ( ! current_user_can( 'promote_users' ) ) { |
||
| 17 | return new WP_Error( 'unauthorized', 'Your token must have permission to promote users on this blog.', 401 ); |
||
| 18 | } |
||
| 19 | |||
| 20 | $this->blog_id = $blog_id; |
||
| 21 | $this->args = $this->query_args(); |
||
| 22 | $this->is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; |
||
| 23 | $this->found = $this->get_found(); |
||
| 24 | |||
| 25 | return array( |
||
| 26 | 'found' => $this->found, |
||
| 27 | 'invites' => $this->get_invites(), |
||
| 28 | ); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns the total number of invites, ignoring limits and offsets. |
||
| 33 | * @return int |
||
| 34 | */ |
||
| 35 | function get_found() { |
||
| 36 | global $wpdb, $wpcom_invite_users; |
||
| 37 | |||
| 38 | $total = 0; |
||
| 39 | if ( $this->is_wpcom ) { |
||
| 40 | $total = $wpcom_invite_users->count_blog_invitiations( $this->blog_id, null, 'pending' == $this->args['status'] ); |
||
| 41 | } else { |
||
| 42 | $total = $invites = $wpdb->get_var( |
||
| 43 | $wpdb->prepare( |
||
| 44 | "SELECT count( option_id ) FROM $wpdb->options WHERE option_name LIKE %s", |
||
| 45 | 'new_user_%' |
||
| 46 | ) |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | return intval( $total ); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the invitations for a given site. |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | function get_invites() { |
||
| 58 | global $wpdb, $wpcom_invite_users; |
||
| 59 | |||
| 60 | $invites = array(); |
||
| 61 | if ( $this->is_wpcom ) { |
||
| 62 | $invites = $wpcom_invite_users->get_blog_invitations( |
||
| 63 | $this->blog_id, |
||
| 64 | null, |
||
| 65 | array( |
||
| 66 | 'offset' => intval( $this->args['offset'] ), |
||
| 67 | 'per_page' => intval( $this->args['number'] ), |
||
| 68 | 'pending_only' => ( 'pending' == $this->args['status'] ), |
||
| 69 | ) |
||
| 70 | ); |
||
| 71 | } else { |
||
| 72 | $invites = $wpdb->get_results( |
||
| 73 | $wpdb->prepare( |
||
| 74 | "SELECT * FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_id DESC LIMIT %d, %d", |
||
| 75 | 'new_user_%', |
||
| 76 | intval( $this->args['offset'] ), |
||
| 77 | intval( $this->args['number'] ) |
||
| 78 | ) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | return empty( $invites ) ? array() : array_map( array( $this, 'build_invite' ), $invites ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Given an invite, returns an array with expected shape. |
||
| 87 | * @param array $invite |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | function build_invite( $invite ) { |
||
| 91 | $invite_key = $this->is_wpcom ? $invite->invite_slug : $invite->option_name; |
||
| 92 | $invite = $this->is_wpcom ? (array) $invite : (array) unserialize( $invite->option_value ); |
||
|
0 ignored issues
–
show
|
|||
| 93 | |||
| 94 | return array( |
||
| 95 | 'invite_key' => $invite_key, |
||
| 96 | 'role' => $this->is_wpcom ? $invite['meta']['role'] : $invite['role'], |
||
| 97 | 'user' => $this->get_user( $invite ), |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Given an invite, returns a user object using the get_author() method in class.json-api-endpoints.php. |
||
| 103 | * @param array $invite |
||
| 104 | * @return array|string |
||
| 105 | */ |
||
| 106 | function get_user( $invite ) { |
||
| 107 | if ( ! $this->is_wpcom ) { |
||
| 108 | return $this->get_author( $invite['user_id'] ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $user = get_user_by( 'login', $invite['meta']['sent_to'] ); |
||
| 112 | |||
| 113 | // If a user did not exist, mock a user to pass to get_author() |
||
| 114 | $no_user = false === $user; |
||
| 115 | if( $no_user ) { |
||
| 116 | $user = new stdClass(); |
||
| 117 | $user->comment_author = ''; |
||
| 118 | $user->comment_author_url = ''; |
||
| 119 | $user->comment_author_email = $invite['meta']['sent_to']; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this->get_author( $user, $no_user ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 |