PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.33
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.33
3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 2.44 All 141 releases
photonic / Platforms / DeviantArt.php

DeviantArt.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.33, at Platforms/DeviantArt.php

313 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Photonic_Plugin\Platforms;
3
4 use Photonic_Plugin\Components\Album_List;
5 use Photonic_Plugin\Components\Error;
6 use Photonic_Plugin\Components\Pagination;
7 use Photonic_Plugin\Components\Photo_List;
8 use Photonic_Plugin\Core\Photonic;
9 use Photonic_Plugin\Components\Album;
10 use Photonic_Plugin\Components\Photo;
11
12 require_once 'OAuth2.php';
13 require_once 'Level_One_Module.php';
14 require_once 'Level_Two_Module.php';
15 require_once 'Pageable.php';
16
17 /**
18 * Fetches photos from a user's Google Photos account.
19 * Lacks support for dual title / description fields, doesn't provide download URLs, and video support is ambiguous.
20 */
21 class DeviantArt extends OAuth2 implements Level_One_Module, Level_Two_Module, Pageable {
22 public $refresh_token_valid;
23
24 protected function __construct() {
25 parent::__construct();
26 global $photonic_deviantart_client_id, $photonic_deviantart_client_secret, $photonic_deviantart_refresh_token;
27
28 if (!empty($photonic_deviantart_client_id) && !empty($photonic_deviantart_client_secret)) {
29 $this->client_id = trim($photonic_deviantart_client_id);
30 $this->client_secret = trim($photonic_deviantart_client_secret);
31
32 $transient_refresh = get_transient('photonic_deviantart_refresh_token_' . $this->client_id);
33 if (!empty($transient_refresh)) {
34 $photonic_deviantart_refresh_token = $transient_refresh;
35 }
36 }
37
38 $this->provider = 'deviantart';
39 $this->oauth_version = '2.0';
40 $this->response_type = 'code';
41 $this->scope = 'basic';
42 $this->link_lightbox_title = false; // empty($photonic_google_disable_title_link);
43 $this->oauth_done = false;
44
45 // Documentation
46 $this->doc_links = [
47 'general' => 'https://aquoid.com/plugins/photonic/deviantart/',
48 'photos' => 'https://aquoid.com/plugins/photonic/deviantart/',
49 'albums' => 'https://aquoid.com/plugins/photonic/deviantart/',
50 ];
51
52 $this->authenticate($photonic_deviantart_refresh_token);
53 }
54
55 public function get_gallery_images($attr = []): array {
56 global $photonic_deviantart_refresh_token;
57
58 $this->push_to_stack('Get Gallery Images');
59 $attr = array_merge(
60 $this->common_parameters,
61 [
62 'caption' => 'title',
63 'thumb_size' => '150',
64 'main_size' => '1600',
65 'tile_size' => '1600',
66 'crop_thumb' => 'crop',
67
68 // Google ...
69 'count' => 100,
70 'media' => 'photos',
71 'video_size' => 'dv',
72 'date_filters' => '',
73 'content_filters' => '',
74 'access' => 'all',
75 ],
76 $attr
77 );
78
79 $attr = array_map('trim', $attr);
80
81 $attr['overlay_size'] = empty($attr['overlay_size']) ? $attr['thumb_size'] : $attr['overlay_size'];
82 $attr['overlay_video_size'] = empty($attr['overlay_video_size']) ? $attr['video_size'] : $attr['overlay_video_size'];
83 $attr['overlay_crop'] = empty($attr['overlay_crop']) ? $attr['crop_thumb'] : $attr['overlay_crop'];
84
85 if (empty($this->client_id)) {
86 $this->pop_from_stack();
87 return [new Error(esc_html__('DeviantArt Client ID not defined.', 'photonic') . Photonic::doc_link($this->doc_links['general']))];
88 }
89
90 if (empty($this->client_secret)) {
91 $this->pop_from_stack();
92 return [new Error(esc_html__('DeviantArt Client Secret not defined.', 'photonic') . Photonic::doc_link($this->doc_links['general']))];
93 }
94
95 if (empty($photonic_deviantart_refresh_token)) {
96 $this->pop_from_stack();
97 return [new Error(sprintf(esc_html__('DeviantArt Refresh Token not defined. Please authenticate from %s.', 'photonic'), '<em>Photonic &rarr; Authentication</em>') . Photonic::doc_link($this->doc_links['general']))];
98 }
99
100 if (!$this->refresh_token_valid) {
101 $this->pop_from_stack();
102 $error = sprintf(esc_html__('DeviantArt Refresh Token invalid. Please authenticate from %s.', 'photonic'), '<em>Photonic &rarr; Authentication</em>');
103 if (!empty($this->auth_error)) {
104 $error .= '<br/>' . sprintf(esc_html__('Error encountered during authentication: %s', 'photonic'), '<br/><pre>' . $this->auth_error . '</pre>');
105 }
106 return [new Error($error . Photonic::doc_link($this->doc_links['general']))];
107 }
108
109 if (empty($attr['view'])) {
110 $this->pop_from_stack();
111 return [new Error(sprintf(esc_html__('The %s parameter is mandatory for the shortcode.', 'photonic'), '<code>view</code>'))];
112 }
113
114 $query_urls = [];
115 if ('galleries' === $attr['view']) {
116 $query_urls[] = 'https://www.deviantart.com/api/v1/oauth2/gallery/9E8178D7-1A81-78A8-84EE-60B224339310';
117 $query_urls[] = 'https://www.deviantart.com/api/v1/oauth2/deviation/A1405364-F308-5B73-131F-C51EBAFBC936';
118 }
119
120 $out = $this->make_call($query_urls, $attr);
121 $this->pop_from_stack();
122 if (!empty($this->stack_trace[$this->gallery_index])) {
123 $out[] = $this->stack_trace[$this->gallery_index];
124 }
125
126 return [];
127 }
128
129 private function make_call($query_urls, $attr): array {
130 global $photonic_deviantart_refresh_token;
131 $this->push_to_stack('Making calls');
132
133 $components = [];
134
135 foreach ($query_urls as $query_url) {
136 $this->push_to_stack("Query $query_url");
137 if (!empty($photonic_deviantart_refresh_token) && !empty($this->access_token)) {
138 $query_url = add_query_arg('access_token', $this->access_token, $query_url);
139 }
140
141 $response = wp_remote_request($query_url);
142 if (!is_wp_error($response)) {
143 $this->push_to_stack('Processing Response');
144 $body = wp_remote_retrieve_body($response);
145
146 $output = $this->process_response($body, $attr);
147
148 if (!is_null($output)) {
149 $components[] = $output;
150 }
151
152 $this->pop_from_stack();
153 }
154 else {
155 $this->pop_from_stack(); // "Query $query_url"
156 $this->pop_from_stack(); // 'Making calls'
157 return [new Error($response->get_error_message())];
158 }
159
160 $this->pop_from_stack();
161 }
162
163 $this->pop_from_stack();
164 return $components;
165 }
166
167 private function process_response($body, $short_code) {
168 global $photonic_google_photo_title_display, $photonic_google_photos_per_row_constraint, $photonic_gallery_template_page,
169 $photonic_google_photos_constrain_by_count, $photonic_google_photo_pop_title_display, $photonic_google_hide_album_photo_count_display;
170
171 if (!empty($body)) {
172 $body = json_decode($body);
173 $row_constraints = ['constraint-type' => $photonic_google_photos_per_row_constraint, 'count' => $photonic_google_photos_constrain_by_count];
174 $display = $short_code['display'];
175
176 if (isset($body->results)) {
177 print_r($body->results);
178 }
179 /* if (isset($body->albums) || isset($body->sharedAlbums)) {
180 $albums = $body->albums ?? $body->sharedAlbums;
181
182 $pagination = $this->get_pagination($body, $short_code);
183 $dummy_options = [];
184 $albums = $this->build_level_2_objects($albums, $short_code, $remove, $dummy_options, $pagination);
185
186 global $photonic_google_photos_layout_engine;
187 $layout_engine = $short_code['layout_engine'] ?? $photonic_google_photos_layout_engine;
188 if ('css' === $layout_engine) {
189 $this->update_thumbnail_information($albums, $short_code);
190 }
191
192 if ($deferred) {
193 return $albums;
194 }
195
196 $album_list = new Album_List($short_code);
197
198 $album_list->albums = $albums;
199 $album_list->row_constraints = $row_constraints;
200 $album_list->type = 'albums';
201 $album_list->singular_type = 'album';
202 $album_list->title_position = $photonic_google_photo_title_display;
203 $album_list->level_1_count_display = !empty($photonic_google_hide_album_photo_count_display);
204 $album_list->pagination = $pagination;
205 $album_list->album_opens_gallery = ('page' === $short_code['popup'] && !empty($photonic_gallery_template_page) && is_string(get_post_status($photonic_gallery_template_page)));
206
207 return $album_list;
208 }
209 elseif (isset($body->mediaItems)) {
210 if ('local' === $display) {
211 $title_position = $photonic_google_photo_title_display;
212 }
213 else {
214 $row_constraints = ['constraint-type' => 'padding'];
215 $title_position = $photonic_google_photo_pop_title_display;
216 }
217
218 $pagination = $this->get_pagination($body, $short_code);
219
220 $photos = $body->mediaItems;
221 $photos = $this->build_level_1_objects($photos, $short_code);
222
223 $photo_list = new Photo_List($short_code);
224 $photo_list->photos = $photos;
225 $photo_list->title_position = $title_position;
226 $photo_list->row_constraints = $row_constraints;
227 $photo_list->parent = 'album';
228 $photo_list->pagination = $pagination;
229
230 return $photo_list;
231 }
232 elseif (isset($body->error)) {
233 $err = esc_html__('Failed to get data. Error:', 'photonic') . "<br/><code>\n";
234 $err .= $body->error->message;
235 $err .= "</code><br/>\n";
236
237 return new Error($err);
238 }*/
239 }
240 else {
241 $err = esc_html__('Failed to get data. Error:', 'photonic') . "<br/><code>\n";
242 $err .= $body;
243 $err .= "</code><br/>\n";
244
245 return new Error($err);
246 }
247
248 return null;
249 }
250
251 public function build_level_1_objects($response, array $short_code, $module_parameters = [], $options = []): array {
252 // TODO: Implement build_level_1_objects() method.
253 }
254
255 public function build_level_2_objects($objects_or_response, array $short_code, array $filter_list = [], array &$options = [], ?Pagination &$pagination = null): array {
256 // TODO: Implement build_level_2_objects() method.
257 }
258
259 public function authentication_URL() {
260 return 'https://www.deviantart.com/oauth2/authorize';
261 }
262
263 public function access_token_URL() {
264 return 'https://www.deviantart.com/oauth2/token';
265 }
266
267 public function renew_token($refresh_token): array {
268 $token = [];
269 $error = '';
270 //print_r($refresh_token."<br/>");
271 $response = Photonic::http(
272 $this->access_token_URL(),
273 'GET',
274 [
275 'client_id' => $this->client_id,
276 'client_secret' => $this->client_secret,
277 'refresh_token' => $refresh_token,
278 'grant_type' => 'refresh_token'
279 ]
280 );
281 //print_r($response);
282
283 if (!is_wp_error($response)) {
284 $token = $this->parse_token($response);
285 if (!empty($token)) {
286 $token['client_id'] = $this->client_id;
287 }
288 set_transient('photonic_' . $this->provider . '_token', $token, $token['oauth_token_expires']);
289 set_transient('photonic_deviantart_refresh_token_' . $this->client_id, $token['oauth_refresh_token'], 90 * 24 * 3600);
290 if (empty($token)) {
291 $error = print_r(wp_remote_retrieve_body($response), true);
292 }
293 }
294 else {
295 $error = $response->get_error_message();
296 }
297
298 return [$token, $error];
299 }
300
301 protected function set_token_validity($validity) {
302 $this->refresh_token_valid = $validity;
303 }
304
305 public function is_token_expiring_soon($soon_limit) {
306 // TODO: Implement is_token_expiring_soon() method.
307 }
308
309 public function get_pagination($entity, array $short_code = []): Pagination {
310 // TODO: Implement get_pagination() method.
311 }
312 }
313