providers
1 year ago
class-fast-image.php
2 years ago
class-folders.php
6 months ago
class-frontend.php
6 months ago
class-galleries.php
6 months ago
class-multilang.php
2 years ago
class-remote-library-api.php
1 year ago
class-remote-library.php
6 months ago
class-settings.php
6 months ago
class-tour.php
2 years ago
class-welcome.php
2 years ago
class-widgets.php
2 years ago
functions.php
3 years ago
class-remote-library-api.php
238 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 | // add provider field |
| 71 | $this->rl->settings->settings['remote_library']['fields'][$provider->slug] = $this->rl->providers[$provider->slug]['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 | * Get response data. |
| 82 | * |
| 83 | * @param $subdata Subargument if needed |
| 84 | * @return mixed |
| 85 | */ |
| 86 | public function get_response_data( $subdata = '' ) { |
| 87 | if ( ! empty( $subdata ) && array_key_exists( $subdata, $this->response_data ) ) |
| 88 | return $this->response_data[$subdata]; |
| 89 | else |
| 90 | return $this->response_data; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get images from specified provider. |
| 95 | * |
| 96 | * @param array $results Current results |
| 97 | * @param string $terms Search phrase |
| 98 | * @param string $provider Current media provider |
| 99 | * @param array $args Additional arguments |
| 100 | * @return array |
| 101 | */ |
| 102 | public function get_images( $results, $terms, $provider, $args = [] ) { |
| 103 | if ( $provider === $this->slug ) { |
| 104 | // make sure search phrase exists |
| 105 | if ( ! array_key_exists( 'media_search', $args ) ) |
| 106 | $args['media_search'] = $terms; |
| 107 | |
| 108 | $new_results = apply_filters( 'rl_remote_library_api_get_provider_images', $this->get_results( $terms, $args ), $results, $args ); |
| 109 | |
| 110 | // valid data? combine results |
| 111 | if ( ! is_wp_error( $new_results ) && ! empty( $new_results ) ) |
| 112 | $results = array_merge( $results, $new_results ); |
| 113 | } |
| 114 | |
| 115 | return apply_filters( 'rl_remote_library_api_get_images', $results ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get images from media provider. |
| 120 | * |
| 121 | * @param string $search_phrase Search phrase |
| 122 | * @param array $args Additional arguments |
| 123 | * @return array |
| 124 | */ |
| 125 | public function get_results( $search_phrase, $args = [] ) { |
| 126 | // prepare data for remote query |
| 127 | $this->prepare_query( $search_phrase, $args ); |
| 128 | |
| 129 | // get query |
| 130 | $query = apply_filters( 'rl_remote_library_api_query', $this->query, $this->query_args ); |
| 131 | |
| 132 | if ( $this->rl->options['remote_library']['caching'] ) { |
| 133 | // set transient name |
| 134 | $transient_name = sha1( serialize( $query ) ) . sha1( serialize( $this->query_remote_args ) ); |
| 135 | |
| 136 | // get remote query transient |
| 137 | $transient = get_transient( $transient_name ); |
| 138 | } else |
| 139 | $transient = false; |
| 140 | |
| 141 | // transient exists? |
| 142 | if ( $transient !== false ) { |
| 143 | // get cached results |
| 144 | $results = $transient; |
| 145 | } else { |
| 146 | // run remote query |
| 147 | if ( $this->query_type === 'post' ) |
| 148 | $response = wp_remote_post( $query, $this->query_remote_args ); |
| 149 | else |
| 150 | $response = wp_remote_get( $query, $this->query_remote_args ); |
| 151 | |
| 152 | // wp error? |
| 153 | if ( is_wp_error( $response ) ) |
| 154 | $results = $response; |
| 155 | // invalid response? |
| 156 | elseif ( ! isset( $response['response']['code'], $response['response']['message'] ) || $response['response']['code'] !== 200 || $response['response']['message'] !== 'OK' ) |
| 157 | $results = new WP_Error( 'rl_remote_library_get_results', __( 'Request error', 'responsive-lightbox' ) ); |
| 158 | else { |
| 159 | // get query results |
| 160 | $results = apply_filters( 'rl_remote_library_api_get_query_results', $this->get_query_results( $response, $args ), $response, $args ); |
| 161 | |
| 162 | // set transient for valid results |
| 163 | if ( ! is_wp_error( $results ) && $this->rl->options['remote_library']['caching'] ) |
| 164 | set_transient( $transient_name, $results, (int) ( $this->rl->options['remote_library']['cache_expiry'] * 3600 ) ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return apply_filters( 'rl_remote_library_api_get_results', $results, $args ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Validate settings. |
| 173 | * |
| 174 | * @param array $input POST data |
| 175 | */ |
| 176 | abstract public function validate_settings( $input ); |
| 177 | |
| 178 | /** |
| 179 | * Sanitize all returned results. |
| 180 | * |
| 181 | * @param array $results Results from media provider request |
| 182 | * @return array |
| 183 | */ |
| 184 | public function sanitize_results( $results ) { |
| 185 | return is_array( $results ) ? array_filter( array_map( [ $this, 'sanitize_result' ], $results ) ) : []; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sanitize single result. |
| 190 | * |
| 191 | * @param array $result Single result |
| 192 | */ |
| 193 | abstract public function sanitize_result( $result ); |
| 194 | |
| 195 | /** |
| 196 | * Create attribution. |
| 197 | * |
| 198 | * @param string $name Image name |
| 199 | * @param string $link Image URL |
| 200 | * @param string $user_name User name |
| 201 | * @param string $user_link User URL |
| 202 | * @return string |
| 203 | */ |
| 204 | public function get_attribution( $name, $link = null, $user_name = null, $user_link = null ) { |
| 205 | if ( empty( $link ) ) |
| 206 | $source_text = sprintf( __( 'Image from %s', 'responsive-lightbox' ), esc_html( $name ) ); |
| 207 | else |
| 208 | $source_text = sprintf( __( 'Image from <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $link ), esc_html( $name ) ); |
| 209 | |
| 210 | if ( empty( $user_name ) && empty( $user_link ) ) |
| 211 | $user_text = ''; |
| 212 | elseif ( empty( $user_link ) ) |
| 213 | $user_text = sprintf( __( 'via %s', 'responsive-lightbox' ), esc_html( $user_name ) ); |
| 214 | else |
| 215 | $user_text = sprintf( __( 'via <a href="%s" target="_blank">%s</a>', 'responsive-lightbox' ), esc_url( $user_link ), esc_html( $user_name ) ); |
| 216 | |
| 217 | return trim( $source_text . ' ' . $user_text ); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get allowed formats. |
| 222 | * |
| 223 | * @return array |
| 224 | */ |
| 225 | public function get_allowed_formats() { |
| 226 | return $this->allowed_formats; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get allowed hosts. |
| 231 | * |
| 232 | * @return array |
| 233 | */ |
| 234 | public function get_allowed_hosts() { |
| 235 | return $this->allowed_hosts; |
| 236 | } |
| 237 | } |
| 238 |