← All changes
|
jetpack_vendor/automattic/jetpack-connection/src/class-package-version-tracker.php
+26
-1
13.6.2
→
16.3-beta
View file →
| @@ -6,8 +6,10 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Connection; |
| 9 | 9 | |
| 10 | +use Jetpack_Options; | |
| 11 | + | |
| 10 | 12 | /** |
| 11 | 13 | * The Package_Version_Tracker class. |
| 12 | 14 | */ |
| 13 | 15 | class Package_Version_Tracker { |
| @@ -40,8 +42,18 @@ | ||
| 40 | 42 | */ |
| 41 | 43 | const RATE_LIMITER_TIMEOUT = MINUTE_IN_SECONDS; |
| 42 | 44 | |
| 43 | 45 | /** |
| 46 | + * Runs the package version check on the `shutdown` hook. | |
| 47 | + * | |
| 48 | + * Static so the callback can be unregistered, and so the tracker is only | |
| 49 | + * constructed when the hook fires rather than during configuration. | |
| 50 | + */ | |
| 51 | + public static function update_on_shutdown() { | |
| 52 | + ( new self() )->maybe_update_package_versions(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 44 | 56 | * Uses the jetpack_package_versions filter to obtain the package versions from packages that need |
| 45 | 57 | * version tracking. If the package versions have changed, updates the option and notifies WPCOM. |
| 46 | 58 | */ |
| 47 | 59 | public function maybe_update_package_versions() { |
| @@ -49,8 +61,14 @@ | ||
| 49 | 61 | if ( ! did_action( 'init' ) ) { |
| 50 | 62 | return; |
| 51 | 63 | } |
| 52 | 64 | |
| 65 | + // Only attempt to update the option on POST requests. | |
| 66 | + // This will prevent the option from being updated multiple times due to concurrent requests. | |
| 67 | + if ( ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) ) { | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + | |
| 53 | 71 | // The version check is being rate limited. |
| 54 | 72 | if ( $this->is_rate_limiting() ) { |
| 55 | 73 | return; |
| 56 | 74 | } |
| @@ -91,8 +109,14 @@ | ||
| 91 | 109 | */ |
| 92 | 110 | protected function update_package_versions_option( $package_versions ) { |
| 93 | 111 | if ( ! $this->is_sync_enabled() ) { |
| 94 | 112 | $this->update_package_versions_via_remote_request( $package_versions ); |
| 113 | + // Remove the checksum for package versions, so it gets recalculated when sync gets activated. | |
| 114 | + $jetpack_callables_sync_checksum = Jetpack_Options::get_raw_option( 'jetpack_callables_sync_checksum' ); | |
| 115 | + if ( isset( $jetpack_callables_sync_checksum['jetpack_package_versions'] ) ) { | |
| 116 | + unset( $jetpack_callables_sync_checksum['jetpack_package_versions'] ); | |
| 117 | + Jetpack_Options::update_raw_option( 'jetpack_callables_sync_checksum', $jetpack_callables_sync_checksum ); | |
| 118 | + } | |
| 95 | 119 | return; |
| 96 | 120 | } |
| 97 | 121 | |
| 98 | 122 | update_option( self::PACKAGE_VERSION_OPTION, $package_versions ); |
| @@ -137,9 +161,10 @@ | ||
| 137 | 161 | |
| 138 | 162 | $body = wp_json_encode( |
| 139 | 163 | array( |
| 140 | 164 | 'package_versions' => $package_versions, |
| 141 | - ) | |
| 165 | + ), | |
| 166 | + JSON_UNESCAPED_SLASHES | |
| 142 | 167 | ); |
| 143 | 168 | |
| 144 | 169 | $response = Client::wpcom_json_api_request_as_blog( |
| 145 | 170 | sprintf( '/sites/%d/jetpack-package-versions', $site_id ), |