PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.1
WP-Stateless – Google Cloud Storage v2.2.1
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.2.1, at lib/classes/class-api.php

231 lines 5.0 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 //die( '<pre>' . print_r($request->get_param('key'),true) . '</pre>' );
150
151 if( !$request ) {
152 return false;
153 }
154
155 if( !$request->get_param('key') ) {
156 return false;
157 }
158
159 $settings = apply_filters('stateless::get_settings', array());
160
161 if( !$settings[ 'api_key' ] ) {
162 return false;
163 }
164
165 if( $request->get_param('key') !== $settings[ 'api_key' ] ) {
166 return false;
167 }
168
169 return true;
170
171
172 }
173
174 /**
175 * Check blog param and switch to requested blog
176 *
177 * @param bool $request
178 * @return bool
179 */
180 static public function switchBlog( $request = false ){
181
182 if(!is_multisite()){
183 return true;
184 }
185
186 if( !$request ) {
187 return false;
188 }
189
190 $blog_id = $request->get_param('blog');
191 if( !$blog_id ) {
192 return false;
193 }
194
195 $current_blog_id = get_current_blog_id();
196 if($current_blog_id == $blog_id){
197 return true;
198 }
199
200 return switch_to_blog( $blog_id );
201 }
202
203 /**
204 * Applying mapping for media item
205 *
206 * @param $image
207 * @return array
208 */
209 static private function mediaMapping($image){
210
211 if(!$image){
212 return array();
213 }
214
215 return array(
216 'attachment_id' => $image->ID,
217 'date_create' => $image->post_date,
218 'parent' => $image->post_parent,
219 'link' => wp_get_attachment_url($image->ID),
220 'title' => $image->post_title,
221 'description' => $image->post_content,
222 'thumbnail' => wp_get_attachment_image_src($image->ID)[0]
223 );
224 }
225
226 }
227
228 }
229
230 }
231