WPCOM_JSON_API_GET_Site_Endpoint

The Jetpack by WordPress.com WPCOM JSON API GET Site Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-get-site-endpoint.php  
  1. class WPCOM_JSON_API_GET_Site_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. public static $site_format = array( 
  4. 'ID' => '(int) Site ID',  
  5. 'name' => '(string) Title of site',  
  6. 'description' => '(string) Tagline or description of site',  
  7. 'URL' => '(string) Full URL to the site',  
  8. 'jetpack' => '(bool) Whether the site is a Jetpack site or not',  
  9. 'post_count' => '(int) The number of posts the site has',  
  10. 'subscribers_count' => '(int) The number of subscribers the site has',  
  11. 'lang' => '(string) Primary language code of the site',  
  12. 'icon' => '(array) An array of icon formats for the site',  
  13. 'logo' => '(array) The site logo, set in the Customizer',  
  14. 'visible' => '(bool) If this site is visible in the user\'s site list',  
  15. 'is_private' => '(bool) If the site is a private site or not',  
  16. 'is_following' => '(bool) If the current user is subscribed to this site in the reader',  
  17. 'options' => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site. Note: Post formats is deprecated, please see /sites/$id/post-formats/',  
  18. 'updates' => '(array) An array of available updates for plugins, themes, wordpress, and languages.',  
  19. 'meta' => '(object) Meta data',  
  20. ); 
  21.  
  22. // /sites/mine 
  23. // /sites/%s -> $blog_id 
  24. function callback( $path = '', $blog_id = 0 ) { 
  25. global $wpdb; 
  26. if ( 'mine' === $blog_id ) { 
  27. $api = WPCOM_JSON_API::init(); 
  28. if ( !$api->token_details || empty( $api->token_details['blog_id'] ) ) { 
  29. return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 ); 
  30. $blog_id = $api->token_details['blog_id']; 
  31.  
  32. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  33. if ( is_wp_error( $blog_id ) ) { 
  34. return $blog_id; 
  35.  
  36. $response = $this->build_current_site_response(); 
  37.  
  38. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  39. do_action( 'wpcom_json_api_objects', 'sites' ); 
  40.  
  41. return $response; 
  42.  
  43. /** 
  44. * Collects the necessary information to return for a site's response. 
  45. * @return (array) 
  46. */ 
  47. public function build_current_site_response( ) { 
  48.  
  49. global $wpdb, $wp_version; 
  50.  
  51. $response_format = self::$site_format; 
  52.  
  53. $is_user_logged_in = is_user_logged_in(); 
  54.  
  55. $visible = array(); 
  56.  
  57. if ( $is_user_logged_in ) { 
  58. $current_user = wp_get_current_user(); 
  59. $visible = get_user_meta( $current_user->ID, 'blog_visibility', true ); 
  60.  
  61. if ( !is_array( $visible ) ) 
  62. $visible = array(); 
  63.  
  64.  
  65. $blog_id = (int) $this->api->get_blog_id_for_output(); 
  66.  
  67. /** This filter is documented in class.json-api-endpoints.php */ 
  68. $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id ); 
  69. $site_url = get_option( 'siteurl' ); 
  70.  
  71. if ( $is_jetpack ) { 
  72. remove_filter( 'option_stylesheet', 'fix_theme_location' ); 
  73. if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) { 
  74. add_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 ); 
  75. foreach ( array_keys( $response_format ) as $key ) { 
  76. switch ( $key ) { 
  77. case 'ID' : 
  78. $response[$key] = $blog_id; 
  79. break; 
  80. case 'name' : 
  81. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); 
  82. break; 
  83. case 'description' : 
  84. $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES ); 
  85. break; 
  86. case 'URL' : 
  87. $response[$key] = (string) home_url(); 
  88. break; 
  89. case 'jetpack' : 
  90. $response[$key] = $is_jetpack; // jetpack magic affects this value 
  91. break; 
  92. case 'is_private' : 
  93. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  94. $public_setting = get_option( 'blog_public' ); 
  95. if ( -1 == $public_setting ) 
  96. $response[$key] = true; 
  97. else 
  98. $response[$key] = false; 
  99. } else { 
  100. $response[$key] = false; // magic 
  101. break; 
  102. case 'visible' : 
  103. if ( $is_user_logged_in ) { 
  104. $is_visible = true; 
  105. if ( isset( $visible[$blog_id] ) ) { 
  106. $is_visible = (bool) $visible[$blog_id]; 
  107. // null and true are visible 
  108. $response[$key] = $is_visible; 
  109. break; 
  110. case 'post_count' : 
  111. if ( $is_user_logged_in ) 
  112. $response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); 
  113. break; 
  114. case 'lang' : 
  115. if ( $is_user_logged_in ) 
  116. $response[$key] = (string) get_bloginfo( 'language' ); 
  117. break; 
  118. case 'icon' : 
  119. if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) { 
  120. $domain = blavatar_domain( home_url() ); 
  121. if ( blavatar_exists( $domain ) ) { 
  122. $response[ $key ] = array( 
  123. 'img' => (string) remove_query_arg( 's', blavatar_url( $domain, 'img' ) ),  
  124. 'ico' => (string) remove_query_arg( 's', blavatar_url( $domain, 'ico' ) ),  
  125. ); 
  126. } else { 
  127. // This is done so that we can access the updated blavatar on .com via the /me/sites endpoint 
  128. if( is_jetpack_site() ) { 
  129.  
  130. $site_icon_url = get_option( 'jetpack_site_icon_url' ); 
  131. if( $site_icon_url ) { 
  132. $response[ $key ] = array( 
  133. 'img' => (string) jetpack_photon_url( $site_icon_url, array() , 'https' ),  
  134. 'ico' => (string) jetpack_photon_url( $site_icon_url, array( 'w' => 16 ), 'https' ) 
  135. ); 
  136. } elseif ( function_exists( 'jetpack_site_icon_url' ) && function_exists( 'jetpack_photon_url' ) ) { 
  137. $response[ $key ] = array( 
  138. 'img' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 80 ), array( 'w' => 80 ), 'https' ),  
  139. 'ico' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 16 ), array( 'w' => 16 ), 'https' ),  
  140. ); 
  141. break; 
  142. case 'logo' : 
  143. // Set an empty response array. 
  144. $response[$key] = array( 
  145. 'id' => (int) 0,  
  146. 'sizes' => array(),  
  147. 'url' => '',  
  148. ); 
  149.  
  150. // Get current site logo values. 
  151. $logo = get_option( 'site_logo' ); 
  152.  
  153. // Update the response array if there's a site logo currenty active. 
  154. if ( $logo && 0 != $logo['id'] ) { 
  155. $response[$key]['id'] = $logo['id']; 
  156. $response[$key]['url'] = $logo['url']; 
  157.  
  158. foreach ( $logo['sizes'] as $size => $properties ) { 
  159. $response[$key]['sizes'][$size] = $properties; 
  160. break; 
  161. case 'subscribers_count' : 
  162.  
  163. if ( function_exists( 'wpcom_subs_total_wpcom_subscribers' ) ) { 
  164. $total_wpcom_subs = wpcom_subs_total_wpcom_subscribers( 
  165. array( 
  166. 'blog_id' => $blog_id,  
  167. ); 
  168. $response[$key] = $total_wpcom_subs; 
  169. } else { 
  170. $response[$key] = 0; // magic 
  171. break; 
  172. case 'is_following': 
  173. $response[$key] = (bool) $this->api->is_following( $blog_id ); 
  174. break; 
  175. case 'options': 
  176. // Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs 
  177. $has_videopress = false; 
  178. if ( get_option( 'video_upgrade' ) == '1' ) { 
  179. $has_videopress = true; 
  180. } else { 
  181. if ( class_exists( 'Jetpack_Options' ) ) { 
  182. $videopress = Jetpack_Options::get_option( 'videopress', array() ); 
  183. if ( isset( $videopress['blog_id'] ) && $videopress['blog_id'] > 0 ) { 
  184. $has_videopress = true; 
  185.  
  186. // deprecated - see separate endpoint. get a list of supported post formats 
  187. $all_formats = get_post_format_strings(); 
  188. $supported = get_theme_support( 'post-formats' ); 
  189.  
  190. $supported_formats = array(); 
  191.  
  192. if ( isset( $supported[0] ) ) { 
  193. foreach ( $supported[0] as $format ) { 
  194. $supported_formats[ $format ] = $all_formats[ $format ]; 
  195.  
  196. // determine if sharing buttons should be visible by default 
  197. $default_sharing_status = false; 
  198. if ( class_exists( 'Sharing_Service' ) ) { 
  199. $ss = new Sharing_Service(); 
  200. $blog_services = $ss->get_blog_services(); 
  201. $default_sharing_status = ! empty( $blog_services['visible'] ); 
  202.  
  203. $is_mapped_domain = false; 
  204.  
  205. if ( function_exists( 'get_primary_redirect' ) ) { 
  206. $primary_redirect = strtolower( get_primary_redirect() ); 
  207. if ( false === strpos( $primary_redirect, '.wordpress.com' ) ) { 
  208. $is_mapped_domain = true; 
  209.  
  210. $is_redirect = false; 
  211.  
  212. if ( function_exists( 'get_primary_domain_mapping_record' ) ) { 
  213. if ( get_primary_domain_mapping_record()->type == 1 ) { 
  214. $is_redirect = true; 
  215.  
  216. if ( function_exists( 'get_mime_types' ) ) { 
  217. $allowed_file_types = get_mime_types(); 
  218. } else { 
  219. // http://codex.wordpress.org/Uploading_Files 
  220. $mime_types = get_allowed_mime_types(); 
  221. foreach ( $mime_types as $type => $mime_type ) { 
  222. $extras = explode( '|', $type ); 
  223. foreach ( $extras as $extra ) { 
  224. $allowed_file_types[] = $extra; 
  225.  
  226. if ( function_exists( 'get_blog_details' ) ) { 
  227. $blog_details = get_blog_details(); 
  228. if ( ! empty( $blog_details->registered ) ) { 
  229. $registered_date = $blog_details->registered; 
  230.  
  231. $upgraded_filetypes_enabled = false; 
  232. if ( $is_jetpack || get_option( 'use_upgraded_upload_filetypes' ) ) { 
  233. $upgraded_filetypes_enabled = true; 
  234.  
  235. $wordads = false; 
  236. if ( function_exists( 'has_any_blog_stickers' ) ) { 
  237. $wordads = has_any_blog_stickers( array( 'wordads-approved', 'wordads-approved-misfits' ), $blog_id ); 
  238.  
  239. $response[$key] = array( 
  240. 'timezone' => (string) get_option( 'timezone_string' ),  
  241. 'gmt_offset' => (float) get_option( 'gmt_offset' ),  
  242. 'videopress_enabled' => $has_videopress,  
  243. 'upgraded_filetypes_enabled' => $upgraded_filetypes_enabled,  
  244. 'login_url' => wp_login_url(),  
  245. 'admin_url' => get_admin_url(),  
  246. 'is_mapped_domain' => $is_mapped_domain,  
  247. 'is_redirect' => $is_redirect,  
  248. 'unmapped_url' => get_site_url( $blog_id ),  
  249. 'featured_images_enabled' => current_theme_supports( 'post-thumbnails' ),  
  250. 'theme_slug' => get_option( 'stylesheet' ),  
  251. 'header_image' => get_theme_mod( 'header_image_data' ),  
  252. 'background_color' => get_theme_mod( 'background_color' ),  
  253. 'image_default_link_type' => get_option( 'image_default_link_type' ),  
  254. 'image_thumbnail_width' => (int) get_option( 'thumbnail_size_w' ),  
  255. 'image_thumbnail_height' => (int) get_option( 'thumbnail_size_h' ),  
  256. 'image_thumbnail_crop' => get_option( 'thumbnail_crop' ),  
  257. 'image_medium_width' => (int) get_option( 'medium_size_w' ),  
  258. 'image_medium_height' => (int) get_option( 'medium_size_h' ),  
  259. 'image_large_width' => (int) get_option( 'large_size_w' ),  
  260. 'image_large_height' => (int) get_option( 'large_size_h' ),  
  261. 'permalink_structure' => get_option( 'permalink_structure' ),  
  262. 'post_formats' => $supported_formats,  
  263. 'default_post_format' => get_option( 'default_post_format' ),  
  264. 'default_category' => (int) get_option( 'default_category' ),  
  265. 'allowed_file_types' => $allowed_file_types,  
  266. 'show_on_front' => get_option( 'show_on_front' ),  
  267. /** This filter is documented in modules/likes.php */ 
  268. 'default_likes_enabled' => (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ),  
  269. 'default_sharing_status' => (bool) $default_sharing_status,  
  270. 'default_comment_status' => ( 'closed' == get_option( 'default_comment_status' ) ? false : true ),  
  271. 'default_ping_status' => ( 'closed' == get_option( 'default_ping_status' ) ? false : true ),  
  272. 'software_version' => $wp_version,  
  273. 'created_at' => ! empty( $registered_date ) ? $this->format_date( $registered_date ) : '0000-00-00T00:00:00+00:00',  
  274. 'wordads' => $wordads,  
  275. ); 
  276.  
  277. if ( 'page' === get_option( 'show_on_front' ) ) { 
  278. $response['options']['page_on_front'] = (int) get_option( 'page_on_front' ); 
  279. $response['options']['page_for_posts'] = (int) get_option( 'page_for_posts' ); 
  280.  
  281. if ( $is_jetpack ) { 
  282. $response['options']['jetpack_version'] = get_option( 'jetpack_version' ); 
  283.  
  284. if ( get_option( 'jetpack_main_network_site' ) ) { 
  285. $response['options']['main_network_site'] = (string) rtrim( get_option( 'jetpack_main_network_site' ), '/' ); 
  286.  
  287. if ( is_array( Jetpack_Options::get_option( 'active_modules' ) ) ) { 
  288. $response['options']['active_modules'] = (array) array_values( Jetpack_Options::get_option( 'active_modules' ) ); 
  289.  
  290. if ( $jetpack_wp_version = get_option( 'jetpack_wp_version' ) ) { 
  291. $response['options']['software_version'] = (string) $jetpack_wp_version; 
  292. } else if ( $jetpack_update = get_option( 'jetpack_updates' ) ) { 
  293. if ( is_array( $jetpack_update ) && isset( $jetpack_update['wp_version'] ) ) { 
  294. $response['options']['software_version'] = (string) $jetpack_update['wp_version']; 
  295. } else { 
  296. $response[ 'options' ][ 'software_version' ] = null; 
  297. } else { 
  298. $response['options']['software_version'] = null; 
  299.  
  300. $response['options']['max_upload_size'] = get_option( 'jetpack_max_upload_size', false ); 
  301.  
  302. // Sites have to prove that they are not main_network site. 
  303. // If the sync happends right then we should be able to see that we are not dealing with a network site 
  304. $response['options']['is_multi_network'] = (bool) get_option( 'jetpack_is_main_network', true ); 
  305. $response['options']['is_multi_site'] = (bool) get_option( 'jetpack_is_multi_site', true ); 
  306.  
  307.  
  308. if ( ! current_user_can( 'edit_posts' ) ) 
  309. unset( $response[$key] ); 
  310. break; 
  311. case 'meta' : 
  312. /** 
  313. * Filters the URL scheme used when querying your site's REST API endpoint. 
  314. * @module json-api 
  315. * @since 3.2.0 
  316. * @param string parse_url( get_option( 'home' ), PHP_URL_SCHEME ) URL scheme parsed from home URL. 
  317. */ 
  318. $xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) ); 
  319. $xmlrpc_url = site_url( 'xmlrpc.php', $xmlrpc_scheme ); 
  320. $response[$key] = (object) array( 
  321. 'links' => (object) array( 
  322. 'self' => (string) $this->get_site_link( $blog_id ),  
  323. 'help' => (string) $this->get_site_link( $blog_id, 'help' ),  
  324. 'posts' => (string) $this->get_site_link( $blog_id, 'posts/' ),  
  325. 'comments' => (string) $this->get_site_link( $blog_id, 'comments/' ),  
  326. 'xmlrpc' => (string) $xmlrpc_url,  
  327. ),  
  328. ); 
  329. break; 
  330.  
  331. if ( $is_jetpack ) { 
  332.  
  333. // Add the updates only make them visible if the user has manage options permission. 
  334. $jetpack_update = (array) get_option( 'jetpack_updates' ); 
  335. if ( ! empty( $jetpack_update ) && current_user_can( 'manage_options' ) ) { 
  336.  
  337. if ( isset( $jetpack_update['wp_version'] ) ) { 
  338. // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates 
  339. unset( $jetpack_update['wp_version'] ); 
  340.  
  341. if ( isset( $jetpack_update['site_is_version_controlled'] ) ) { 
  342. // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates 
  343. unset( $jetpack_update['site_is_version_controlled'] ); 
  344.  
  345. $response['updates'] = (array) $jetpack_update; 
  346.  
  347. add_filter( 'option_stylesheet', 'fix_theme_location' ); 
  348. if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) { 
  349. remove_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 ); 
  350.  
  351. return $response; 
  352.  
  353.  
  354. function force_http( $url, $scheme, $orig_scheme ) { 
  355. return preg_replace('/^https:\/\//', 'http://', $url, 1 ); 
  356.