| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! class_exists( 'Blockspare_Design_Image_Importer' ) ) : |
| 4 |
|
| 5 |
class Blockspare_Design_Image_Importer{ |
| 6 |
|
| 7 |
private static $instance; |
| 8 |
|
| 9 |
private $already_imported_ids = array(); |
| 10 |
|
| 11 |
public static function get_instance() { |
| 12 |
if ( ! isset( self::$instance ) ) { |
| 13 |
self::$instance = new self(); |
| 14 |
} |
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
|
| 20 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 21 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 22 |
} |
| 23 |
if ( ! function_exists( 'wp_crop_image' ) ) { |
| 24 |
include( ABSPATH . 'wp-admin/includes/image.php' ); |
| 25 |
} |
| 26 |
|
| 27 |
WP_Filesystem(); |
| 28 |
} |
| 29 |
|
| 30 |
public function blockspare_process( $attachments ) { |
| 31 |
|
| 32 |
$downloaded_images = array(); |
| 33 |
|
| 34 |
foreach ( $attachments as $key => $attachment ) { |
| 35 |
$downloaded_images[] = $this->import( $attachment ); |
| 36 |
} |
| 37 |
|
| 38 |
return $downloaded_images; |
| 39 |
} |
| 40 |
|
| 41 |
public function blockspare_get_hash_image( $attachment_url ) { |
| 42 |
return sha1( $attachment_url ); |
| 43 |
} |
| 44 |
|
| 45 |
private function blockspare_get_saved_image( $attachment ) { |
| 46 |
|
| 47 |
global $wpdb; |
| 48 |
$post_id = $wpdb->get_var( $wpdb->prepare( 'SELECT `post_id` FROM `' . $wpdb->postmeta . '` WHERE `meta_key` = \'blockspare_templates_image_hash\' AND `meta_value` = %s;', $this->blockspare_get_hash_image( $attachment['url'] ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 49 |
|
| 50 |
if ( empty( $post_id ) ) { |
| 51 |
$filename = basename( $attachment['url'] ); |
| 52 |
|
| 53 |
$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s", '%/' . $filename . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
if ( $post_id ) { |
| 58 |
$new_attachment = array( |
| 59 |
'id' => $post_id, |
| 60 |
'url' => wp_get_attachment_url( $post_id ), |
| 61 |
); |
| 62 |
$this->already_imported_ids[] = $post_id; |
| 63 |
|
| 64 |
return array( |
| 65 |
'status' => true, |
| 66 |
'attachment' => $new_attachment, |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return array( |
| 71 |
'status' => false, |
| 72 |
'attachment' => $attachment, |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function blockspare_import( $attachment ) { |
| 77 |
|
| 78 |
$saved_image = $this->blockspare_get_saved_image( $attachment ); |
| 79 |
if ( $saved_image['status'] ) { |
| 80 |
return $saved_image['attachment']; |
| 81 |
} |
| 82 |
|
| 83 |
$file_content = wp_remote_retrieve_body( |
| 84 |
wp_safe_remote_get( |
| 85 |
$attachment['url'], |
| 86 |
array( |
| 87 |
'timeout' => '60', |
| 88 |
'sslverify' => false, |
| 89 |
) |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
|
| 94 |
if ( empty( $file_content ) ) { |
| 95 |
|
| 96 |
return $attachment; |
| 97 |
} |
| 98 |
|
| 99 |
$filename = basename( $attachment['url'] ); |
| 100 |
|
| 101 |
$upload = wp_upload_bits( $filename, null, $file_content ); |
| 102 |
$post = array( |
| 103 |
'post_title' => $filename, |
| 104 |
'guid' => $upload['url'], |
| 105 |
); |
| 106 |
|
| 107 |
$info = wp_check_filetype( $upload['file'] ); |
| 108 |
if ( $info ) { |
| 109 |
$post['post_mime_type'] = $info['type']; |
| 110 |
} else { |
| 111 |
// For now just return the origin attachment. |
| 112 |
return $attachment; |
| 113 |
} |
| 114 |
|
| 115 |
$post_id = wp_insert_attachment( $post, $upload['file'] ); |
| 116 |
apply_filters('wp_handle_upload', array('file' => $file_path, 'url' => $file_url, 'type' => $file_type), 'upload'); |
| 117 |
|
| 118 |
$metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] ); |
| 119 |
wp_update_attachment_metadata( |
| 120 |
$post_id, |
| 121 |
$metadata |
| 122 |
|
| 123 |
); |
| 124 |
update_post_meta( $post_id, 'blockspare_templates_image_hash', $this->blockspare_get_hash_image( $attachment['url'] ) ); |
| 125 |
|
| 126 |
$new_attachment = array( |
| 127 |
'id' => $post_id, |
| 128 |
'url' => $upload['url'], |
| 129 |
); |
| 130 |
|
| 131 |
$this->already_imported_ids[] = $post_id; |
| 132 |
|
| 133 |
return $new_attachment; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
Blockspare_Design_Image_Importer::get_instance(); |
| 138 |
endif; |