PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.3.1
Responsive Lightbox & Gallery v2.3.1
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / providers / class-unsplash.php
responsive-lightbox / includes / providers Last commit date
class-flickr.php 5 years ago class-unsplash.php 5 years ago class-wikimedia.php 5 years ago
class-unsplash.php
210 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) )
3 exit;
4
5 /**
6 * Responsive Lightbox Remote Library Unsplash class.
7 *
8 * @class Responsive_Lightbox_Remote_Library_Unsplash
9 */
10 class Responsive_Lightbox_Remote_Library_Unsplash extends Responsive_Lightbox_Remote_Library_API {
11
12 /**
13 * Constructor.
14 *
15 * @return void
16 */
17 public function __construct() {
18 // provider slug
19 $this->slug = 'unsplash';
20
21 // provider name
22 $this->name = __( 'Unsplash', 'responsive-lightbox' );
23
24 // default values
25 $this->defaults = array(
26 'active' => false,
27 'api_key' => ''
28 );
29
30 // setting fields
31 $this->fields = array(
32 'title' => $this->name,
33 'section' => 'responsive_lightbox_remote_library_providers',
34 'type' => 'custom',
35 'callback' => array( $this, 'render_field' )
36 );
37
38 // add provider
39 parent::add_provider( $this );
40 }
41
42 /**
43 * Render field.
44 *
45 * @return void
46 */
47 public function render_field() {
48 echo '
49 <p><label><input id="rl_unsplash_active" class="rl-media-provider-expandable" type="checkbox" name="responsive_lightbox_remote_library[unsplash][active]" value="1" ' . checked( $this->rl->options['remote_library']['unsplash']['active'], true, false ) . ' />' . __( 'Enable Unsplash.', 'responsive-lightbox' ) . '</label></p>
50 <div class="rl-media-provider-options"' . ( $this->rl->options['remote_library']['unsplash']['active'] ? '' : ' style="display: none;"' ) . '>
51 <p><input id="rl_unsplash_api_key" class="large-text" placeholder="' . __( 'Access key', 'responsive-lightbox' ) . '" type="text" value="' . $this->rl->options['remote_library']['unsplash']['api_key'] . '" name="responsive_lightbox_remote_library[unsplash][api_key]"></p>
52 <p class="description">' . sprintf( __( 'Provide your <a href="%s">Unsplash API key</a>.', 'responsive-lightbox' ), 'https://unsplash.com/oauth/applications/new' ) . '</p>
53 </div>';
54 }
55
56 /**
57 * Validate settings.
58 *
59 * @param array $input POST data
60 * @return array Validated settings
61 */
62 public function validate_settings( $input ) {
63 if ( ! isset( $_POST['responsive_lightbox_remote_library'] ) )
64 $input['unsplash'] = $this->rl->defaults['remote_library']['unsplash'];
65 else {
66 // active
67 $input['unsplash']['active'] = isset( $_POST['responsive_lightbox_remote_library']['unsplash']['active'] );
68
69 // api key
70 $input['unsplash']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $_POST['responsive_lightbox_remote_library']['unsplash']['api_key'] );
71 }
72
73 return $input;
74 }
75
76 /**
77 * Prepare data to run remote query.
78 *
79 * @param string $search_phrase Search phrase
80 * @param array $args Provider arguments
81 * @return void
82 */
83 public function prepare_query( $search_phrase, $args = array() ) {
84 // check page parameter
85 if ( isset( $args['preview_page'] ) )
86 $args['preview_page'] = (int) $args['preview_page'];
87 else
88 $args['preview_page'] = 1;
89
90 if ( $args['preview_page'] < 1 )
91 $args['preview_page'] = 1;
92
93 // check per page parameter
94 if ( isset( $args['preview_per_page'] ) )
95 $args['preview_per_page'] = (int) $args['preview_per_page'];
96 else
97 $args['preview_per_page'] = 20;
98
99 if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 30 )
100 $args['preview_per_page'] = 20;
101
102 // set query arguments
103 $this->query_args = $args;
104
105 $query_args = array(
106 'per_page' => $args['preview_per_page'],
107 'page' => $args['preview_page'],
108 'order_by' => 'latest'
109 );
110
111 if ( $search_phrase !== '' ) {
112 $query_args['query'] = urlencode( $search_phrase );
113
114 $url = 'https://api.unsplash.com/search/photos';
115 } else
116 $url = 'https://api.unsplash.com/photos';
117
118 // set query string
119 $this->query = add_query_arg( $query_args, $url );
120
121 // set query remote arguments
122 $this->query_remote_args = array(
123 'timeout' => 30,
124 'headers' => array(
125 'Authorization' => 'Client-ID ' . $this->rl->options['remote_library']['unsplash']['api_key'],
126 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
127 )
128 );
129 }
130
131 /**
132 * Get images from media provider.
133 *
134 * @param mixed $response Remote response
135 * @param array $args Query arguments
136 * @return array Valid images or WP_Error
137 */
138 public function get_query_results( $response, $args = array() ) {
139 $results = array();
140 $error = new WP_Error( 'rl_remote_library_unsplash_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
141
142 // retrieve body
143 $response_body = wp_remote_retrieve_body( $response );
144
145 // any data?
146 if ( $response_body !== '' ) {
147 $response_json = json_decode( $response_body, true );
148
149 // invalid data?
150 if ( $response_json === null )
151 $results = $error;
152 else {
153 // search phrase query?
154 if ( $args['media_search'] !== '' ) {
155 // get results
156 $results = isset( $response_json['results'] ) && is_array( $response_json['results'] ) ? $response_json['results'] : array();
157
158 // sanitize results
159 $results = $this->sanitize_results( $results );
160 } else
161 $results = $this->sanitize_results( $response_json );
162 }
163 } else
164 $results = $error;
165
166 return $results;
167 }
168
169 /**
170 * Sanitize single result.
171 *
172 * @param array $result Single result
173 * @return mixed Array on success, otherwise false
174 */
175 public function sanitize_result( $result ) {
176 // set dimensions
177 $width = (int) $result['width'];
178 $height = (int) $result['height'];
179 $thumbnail_width = 200;
180
181 // calculate new height based on original ratio
182 $thumbnail_height = (int) floor( $thumbnail_width / ( $width / $height ) );
183
184 $imagedata = array(
185 'id' => 0,
186 'link' => '',
187 'source' => $result['links']['html'],
188 'title' => $result['id'],
189 'caption' => $this->get_attribution( 'Unsplash', $result['links']['html'], $result['user']['name'], $result['user']['links']['html'] ),
190 'description' => ! empty( $result['description'] ) ? $result['description'] : '',
191 'alt' => '',
192 'url' => $result['urls']['raw'],
193 'width' => $width,
194 'height' => $height,
195 'thumbnail_url' => $result['urls']['thumb'],
196 'thumbnail_width' => $thumbnail_width,
197 'thumbnail_height' => $thumbnail_height,
198 'media_provider' => 'unsplash',
199 'filename' => basename( $result['urls']['raw'] ),
200 'dimensions' => $width . ' x ' . $height
201 );
202
203 // create thumbnail link
204 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
205
206 return $imagedata;
207 }
208 }
209
210 new Responsive_Lightbox_Remote_Library_Unsplash();