identity-crisis
1 year ago
sso
1 year ago
webhooks
1 year ago
class-authorize-json-api.php
1 year ago
class-client.php
1 year ago
class-connection-assets.php
1 year ago
class-connection-notice.php
1 year ago
class-error-handler.php
1 year ago
class-heartbeat.php
1 year ago
class-initial-state.php
1 year ago
class-manager.php
1 year ago
class-nonce-handler.php
2 years ago
class-package-version-tracker.php
1 year ago
class-package-version.php
1 year ago
class-partner-coupon.php
1 year ago
class-partner.php
1 year ago
class-plugin-storage.php
1 year ago
class-plugin.php
1 year ago
class-rest-authentication.php
1 year ago
class-rest-connector.php
1 year ago
class-secrets.php
1 year ago
class-server-sandbox.php
1 year ago
class-terms-of-service.php
2 years ago
class-tokens-locks.php
2 years ago
class-tokens.php
2 years ago
class-tracking.php
1 year ago
class-urls.php
1 year ago
class-utils.php
1 year ago
class-webhooks.php
1 year ago
class-xmlrpc-async-call.php
1 year ago
class-xmlrpc-connector.php
1 year ago
interface-manager.php
3 years ago
class-package-version-tracker.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Package_Version_Tracker class. |
| 4 | * |
| 5 | * @package automattic/jetpack-connection |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Connection; |
| 9 | |
| 10 | /** |
| 11 | * The Package_Version_Tracker class. |
| 12 | */ |
| 13 | class Package_Version_Tracker { |
| 14 | |
| 15 | const PACKAGE_VERSION_OPTION = 'jetpack_package_versions'; |
| 16 | |
| 17 | /** |
| 18 | * The cache key for storing a failed request to update remote package versions. |
| 19 | * The caching logic is that when a failed request occurs, we cache it temporarily |
| 20 | * with a set expiration time. |
| 21 | * Only after the key has expired, we'll be able to repeat a remote request. |
| 22 | * This also implies that the cached value is redundant, however we chose the datetime |
| 23 | * of the failed request to avoid using booleans. |
| 24 | */ |
| 25 | const CACHED_FAILED_REQUEST_KEY = 'jetpack_failed_update_remote_package_versions'; |
| 26 | |
| 27 | /** |
| 28 | * The min time difference in seconds for attempting to |
| 29 | * update remote tracked package versions after a failed remote request. |
| 30 | */ |
| 31 | const CACHED_FAILED_REQUEST_EXPIRATION = 1 * HOUR_IN_SECONDS; |
| 32 | |
| 33 | /** |
| 34 | * Transient key for rate limiting the package version requests; |
| 35 | */ |
| 36 | const RATE_LIMITER_KEY = 'jetpack_update_remote_package_last_query'; |
| 37 | |
| 38 | /** |
| 39 | * Only allow one versions check (and request) per minute. |
| 40 | */ |
| 41 | const RATE_LIMITER_TIMEOUT = MINUTE_IN_SECONDS; |
| 42 | |
| 43 | /** |
| 44 | * Uses the jetpack_package_versions filter to obtain the package versions from packages that need |
| 45 | * version tracking. If the package versions have changed, updates the option and notifies WPCOM. |
| 46 | */ |
| 47 | public function maybe_update_package_versions() { |
| 48 | // Do not run too early or all the modules may not be loaded. |
| 49 | if ( ! did_action( 'init' ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // The version check is being rate limited. |
| 54 | if ( $this->is_rate_limiting() ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Obtains the package versions. |
| 60 | * |
| 61 | * @since 1.30.2 |
| 62 | * |
| 63 | * @param array An associative array of Jetpack package slugs and their corresponding versions as key/value pairs. |
| 64 | */ |
| 65 | $filter_versions = apply_filters( 'jetpack_package_versions', array() ); |
| 66 | |
| 67 | if ( ! is_array( $filter_versions ) ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $option_versions = get_option( self::PACKAGE_VERSION_OPTION, array() ); |
| 72 | |
| 73 | foreach ( $filter_versions as $package => $version ) { |
| 74 | if ( ! is_string( $package ) || ! is_string( $version ) ) { |
| 75 | unset( $filter_versions[ $package ] ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if ( ! is_array( $option_versions ) |
| 80 | || count( array_diff_assoc( $filter_versions, $option_versions ) ) |
| 81 | || count( array_diff_assoc( $option_versions, $filter_versions ) ) |
| 82 | ) { |
| 83 | $this->update_package_versions_option( $filter_versions ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Updates the package versions option. |
| 89 | * |
| 90 | * @param array $package_versions The package versions. |
| 91 | */ |
| 92 | protected function update_package_versions_option( $package_versions ) { |
| 93 | if ( ! $this->is_sync_enabled() ) { |
| 94 | $this->update_package_versions_via_remote_request( $package_versions ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | update_option( self::PACKAGE_VERSION_OPTION, $package_versions ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Whether Jetpack Sync is enabled. |
| 103 | * |
| 104 | * @return boolean true if Sync is present and enabled, false otherwise |
| 105 | */ |
| 106 | protected function is_sync_enabled() { |
| 107 | if ( class_exists( 'Automattic\Jetpack\Sync\Settings' ) && \Automattic\Jetpack\Sync\Settings::is_sync_enabled() ) { |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Fallback for updating the package versions via a remote request when Sync is not present. |
| 117 | * |
| 118 | * Updates the package versions as follows: |
| 119 | * - Sends the updated package versions to wpcom. |
| 120 | * - Updates the 'jetpack_package_versions' option. |
| 121 | * |
| 122 | * @param array $package_versions The package versions. |
| 123 | */ |
| 124 | protected function update_package_versions_via_remote_request( $package_versions ) { |
| 125 | $connection = new Manager(); |
| 126 | if ( ! $connection->is_connected() ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 131 | |
| 132 | $last_failed_attempt_within_hour = get_transient( self::CACHED_FAILED_REQUEST_KEY ); |
| 133 | |
| 134 | if ( $last_failed_attempt_within_hour ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $body = wp_json_encode( |
| 139 | array( |
| 140 | 'package_versions' => $package_versions, |
| 141 | ) |
| 142 | ); |
| 143 | |
| 144 | $response = Client::wpcom_json_api_request_as_blog( |
| 145 | sprintf( '/sites/%d/jetpack-package-versions', $site_id ), |
| 146 | '2', |
| 147 | array( |
| 148 | 'headers' => array( 'content-type' => 'application/json' ), |
| 149 | 'method' => 'POST', |
| 150 | ), |
| 151 | $body, |
| 152 | 'wpcom' |
| 153 | ); |
| 154 | |
| 155 | if ( 200 === wp_remote_retrieve_response_code( $response ) ) { |
| 156 | update_option( self::PACKAGE_VERSION_OPTION, $package_versions ); |
| 157 | } else { |
| 158 | set_transient( self::CACHED_FAILED_REQUEST_KEY, time(), self::CACHED_FAILED_REQUEST_EXPIRATION ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check if version check is being rate limited, and update the rate limiting transient if needed. |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | private function is_rate_limiting() { |
| 168 | if ( get_transient( static::RATE_LIMITER_KEY ) ) { |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | set_transient( static::RATE_LIMITER_KEY, time(), static::RATE_LIMITER_TIMEOUT ); |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 |