| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image Importer |
| 4 |
* |
| 5 |
* => How to use? |
| 6 |
* |
| 7 |
* $image = array( |
| 8 |
* 'url' => '<image-url>', |
| 9 |
* 'id' => '<image-id>', |
| 10 |
* ); |
| 11 |
* |
| 12 |
* $downloaded_image = CartFlows_Importer_Core::get_instance()->import( $image ); |
| 13 |
* |
| 14 |
* @package CartFlows |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
if ( ! class_exists( 'CartFlows_Importer_Core' ) ) : |
| 23 |
|
| 24 |
/** |
| 25 |
* CartFlows Importer |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
class CartFlows_Importer_Core { |
| 30 |
|
| 31 |
/** |
| 32 |
* Instance |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @var object Class object. |
| 36 |
* @access private |
| 37 |
*/ |
| 38 |
private static $instance; |
| 39 |
|
| 40 |
/** |
| 41 |
* Images IDs |
| 42 |
* |
| 43 |
* @var array The Array of already image IDs. |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
private $already_imported_ids = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Initiator |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* @return object initialized object of class. |
| 53 |
*/ |
| 54 |
public static function get_instance() { |
| 55 |
if ( ! isset( self::$instance ) ) { |
| 56 |
self::$instance = new self(); |
| 57 |
} |
| 58 |
return self::$instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Constructor |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
|
| 68 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 69 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 70 |
} |
| 71 |
|
| 72 |
WP_Filesystem(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Process Image Download |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* @param array $attachments Attachment array. |
| 80 |
* @return array Attachment array. |
| 81 |
*/ |
| 82 |
public function process( $attachments ) { |
| 83 |
|
| 84 |
$downloaded_images = array(); |
| 85 |
|
| 86 |
foreach ( $attachments as $key => $attachment ) { |
| 87 |
$downloaded_images[] = $this->import( $attachment ); |
| 88 |
} |
| 89 |
|
| 90 |
return $downloaded_images; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get Hash Image. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* @param string $attachment_url Attachment URL. |
| 98 |
* @return string Hash string. |
| 99 |
*/ |
| 100 |
private function get_hash_image( $attachment_url ) { |
| 101 |
return sha1( $attachment_url ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get Saved Image. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
* @param string $attachment Attachment Data. |
| 109 |
* @return string Hash string. |
| 110 |
*/ |
| 111 |
private function get_saved_image( $attachment ) { |
| 112 |
|
| 113 |
wcf()->logger->import_log( 'importer-core.php File' ); |
| 114 |
|
| 115 |
if ( apply_filters( 'cartflows_image_importer_skip_image', false, $attachment ) ) { |
| 116 |
|
| 117 |
self::log( 'Download (✕) Replace (✕) - ' . $attachment['url'] ); |
| 118 |
|
| 119 |
return $attachment; |
| 120 |
} |
| 121 |
|
| 122 |
global $wpdb; |
| 123 |
|
| 124 |
// Already imported? Then return! |
| 125 |
if ( isset( $this->already_imported_ids[ $attachment['id'] ] ) ) { |
| 126 |
|
| 127 |
self::log( 'Download (✓) Replace (✓) - ' . $attachment['url'] ); |
| 128 |
|
| 129 |
return $this->already_imported_ids[ $attachment['id'] ]; |
| 130 |
} |
| 131 |
|
| 132 |
// 1. Is already imported in Batch Import Process? |
| 133 |
$post_id = $wpdb->get_var( |
| 134 |
$wpdb->prepare( |
| 135 |
'SELECT `post_id` FROM `' . $wpdb->postmeta . '` |
| 136 |
WHERE `meta_key` = \'_cartflows_image_hash\' |
| 137 |
AND `meta_value` = %s |
| 138 |
;', |
| 139 |
$this->get_hash_image( $attachment['url'] ) |
| 140 |
) |
| 141 |
); // db call ok; no-cache ok. |
| 142 |
|
| 143 |
// 2. Is image already imported though XML? |
| 144 |
if ( empty( $post_id ) ) { |
| 145 |
|
| 146 |
// Get file name without extension. |
| 147 |
// To check it exist in attachment. |
| 148 |
$filename = preg_replace( '/\\.[^.\\s]{3,4}$/', '', basename( $attachment['url'] ) ); |
| 149 |
|
| 150 |
$post_id = $wpdb->get_var( |
| 151 |
$wpdb->prepare( |
| 152 |
'SELECT `post_id` FROM `' . $wpdb->postmeta . '` |
| 153 |
WHERE `meta_key` = \'_wp_attached_file\' |
| 154 |
AND `meta_value` LIKE %s |
| 155 |
;', |
| 156 |
'%' . $filename . '%' |
| 157 |
) |
| 158 |
); // db call ok; no-cache ok. |
| 159 |
|
| 160 |
self::log( 'Download (✕) Replace (✕) - ' . $attachment['url'] ); |
| 161 |
} |
| 162 |
|
| 163 |
if ( $post_id ) { |
| 164 |
|
| 165 |
$new_imgage_url = wp_get_attachment_url( $post_id ); |
| 166 |
$new_attachment = array( |
| 167 |
'id' => $post_id, |
| 168 |
'url' => $new_imgage_url, |
| 169 |
); |
| 170 |
$this->already_imported_ids[ $attachment['id'] ] = $new_attachment; |
| 171 |
|
| 172 |
self::log( 'Download (✓) Replace (✓) - ' . $new_imgage_url ); |
| 173 |
return $new_attachment; |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Import Image |
| 181 |
* |
| 182 |
* @since 1.0.0 |
| 183 |
* @param array $attachment Attachment array. |
| 184 |
* @return array Attachment array. |
| 185 |
*/ |
| 186 |
public function import( $attachment ) { |
| 187 |
|
| 188 |
$saved_image = $this->get_saved_image( $attachment ); |
| 189 |
if ( $saved_image ) { |
| 190 |
return $saved_image; |
| 191 |
} |
| 192 |
|
| 193 |
$args = array( |
| 194 |
'timeout' => 300, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 195 |
); |
| 196 |
|
| 197 |
$file_content = wp_remote_retrieve_body( wp_safe_remote_get( $attachment['url'], $args ) ); |
| 198 |
|
| 199 |
// Empty file content? |
| 200 |
if ( empty( $file_content ) ) { |
| 201 |
|
| 202 |
self::log( 'Download (✕) Replace (✕) - ' . $attachment['url'] ); |
| 203 |
self::log( 'Error: Failed wp_remote_retrieve_body().' ); |
| 204 |
|
| 205 |
return $attachment; |
| 206 |
} |
| 207 |
|
| 208 |
// Extract the file name and extension from the URL. |
| 209 |
$filename = basename( $attachment['url'] ); |
| 210 |
|
| 211 |
$upload = wp_upload_bits( |
| 212 |
$filename, |
| 213 |
null, |
| 214 |
$file_content |
| 215 |
); |
| 216 |
|
| 217 |
$post = array( |
| 218 |
'post_title' => $filename, |
| 219 |
'guid' => $upload['url'], |
| 220 |
); |
| 221 |
|
| 222 |
$info = wp_check_filetype( $upload['file'] ); |
| 223 |
if ( $info ) { |
| 224 |
$post['post_mime_type'] = $info['type']; |
| 225 |
} else { |
| 226 |
// For now just return the origin attachment. |
| 227 |
return $attachment; |
| 228 |
} |
| 229 |
|
| 230 |
$post_id = wp_insert_attachment( $post, $upload['file'] ); |
| 231 |
wp_update_attachment_metadata( |
| 232 |
$post_id, |
| 233 |
wp_generate_attachment_metadata( $post_id, $upload['file'] ) |
| 234 |
); |
| 235 |
update_post_meta( $post_id, '_cartflows_image_hash', $this->get_hash_image( $attachment['url'] ) ); |
| 236 |
|
| 237 |
$new_attachment = array( |
| 238 |
'id' => $post_id, |
| 239 |
'url' => $upload['url'], |
| 240 |
); |
| 241 |
|
| 242 |
self::log( 'Download (✓) Replace (✓) - ' . $attachment['url'] ); |
| 243 |
|
| 244 |
$this->already_imported_ids[ $attachment['id'] ] = $new_attachment; |
| 245 |
|
| 246 |
return $new_attachment; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Debugging Log. |
| 251 |
* |
| 252 |
* @since 1.0.0 |
| 253 |
* @param mixed $log Log data. |
| 254 |
* @return void |
| 255 |
*/ |
| 256 |
public static function log( $log ) { |
| 257 |
|
| 258 |
if ( ! WP_DEBUG_LOG ) { |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 263 |
// Logging is for debugging.Hence ignoring phpcs rule. |
| 264 |
wcf()->logger->import_log( print_r( $log, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 265 |
} else { |
| 266 |
wcf()->logger->import_log( $log ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Initialize class object with 'get_instance()' method |
| 274 |
*/ |
| 275 |
CartFlows_Importer_Core::get_instance(); |
| 276 |
|
| 277 |
endif; |
| 278 |
|