Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 73 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
SitePagesController | |
0.00% |
0 / 73 |
|
0.00% |
0 / 5 |
462 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
publish_site_pages | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
set_homepage | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
set_site_pages | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
publish_page | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
4 | |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
7 | use NewfoldLabs\WP\Module\Onboarding\Data\Patterns; |
8 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\WonderBlocksService; |
9 | |
10 | /** |
11 | * Class SitePagesController |
12 | */ |
13 | class SitePagesController { |
14 | /** |
15 | * The namespace of this controller's route. |
16 | * |
17 | * @var string |
18 | */ |
19 | protected $namespace = 'newfold-onboarding/v1'; |
20 | |
21 | /** |
22 | * The endpoint base |
23 | * |
24 | * @var string |
25 | */ |
26 | protected $rest_base = '/site-pages'; |
27 | |
28 | /** |
29 | * Registers rest routes for SitePagesController class. |
30 | * |
31 | * @return void |
32 | */ |
33 | public function register_routes() { |
34 | \register_rest_route( |
35 | $this->namespace, |
36 | $this->rest_base . '/publish', |
37 | array( |
38 | 'methods' => \WP_REST_Server::CREATABLE, |
39 | 'callback' => array( $this, 'publish_site_pages' ), |
40 | 'permission_callback' => array( Permissions::class, 'custom_post_authorized_admin' ), |
41 | ) |
42 | ); |
43 | } |
44 | |
45 | /** |
46 | * Endpoint create_page |
47 | * |
48 | * @return \WP_REST_Response|\WP_Error |
49 | */ |
50 | public function publish_site_pages() { |
51 | $flow_data_option = \get_option( Options::get_option_name( 'flow' ), false ); |
52 | if ( false === $flow_data_option || ! isset( $flow_data_option['data'] ) ) { |
53 | return new \WP_Error( |
54 | 'nfd_onboarding_error', |
55 | 'Flow data does not exist to publish site pages', |
56 | array( 'status' => 500 ) |
57 | ); |
58 | } |
59 | |
60 | $flow_data = $flow_data_option['data']; |
61 | |
62 | $homepage_set = $this->set_homepage( $flow_data['sitePages']['homepage'] ); |
63 | if ( is_wp_error( $homepage_set ) ) { |
64 | return $homepage_set; |
65 | } |
66 | |
67 | $site_pages_set = $this->set_site_pages( $flow_data['sitePages']['other'] ); |
68 | if ( is_wp_error( $site_pages_set ) ) { |
69 | return $site_pages_set; |
70 | } |
71 | |
72 | return new \WP_REST_Response( |
73 | array(), |
74 | 201 |
75 | ); |
76 | } |
77 | |
78 | /** |
79 | * Set the homepage pattern chosen |
80 | * |
81 | * @param string $homepage_pattern_slug Homepage Pattern |
82 | * |
83 | * @return boolean|\WP_Error |
84 | */ |
85 | private function set_homepage( $homepage_pattern_slug ) { |
86 | if ( empty( $homepage_pattern_slug ) ) { |
87 | return true; |
88 | } |
89 | |
90 | $pattern_data = Patterns::get_pattern_from_slug( $homepage_pattern_slug ); |
91 | if ( ! $pattern_data ) { |
92 | return new \WP_Error( |
93 | 'nfd_onboarding_error', |
94 | "Page Pattern for $homepage_pattern_slug not found.", |
95 | array( 'status' => 500 ) |
96 | ); |
97 | } |
98 | |
99 | $show_pages_on_front = \get_option( Options::get_option_name( 'show_on_front', false ) ); |
100 | |
101 | // Check if default homepage is posts |
102 | if ( 'posts' === $show_pages_on_front ) { |
103 | \update_option( Options::get_option_name( 'show_on_front', false ), 'page' ); |
104 | } |
105 | |
106 | $post_id = $this->publish_page( __( 'Home', 'wp-module-onboarding' ), $pattern_data['content'], true, $pattern_data['meta'] ); |
107 | |
108 | if ( is_wp_error( $post_id ) ) { |
109 | return $post_id; |
110 | } |
111 | |
112 | \update_option( Options::get_option_name( 'page_on_front', false ), $post_id ); |
113 | |
114 | if ( WonderBlocksService::is_valid_slug( $homepage_pattern_slug ) ) { |
115 | WonderBlocksService::delete_templates_cache_from_slug( $homepage_pattern_slug ); |
116 | } |
117 | |
118 | return true; |
119 | } |
120 | |
121 | /** |
122 | * Set the site page chosen |
123 | * |
124 | * @param array $site_pages_pattern_slugs Homepage Pattern |
125 | * |
126 | * @return boolean|\WP_Error |
127 | */ |
128 | private function set_site_pages( $site_pages_pattern_slugs ) { |
129 | if ( empty( $site_pages_pattern_slugs ) ) { |
130 | return true; |
131 | } |
132 | |
133 | foreach ( $site_pages_pattern_slugs as $key => $site_page ) { |
134 | $pattern_data = Patterns::get_pattern_from_slug( $site_page['slug'] ); |
135 | if ( ! $pattern_data ) { |
136 | return new \WP_Error( |
137 | 'nfd_onboarding_error', |
138 | "Page Pattern for $site_page[slug] not found.", |
139 | array( 'status' => 500 ) |
140 | ); |
141 | } |
142 | $page_data = $this->publish_page( $site_page['title'], $pattern_data['content'], false, $pattern_data['meta'] ); |
143 | if ( is_wp_error( $page_data ) ) { |
144 | return $page_data; |
145 | } |
146 | |
147 | if ( WonderBlocksService::is_valid_slug( $site_page['slug'] ) ) { |
148 | WonderBlocksService::delete_templates_cache_from_slug( $site_page['slug'] ); |
149 | } |
150 | } |
151 | return true; |
152 | } |
153 | |
154 | /** |
155 | * Set the Publish Page |
156 | * |
157 | * @param string $title Site Page Title |
158 | * @param string $content Pattern Content |
159 | * @param boolean $is_template_no_title Check for Title |
160 | * @param array $meta The page post_meta. |
161 | * |
162 | * @return int|\WP_Error |
163 | */ |
164 | private function publish_page( $title, $content, $is_template_no_title = false, $meta = false ) { |
165 | $post = array( |
166 | 'post_title' => $title, |
167 | 'post_status' => 'publish', |
168 | 'post_content' => $content, |
169 | 'post_type' => 'page', |
170 | ); |
171 | |
172 | if ( $meta ) { |
173 | $post['meta_input'] = $meta; |
174 | } |
175 | |
176 | if ( $is_template_no_title ) { |
177 | $post['page_template'] = 'no-title'; |
178 | } |
179 | |
180 | return \wp_insert_post( $post ); |
181 | } |
182 | } |