Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move WordPress.com themes api into jetpack-mu-wpcom package #41770

Open
wants to merge 20 commits into
base: update/php-8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Moved WordPress.com themes api work from wpcomsh into jetpack-mu-wpcom so it can be used on WPcom as well
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public static function load_features() {
require_once __DIR__ . '/features/wpcom-logout/wpcom-logout.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';

// We include WPCom Themes results and installation on WoA sites only and non-WP_CLI context.
// This is so that these features don't apply to Simple sites. We anticpate changing this in future
// because these features will be needed for Simple sites too.
if ( ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) {
require_once __DIR__ . '/features/wpcom-themes/wpcomsh-themes.php';
}

// Initializers, if needed.
\Marketplace_Products_Updater::init();
\Automattic\Jetpack\Classic_Theme_Helper\Main::init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package wpcom-themes
*/

if ( class_exists( 'WPCom_Themes_Api' ) ) {
return;
}
Comment on lines +9 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unnecessary.


/**
* Fetches themes from the WordPress.com themes API.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package wpcom-themes
*/

if ( class_exists( 'WPCom_Themes_Cache' ) ) {
return;
}
Comment on lines +9 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unnecessary.


/**
* Basic cache implementation for themes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package wpcom-themes
*/

if ( class_exists( 'WPCom_Themes_Mapper' ) ) {
return;
}
Comment on lines +9 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unnecessary.


/**
* Maps theme objects between WPCom and WPOrg formats.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package wpcom-themes
*/

if ( class_exists( 'WPCom_Themes_Merger' ) ) {
return;
}
Comment on lines +9 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unnecessary.


/**
* Merges theme objects between WPCom and WPOrg repositories.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package wpcom-themes
*/

if ( class_exists( 'WPCom_Themes_Service' ) ) {
return;
}
Comment on lines +9 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unnecessary.


/**
* WordPress.com themes service.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
/**
* Registers a filter for the themes API result to add WPCom themes.
*
* @package wpcom-themes
*/

/**
* Loads the WPCom themes service instance.
*
* @return WPCom_Themes_Service
*/
function wpcomsh_get_wpcom_themes_service_instance(): WPCom_Themes_Service {
static $instance;
if ( ! $instance ) {
// Load dependencies.
require_once __DIR__ . '/includes/class-wpcom-themes-mapper.php';
require_once __DIR__ . '/includes/class-wpcom-themes-merger.php';
require_once __DIR__ . '/includes/class-wpcom-themes-cache.php';
require_once __DIR__ . '/includes/class-wpcom-themes-service.php';
require_once __DIR__ . '/includes/class-wpcom-themes-api.php';

// Resolve and Inject dependencies.
$mapper = new WPCom_Themes_Mapper();
$merger = new WPCom_Themes_Merger();
$cache = new WPCom_Themes_Cache();
$api = new WPCom_Themes_Api( $cache );
$wpcom_themes_service = new WPCom_Themes_Service( $api, $mapper, $merger );

$instance = $wpcom_themes_service;
}

return $instance;
}

/**
* Process the themes API result and add the recommended WPCom themes to the Popular tab.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass
*/
function wpcomsh_popular_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
$browse = $args->browse ?? '';
if ( 'query_themes' !== $action || 'popular' !== $browse ) {
return $res;
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

// Add results to the resulting array.
return $wpcom_themes_service->filter_themes_api_result_recommended( $res );
}
add_filter( 'themes_api_result', 'wpcomsh_popular_wpcom_themes_api_result', 0, 3 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will want these to also run on WPcom in the untangling themes PR, right? Should we move them to run on WPcom now, or only in the untangling themes PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I a bit torn on if we should rename these functions and move the filters over entirely or not. I think it's best to leave it as is. We don't need any of this on WPcom yet. It's probably best to enable it in untangling themes as that's when it's actually needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree. If we keep this PR as just a code moving task, then we can consider changes in the future on a case by case basis. If we start changing things as well as moving them its gets harder to reason about.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when I thought about the deploy process for this - because wpcomsh and jetpack-mu-wpcom are deployed at different times, I think it does make more sense to rename the functions now - that way we avoid errors when a function is redefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about this too, but I think it's not an issue. I believe WoA sites run jetpack-mu-wpcom bundled as wpcomsh. They don't run the WPcom mu-plugin AND wpcomsh. So there should only be one version of anything running, even if deploy cycles are different. We should absolutely confirm to be sure though!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I wasn't sure exactly how they fit together but @zinigor suggested it might be a problem and I thought better safe than sorry, since we want to rename anyway.

Copy link
Member

@zinigor zinigor Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe WoA sites run jetpack-mu-wpcom bundled as wpcomsh. They don't run the WPcom mu-plugin AND wpcomsh.

That's correct! I sometimes get confused with what environment gets affected with what. WoA and Atomic sites should be fine in this regard. The WordPress.com environment though will have these files removed added, but not used?

If that's so, then I'm sorry for causing additional anxiety about deploying this.


/**
* Process the themes API result and add the latest WPCom themes to the Latest tab.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass
*/
function wpcomsh_latest_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
$browse = $args->browse ?? '';
if ( 'query_themes' !== $action || 'new' !== $browse ) {
return $res;
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

// Add results to the resulting array.
return $wpcom_themes_service->filter_themes_api_result_latest( $res );
}
add_filter( 'themes_api_result', 'wpcomsh_latest_wpcom_themes_api_result', 0, 3 );

/**
* Process the themes API result and add the WPCom block themes to the Block themes tab.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass
*/
function wpcomsh_block_themes_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
$tag = $args->tag ?? '';
if ( 'query_themes' !== $action || 'full-site-editing' !== $tag ) {
return $res;
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

// Add results to the resulting array.
return $wpcom_themes_service->filter_themes_api_result_block_themes( $res );
}
add_filter( 'themes_api_result', 'wpcomsh_block_themes_wpcom_themes_api_result', 0, 3 );

/**
* Process the themes API result and add WPCom themes when using the search.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass
*/
function wpcomsh_search_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
$search = $args->search ?? '';
if ( 'query_themes' !== $action || '' === $search ) {
return $res;
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

// Add results to the resulting array.
return $wpcom_themes_service->filter_themes_api_result_search( $res, $search );
}
add_filter( 'themes_api_result', 'wpcomsh_search_wpcom_themes_api_result', 0, 3 );

/**
* Process the themes API result and add WPCom themes when using the filter feature.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass
*/
function wpcomsh_feature_filter_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
$tags = $args->tag ?? array();
if ( 'query_themes' !== $action || ! $tags ) {
return $res;
}

if ( ! is_array( $tags ) ) {
$tags = array( $tags );
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

// Add results to the resulting array.
return $wpcom_themes_service->filter_themes_api_result_feature_filter( $res, $tags );
}
add_filter( 'themes_api_result', 'wpcomsh_feature_filter_wpcom_themes_api_result', 0, 3 );

/**
* Process the themes API result theme_information for WPCom themes if needed.
*
* @param mixed $res The result object.
* @param string $action The action.
* @param mixed $args The arguments.
*
* @return mixed|stdClass|WP_Error|null
*/
function wpcomsh_theme_information_wpcom_themes_api_result( $res, string $action, $args ) {
// Pre-requisites checks.
if ( 'theme_information' !== $action ) {
return $res;
}

$wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance();

return $wpcom_themes_service->get_theme( $args->slug ) ?? $res;
}
add_filter( 'themes_api_result', 'wpcomsh_theme_information_wpcom_themes_api_result', 0, 3 );

/**
* Include the creation date in the themes API response.
*
* @param array $args The arguments.
*
* @return array The arguments with the creation time included.
*/
function wpcomsh_include_themes_creation_date( $args ) {
$args['fields']['creation_time'] = true;

return $args;
}
add_filter( 'install_themes_table_api_args_new', 'wpcomsh_include_themes_creation_date' );
add_filter( 'install_themes_table_api_args_search', 'wpcomsh_include_themes_creation_date' );
2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/composer.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Moved WordPress.com themes api work from wpcomsh into jetpack-mu-wpcom so it can be used on WPcom as well
2 changes: 1 addition & 1 deletion projects/plugins/wpcomsh/composer.lock

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

Loading
Loading