WPCOM_JSON_API_Update_Invites_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-update-invites-endpoint.php  
  1. class WPCOM_JSON_API_Update_Invites_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. public $blog_id; 
  3. public $invite_id; 
  4. public $is_wpcom; 
  5. public $invite; 
  6.  
  7. function callback( $path = '', $blog_id = 0, $invite_id = 0 ) { 
  8. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  9. if ( is_wp_error( $blog_id ) ) { 
  10. return $blog_id; 
  11.  
  12. if ( ! is_multisite() ) { 
  13. return new WP_Error( 'forbidden', 'To modify invites, site must be on a multisite installation.', 403 ); 
  14.  
  15. if ( ! current_user_can( 'promote_users' ) ) { 
  16. return new WP_Error( 'unauthorized', 'Your token must have permission to promote users on this blog.', 401 ); 
  17.  
  18. $this->blog_id = $blog_id; 
  19. $this->invite_id = $invite_id; 
  20. $this->is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; 
  21.  
  22. $invite = $this->get_invite(); 
  23. if ( false === $invite ) { 
  24. return new WP_Error( 'unknown_invite', 'Requested invite was not found.', 404 ); 
  25.  
  26. $this->invite = $invite; 
  27.  
  28. $returnValue = false; 
  29. if ( $this->api->ends_with( $this->path, '/delete' ) ) { 
  30. $returnValue = array( 
  31. 'invite_key' => $invite_id,  
  32. 'deleted' => $this->delete_invite(),  
  33. ); 
  34. } else { 
  35. $returnValue = array( 
  36. 'result' => $this->is_wpcom ? $this->resend_wpcom_invite() : $this->resend_self_hosted_invite() 
  37. ); 
  38.  
  39. return $returnValue; 
  40.  
  41. /** 
  42. * Returns an invite if found or false if not found. 
  43. * @return bool|object 
  44. */ 
  45. function get_invite() { 
  46. global $wpdb, $wpcom_invite_users; 
  47.  
  48. $invite = false; 
  49. if ( $this->is_wpcom ) { 
  50. $invite = $wpcom_invite_users->get_invitation( $this->invite_id ); 
  51. } else { 
  52. $query = $wpdb->prepare( "SELECT * FROM $wpdb->options WHERE option_name = %s LIMIT 1", $this->invite_id ); 
  53. $invite = $wpdb->get_results( $query ); 
  54.  
  55. $invite = empty( $invite ) ? false : $invite; 
  56.  
  57. return $invite; 
  58.  
  59. /** 
  60. * Deletes an invitation. 
  61. * @return bool Whether the invite was deleted successfully. 
  62. */ 
  63. function delete_invite() { 
  64. global $wpdb, $wpcom_invite_users; 
  65.  
  66. if ( $this->is_wpcom ) { 
  67. return (bool) $wpcom_invite_users->delete_invitation( $this->invite_id ); 
  68. } else { 
  69. $query = $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $this->invite_id ); 
  70. return 0 < $wpdb->query( $query ); 
  71.  
  72. /** 
  73. * Sends an invitation email to a user to join a self-hosted site. 
  74. * This method duplicates the invitation email functionality that is present 
  75. * in wp-admin/user-new.php. Ideally, we should factor out the functionality 
  76. * in wp-admin/user-new.php that actually invites a user and sends the invite 
  77. * from the data validation checks that expect $_POST and $_REQUEST. 
  78. * @return bool Whether the email was sent successfully. 
  79. */ 
  80. function resend_self_hosted_invite() { 
  81. $invite = (array) unserialize( $this->invite[0]->option_value ); 
  82. $roles = get_editable_roles(); 
  83. $role = $roles[ $invite['role'] ]; 
  84. $newuser_key = str_replace( 'new_user_', '', $this->invite_id ); 
  85.  
  86. /** translators: 1: Site title 2: Site URL 3: Role name 4: URL to accept invitation */ 
  87. $message = __( 'Hi,  
  88.  
  89. You\'ve been invited to join \'%1$s\' at 
  90. %2$s with the role of %3$s. 
  91.  
  92. Please click the following link to confirm the invite: 
  93. %4$s', 'jetpack' ); 
  94.  
  95. return wp_mail( 
  96. $invite['email'],  
  97. sprintf( __( '[%s] Joining confirmation', 'jetpack' ), wp_specialchars_decode( get_option( 'blogname' ) ) ),  
  98. sprintf( 
  99. $message,  
  100. get_option( 'blogname' ),  
  101. home_url(),  
  102. wp_specialchars_decode( translate_user_role( $role['name'] ) ),  
  103. home_url( "/newbloguser/$newuser_key/" ) 
  104. ); 
  105.  
  106. /** 
  107. * Sends an invitation email to a user to join a WordPress.com site. 
  108. * @return bool Whether the invitation was sent successfully. 
  109. */ 
  110. function resend_wpcom_invite() { 
  111. global $wpcom_invite_users; 
  112.  
  113. $wpcom_invite_users->update_invitation( $this->invite->invite_slug, array( 'invite_date' => gmdate( 'Y-m-d H:i:s' ) ) ); 
  114.  
  115. if ( 'follower' == $this->invite->meta['role'] && ! is_private_blog() ) { 
  116. $wpcom_invite_users->invite_followers( $this->invite->meta['sent_to'] ); 
  117. } else { 
  118. $wpcom_invite_users->send_invitation( $this->invite->invite_slug ); 
  119.  
  120. return true;