Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WPCom_Themes_Mapper | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| map_wpcom_to_wporg | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
2 | |||
| build_theme_tags | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPCom_Themes_Mapper. |
| 4 | * Responsible for mapping theme objects between WPCom and WPOrg formats. |
| 5 | * |
| 6 | * @package wpcom-themes |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Maps theme objects between WPCom and WPOrg formats. |
| 11 | */ |
| 12 | class 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 = $wpcom_theme->demo_uri . '?demo=true&iframe=true&theme_preview=true'; |
| 50 | $theme->author = array( 'display_name' => $wpcom_theme->author ); |
| 51 | $theme->screenshot_url = $wpcom_theme->screenshot; |
| 52 | $theme->homepage = "https://wordpress.com/theme/$wpcom_theme->id"; |
| 53 | $theme->description = $wpcom_theme->description; |
| 54 | |
| 55 | // Some themes returned by the API do not have a download URI, but they are installable since they're managed by WP.com. |
| 56 | // 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. |
| 57 | $theme->download_link = $wpcom_theme->id; |
| 58 | $theme->is_commercial = false; |
| 59 | $theme->external_support_url = false; |
| 60 | $theme->is_community = false; |
| 61 | $theme->compatible_wp = true; |
| 62 | $theme->compatible_php = true; |
| 63 | $theme->num_ratings = 0; |
| 64 | $theme->rating = 0; |
| 65 | $theme->requires = '5.8'; |
| 66 | $theme->requires_php = '7.4'; |
| 67 | $theme->active = $wpcom_theme->id === $current_theme->get_stylesheet(); |
| 68 | $theme->installed = $wp_theme->exists(); |
| 69 | $theme->block_theme = $wpcom_theme->block_theme; |
| 70 | $theme->version = $wpcom_theme->version; |
| 71 | $theme->creation_time = $wpcom_theme->date_added; |
| 72 | $theme->is_wpcom_theme = true; |
| 73 | $theme->tags = $this->build_theme_tags( $wpcom_theme ); |
| 74 | $theme->theme_tier = $wpcom_theme->theme_tier->slug ?? null; |
| 75 | $theme->is_retired = $wpcom_theme->retired ?? false; |
| 76 | |
| 77 | return $theme; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates an array of theme tags from a WPCom theme object. |
| 82 | * |
| 83 | * @param stdClass $theme WPCom theme object. |
| 84 | * |
| 85 | * @return array Theme tags. |
| 86 | */ |
| 87 | private function build_theme_tags( $theme ) { |
| 88 | $tags = array(); |
| 89 | |
| 90 | foreach ( $theme->taxonomies->theme_subject ?? array() as $subject ) { |
| 91 | $tags[] = $this::SUBJECT_MAP[ $subject->slug ] ?? $subject->slug; |
| 92 | } |
| 93 | |
| 94 | foreach ( (array) $theme->taxonomies as $taxonomy ) { |
| 95 | foreach ( $taxonomy as $item ) { |
| 96 | if ( isset( $item->slug ) ) { |
| 97 | $tags[] = $item->slug; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $tags; |
| 103 | } |
| 104 | } |