From 17f392cfd11a986fdc30d689022d6994b0c8fac5 Mon Sep 17 00:00:00 2001 From: gavande1 Date: Thu, 7 Sep 2023 09:20:52 +0000 Subject: [PATCH] WPCOM Marketplace: Add updater for providing updates for Marketplace Products (#32872) * [not verified] Add Marketplace Products Updater class * [not verified] Hooks new class into plugins loaded lifecycle * [not verified] Add change log entry * [not verified] Remove theme update hook * [not verified] Bump package version * [not verified] Bump package version * [not verified] apply filter with higher priority * [not verified] Add caching to prevent multiple request to update server * [not verified] WPCOM Marketplace: Add ability of updating theme in updater (#32885) * Add ability of updating theme in updater * Use different cache key for plugins/themes * Add last emtpy line * fix composer.lock conflicts * Remove plugin changelog file * Update doc comment Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/6107617403 --- composer.lock | 8 +- .../automattic/jetpack-mu-wpcom/CHANGELOG.md | 3 + .../src/class-jetpack-mu-wpcom.php | 13 ++ .../class-marketplace-products-updater.php | 178 ++++++++++++++++++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + vendor/composer/installed.json | 6 +- vendor/composer/installed.php | 6 +- 8 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 vendor/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php diff --git a/composer.lock b/composer.lock index 5e56961a..79953631 100644 --- a/composer.lock +++ b/composer.lock @@ -4,15 +4,15 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "404ebd60ef4de467c9fe570477869c73", + "content-hash": "a54547d3c7e4709f953241ba44ba4e46", "packages": [ { "name": "automattic/jetpack-mu-wpcom", - "version": "4.8.0-alpha.1694038209", + "version": "4.8.0-alpha.1694077071", "dist": { "type": "path", "url": "/tmp/jetpack-build/Automattic/jetpack-mu-wpcom", - "reference": "07d370f097d8855b8280ca7cf3e36d42741b096d" + "reference": "016e16c97a3d77224474d71fbd508ce51380de8c" }, "require-dev": { "automattic/jetpack-changelogger": "^3.3.8", @@ -78,7 +78,7 @@ "dist": { "type": "path", "url": "/tmp/jetpack-build/Automattic/jetpack-changelogger", - "reference": "0abb2aa1cbdc7784fd719744c1dd494400c04a77" + "reference": "7da361b25bb55c635e6e7dacc907693c41b285cf" }, "require": { "php": ">=5.6", diff --git a/vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md b/vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md index 6a6de05f..0514720d 100644 --- a/vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md +++ b/vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 This is an alpha version! The changes listed here are not final. +### Added +- Add updater for WPCOM Marketplace plugins + ### Changed - Update version numbers diff --git a/vendor/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php b/vendor/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php index 796e779b..592bdf0a 100644 --- a/vendor/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php +++ b/vendor/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php @@ -37,6 +37,8 @@ public static function init() { add_action( 'plugins_loaded', array( __CLASS__, 'load_launchpad' ), 0 ); add_action( 'plugins_loaded', array( __CLASS__, 'load_block_theme_previews' ) ); + add_action( 'plugins_loaded', array( __CLASS__, 'load_marketplace_products_updater' ) ); + // Unified navigation fix for changes in WordPress 6.2. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'unbind_focusout_on_wp_admin_bar_menu_toggle' ) ); @@ -144,4 +146,15 @@ public static function load_block_theme_previews() { public static function unbind_focusout_on_wp_admin_bar_menu_toggle() { wp_add_inline_script( 'common', '(function($){ $(document).on("wp-responsive-activate", function(){ $(".is-nav-unification #wp-admin-bar-menu-toggle, .is-nav-unification #adminmenumain").off("focusout"); } ); }(jQuery) );' ); } + + /** + * Load WPCOM Marketplace products updates provider. + * + * @return void + */ + public static function load_marketplace_products_updater() { + require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php'; + + \Marketplace_Products_Updater::init(); + } } diff --git a/vendor/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php b/vendor/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php new file mode 100644 index 00000000..a52936f1 --- /dev/null +++ b/vendor/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php @@ -0,0 +1,178 @@ + $remote_plugin_info['slug'], + 'plugin' => $filename, + 'new_version' => $remote_plugin_info['version'], + 'package' => $remote_plugin_info['download_link'], + ); + + if ( version_compare( $local_plugin_info['Version'], $update->new_version, '<' ) ) { + $transient->response[ $update->plugin ] = $update; + + unset( $transient->no_update[ $filename ] ); + } else { + // Clear package since we don't want to store download link under current version. + $update->package = ''; + + $transient->no_update[ $update->plugin ] = $update; + + unset( $transient->response[ $filename ] ); + } + } + + return $transient; + } + + /** + * Fetch and process themes updates. + * + * @param object $transient The update_themes transient object. + * + * @return object. + */ + public static function transient_update_themes( $transient ) { + $updates = self::fetch_updates( 'themes' ); + + foreach ( $updates as $remote_theme_info ) { + $slug = $remote_theme_info['slug']; + + $local_theme_info = wp_get_theme( $slug ); + + // Do not attempt to add update if theme info is not found. Maybe theme does not exists on the site. + if ( ! $local_theme_info->exists() ) { + continue; + } + + $update = array( + 'theme' => $slug, + 'package' => $remote_theme_info['download_link'], + 'new_version' => $remote_theme_info['version'], + ); + + if ( version_compare( $local_theme_info->get( 'Version' ), $update['new_version'], '<' ) ) { + $transient->response[ $slug ] = $update; + } else { + // Clear package since we don't want to store download link under current version. + $update['package'] = ''; + + $transient->no_update[ $slug ] = $update; + } + } + + return $transient; + } + + /** + * Fetch the product updates from WPCOM servers. + * + * @param string $type The update type, mainly plugins or themes. + * + * @return array + */ + public static function fetch_updates( $type ) { + if ( ! method_exists( 'Automattic\Jetpack\Connection\Client', 'wpcom_json_api_request_as_blog' ) ) { + return array(); + } + + $cache_key = sprintf( '_wpcom_marketplace_%s_updates', $type ); + $updates = get_transient( $cache_key ); + + if ( false !== $updates ) { + return $updates; + } + + $response = Client::wpcom_json_api_request_as_blog( + sprintf( '/marketplace/%s/updates', $type ), + '2', + array(), + null, + 'wpcom' + ); + + if ( is_wp_error( $response ) ) { + return array(); + } + + $updates = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( ! is_array( $updates ) || ! array_key_exists( 'updates', $updates ) ) { + return array(); + } + + set_transient( $cache_key, $updates['updates'], 12 * HOUR_IN_SECONDS ); + + return $updates['updates']; + } + + /** + * Build plugin path + * + * @param mixed $slug the plugin slug. + * + * @return string + */ + public static function build_plugin_path( $slug ) { + return sprintf( '%s/%s.php', $slug, $slug ); + } + + /** + * Get the local plugin data. + * + * @param string $filename The plugin file path. + * @return string + */ + public static function get_local_plugin_data( $filename ) { + if ( ! function_exists( 'get_plugin_data' ) ) { + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + + return get_plugin_data( WP_PLUGIN_DIR . '/' . $filename ); + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index bb4f7435..b52d15ff 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -9,5 +9,6 @@ 'Automattic\\Jetpack\\Jetpack_Mu_Wpcom' => $vendorDir . '/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php', 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', 'Launchpad_Task_Lists' => $vendorDir . '/automattic/jetpack-mu-wpcom/src/features/launchpad/class-launchpad-task-lists.php', + 'Marketplace_Products_Updater' => $vendorDir . '/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php', 'WPCOM_REST_API_V2_Endpoint_Launchpad' => $vendorDir . '/automattic/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-launchpad.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 8f46ba8d..103d5c65 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -10,6 +10,7 @@ class ComposerStaticInitd9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_plugin 'Automattic\\Jetpack\\Jetpack_Mu_Wpcom' => __DIR__ . '/..' . '/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php', 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', 'Launchpad_Task_Lists' => __DIR__ . '/..' . '/automattic/jetpack-mu-wpcom/src/features/launchpad/class-launchpad-task-lists.php', + 'Marketplace_Products_Updater' => __DIR__ . '/..' . '/automattic/jetpack-mu-wpcom/src/features/marketplace-products-updater/class-marketplace-products-updater.php', 'WPCOM_REST_API_V2_Endpoint_Launchpad' => __DIR__ . '/..' . '/automattic/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-launchpad.php', ); diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 1412927d..1de930ac 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -2,12 +2,12 @@ "packages": [ { "name": "automattic/jetpack-mu-wpcom", - "version": "4.8.0-alpha.1694038209", - "version_normalized": "4.8.0.0-alpha1694038209", + "version": "4.8.0-alpha.1694077071", + "version_normalized": "4.8.0.0-alpha1694077071", "dist": { "type": "path", "url": "/tmp/jetpack-build/Automattic/jetpack-mu-wpcom", - "reference": "07d370f097d8855b8280ca7cf3e36d42741b096d" + "reference": "016e16c97a3d77224474d71fbd508ce51380de8c" }, "require-dev": { "automattic/jetpack-changelogger": "^3.3.8", diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 7f8297bf..016df567 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -11,9 +11,9 @@ ), 'versions' => array( 'automattic/jetpack-mu-wpcom' => array( - 'pretty_version' => '4.8.0-alpha.1694038209', - 'version' => '4.8.0.0-alpha1694038209', - 'reference' => '07d370f097d8855b8280ca7cf3e36d42741b096d', + 'pretty_version' => '4.8.0-alpha.1694077071', + 'version' => '4.8.0.0-alpha1694077071', + 'reference' => '016e16c97a3d77224474d71fbd508ce51380de8c', 'type' => 'jetpack-library', 'install_path' => __DIR__ . '/../automattic/jetpack-mu-wpcom', 'aliases' => array(),