Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApprovedThemesController | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
get_approved_theme_slugs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_installed_themes | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
get_approved_themes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi\Themes; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\Models\Theme; |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Data\Data; |
7 | |
8 | /** |
9 | * Class ApprovedThemeController |
10 | */ |
11 | class ApprovedThemesController extends \WP_REST_Controller { |
12 | |
13 | /** |
14 | * The namespace of this controller's route. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $namespace = 'newfold-onboarding/v1'; |
19 | |
20 | /** |
21 | * The base of this controller's route. |
22 | * |
23 | * @var string |
24 | */ |
25 | protected $rest_base = 'themes'; |
26 | |
27 | /** |
28 | * Registers the settings route |
29 | */ |
30 | public function register_routes() { |
31 | \register_rest_route( |
32 | $this->namespace, |
33 | '/' . $this->rest_base, |
34 | array( |
35 | array( |
36 | 'methods' => \WP_REST_Server::READABLE, |
37 | 'callback' => array( $this, 'get_approved_themes' ), |
38 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
39 | ), |
40 | ) |
41 | ); |
42 | } |
43 | |
44 | /** |
45 | * Get a list of approved themes. |
46 | * |
47 | * [TODO] Retrieve the list from Hiive. |
48 | * |
49 | * @return array|\WP_Error |
50 | */ |
51 | protected function get_approved_theme_slugs() { |
52 | |
53 | // Key-Value array implementation of approved themes for faster lookup |
54 | return array( 'twentytwentytwo' => 1 ); |
55 | } |
56 | |
57 | /** |
58 | * Retrieves list of installed themes. |
59 | * |
60 | * @return array|\WP_Error |
61 | */ |
62 | public function get_installed_themes() { |
63 | |
64 | $request = new \WP_REST_Request( |
65 | 'GET', |
66 | '/wp/v2/' . $this->rest_base |
67 | ); |
68 | $response = \rest_do_request( $request ); |
69 | |
70 | if ( 200 === $response->status ) { |
71 | $current_brand = Data::current_brand(); |
72 | $current_brand_name = $current_brand['name'] ? $current_brand['name'] : 'Newfold Digital'; |
73 | $themes_data = json_decode( \wp_json_encode( $response->data ), true ); |
74 | foreach ( $themes_data as $theme_data ) { |
75 | $theme = new Theme( $theme_data['stylesheet'] ); |
76 | $theme->set_theme_image( $theme_data['screenshot'] ); |
77 | if ( false !== strpos( $theme_data['author']['raw'], $current_brand_name ) ) { |
78 | $theme->set_is_newfold_theme( true ); |
79 | } |
80 | $installed_themes[] = $theme; |
81 | } |
82 | |
83 | return $installed_themes; |
84 | } |
85 | |
86 | return new \WP_Error( |
87 | $response->status, |
88 | 'Failed to fetch installed themes.' |
89 | ); |
90 | } |
91 | |
92 | /** |
93 | * Retrieves the themes offered by the Onboarding Module. |
94 | * |
95 | * @return array|\WP_Error |
96 | */ |
97 | public function get_approved_themes() { |
98 | $approved_theme_slugs = $this->get_approved_theme_slugs(); |
99 | $installed_themes = $this->get_installed_themes(); |
100 | |
101 | if ( \is_wp_error( $installed_themes ) ) { |
102 | return $installed_themes; |
103 | } |
104 | |
105 | foreach ( $installed_themes as $installed_theme ) { |
106 | if ( $installed_theme->get_is_newfold_theme() |
107 | || array_key_exists( $installed_theme->get_theme_name(), $approved_theme_slugs ) ) { |
108 | $approved_themes[] = $installed_theme; |
109 | } |
110 | } |
111 | |
112 | return $approved_themes; |
113 | } |
114 | } |