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