| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Attachment; |
| 4 |
|
| 5 |
use ImportWP\Common\Util\Logger; |
| 6 |
|
| 7 |
class Attachment |
| 8 |
{ |
| 9 |
public function insert_attachment($parent_id, $dest, $mime, $args = array()) |
| 10 |
{ |
| 11 |
|
| 12 |
Logger::write(__CLASS__ . '::insert_attachment -parent=' . $parent_id . ' -mime=' . $mime); |
| 13 |
|
| 14 |
$title = isset($args['title']) ? $args['title'] : false; |
| 15 |
$alt = isset($args['alt']) ? $args['alt'] : false; |
| 16 |
$caption = isset($args['caption']) ? $args['caption'] : false; |
| 17 |
$description = isset($args['description']) ? $args['description'] : false; |
| 18 |
|
| 19 |
$attachment = array( |
| 20 |
'post_mime_type' => $mime, |
| 21 |
'post_parent' => $parent_id, |
| 22 |
'post_title' => !empty($title) ? $title : preg_replace('/\.[^.]+$/', '', basename($dest)), |
| 23 |
'post_content' => !empty($description) ? $description : '', |
| 24 |
'post_excerpt' => !empty($caption) ? $caption : '', |
| 25 |
'post_status' => 'inherit' |
| 26 |
); |
| 27 |
|
| 28 |
if (!empty($alt)) { |
| 29 |
$attachment['meta_input'] = array('_wp_attachment_image_alt' => $alt); |
| 30 |
} |
| 31 |
|
| 32 |
$attachment_id = wp_insert_attachment($attachment, $dest, $parent_id, true); |
| 33 |
|
| 34 |
if (is_wp_error($attachment_id)) { |
| 35 |
return $attachment_id; |
| 36 |
} |
| 37 |
|
| 38 |
return $attachment_id; |
| 39 |
} |
| 40 |
|
| 41 |
public function store_attachment_hash($attachment_id, $dest, $salt = '') |
| 42 |
{ |
| 43 |
$hash = md5($dest . $salt); |
| 44 |
Logger::write(__CLASS__ . '::store_attachment_hash -hash=' . $hash . ' -dest=' . $dest); |
| 45 |
update_post_meta($attachment_id, '_iwp_attachment_src', $hash); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_attachment_by_hash($dest, $salt = '') |
| 49 |
{ |
| 50 |
global $wpdb; |
| 51 |
$hash = md5($dest . $salt); |
| 52 |
$query = $wpdb->prepare("SELECT p.ID FROM {$wpdb->postmeta} as pm INNER JOIN {$wpdb->posts} as p ON pm.post_id = p.ID WHERE p.post_type='attachment' AND pm.meta_key='_iwp_attachment_src' AND pm.meta_value=%s ORDER BY p.ID DESC LIMIT 1", [$hash]); |
| 53 |
$attachment_id = intval($wpdb->get_var($query)); |
| 54 |
if ($attachment_id > 0) { |
| 55 |
Logger::write(__CLASS__ . '::get_attachment_by_hash -use-existing=' . $attachment_id . ' -hash' . $hash); |
| 56 |
} else { |
| 57 |
Logger::write(__CLASS__ . '::get_attachment_by_hash -skipped-hash=' . $hash . ' -dest=' . $dest); |
| 58 |
} |
| 59 |
return $attachment_id; |
| 60 |
} |
| 61 |
|
| 62 |
public function attachment_partial_url_to_postid($url) |
| 63 |
{ |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
$dir = wp_get_upload_dir(); |
| 67 |
$path = $url; |
| 68 |
|
| 69 |
$site_url = parse_url($dir['url']); |
| 70 |
$image_path = parse_url($path); |
| 71 |
|
| 72 |
// Force the protocols to match if needed. |
| 73 |
if (isset($image_path['scheme']) && ($image_path['scheme'] !== $site_url['scheme'])) { |
| 74 |
$path = str_replace($image_path['scheme'], $site_url['scheme'], $path); |
| 75 |
} |
| 76 |
|
| 77 |
if (0 === strpos($path, $dir['baseurl'] . '/')) { |
| 78 |
$path = substr($path, strlen($dir['baseurl'] . '/')); |
| 79 |
} |
| 80 |
|
| 81 |
$sql = $wpdb->prepare( |
| 82 |
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s", |
| 83 |
'%' . $path |
| 84 |
); |
| 85 |
|
| 86 |
$results = $wpdb->get_results($sql); |
| 87 |
$post_id = null; |
| 88 |
|
| 89 |
if (count($results) > 0) { |
| 90 |
$contains_slash = strpos($path, '/'); |
| 91 |
|
| 92 |
foreach ($results as $result) { |
| 93 |
if ($path === $result->meta_value) { |
| 94 |
|
| 95 |
// escape if we have a direct match, if not return |
| 96 |
$post_id = $result->post_id; |
| 97 |
break; |
| 98 |
} elseif (!$contains_slash && $path === basename($result->meta_value)) { |
| 99 |
|
| 100 |
// escape if we are only matching the filename |
| 101 |
$post_id = $result->post_id; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $post_id; |
| 107 |
} |
| 108 |
|
| 109 |
public function generate_image_sizes($attachment_id, $source) |
| 110 |
{ |
| 111 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 112 |
require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 113 |
$attach_data = wp_generate_attachment_metadata($attachment_id, $source); |
| 114 |
wp_update_attachment_metadata($attachment_id, $attach_data); |
| 115 |
} |
| 116 |
} |
| 117 |
|