checkbox.php
1 month ago
date.php
1 month ago
datetime.php
1 month ago
field.php
1 month ago
gallery.php
1 month ago
map.php
1 month ago
media-url.php
1 month ago
media.php
1 month ago
number.php
1 month ago
post.php
1 month ago
radio.php
1 month ago
repeater.php
1 month ago
select.php
1 month ago
switcher.php
1 month ago
text.php
1 month ago
time.php
1 month ago
toggle.php
1 month ago
user.php
1 month ago
gallery.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | use PMXI_API; |
| 8 | |
| 9 | class PMXI_Addon_Gallery_Field extends PMXI_Addon_Field { |
| 10 | |
| 11 | static $repeater_path = 'gallery'; |
| 12 | |
| 13 | public static function isImage($url) { |
| 14 | $ext = pmxi_getExtensionFromStr($url); |
| 15 | $exts = explode('|',wp_all_import_supported_image_extensions()); |
| 16 | |
| 17 | if (in_array($ext, $exts)) return true; |
| 18 | |
| 19 | // If file lacks an extension, try to get it from the remote file |
| 20 | $ext = pmxi_get_remote_image_ext($url); |
| 21 | return in_array($ext, $exts); |
| 22 | } |
| 23 | |
| 24 | public function beforeImport($postId, $value, $data, $logger, $rawData) { |
| 25 | $delim = $value['delim'] ?? ','; |
| 26 | $gallery = $value['gallery'] ?? ''; |
| 27 | $search_in_files = empty($value['search_in_files']) ? 0 : 1; |
| 28 | $search_logic = $value['search_logic'] ?? 'by_url'; |
| 29 | $search_in_gallery = empty($value['search_in_media']) ? 0 : 1; |
| 30 | $only_append_new = $value['only_append_new'] ?? false; |
| 31 | $post_type = $data['articleData']['post_type'] ?? false; |
| 32 | |
| 33 | $current_ids = currentValue($post_type, $postId, $this->key, []); |
| 34 | |
| 35 | // Replace all newline characters with the delimiter |
| 36 | $urls = preg_replace("#\r\n|\r|\n#", $delim, trim($gallery)); |
| 37 | // Remove empty values |
| 38 | $urls = array_values(array_filter(explode($delim, $urls))); |
| 39 | |
| 40 | $ids = $only_append_new ? $current_ids : []; |
| 41 | |
| 42 | foreach ($urls as $url) { |
| 43 | |
| 44 | // Trim urls to ensure matching of existing images by URL works. |
| 45 | $url = trim($url); |
| 46 | |
| 47 | if (!self::isImage($url)) { |
| 48 | $attachment_id = self::import_file($url, $postId, $logger, $search_in_gallery, $search_in_files, $search_logic, $data); |
| 49 | } else { |
| 50 | $attachment_id = self::import_image($url, $postId, $logger, $search_in_gallery, $search_in_files, $search_logic, $data); |
| 51 | } |
| 52 | |
| 53 | // Add the attachment ID to the list of IDs if it's not already there |
| 54 | if ($attachment_id && !in_array($attachment_id, $ids)) { |
| 55 | $ids[] = $attachment_id; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return $ids; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Extracted from wpai-acf-add-on/src/ACFService.php |
| 64 | * @param $img_url |
| 65 | * @param $pid |
| 66 | * @param $logger |
| 67 | * @param bool $search_in_gallery |
| 68 | * @param bool $search_in_files |
| 69 | * |
| 70 | * @param array $importData |
| 71 | * @return bool|int|\WP_Error |
| 72 | */ |
| 73 | public static function import_image($img_url, $pid, $logger, $search_in_gallery = FALSE, $search_in_files = FALSE, $search_logic = 'by_filename', $importData = array()) { |
| 74 | |
| 75 | // Search image attachment by ID. |
| 76 | if ($search_in_gallery and is_numeric($img_url)) { |
| 77 | if (wp_get_attachment_url($img_url)) { |
| 78 | return $img_url; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | $downloadFiles = "yes"; |
| 83 | $fileName = ""; |
| 84 | |
| 85 | if ($search_in_gallery) { |
| 86 | $attch_id = searchExistingImage($img_url, $pid, $search_logic, 'images', $importData, $logger); |
| 87 | |
| 88 | if ($attch_id) { |
| 89 | return $attch_id; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Search for existing image in /files folder. |
| 94 | if ($search_in_files) { |
| 95 | $downloadFiles = "no"; |
| 96 | $fileName = wp_all_import_basename(wp_parse_url(trim($img_url), PHP_URL_PATH)); |
| 97 | } |
| 98 | |
| 99 | return PMXI_API::upload_image($pid, $img_url, $downloadFiles, $logger, true, $fileName, 'images', false, $importData['articleData'], $importData); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param $atch_url |
| 104 | * @param $pid |
| 105 | * @param $logger |
| 106 | * @param bool $fast |
| 107 | * @param bool $search_in_gallery |
| 108 | * @param bool $search_in_files |
| 109 | * |
| 110 | * @param array $importData |
| 111 | * @return bool|int|\WP_Error |
| 112 | */ |
| 113 | public static function import_file($atch_url, $pid, $logger, $search_in_gallery = FALSE, $search_in_files = FALSE, $search_logic = 'by_filename', $importData = array()) { |
| 114 | // search file attachment by ID |
| 115 | if ($search_in_gallery and is_numeric($atch_url)) { |
| 116 | if (wp_get_attachment_url($atch_url)) { |
| 117 | return $atch_url; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $downloadFiles = "yes"; |
| 122 | $fileName = ""; |
| 123 | |
| 124 | if ($search_in_gallery) { |
| 125 | $attch_id = searchExistingImage($atch_url, $pid, $search_logic, 'files', $importData, $logger); |
| 126 | |
| 127 | if ($attch_id) { |
| 128 | // Attach media to current post if it's currently unattached. |
| 129 | $attch = get_post($attch_id); |
| 130 | |
| 131 | if (empty($attch->post_parent)) { |
| 132 | wp_update_post( |
| 133 | array( |
| 134 | 'ID' => $attch_id, |
| 135 | 'post_parent' => $pid |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | return $attch_id; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Search for existing image in /files folder. |
| 145 | if ($search_in_files) { |
| 146 | $downloadFiles = "no"; |
| 147 | $fileName = basename($atch_url); |
| 148 | } |
| 149 | |
| 150 | return PMXI_API::upload_image($pid, $atch_url, $downloadFiles, $logger, true, $fileName, 'files', false, $importData['articleData'], $importData); |
| 151 | } |
| 152 | } |
| 153 |