WPCOM_JSON_API_List_Invites_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Invites Endpoint class.

Defined (1)

The class is defined in the following location(s).

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