PluginProbe
WP-Stateless – Google Cloud Storage / 2.3.2
WP-Stateless – Google Cloud Storage v2.3.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-api.php

class-api.php in WP-Stateless – Google Cloud Storage 2.3.2, at lib/classes/class-api.php

229 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API Handler
4 *
5 *
6 *
7 * @since 1.0.0
8 */
9 namespace wpCloud\StatelessMedia {
10
11 if( !class_exists( 'wpCloud\StatelessMedia\API' ) ) {
12
13
14 final class API {
15
16 /**
17 * API Status Endpoint.
18 *
19 * @return array
20 */
21 static public function status() {
22
23 return array(
24 "ok" => true,
25 "message" => "API up."
26 );
27
28 }
29
30 /**
31 * Jobs Endpoint.
32 *
33 * @return array
34 */
35 static public function jobs() {
36
37 return array(
38 "ok" => true,
39 "message" => "Job endpoint up.",
40 "jobs" => array()
41 );
42
43 }
44
45 /**
46 * Get settings Endpoint.
47 *
48 * @param $request
49 * @return array
50 */
51 static public function getSettings( $request ) {
52
53 if( !self::authRequest( $request ) ) {
54 return array("ok" => false, "message" => __( "Auth fail." ));
55 }
56
57 $settings = apply_filters('stateless::get_settings', array());
58
59 return array(
60 "ok" => true,
61 "message" => "getSettings endpoint.",
62 "settings" => $settings
63 );
64
65 }
66
67 /**
68 * Get media library Endpoint.
69 *
70 * @param $request
71 * @return array
72 */
73 static public function getMediaLibrary( $request ) {
74
75 if( !self::authRequest( $request ) ) {
76 return array("ok" => false, "message" => __( "Auth fail." ));
77 }
78
79 if( !self::switchBlog( $request ) ){
80 return array("ok" => false, "message" => __( "Missed blog param." ));
81 }
82
83 $query_images_args = array(
84 'post_type' => 'attachment',
85 'post_mime_type' =>'image',
86 'post_status' => 'inherit',
87 'posts_per_page' => -1,
88 );
89
90 $query_images = new \WP_Query( $query_images_args );
91 $media = array();
92 foreach ( $query_images->posts as $image) {
93 $media[] = self::mediaMapping($image);
94 }
95
96 return array(
97 "ok" => true,
98 "message" => "getMediaLibrary endpoint.",
99 "mediaLibrary" => $media
100 );
101
102 }
103
104 /**
105 * Get media item Endpoint.
106 *
107 * @param $request
108 * @return array
109 */
110 static public function getMediaItem( $request ) {
111
112 if( !self::authRequest( $request ) ) {
113 return array("ok" => false, "message" => __( "Auth fail." ));
114 }
115
116 if( !self::switchBlog( $request ) ){
117 return array("ok" => false, "message" => __( "Missed blog param." ));
118 }
119
120 $attachment_id = $request->get_param('attachment_id');
121 if(!$attachment_id){
122 return array("ok" => false, "message" => __( "Missing attachment id." ));
123 }
124
125 $attachment = get_post($attachment_id);
126
127 if(!$attachment){
128 return array("ok" => false, "message" => __( "Wrong attachment id." ));
129 }
130
131 $item = self::mediaMapping($attachment);
132
133 return array(
134 "ok" => true,
135 "message" => "getMediaItem endpoint.",
136 "mediaItem" => $item
137 );
138
139 }
140
141 /**
142 * Handle Auth.
143 *
144 * @param $request
145 * @return bool
146 */
147 static public function authRequest( $request = false ) {
148
149 if( !$request ) {
150 return false;
151 }
152
153 if( !$request->get_param('key') ) {
154 return false;
155 }
156
157 $settings = apply_filters('stateless::get_settings', array());
158
159 if( !$settings[ 'api_key' ] ) {
160 return false;
161 }
162
163 if( $request->get_param('key') !== $settings[ 'api_key' ] ) {
164 return false;
165 }
166
167 return true;
168
169
170 }
171
172 /**
173 * Check blog param and switch to requested blog
174 *
175 * @param bool $request
176 * @return bool
177 */
178 static public function switchBlog( $request = false ){
179
180 if(!is_multisite()){
181 return true;
182 }
183
184 if( !$request ) {
185 return false;
186 }
187
188 $blog_id = $request->get_param('blog');
189 if( !$blog_id ) {
190 return false;
191 }
192
193 $current_blog_id = get_current_blog_id();
194 if($current_blog_id == $blog_id){
195 return true;
196 }
197
198 return switch_to_blog( $blog_id );
199 }
200
201 /**
202 * Applying mapping for media item
203 *
204 * @param $image
205 * @return array
206 */
207 static private function mediaMapping($image){
208
209 if(!$image){
210 return array();
211 }
212
213 return array(
214 'attachment_id' => $image->ID,
215 'date_create' => $image->post_date,
216 'parent' => $image->post_parent,
217 'link' => wp_get_attachment_url($image->ID),
218 'title' => $image->post_title,
219 'description' => $image->post_content,
220 'thumbnail' => wp_get_attachment_image_src($image->ID)[0]
221 );
222 }
223
224 }
225
226 }
227
228 }
229