Skip to content

Commit

Permalink
WPCOM Marketplace: Add updater for providing updates for Marketplace …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
gavande1 authored and matticbot committed Sep 7, 2023
1 parent 7617fc0 commit 17f392c
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 10 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions vendor/automattic/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );

Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* Marketplace Product Updates Provider
*
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Connection\Client;

/**
* Marketplace Product Updates
*
* This file hooks into the WordPress core's update API to provides new versions updates
* related to the WPCOM Marketplace products.
*
* @package automattic/jetpack-mu-wpcom
*/
class Marketplace_Products_Updater {
/**
* Register the hooks.
*
* @return void
*/
public static function init() {
add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'transient_update_plugins' ), 100 );
add_filter( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 100 );
}

/**
* Fetch and process plugin updates.
*
* @param object $transient The update_plugins transient object.
*
* @return object The same or a modified version of the transient.
*/
public static function transient_update_plugins( $transient ) {
$updates = self::fetch_updates( 'plugins' );

foreach ( $updates as $remote_plugin_info ) {
$filename = self::build_plugin_path( $remote_plugin_info['slug'] );

$local_plugin_info = self::get_local_plugin_data( $filename );

// Do not attempt to append update if plugin info not found. Maybe plugin does not exists on the site.
if ( is_wp_error( $local_plugin_info ) ) {
continue;
}

$update = (object) array(
'slug' => $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 );
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 17f392c

Please sign in to comment.