Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
42 / 46
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCom_Themes_Mapper
91.30% covered (success)
91.30%
42 / 46
50.00% covered (danger)
50.00%
1 / 2
6.02
0.00% covered (danger)
0.00%
0 / 1
 map_wpcom_to_wporg
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
1
 build_theme_tags
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
8.12
1<?php
2/**
3 * Class WPCom_Themes_Mapper.
4 * Responsible for mapping theme objects between WPCom and WPOrg formats.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 */
8
9/**
10 * Maps theme objects between WPCom and WPOrg formats.
11 */
12class WPCom_Themes_Mapper {
13
14    /**
15     * Maps a WPCom theme subject to .org theme subjects.
16     */
17    private const SUBJECT_MAP = array(
18        'business'             => 'e-commerce',
19        'store'                => 'e-commerce',
20        'real-estate'          => 'e-commerce',
21        'health-wellness'      => 'e-commerce',
22        'restaurant'           => 'food-and-drink',
23        'travel-lifestyle'     => 'holiday',
24        'art-design'           => 'photography',
25        'about'                => 'blog',
26        'authors-writers'      => 'blog',
27        'newsletter'           => 'news',
28        'magazine'             => 'news',
29        'music'                => 'portfolio',
30        'fashion-beauty'       => 'e-commerce',
31        'community-non-profit' => 'blog',
32        'podcast'              => 'portfolio',
33    );
34
35    /**
36     * Maps a WPCom theme object to a WPOrg theme object.
37     *
38     * @param stdClass $wpcom_theme WPCom theme object.
39     *
40     * @return stdClass WPOrg theme object.
41     */
42    public function map_wpcom_to_wporg( stdClass $wpcom_theme ): stdClass {
43        $wp_theme      = wp_get_theme( $wpcom_theme->id );
44        $current_theme = wp_get_theme();
45
46        $theme                 = new stdClass();
47        $theme->name           = $wpcom_theme->name;
48        $theme->slug           = $wpcom_theme->id;
49        $theme->preview_url    = add_query_arg(
50            array(
51                'demo'          => 'true',
52                'iframe'        => 'true',
53                'theme_preview' => 'true',
54            ),
55            $wpcom_theme->demo_uri
56        );
57        $theme->author         = array( 'display_name' => $wpcom_theme->author );
58        $theme->screenshot_url = $wpcom_theme->screenshot;
59        $theme->homepage       = "https://wordpress.com/theme/$wpcom_theme->id";
60        $theme->description    = $wpcom_theme->description;
61
62        // Some themes returned by the API do not have a download URI, but they are installable since they're managed by WP.com.
63        // In those cases we generate a fake download url so that we can find the theme by download url even though it's not a real download link.
64        $theme->download_link        = $wpcom_theme->id;
65        $theme->is_commercial        = false;
66        $theme->external_support_url = false;
67        $theme->is_community         = false;
68        $theme->compatible_wp        = true;
69        $theme->compatible_php       = true;
70        $theme->num_ratings          = 0;
71        $theme->rating               = 0;
72        $theme->requires             = '5.8';
73        $theme->requires_php         = '7.4';
74        $theme->active               = $wpcom_theme->id === $current_theme->get_stylesheet();
75        $theme->installed            = $wp_theme->exists();
76        $theme->block_theme          = $wpcom_theme->block_theme;
77        $theme->version              = $wpcom_theme->version;
78        $theme->creation_time        = $wpcom_theme->date_added;
79        $theme->is_wpcom_theme       = true;
80        $theme->tags                 = $this->build_theme_tags( $wpcom_theme );
81        $theme->theme_tier           = $wpcom_theme->theme_tier->slug ?? null;
82        $theme->is_retired           = $wpcom_theme->retired ?? false;
83        // Where it lives on WordPress.com, such as `pub/twentytwentyfive`, which a Simple site activates.
84        $theme->wpcom_stylesheet = $wpcom_theme->stylesheet ?? null;
85
86        return $theme;
87    }
88
89    /**
90     * Creates an array of theme tags from a WPCom theme object.
91     *
92     * @param stdClass $theme WPCom theme object.
93     *
94     * @return array Theme tags.
95     */
96    private function build_theme_tags( $theme ) {
97        $tags = array();
98
99        foreach ( $theme->taxonomies->theme_subject ?? array() as $subject ) {
100            $tags[] = $this::SUBJECT_MAP[ $subject->slug ] ?? $subject->slug;
101        }
102
103        foreach ( (array) $theme->taxonomies as $taxonomy ) {
104            foreach ( $taxonomy as $item ) {
105                if ( isset( $item->slug ) ) {
106                    $tags[] = $item->slug;
107                }
108            }
109        }
110
111        return $tags;
112    }
113}