| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
|
| 7 |
/** |
| 8 |
* Assets class |
| 9 |
*/ |
| 10 |
class Assets { |
| 11 |
|
| 12 |
const IMAGE_ASSET_URL = 'https://apps.wowoptin.com/wp-json/wmedia/v1/images'; |
| 13 |
const VIDEO_ASSET_URL = 'https://apps.wowoptin.com/wp-json/wmedia/v1/videos'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Undocumented function |
| 17 |
* |
| 18 |
* @param string $s search string. |
| 19 |
* @param int $page page number. |
| 20 |
* @param int $limit limit. |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public static function get_videos( $s, $page, $limit ) { |
| 24 |
$url = add_query_arg( |
| 25 |
array( |
| 26 |
'license' => Utils::get_license_key(), |
| 27 |
'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0', |
| 28 |
'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0', |
| 29 |
's' => $s, |
| 30 |
'page' => $page, |
| 31 |
'limit' => $limit, |
| 32 |
), |
| 33 |
self::VIDEO_ASSET_URL |
| 34 |
); |
| 35 |
|
| 36 |
$response = wp_remote_get( |
| 37 |
$url, |
| 38 |
array( |
| 39 |
'timeout' => 30, |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 44 |
Utils::log_to_file( $response ); |
| 45 |
return array(); |
| 46 |
} |
| 47 |
|
| 48 |
$data = json_decode( $response['body'] ); |
| 49 |
|
| 50 |
return $data; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Undocumented function |
| 55 |
* |
| 56 |
* @param string $s search string. |
| 57 |
* @param int $page page number. |
| 58 |
* @param int $limit limit. |
| 59 |
* @param string $src src. |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public static function get_images( $s, $page, $limit, $src ) { |
| 63 |
if ( 'stock' === $src ) { |
| 64 |
return self::get_stock_images( $s, $page, $limit ); |
| 65 |
} elseif ( 'own' === $src ) { |
| 66 |
return self::get_own_images( $s, $page, $limit ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Undocumented function |
| 72 |
* |
| 73 |
* @param string $s search string. |
| 74 |
* @param int $page page number. |
| 75 |
* @param int $limit limit. |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
private static function get_stock_images( $s, $page, $limit ) { |
| 79 |
$url = add_query_arg( |
| 80 |
array( |
| 81 |
'license' => Utils::get_license_key(), |
| 82 |
'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0', |
| 83 |
'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0', |
| 84 |
's' => $s, |
| 85 |
'page' => $page, |
| 86 |
'limit' => $limit, |
| 87 |
), |
| 88 |
self::IMAGE_ASSET_URL |
| 89 |
); |
| 90 |
|
| 91 |
$response = wp_remote_get( |
| 92 |
$url, |
| 93 |
array( |
| 94 |
'timeout' => 30, |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 99 |
Utils::log_to_file( $response ); |
| 100 |
return array(); |
| 101 |
} |
| 102 |
|
| 103 |
$data = json_decode( $response['body'] ); |
| 104 |
|
| 105 |
return $data; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Undocumented function |
| 110 |
* |
| 111 |
* @param string $url image url. |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public static function get_image( $url ) { |
| 115 |
$url = add_query_arg( |
| 116 |
array( |
| 117 |
'license' => Utils::get_license_key(), |
| 118 |
'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0', |
| 119 |
'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0', |
| 120 |
'image_url' => $url, |
| 121 |
), |
| 122 |
self::IMAGE_ASSET_URL . '/download' |
| 123 |
); |
| 124 |
|
| 125 |
$response = wp_remote_get( |
| 126 |
$url, |
| 127 |
array( |
| 128 |
'timeout' => 30, |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 133 |
Utils::log_to_file( $response ); |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
$data = json_decode( $response['body'] ); |
| 138 |
|
| 139 |
return isset( $data->download_url ) ? $data->download_url : null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get Image file name from url |
| 144 |
* |
| 145 |
* @param string $image_url image url. |
| 146 |
* @return string|null |
| 147 |
*/ |
| 148 |
public static function get_image_filename( $image_url ) { |
| 149 |
$parsed_url = wp_parse_url( $image_url ); |
| 150 |
|
| 151 |
if ( false === $parsed_url ) { |
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
$original_filename = null; |
| 156 |
|
| 157 |
// Special Case for unsplash images. |
| 158 |
if ( str_starts_with( $image_url, 'https://api.unsplash.com/photos/' ) ) { |
| 159 |
$paths = explode( '/', $parsed_url['path'] ); |
| 160 |
$original_filename = isset( $paths[2] ) ? 'unsplash-' . $paths[2] : null; |
| 161 |
} else { |
| 162 |
$original_filename = basename( $parsed_url['path'] ); |
| 163 |
} |
| 164 |
|
| 165 |
if ( empty( $original_filename ) ) { |
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
return 'wowoptin-' . sanitize_file_name( $original_filename ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Undocumented function |
| 174 |
* |
| 175 |
* @param string $s search string. |
| 176 |
* @param int $page page number. |
| 177 |
* @param int $limit limit. |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
private static function get_own_images( $s, $page, $limit ) { |
| 181 |
$data = array(); |
| 182 |
|
| 183 |
$args = array( |
| 184 |
'post_type' => 'attachment', |
| 185 |
'post_mime_type' => 'image', |
| 186 |
'post_status' => 'published', |
| 187 |
'posts_per_page' => $limit, |
| 188 |
'paged' => $page + 1, |
| 189 |
); |
| 190 |
|
| 191 |
if ( ! empty( $s ) ) { |
| 192 |
$args['s'] = $s; |
| 193 |
} |
| 194 |
|
| 195 |
$query = new WP_Query( $args ); |
| 196 |
$posts = $query->posts; |
| 197 |
|
| 198 |
foreach ( $posts as $post ) { |
| 199 |
$image = wp_get_attachment_image_src( $post->ID, 'full' ); |
| 200 |
// $author = get_the_author_meta( 'display_name', $post->post_author ); |
| 201 |
// $author_url = get_the_author_meta( 'user_url', $post->post_author ); |
| 202 |
$data[] = array( |
| 203 |
'id' => $post->ID, |
| 204 |
'width' => $image[1], |
| 205 |
'height' => $image[2], |
| 206 |
'full' => $image[0], |
| 207 |
'thumb' => wp_get_attachment_image_url( $post->ID, 'thumb' ), |
| 208 |
'author' => '', |
| 209 |
'author_url' => '', |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
// $provider = get_bloginfo( 'name' ); |
| 214 |
// $provider_url = get_bloginfo( 'url' ); |
| 215 |
|
| 216 |
return array( |
| 217 |
'data' => array( |
| 218 |
'page' => $page, |
| 219 |
'limit' => $limit, |
| 220 |
'total' => $query->found_posts, |
| 221 |
'data' => $data, |
| 222 |
), |
| 223 |
|
| 224 |
'provider' => 'own', |
| 225 |
'provider_url' => '', |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|