PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.7.2
Responsive Lightbox & Gallery v2.7.2
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 / class-remote-library-api.php
responsive-lightbox / includes Last commit date
galleries 4 months ago providers 4 months ago settings 4 months ago class-fast-image.php 2 years ago class-folders.php 4 months ago class-frontend.php 4 months ago class-galleries.php 4 months ago class-multilang.php 2 years ago class-remote-library-api.php 4 months ago class-remote-library.php 4 months ago class-settings-api.php 4 months ago class-settings-data.php 4 months ago class-settings-pages.php 4 months ago class-settings.php 4 months ago class-tour.php 4 months ago class-welcome.php 2 years ago class-widgets.php 2 years ago functions.php 3 years ago
class-remote-library-api.php
263 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Responsive Lightbox Remote Library API class.
8 *
9 * @class Responsive_Lightbox_Remote_Library_API
10 */
11 abstract class Responsive_Lightbox_Remote_Library_API {
12
13 protected $allowed_hosts = [];
14 protected $allowed_formats = [
15 'bmp' => 'image/bmp',
16 'gif' => 'image/gif',
17 'jpe' => 'image/jpeg',
18 'jpeg' => 'image/jpeg',
19 'jpg' => 'image/jpeg',
20 'png' => 'image/png',
21 'tif' => 'image/tiff',
22 'tiff' => 'image/tiff',
23 'webp' => 'image/webp'
24 ];
25 protected $rl;
26 protected $slug;
27 protected $name;
28 protected $defaults;
29 protected $fields;
30 protected $query;
31 protected $query_type = 'get';
32 protected $query_args = [];
33 protected $query_remote_args = [];
34 protected $response_data = [];
35 protected $response_data_args = [];
36
37 /**
38 * Class constructor.
39 *
40 * @return void
41 */
42 public function __construct() {
43 // get main instance
44 $this->rl = Responsive_Lightbox();
45 }
46
47 /**
48 * Register media provider.
49 *
50 * @param object $provider Provider class object
51 * @return void
52 */
53 public function add_provider( $provider ) {
54 // update main instance
55 $this->rl = Responsive_Lightbox();
56
57 // add provider
58 $this->rl->providers[$provider->slug] = [
59 'instance' => $provider,
60 'slug' => ! empty( $provider->slug ) ? sanitize_title( $provider->slug ) : '',
61 'name' => ! empty( $provider->name ) ? esc_html( $provider->name ) : '',
62 'defaults' => ! empty( $provider->defaults ) && is_array( $provider->defaults ) ? $provider->defaults : [],
63 'fields' => ! empty( $provider->fields ) && is_array( $provider->fields ) ? $provider->fields : [],
64 'response_args' => ! empty( $provider->response_data_args ) && is_array( $provider->response_data_args ) ? $provider->response_data_args : []
65 ];
66
67 // add provider default values
68 $this->rl->defaults['remote_library'][$provider->slug] = $this->rl->providers[$provider->slug]['defaults'];
69
70 // register provider fields via Settings API filter
71 add_filter( 'rl_remote_library_provider_fields', [ $this, 'register_provider_fields' ] );
72
73 // validate provider settings
74 add_filter( 'rl_remote_library_settings', [ $this, 'validate_settings' ] );
75
76 // provider query
77 add_filter( 'rl_remote_library_query', [ $this, 'get_images' ], 10, 4 );
78 }
79
80 /**
81 * Register provider fields via Settings API filter.
82 *
83 * @param array $fields Existing provider fields.
84 * @return array Updated fields array.
85 */
86 public function register_provider_fields( $fields ) {
87 if ( ! empty( $this->rl->providers[$this->slug]['fields'] ) ) {
88 $provider_fields = $this->rl->providers[$this->slug]['fields'];
89
90 // Support both single field (legacy) and multiple fields array (new pattern)
91 // Single field has 'type' key; multiple fields array does not
92 if ( isset( $provider_fields['type'] ) ) {
93 // Legacy single field format - register as-is
94 $fields[$this->slug] = $provider_fields;
95 } else {
96 // New multi-field format - merge all fields with provider-prefixed keys
97 foreach ( $provider_fields as $field_id => $field ) {
98 $fields[$this->slug . '_' . $field_id] = $field;
99 }
100 }
101 }
102 return $fields;
103 }
104
105 /**
106 * Get response data.
107 *
108 * @param $subdata Subargument if needed
109 * @return mixed
110 */
111 public function get_response_data( $subdata = '' ) {
112 if ( ! empty( $subdata ) && array_key_exists( $subdata, $this->response_data ) )
113 return $this->response_data[$subdata];
114 else
115 return $this->response_data;
116 }
117
118 /**
119 * Get images from specified provider.
120 *
121 * @param array $results Current results
122 * @param string $terms Search phrase
123 * @param string $provider Current media provider
124 * @param array $args Additional arguments
125 * @return array
126 */
127 public function get_images( $results, $terms, $provider, $args = [] ) {
128 if ( $provider === $this->slug ) {
129 // make sure search phrase exists
130 if ( ! array_key_exists( 'media_search', $args ) )
131 $args['media_search'] = $terms;
132
133 $new_results = apply_filters( 'rl_remote_library_api_get_provider_images', $this->get_results( $terms, $args ), $results, $args );
134
135 // valid data? combine results
136 if ( ! is_wp_error( $new_results ) && ! empty( $new_results ) )
137 $results = array_merge( $results, $new_results );
138 }
139
140 return apply_filters( 'rl_remote_library_api_get_images', $results );
141 }
142
143 /**
144 * Get images from media provider.
145 *
146 * @param string $search_phrase Search phrase
147 * @param array $args Additional arguments
148 * @return array
149 */
150 public function get_results( $search_phrase, $args = [] ) {
151 // prepare data for remote query
152 $this->prepare_query( $search_phrase, $args );
153
154 // get query
155 $query = apply_filters( 'rl_remote_library_api_query', $this->query, $this->query_args );
156
157 if ( $this->rl->options['remote_library']['caching'] ) {
158 // set transient name
159 $transient_name = sha1( serialize( $query ) ) . sha1( serialize( $this->query_remote_args ) );
160
161 // get remote query transient
162 $transient = get_transient( $transient_name );
163 } else
164 $transient = false;
165
166 // transient exists?
167 if ( $transient !== false ) {
168 // get cached results
169 $results = $transient;
170 } else {
171 // run remote query
172 if ( $this->query_type === 'post' )
173 $response = wp_remote_post( $query, $this->query_remote_args );
174 else
175 $response = wp_remote_get( $query, $this->query_remote_args );
176
177 // wp error?
178 if ( is_wp_error( $response ) )
179 $results = $response;
180 // invalid response?
181 elseif ( ! isset( $response['response']['code'], $response['response']['message'] ) || $response['response']['code'] !== 200 || $response['response']['message'] !== 'OK' )
182 $results = new WP_Error( 'rl_remote_library_get_results', __( 'Request error', 'responsive-lightbox' ) );
183 else {
184 // get query results
185 $results = apply_filters( 'rl_remote_library_api_get_query_results', $this->get_query_results( $response, $args ), $response, $args );
186
187 // set transient for valid results
188 if ( ! is_wp_error( $results ) && $this->rl->options['remote_library']['caching'] )
189 set_transient( $transient_name, $results, (int) ( $this->rl->options['remote_library']['cache_expiry'] * 3600 ) );
190 }
191 }
192
193 return apply_filters( 'rl_remote_library_api_get_results', $results, $args );
194 }
195
196 /**
197 * Validate settings.
198 *
199 * @param array $input POST data
200 */
201 abstract public function validate_settings( $input );
202
203 /**
204 * Sanitize all returned results.
205 *
206 * @param array $results Results from media provider request
207 * @return array
208 */
209 public function sanitize_results( $results ) {
210 return is_array( $results ) ? array_filter( array_map( [ $this, 'sanitize_result' ], $results ) ) : [];
211 }
212
213 /**
214 * Sanitize single result.
215 *
216 * @param array $result Single result
217 */
218 abstract public function sanitize_result( $result );
219
220 /**
221 * Create attribution.
222 *
223 * @param string $name Image name
224 * @param string $link Image URL
225 * @param string $user_name User name
226 * @param string $user_link User URL
227 * @return string
228 */
229 public function get_attribution( $name, $link = null, $user_name = null, $user_link = null ) {
230 if ( empty( $link ) )
231 $source_text = sprintf( __( 'Image from %s', 'responsive-lightbox' ), esc_html( $name ) );
232 else
233 $source_text = sprintf( __( 'Image from <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $link ), esc_html( $name ) );
234
235 if ( empty( $user_name ) && empty( $user_link ) )
236 $user_text = '';
237 elseif ( empty( $user_link ) )
238 $user_text = sprintf( __( 'via %s', 'responsive-lightbox' ), esc_html( $user_name ) );
239 else
240 $user_text = sprintf( __( 'via <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $user_link ), esc_html( $user_name ) );
241
242 return trim( $source_text . ' ' . $user_text );
243 }
244
245 /**
246 * Get allowed formats.
247 *
248 * @return array
249 */
250 public function get_allowed_formats() {
251 return $this->allowed_formats;
252 }
253
254 /**
255 * Get allowed hosts.
256 *
257 * @return array
258 */
259 public function get_allowed_hosts() {
260 return $this->allowed_hosts;
261 }
262 }
263