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 / class-remote-library-api.php
responsive-lightbox / includes Last commit date
providers 5 years ago class-fast-image.php 8 years ago class-folders-walker.php 7 years ago class-folders.php 5 years ago class-frontend.php 5 years ago class-galleries.php 5 years ago class-multilang.php 5 years ago class-remote-library-api.php 5 years ago class-remote-library.php 5 years ago class-settings.php 5 years ago class-tour.php 5 years ago class-welcome.php 5 years ago class-widgets.php 5 years ago functions.php 5 years ago
class-remote-library-api.php
207 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 $rl;
14 protected $slug;
15 protected $name;
16 protected $defaults;
17 protected $fields;
18 protected $query;
19 protected $query_type = 'get';
20 protected $query_args = array();
21 protected $query_remote_args = array();
22 protected $response_data = array();
23 protected $response_data_args = array();
24
25 /**
26 * Constructor.
27 *
28 * @return void
29 */
30 public function __construct() {
31 // assign main instance
32 $this->rl = Responsive_Lightbox();
33 }
34
35 /**
36 * Register media provider.
37 *
38 * @param object $provider Provider class object
39 * @return void
40 */
41 public function add_provider( $provider ) {
42 // update main instance
43 $this->rl = Responsive_Lightbox();
44
45 // add provider
46 $this->rl->providers[$provider->slug] = array(
47 'instance' => $provider,
48 'slug' => ! empty( $provider->slug ) ? sanitize_title( $provider->slug ) : '',
49 'name' => ! empty( $provider->name ) ? esc_html( $provider->name ) : '',
50 'defaults' => ! empty( $provider->defaults ) && is_array( $provider->defaults ) ? $provider->defaults : array(),
51 'fields' => ! empty( $provider->fields ) && is_array( $provider->fields ) ? $provider->fields : array(),
52 'response_args' => ! empty( $provider->response_data_args ) && is_array( $provider->response_data_args ) ? $provider->response_data_args : array()
53 );
54
55 // add provider default values
56 $this->rl->defaults['remote_library'][$provider->slug] = $this->rl->providers[$provider->slug]['defaults'];
57
58 // add provider field
59 $this->rl->settings->settings['remote_library']['fields'][$provider->slug] = $this->rl->providers[$provider->slug]['fields'];
60
61 // validate provider settings
62 add_filter( 'rl_remote_library_settings', array( $this, 'validate_settings' ) );
63
64 // provider query
65 add_filter( 'rl_remote_library_query', array( $this, 'get_images' ), 10, 4 );
66 }
67
68 /**
69 * Get response data.
70 *
71 * @param $subdata Subargument if needed
72 * @return mixed
73 */
74 public function get_response_data( $subdata = '' ) {
75 if ( ! empty( $subdata ) && array_key_exists( $subdata, $this->response_data ) )
76 return $this->response_data[$subdata];
77 else
78 return $this->response_data;
79 }
80
81 /**
82 * Get images from specified provider.
83 *
84 * @param array $results Current results
85 * @param string $terms Search phrase
86 * @param string $provider Current media provider
87 * @param array $args Additional arguments
88 * @return array
89 */
90 public function get_images( $results, $terms, $provider, $args = array() ) {
91 if ( $provider === $this->slug ) {
92 // make sure search phrase exists
93 if ( ! array_key_exists( 'media_search', $args ) )
94 $args['media_search'] = $terms;
95
96 $new_results = apply_filters( 'rl_remote_library_api_get_provider_images', $this->get_results( $terms, $args ), $results, $args );
97
98 // valid data? combine results
99 if ( ! is_wp_error( $new_results ) && ! empty( $new_results ) )
100 $results = array_merge( $results, $new_results );
101 }
102
103 return apply_filters( 'rl_remote_library_api_get_images', $results );
104 }
105
106 /**
107 * Get images from media provider.
108 *
109 * @param string $search_phrase Search phrase
110 * @param array $args Additional arguments
111 * @return array
112 */
113 public function get_results( $search_phrase, $args = array() ) {
114 // prepare data for remote query
115 $this->prepare_query( $search_phrase, $args );
116
117 // get query
118 $query = apply_filters( 'rl_remote_library_api_query', $this->query, $this->query_args );
119
120 if ( $this->rl->options['remote_library']['caching'] ) {
121 // set transient name
122 $transient_name = sha1( serialize( $query ) ) . sha1( serialize( $this->query_remote_args ) );
123
124 // get remote query transient
125 $transient = get_transient( $transient_name );
126 } else
127 $transient = false;
128
129 // transient exists?
130 if ( $transient !== false ) {
131 // get cached results
132 $results = $transient;
133 } else {
134 // run remote query
135 if ( $this->query_type === 'post' )
136 $response = wp_remote_post( $query, $this->query_remote_args );
137 else
138 $response = wp_remote_get( $query, $this->query_remote_args );
139
140 // wp error?
141 if ( is_wp_error( $response ) )
142 $results = $response;
143 // invalid response?
144 elseif ( ! isset( $response['response']['code'], $response['response']['message'] ) || $response['response']['code'] !== 200 || $response['response']['message'] !== 'OK' )
145 $results = new WP_Error( 'rl_remote_library_get_results', __( 'Request error', 'responsive-lightbox' ) );
146 else {
147 // get query results
148 $results = apply_filters( 'rl_remote_library_api_get_query_results', $this->get_query_results( $response, $args ), $response, $args );
149
150 // set transient for valid results
151 if ( ! is_wp_error( $results ) && $this->rl->options['remote_library']['caching'] )
152 set_transient( $transient_name, $results, (int) ( $this->rl->options['remote_library']['cache_expiry'] * 3600 ) );
153 }
154 }
155
156 return apply_filters( 'rl_remote_library_api_get_results', $results, $args );
157 }
158
159 /**
160 * Validate settings.
161 *
162 * @param array $input POST data
163 */
164 abstract public function validate_settings( $input );
165
166 /**
167 * Sanitize all returned results.
168 *
169 * @param array $results Results from media provider request
170 * @return array
171 */
172 public function sanitize_results( $results ) {
173 return is_array( $results ) ? array_filter( array_map( array( $this, 'sanitize_result' ), $results ) ) : array();
174 }
175
176 /**
177 * Sanitize single result.
178 *
179 * @param array $result Single result
180 */
181 abstract public function sanitize_result( $result );
182
183 /**
184 * Create attribution.
185 *
186 * @param string $name Image name
187 * @param string $link Image URL
188 * @param string $user_name User name
189 * @param string $user_link User URL
190 * @return string
191 */
192 public function get_attribution( $name, $link = null, $user_name = null, $user_link = null ) {
193 if ( empty( $link ) )
194 $source_text = sprintf( __( 'Image from %s', 'responsive-lightbox' ), esc_html( $name ) );
195 else
196 $source_text = sprintf( __( 'Image from <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $link ), esc_html( $name ) );
197
198 if ( empty( $user_name ) && empty( $user_link ) )
199 $user_text = '';
200 elseif ( empty( $user_link ) )
201 $user_text = sprintf( __( 'via %s', 'responsive-lightbox' ), esc_html( $user_name ) );
202 else
203 $user_text = sprintf( __( 'via <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $user_link ), esc_html( $user_name ) );
204
205 return trim( $source_text . ' ' . $user_text );
206 }
207 }