Skip to content
Merged
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: fixed

Themes: retry after a failed read of the WordPress.com theme list, rather than caching the failure for a day.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Themes: share the WordPress.com themes API, cache, mapper and tier reader with wpcomsh rather than each keeping a copy.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Class WPCom_Themes_Api.
* Retrieves themes from the WordPress.com themes API.
*
* @package wpcom-themes
* @package automattic/jetpack-mu-wpcom
*/

/**
Expand Down Expand Up @@ -51,8 +51,13 @@ protected function fetch_themes( string $cache_key, array $params = array() ): a

return $this->cache->run_cached(
$cache_key,
fn() => $this->handle_request( $url )->themes ?? array()
);
function () use ( $url ) {
$body = $this->handle_request( $url );

// Null, not an empty array: run_cached() stores anything else.
return isset( $body->themes ) && is_array( $body->themes ) ? $body->themes : null;
}
) ?? array();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Class WPCom_Themes_Cache.
* Caches WPCom themes.
*
* @package wpcom-themes
* @package automattic/jetpack-mu-wpcom
*/

/**
Expand All @@ -29,8 +29,15 @@ class WPCom_Themes_Cache {
public function run_cached( string $cache_key, callable $lambda, int $ttl = DAY_IN_SECONDS ) {
$data = wp_cache_get( $cache_key, self::CACHE_GROUP );

if ( false === $data || defined( 'IGNORE_CACHED_WPCOM_THEMES' ) ) {
$data = $lambda();
if ( false !== $data && ! defined( 'IGNORE_CACHED_WPCOM_THEMES' ) ) {
return $data;
}

$data = $lambda();

// Null means the read failed rather than came back empty, and a failure is not cached: a
// moment's trouble reaching WordPress.com would otherwise empty the theme lists for a day.
if ( null !== $data ) {
wp_cache_set( $cache_key, $data, self::CACHE_GROUP, $ttl );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Class WPCom_Themes_Mapper.
* Responsible for mapping theme objects between WPCom and WPOrg formats.
*
* @package wpcom-themes
* @package automattic/jetpack-mu-wpcom
*/

/**
Expand Down Expand Up @@ -46,7 +46,14 @@ public function map_wpcom_to_wporg( stdClass $wpcom_theme ): stdClass {
$theme = new stdClass();
$theme->name = $wpcom_theme->name;
$theme->slug = $wpcom_theme->id;
$theme->preview_url = $wpcom_theme->demo_uri . '?demo=true&iframe=true&theme_preview=true';
$theme->preview_url = add_query_arg(
array(
'demo' => 'true',
'iframe' => 'true',
'theme_preview' => 'true',
),
$wpcom_theme->demo_uri
);
$theme->author = array( 'display_name' => $wpcom_theme->author );
$theme->screenshot_url = $wpcom_theme->screenshot;
$theme->homepage = "https://wordpress.com/theme/$wpcom_theme->id";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Class WPCom_Themes_Tiers.
* What a site's plan includes, by theme tier.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Answers which theme tiers a site's plan includes.
*
* Entitlement only. Whether a surface shows a tier it cannot install is that surface's own
* decision: core's Add Themes tabs hide partner themes because they need their own subscription,
* while the Marketplace tab lists them to sell them.
*/
class WPCom_Themes_Tiers {

/**
* The tier every site can use.
*/
const FREE = 'free';

/**
* The tier of a theme as WordPress.com returns it.
*
* @param stdClass $theme A theme from the WordPress.com themes API.
* @return string Tier slug, premium for a theme that reports none.
*/
public static function tier_of( $theme ): string {
return $theme->theme_tier->slug ?? 'premium';
}

/**
* The capability name gating a tier.
*
* @param string $tier Tier slug.
* @return string
*/
public static function feature_for( string $tier ): string {
return $tier . '-themes';
}

/**
* Whether this site's plan includes a tier.
*
* Necessary but not sufficient for the partner tier, which every paid plan includes while each
* partner theme still needs its own subscription that no plan feature reports.
*
* @param string $tier Tier slug.
* @return bool True for the free tier, which needs no plan. For the rest, false when the
* platform cannot answer, so an unknown host offers nothing.
*/
public static function site_can_use( string $tier ): bool {
// There is no free-themes capability, so asking the platform would deny every free theme.
if ( self::FREE === $tier ) {
return true;
}

return function_exists( 'wpcom_site_has_feature' )
&& (bool) wpcom_site_has_feature( self::feature_for( $tier ) );
Comment thread
dsas marked this conversation as resolved.
}
}
Loading
Loading