class.json-api-date.php
4 years ago
class.json-api-links.php
4 years ago
class.json-api-metadata.php
4 years ago
class.json-api-platform-jetpack.php
4 years ago
class.json-api-platform.php
4 years ago
class.json-api-post-base.php
4 years ago
class.json-api-post-jetpack.php
4 years ago
class.json-api-site-base.php
4 years ago
class.json-api-site-jetpack-base.php
4 years ago
class.json-api-site-jetpack.php
4 years ago
class.json-api-token.php
4 years ago
class.json-api-site-jetpack-base.php
344 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * This class extends the SAL_Site class, providing the implementation for |
| 4 | * functions that were declared in that SAL_Site class as well as defining |
| 5 | * base functions to be implemented in class Jetpack_Site. |
| 6 | * |
| 7 | * @see class.json-api-site-jetpack.php for more context on |
| 8 | * the functions extended here. |
| 9 | * |
| 10 | * @package automattic/jetpack |
| 11 | */ |
| 12 | require_once __DIR__ . '/class.json-api-site-base.php'; |
| 13 | |
| 14 | /** |
| 15 | * Base class for Abstract_Jetpack_Site. |
| 16 | */ |
| 17 | abstract class Abstract_Jetpack_Site extends SAL_Site { |
| 18 | |
| 19 | /** |
| 20 | * Defining a base get_constant() function to be extended in the Jetpack_Site class. |
| 21 | * |
| 22 | * If a Jetpack constant name has been defined, this will return the value of the constant. |
| 23 | * |
| 24 | * @param string $name the name of the Jetpack constant to check. |
| 25 | */ |
| 26 | abstract protected function get_constant( $name ); |
| 27 | |
| 28 | /** |
| 29 | * Defining a base current_theme_supports() function to be extended in the Jetpack_Site class. |
| 30 | * |
| 31 | * Returns true if the current theme supports the $feature_name, false otherwise. |
| 32 | * |
| 33 | * @param string $feature_name the name of the Jetpack feature. |
| 34 | */ |
| 35 | abstract protected function current_theme_supports( $feature_name ); |
| 36 | |
| 37 | /** |
| 38 | * Defining a base get_theme_support() function to be extended in the Jetpack_Site class. |
| 39 | * |
| 40 | * Gets theme support arguments to be checked against the specific Jetpack feature. |
| 41 | * |
| 42 | * @param string $feature_name the name of the Jetpack feature to check against. |
| 43 | */ |
| 44 | abstract protected function get_theme_support( $feature_name ); |
| 45 | |
| 46 | /** |
| 47 | * Defining a base get_mock_option() function to be extended in the Jetpack_Site class. |
| 48 | * |
| 49 | * Retrieves a Jetpack option's value, given the option name. |
| 50 | * |
| 51 | * @param string $name the name of the Jetpack option, without the 'jetpack' prefix (eg. 'log' for 'jetpack_log'). |
| 52 | */ |
| 53 | abstract protected function get_mock_option( $name ); |
| 54 | |
| 55 | /** |
| 56 | * Defining a base get_jetpack_version() function to be extended in the Jetpack_Site class. |
| 57 | * |
| 58 | * Returns the current Jetpack version number. |
| 59 | */ |
| 60 | abstract public function get_jetpack_version(); |
| 61 | |
| 62 | /** |
| 63 | * Defining a base get_updates() function to be extended in the Jetpack_Site class. |
| 64 | * |
| 65 | * Gets updates and then stores them in the jetpack_updates option, returning an array with the option schema. |
| 66 | */ |
| 67 | abstract public function get_updates(); |
| 68 | |
| 69 | /** |
| 70 | * Defining a base main_network_site() function to be extended in the Jetpack_Site class. |
| 71 | * |
| 72 | * Returns the site URL for the current network. |
| 73 | */ |
| 74 | abstract protected function main_network_site(); |
| 75 | |
| 76 | /** |
| 77 | * Defining a base wp_version() function to be extended in the Jetpack_Site class. |
| 78 | * |
| 79 | * Returns the WordPress version for the current site. |
| 80 | */ |
| 81 | abstract protected function wp_version(); |
| 82 | |
| 83 | /** |
| 84 | * Defining a base max_upload_size() function to be extended in the Jetpack_Site class. |
| 85 | * |
| 86 | * Returns the maximum upload size allowed in php.ini. |
| 87 | */ |
| 88 | abstract protected function max_upload_size(); |
| 89 | |
| 90 | /** |
| 91 | * Defining a base is_main_network() function to be extended in the Jetpack_Site class. |
| 92 | * |
| 93 | * Returns true if the site is within a system with a multiple networks, false otherwise. |
| 94 | * |
| 95 | * @see /projects/packages/status/src/class-status.php. |
| 96 | */ |
| 97 | abstract protected function is_main_network(); |
| 98 | |
| 99 | /** |
| 100 | * Defining a base is_version_controlled() function to be extended in the Jetpack_Site class. |
| 101 | * |
| 102 | * Returns true if is_vcs_checkout discovers a version control checkout, false otherwise. |
| 103 | * |
| 104 | * @see projects/packages/sync/src/class-functions.php. |
| 105 | */ |
| 106 | abstract protected function is_version_controlled(); |
| 107 | |
| 108 | /** |
| 109 | * Defining a base file_system_write_access() function to be extended in the Jetpack_Site class. |
| 110 | * |
| 111 | * Returns true if the site has file write access false otherwise. |
| 112 | * |
| 113 | * @see projects/packages/sync/src/class-functions.php. |
| 114 | */ |
| 115 | abstract protected function file_system_write_access(); |
| 116 | |
| 117 | /** |
| 118 | * Fetch a list of active plugins that are using Jetpack Connection. |
| 119 | */ |
| 120 | abstract protected function get_connection_active_plugins(); |
| 121 | |
| 122 | /** |
| 123 | * This function is implemented on WPCom sites, where a filter is removed which forces the URL to http. |
| 124 | * |
| 125 | * @see /wpcom/public.api/rest/sal/class.json-api-site-jetpack-shadow.php. |
| 126 | */ |
| 127 | public function before_render() { |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * This function returns the value of the 'WP_MEMORY_LIMIT' constant. |
| 132 | * |
| 133 | * @return int|string |
| 134 | */ |
| 135 | protected function wp_memory_limit() { |
| 136 | return $this->get_constant( 'WP_MEMORY_LIMIT' ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * This function returns the value of the 'WP_MAX_MEMORY_LIMIT' constant. |
| 141 | * |
| 142 | * @return int|string |
| 143 | */ |
| 144 | protected function wp_max_memory_limit() { |
| 145 | return $this->get_constant( 'WP_MAX_MEMORY_LIMIT' ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * If a user has manage options permissions and the site is the main site of the network, make updates visible. |
| 150 | * |
| 151 | * Called after response_keys have been rendered, which itself is used to return all the necessary information for a site’s response. |
| 152 | * |
| 153 | * @param array $response an array of the response keys. |
| 154 | */ |
| 155 | public function after_render( &$response ) { |
| 156 | if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) { |
| 157 | $jetpack_update = $this->get_updates(); |
| 158 | if ( ! empty( $jetpack_update ) ) { |
| 159 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates. |
| 160 | unset( $jetpack_update['wp_version'] ); |
| 161 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates. |
| 162 | unset( $jetpack_update['site_is_version_controlled'] ); |
| 163 | |
| 164 | $response['updates'] = $jetpack_update; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Extends the Jetpack options array with details including site constraints, WordPress and Jetpack versions, and plugins using the Jetpack connection. |
| 171 | * |
| 172 | * @param array $options an array of the Jetpack options. |
| 173 | */ |
| 174 | public function after_render_options( &$options ) { |
| 175 | $options['jetpack_version'] = $this->get_jetpack_version(); |
| 176 | |
| 177 | $main_network_site = $this->main_network_site(); |
| 178 | if ( $main_network_site ) { |
| 179 | $options['main_network_site'] = (string) rtrim( $main_network_site, '/' ); |
| 180 | } |
| 181 | |
| 182 | $active_modules = Jetpack_Options::get_option( 'active_modules' ); |
| 183 | if ( is_array( $active_modules ) ) { |
| 184 | $options['active_modules'] = (array) array_values( $active_modules ); |
| 185 | } |
| 186 | |
| 187 | $options['software_version'] = (string) $this->wp_version(); |
| 188 | $options['max_upload_size'] = $this->max_upload_size(); |
| 189 | $options['wp_memory_limit'] = $this->wp_memory_limit(); |
| 190 | $options['wp_max_memory_limit'] = $this->wp_max_memory_limit(); |
| 191 | |
| 192 | // Sites have to prove that they are not main_network site. |
| 193 | // If the sync happends right then we should be able to see that we are not dealing with a network site. |
| 194 | $options['is_multi_network'] = (bool) $this->is_main_network(); |
| 195 | $options['is_multi_site'] = (bool) $this->is_multisite(); |
| 196 | |
| 197 | $file_mod_disabled_reasons = array_keys( |
| 198 | array_filter( |
| 199 | array( |
| 200 | 'automatic_updater_disabled' => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ), |
| 201 | // WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false. |
| 202 | 'wp_auto_update_core_disabled' => ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ), |
| 203 | 'is_version_controlled' => (bool) $this->is_version_controlled(), |
| 204 | // By default we assume that site does have system write access if the value is not set yet. |
| 205 | 'has_no_file_system_write_access' => ! (bool) $this->file_system_write_access(), |
| 206 | 'disallow_file_mods' => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' ), |
| 207 | ) |
| 208 | ) |
| 209 | ); |
| 210 | |
| 211 | $options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons; |
| 212 | |
| 213 | $options['jetpack_connection_active_plugins'] = $this->get_connection_active_plugins(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * This function returns the values of any active Jetpack modules. |
| 218 | * |
| 219 | * @return array |
| 220 | */ |
| 221 | public function get_jetpack_modules() { |
| 222 | return array_values( Jetpack_Options::get_option( 'active_modules', array() ) ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * This function returns true if a specified Jetpack module is active, false otherwise. |
| 227 | * |
| 228 | * @param string $module The Jetpack module name to check. |
| 229 | * |
| 230 | * @return bool |
| 231 | */ |
| 232 | public function is_module_active( $module ) { |
| 233 | return in_array( $module, Jetpack_Options::get_option( 'active_modules', array() ), true ); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * This function returns false for a check as to whether a site is a VIP site or not. |
| 238 | * |
| 239 | * @return bool Always returns false. |
| 240 | */ |
| 241 | public function is_vip() { |
| 242 | return false; // this may change for VIP Go sites, which sync using Jetpack. |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * If the site's current theme supports post thumbnails, return true (otherwise return false). |
| 247 | * |
| 248 | * @return bool |
| 249 | */ |
| 250 | public function featured_images_enabled() { |
| 251 | return $this->current_theme_supports( 'post-thumbnails' ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns an array of supported post formats. |
| 256 | * |
| 257 | * @return array |
| 258 | */ |
| 259 | public function get_post_formats() { |
| 260 | // deprecated - see separate endpoint. get a list of supported post formats. |
| 261 | $all_formats = get_post_format_strings(); |
| 262 | $supported = $this->get_theme_support( 'post-formats' ); |
| 263 | |
| 264 | $supported_formats = array(); |
| 265 | |
| 266 | if ( isset( $supported[0] ) ) { |
| 267 | foreach ( $supported[0] as $format ) { |
| 268 | $supported_formats[ $format ] = $all_formats[ $format ]; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $supported_formats; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns an array with site icon details. |
| 277 | * |
| 278 | * @return array |
| 279 | */ |
| 280 | public function get_icon() { |
| 281 | $icon_id = get_option( 'site_icon' ); |
| 282 | if ( empty( $icon_id ) ) { |
| 283 | $icon_id = Jetpack_Options::get_option( 'site_icon_id' ); |
| 284 | } |
| 285 | |
| 286 | if ( empty( $icon_id ) ) { |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | $icon = array_filter( |
| 291 | array( |
| 292 | 'img' => wp_get_attachment_image_url( $icon_id, 'full' ), |
| 293 | 'ico' => wp_get_attachment_image_url( $icon_id, array( 16, 16 ) ), |
| 294 | ) |
| 295 | ); |
| 296 | |
| 297 | if ( empty( $icon ) ) { |
| 298 | return null; |
| 299 | } |
| 300 | |
| 301 | if ( current_user_can( 'edit_posts', $icon_id ) ) { |
| 302 | $icon['media_id'] = (int) $icon_id; |
| 303 | } |
| 304 | |
| 305 | return $icon; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Private methods |
| 310 | **/ |
| 311 | |
| 312 | /** |
| 313 | * This function returns true if the current site is the main network site, false otherwise. |
| 314 | * |
| 315 | * @param array $response The array of Jetpack response keys. |
| 316 | * |
| 317 | * @return bool |
| 318 | */ |
| 319 | private function is_main_site( $response ) { |
| 320 | if ( isset( $response['options']->main_network_site, $response['options']->unmapped_url ) ) { |
| 321 | $main_network_site_url = set_url_scheme( $response['options']->main_network_site, 'http' ); |
| 322 | $unmapped_url = set_url_scheme( $response['options']->unmapped_url, 'http' ); |
| 323 | if ( $unmapped_url === $main_network_site_url ) { |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * For Jetpack sites this will always return false. |
| 333 | * |
| 334 | * This is extended for WordPress.com sites in wpcom/public.api/rest/sal/trait.json-api-site-wpcom.php. |
| 335 | * |
| 336 | * @param int $post_id The post id. |
| 337 | * |
| 338 | * @return bool |
| 339 | */ |
| 340 | protected function is_a8c_publication( $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Extended and used in WordPress.com. |
| 341 | return false; |
| 342 | } |
| 343 | } |
| 344 |