| 1 |
<?php |
| 2 |
namespace BBlocks\Inc\Templates; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly |
| 6 |
} |
| 7 |
|
| 8 |
class Image { |
| 9 |
private $already_imported_ids = array(); |
| 10 |
|
| 11 |
private static $instance = null; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
} |
| 15 |
|
| 16 |
public function maybe_import_images( $content ) { |
| 17 |
// Extract all links. |
| 18 |
preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $match ); |
| 19 |
|
| 20 |
$all_links = array_unique( $match[0] ); |
| 21 |
// return $all_links; |
| 22 |
|
| 23 |
// Not have any link. |
| 24 |
if ( empty( $all_links ) ) { |
| 25 |
return $content; |
| 26 |
} |
| 27 |
|
| 28 |
$link_mapping = array(); |
| 29 |
$image_links = array(); |
| 30 |
$other_links = array(); |
| 31 |
|
| 32 |
// // Extract normal and image links. |
| 33 |
foreach ( $all_links as $key => $link ) { |
| 34 |
if ( $this->ast_block_templates_is_valid_image( $link ) ) { |
| 35 |
|
| 36 |
// Get all image links. |
| 37 |
// Avoid *-150x, *-300x and *-1024x images. |
| 38 |
if ( |
| 39 |
false === strpos( $link, '-150x' ) && |
| 40 |
false === strpos( $link, '-300x' ) && |
| 41 |
false === strpos( $link, '-1024x' ) |
| 42 |
) { |
| 43 |
$image_links[] = $link; |
| 44 |
} |
| 45 |
} else { |
| 46 |
|
| 47 |
// Collect other links. |
| 48 |
$other_links[] = $link; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// return $image_links; |
| 53 |
|
| 54 |
// Step 1: Download images. |
| 55 |
if ( ! empty( $image_links ) ) { |
| 56 |
foreach ( $image_links as $key => $image_url ) { |
| 57 |
// Download remote image. |
| 58 |
$image = array( |
| 59 |
'url' => $image_url, |
| 60 |
'id' => 0, |
| 61 |
); |
| 62 |
$downloaded_image = $this->import( $image ); |
| 63 |
|
| 64 |
// Old and New image mapping links. |
| 65 |
$link_mapping[ $image_url ] = $downloaded_image['url']; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Step 3: Replace mapping links. |
| 70 |
foreach ( $link_mapping as $old_url => $new_url ) { |
| 71 |
$old_url = (string) $old_url; |
| 72 |
$content = str_replace( $old_url, $new_url, $content ); |
| 73 |
|
| 74 |
// Replace the slashed URLs if any exist. |
| 75 |
$old_url = str_replace( '/', '/\\', $old_url ); |
| 76 |
$new_url = str_replace( '/', '/\\', $new_url ); |
| 77 |
$content = str_replace( $old_url, $new_url, $content ); |
| 78 |
} |
| 79 |
return $content; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get Saved Image. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* @param string $attachment Attachment Data. |
| 87 |
* @return string Hash string. |
| 88 |
*/ |
| 89 |
private function get_saved_image( $attachment ) { |
| 90 |
|
| 91 |
// if ( apply_filters( 'ast_block_templates_image_importer_skip_image', false, $attachment ) ) { |
| 92 |
// Helper::instance()->ast_block_templates_log( 'BATCH - SKIP Image - {from filter} - ' . $attachment['url'] . ' - Filter name `ast_block_templates_image_importer_skip_image`.' ); |
| 93 |
// return array( |
| 94 |
// 'status' => true, |
| 95 |
// 'attachment' => $attachment, |
| 96 |
// ); |
| 97 |
// } |
| 98 |
|
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
// 1. Is already imported in Batch Import Process? |
| 102 |
$post_id = $wpdb->get_var( $wpdb->prepare( 'SELECT `post_id` FROM `' . $wpdb->postmeta . '` WHERE `meta_key` = \'_ast_block_templates_image_hash\' AND `meta_value` = %s;', $this->get_hash_image( $attachment['url'] ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 103 |
|
| 104 |
// 2. Is image already imported though XML? |
| 105 |
if ( empty( $post_id ) ) { |
| 106 |
|
| 107 |
// Get file name without extension. |
| 108 |
// To check it exist in attachment. |
| 109 |
$filename = basename( $attachment['url'] ); |
| 110 |
|
| 111 |
$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 |
| 112 |
// Helper::instance()->ast_block_templates_log( 'BATCH - SKIP Image {already imported from xml} - ' . $attachment['url'] ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( $post_id ) { |
| 116 |
$new_attachment = array( |
| 117 |
'id' => $post_id, |
| 118 |
'url' => wp_get_attachment_url( $post_id ), |
| 119 |
); |
| 120 |
$this->already_imported_ids[] = $post_id; |
| 121 |
|
| 122 |
return array( |
| 123 |
'status' => true, |
| 124 |
'attachment' => $new_attachment, |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
return array( |
| 129 |
'status' => false, |
| 130 |
'attachment' => $attachment, |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Import Image |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
* @param array $attachment Attachment array. |
| 139 |
* @return array Attachment array. |
| 140 |
*/ |
| 141 |
public function import( $attachment ) { |
| 142 |
|
| 143 |
// Helper::instance()->ast_block_templates_log( 'Source - ' . $attachment['url'] ); |
| 144 |
$saved_image = $this->get_saved_image( $attachment ); |
| 145 |
// Helper::instance()->ast_block_templates_log( 'Log - ' . wp_json_encode( $saved_image['attachment'] ) ); |
| 146 |
|
| 147 |
if ( $saved_image['status'] ) { |
| 148 |
return $saved_image['attachment']; |
| 149 |
} |
| 150 |
|
| 151 |
// Extract the file name and extension from the URL. |
| 152 |
$filename = basename( $attachment['url'] ); |
| 153 |
|
| 154 |
if ( isset( $attachment['engine'] ) && 'unsplash' === $attachment['engine'] ) { |
| 155 |
$filename = 'unsplash-photo-' . $attachment['id'] . '.jpg'; |
| 156 |
} |
| 157 |
|
| 158 |
$file_content = wp_remote_retrieve_body( |
| 159 |
wp_safe_remote_get( |
| 160 |
$attachment['url'], |
| 161 |
array( |
| 162 |
'timeout' => '60', |
| 163 |
) |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
// Empty file content? |
| 168 |
if ( empty( $file_content ) ) { |
| 169 |
|
| 170 |
// Helper::instance()->ast_block_templates_log( 'BATCH - FAIL Image {Error: Failed wp_remote_retrieve_body} - ' . $attachment['url'] ); |
| 171 |
return $attachment; |
| 172 |
} |
| 173 |
|
| 174 |
$upload = wp_upload_bits( $filename, null, $file_content ); |
| 175 |
|
| 176 |
// Helper::instance()->ast_block_templates_log( $filename ); |
| 177 |
// Helper::instance()->ast_block_templates_log( wp_json_encode( $upload ) ); |
| 178 |
|
| 179 |
$post = array( |
| 180 |
'post_title' => $filename, |
| 181 |
'guid' => $upload['url'], |
| 182 |
); |
| 183 |
// Helper::instance()->ast_block_templates_log( wp_json_encode( $post ) ); |
| 184 |
|
| 185 |
$info = wp_check_filetype( $upload['file'] ); |
| 186 |
if ( $info ) { |
| 187 |
$post['post_mime_type'] = $info['type']; |
| 188 |
} else { |
| 189 |
// For now just return the origin attachment. |
| 190 |
return $attachment; |
| 191 |
} |
| 192 |
|
| 193 |
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { |
| 194 |
include ABSPATH . 'wp-admin/includes/image.php'; |
| 195 |
} |
| 196 |
|
| 197 |
$post_id = wp_insert_attachment( $post, $upload['file'] ); |
| 198 |
wp_update_attachment_metadata( |
| 199 |
$post_id, |
| 200 |
wp_generate_attachment_metadata( $post_id, $upload['file'] ) |
| 201 |
); |
| 202 |
update_post_meta( $post_id, '_ast_block_templates_image_hash', $this->get_hash_image( $attachment['url'] ) ); |
| 203 |
|
| 204 |
$new_attachment = array( |
| 205 |
'id' => $post_id, |
| 206 |
'url' => $upload['url'], |
| 207 |
); |
| 208 |
|
| 209 |
// Helper::instance()->ast_block_templates_log( 'BATCH - SUCCESS Image {Imported} - ' . $new_attachment['url'] ); |
| 210 |
|
| 211 |
$this->already_imported_ids[] = $post_id; |
| 212 |
|
| 213 |
return $new_attachment; |
| 214 |
} |
| 215 |
|
| 216 |
public function get_hash_image( $attachment_url ) { |
| 217 |
return sha1( $attachment_url ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
public static function instance() { |
| 222 |
if ( null === self::$instance ) { |
| 223 |
self::$instance = new self(); |
| 224 |
} |
| 225 |
return self::$instance; |
| 226 |
} |
| 227 |
|
| 228 |
public function ast_block_templates_is_valid_image( $link = '' ) { |
| 229 |
return preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-]+\.(jpg|png|gif|jpeg)\/?$/i', $link ); |
| 230 |
} |
| 231 |
} |
| 232 |
new Image(); |