class.json-api-site-jetpack-base.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. require_once dirname( __FILE__ ) . '/class.json-api-site-base.php';
  3. abstract class Abstract_Jetpack_Site extends SAL_Site {
  4. abstract protected function get_constant( $name );
  5. abstract protected function current_theme_supports( $feature_name );
  6. abstract protected function get_theme_support( $feature_name );
  7. abstract protected function get_mock_option( $name );
  8. abstract public function get_jetpack_version();
  9. abstract public function get_updates();
  10. abstract protected function main_network_site();
  11. abstract protected function wp_version();
  12. abstract protected function max_upload_size();
  13. abstract protected function is_main_network();
  14. abstract protected function is_version_controlled();
  15. abstract protected function file_system_write_access();
  16. function before_render() {
  17. }
  18. protected function wp_memory_limit() {
  19. return $this->get_constant( 'WP_MEMORY_LIMIT' );
  20. }
  21. protected function wp_max_memory_limit() {
  22. return $this->get_constant( 'WP_MAX_MEMORY_LIMIT' );
  23. }
  24. function after_render( &$response ) {
  25. // Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
  26. if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) {
  27. $jetpack_update = $this->get_updates();
  28. if ( ! empty( $jetpack_update ) ) {
  29. // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
  30. unset( $jetpack_update['wp_version'] );
  31. // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
  32. unset( $jetpack_update['site_is_version_controlled'] );
  33. $response['updates'] = $jetpack_update;
  34. }
  35. }
  36. }
  37. function after_render_options( &$options ) {
  38. $options['jetpack_version'] = $this->get_jetpack_version();
  39. if ( $main_network_site = $this->main_network_site() ) {
  40. $options['main_network_site'] = (string) rtrim( $main_network_site, '/' );
  41. }
  42. if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) {
  43. $options['active_modules'] = (array) array_values( $active_modules );
  44. }
  45. $options['software_version'] = (string) $this->wp_version();
  46. $options['max_upload_size'] = $this->max_upload_size();
  47. $options['wp_memory_limit'] = $this->wp_memory_limit();
  48. $options['wp_max_memory_limit'] = $this->wp_max_memory_limit();
  49. // Sites have to prove that they are not main_network site.
  50. // If the sync happends right then we should be able to see that we are not dealing with a network site
  51. $options['is_multi_network'] = (bool) $this->is_main_network();
  52. $options['is_multi_site'] = (bool) $this->is_multisite();
  53. $file_mod_disabled_reasons = array_keys( array_filter( array(
  54. 'automatic_updater_disabled' => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ),
  55. // WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
  56. 'wp_auto_update_core_disabled' => ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ),
  57. 'is_version_controlled' => (bool) $this->is_version_controlled(),
  58. // By default we assume that site does have system write access if the value is not set yet.
  59. 'has_no_file_system_write_access' => ! (bool) $this->file_system_write_access(),
  60. 'disallow_file_mods' => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' ),
  61. ) ) );
  62. $options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
  63. }
  64. function get_jetpack_modules() {
  65. if ( is_user_member_of_blog() ) {
  66. return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
  67. }
  68. return null;
  69. }
  70. function is_vip() {
  71. return false; // this may change for VIP Go sites, which sync using Jetpack
  72. }
  73. function featured_images_enabled() {
  74. return $this->current_theme_supports( 'post-thumbnails' );
  75. }
  76. function get_post_formats() {
  77. // deprecated - see separate endpoint. get a list of supported post formats
  78. $all_formats = get_post_format_strings();
  79. $supported = $this->get_theme_support( 'post-formats' );
  80. $supported_formats = array();
  81. if ( isset( $supported[0] ) ) {
  82. foreach ( $supported[0] as $format ) {
  83. $supported_formats[ $format ] = $all_formats[ $format ];
  84. }
  85. }
  86. return $supported_formats;
  87. }
  88. function get_icon() {
  89. $icon_id = get_option( 'site_icon' );
  90. if ( empty( $icon_id ) ) {
  91. $icon_id = Jetpack_Options::get_option( 'site_icon_id' );
  92. }
  93. if ( empty( $icon_id ) ) {
  94. return null;
  95. }
  96. $icon = array_filter( array(
  97. 'img' => wp_get_attachment_image_url( $icon_id, 'full' ),
  98. 'ico' => wp_get_attachment_image_url( $icon_id, array( 16, 16 ) )
  99. ) );
  100. if ( empty( $icon ) ) {
  101. return null;
  102. }
  103. if ( current_user_can( 'edit_posts', $icon_id ) ) {
  104. $icon['media_id'] = (int) $icon_id;
  105. }
  106. return $icon;
  107. }
  108. /**
  109. * Private methods
  110. **/
  111. private function is_main_site( $response ) {
  112. if ( isset( $response['options']->main_network_site, $response['options']->unmapped_url ) ) {
  113. $main_network_site_url = set_url_scheme( $response['options']->main_network_site, 'http' );
  114. $unmapped_url = set_url_scheme( $response['options']->unmapped_url, 'http' );
  115. if ( $unmapped_url === $main_network_site_url ) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. // For Jetpack sites this will always return false
  122. protected function is_a8c_publication( $post_id ) {
  123. return false;
  124. }
  125. }