Jetpack_My_Jetpack_Page

The Jetpack by WordPress.com Jetpack My Page class.

Defined (1)

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

/_inc/lib/admin-pages/class.jetpack-my-jetpack-page.php  
  1. class Jetpack_My_Jetpack_Page extends Jetpack_Admin_Page { 
  2. // Show the settings page only when Jetpack is connected or in dev mode 
  3. protected $dont_show_if_not_active = true; 
  4. function add_page_actions( $hook ) {} // There are no page specific actions to attach to the menu 
  5.  
  6. // Adds the My Jetpack page, but hides it from the submenu 
  7. function get_page_hook() { 
  8. return add_submenu_page( null, __( 'My Jetpack', 'jetpack' ), __( 'My Jetpack', 'jetpack' ), 'jetpack_connect_user', 'my_jetpack', array( $this, 'render' ) ); 
  9.  
  10. // Renders the view file 
  11. function page_render() { 
  12. Jetpack::init()->load_view( 'admin/my-jetpack-page.php' ); 
  13.  
  14. //My Jetpack view tracking, send to MC Stats 
  15. Jetpack::init()->stat( 'admin', 'my-jetpack' ); 
  16. Jetpack::init()->do_stats( 'server_side' ); 
  17.  
  18.  
  19. /** 
  20. * Handle the change in master user 
  21. */ 
  22. function jetpack_my_jetpack_change_user() { 
  23. if ( ! isset( $_POST['_my_jetpack_nonce'] ) || ! wp_verify_nonce( $_POST['_my_jetpack_nonce'], 'jetpack_change_primary_user' ) ) { 
  24. wp_die( __( 'Failed permissions, please try again.', 'jetpack' ) ); 
  25. exit; 
  26.  
  27. if ( isset( $_POST['jetpack-new-master'] ) ) { 
  28. $old_master_user = Jetpack_Options::get_option( 'master_user' ); 
  29. $new_master_user = $_POST['jetpack-new-master']; 
  30. $user_token = Jetpack_Data::get_access_token( $new_master_user ); 
  31. $is_user_connected = $user_token && ! is_wp_error( $user_token ); 
  32. if ( current_user_can( 'manage_options' ) && $is_user_connected ) { 
  33. Jetpack::log( 'switch_master_user', array( 'old_master' => $old_master_user, 'new_master' => $new_master_user ) ); 
  34. Jetpack_Options::update_option( 'master_user', $new_master_user ); 
  35. Jetpack::state( 'message', 'switch_master' ); 
  36.  
  37. //My Jetpack primary user successfully changed, send to MC Stats 
  38. Jetpack::init()->stat( 'admin', 'change-primary-successful' ); 
  39. Jetpack::init()->do_stats( 'server_side' ); 
  40.  
  41. // Change the blog owner dotcom side 
  42. $this->wpcom_switch_blog_owner( $new_master_user ); 
  43.  
  44. /** 
  45. * Tell wpcom that the master user has switched 
  46. * so we can update the 'wpcom_blog_owner' 
  47. */ 
  48. function wpcom_switch_blog_owner( $new_master ) { 
  49. $request = array( 
  50. 'new_blog_owner' => $new_master 
  51. ); 
  52.  
  53. // Tell wpcom about the change 
  54. Jetpack::load_xml_rpc_client(); 
  55. $xml = new Jetpack_IXR_Client( array( 
  56. 'user_id' => get_current_user_id(),  
  57. ) ); 
  58.  
  59. $xml->query( 'jetpack.switchBlogOwner', $request ); 
  60.  
  61. /** 
  62. * Checks to see if there are any other users available to become primary 
  63. * Users must both: 
  64. * - Be linked to wpcom 
  65. * - Be an admin 
  66. * @return bool 
  67. */ 
  68. function jetpack_are_other_users_linked_and_admin() { 
  69. // If only one admin 
  70. $all_users = count_users(); 
  71. if ( 2 > $all_users['avail_roles']['administrator'] ) { 
  72. return false; 
  73.  
  74. $users = get_users(); 
  75. $available = array(); 
  76. // If no one else is linked to dotcom 
  77. foreach ( $users as $user ) { 
  78. if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) { 
  79. $available[] = $user->ID; 
  80.  
  81. if ( 2 > count( $available ) ) { 
  82. return false; 
  83.  
  84. return true; 
  85.  
  86. /** 
  87. * All the data we'll need about the Master User 
  88. * for the My Jetpack page template 
  89. * @return array 
  90. */ 
  91. function jetpack_master_user_data() { 
  92. // If the master user has disappeared, none of this is useful. 
  93. // @todo throw up a warning and offer a solution 
  94. $master = Jetpack_Options::get_option( 'master_user' ); 
  95. if ( ! get_user_by( 'id', $master ) ) { 
  96. return false; 
  97.  
  98. $master_user = get_userdata( $master ); 
  99. $master_user_data_com = Jetpack::get_connected_user_data( $master_user->ID ); 
  100. $gravatar = sprintf( '<a href="%s">%s</a>', get_edit_user_link( $master_user->ID ), get_avatar( $master_user->ID, 40 ) ); 
  101.  
  102. $master_user_data = array( 
  103. 'masterUser' => $master_user,  
  104. 'masterDataCom' => $master_user_data_com,  
  105. 'gravatar' => $gravatar,  
  106. ); 
  107.  
  108. return $master_user_data; 
  109.  
  110. /** 
  111. * All the data we'll need about the Current User 
  112. * @return array 
  113. */ 
  114. function jetpack_current_user_data() { 
  115. global $current_user; 
  116. $is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' ); 
  117. $dotcom_data = Jetpack::get_connected_user_data(); 
  118.  
  119. $current_user_data = array( 
  120. 'isUserConnected' => Jetpack::is_user_connected( $current_user->ID ),  
  121. 'isMasterUser' => $is_master_user,  
  122. 'adminUsername' => $current_user->user_login,  
  123. 'userComData' => $dotcom_data,  
  124. 'gravatar' => sprintf( '<a href="%s">%s</a>', get_edit_user_link( $current_user->ID ), get_avatar( $current_user->ID, 40 ) ),  
  125. ); 
  126.  
  127. return $current_user_data; 
  128.  
  129.  
  130. /** 
  131. * Build an array of My Jetpack stats urls. 
  132. * requires the build URL args passed as an array 
  133. * @param array $my_jetpack_stats 
  134. * @return (array) of built stats urls 
  135. */ 
  136. function build_my_jetpack_stats_urls( $my_jetpack_stats ) { 
  137. $my_jetpack_urls = array(); 
  138.  
  139. foreach ( $my_jetpack_stats as $value ) { 
  140. $my_jetpack_urls[ $value ] = Jetpack::build_stats_url( array( 'x_jetpack-admin' => $value ) ); 
  141.  
  142. return $my_jetpack_urls; 
  143.  
  144.  
  145. // Load up admin scripts 
  146. function page_admin_scripts() { 
  147. wp_enqueue_script( 'jp-connection-js', plugins_url( '_inc/jp-my-jetpack.js', JETPACK__PLUGIN_FILE ), array( 'jquery', 'wp-util' ), JETPACK__VERSION . 'yep' ); 
  148.  
  149. wp_localize_script( 'jp-connection-js', 'jpConnection',  
  150. array( 
  151. 'jetpackIsActive' => Jetpack::is_active(),  
  152. 'isAdmin' => current_user_can( 'jetpack_manage_modules' ),  
  153. 'otherAdminsLinked' => $this->jetpack_are_other_users_linked_and_admin(),  
  154. 'masterUser' => $this->jetpack_master_user_data(),  
  155. 'currentUser' => $this->jetpack_current_user_data(),  
  156. 'my_jetpack_stats_urls' => $this->build_my_jetpack_stats_urls( array( 'change_primary', 'disconnect_site', 'confirm_disconnect', 'support_no_disconnect', 'cancel_disconnect' ) ),  
  157. 'alertText' => __( 'You must link another admin account before switching primary account holders.', 'jetpack' ),  
  158. );