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
media.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | use PMXI_API; |
| 8 | |
| 9 | class PMXI_Addon_Media_Field extends PMXI_Addon_Field { |
| 10 | |
| 11 | static $repeater_path = 'url'; |
| 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 | $img_url = $value['url'] ?? ''; |
| 26 | $search_in_files = empty($value['search_in_files']) ? 0 : 1; |
| 27 | $search_logic = $value['search_logic'] ?? 'by_url'; |
| 28 | $search_in_gallery = empty($value['search_in_media']) ? 0 : 1; |
| 29 | |
| 30 | if (!$img_url) { |
| 31 | return ''; |
| 32 | } |
| 33 | |
| 34 | // Trim urls to ensure matching of existing images by URL works. |
| 35 | $img_url = trim($img_url); |
| 36 | |
| 37 | // Search image attachment by ID. |
| 38 | if ($search_in_gallery and is_numeric($img_url)) { |
| 39 | if (wp_get_attachment_url($img_url)) { |
| 40 | return $img_url; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $fileName = ""; |
| 45 | $downloadFiles = "yes"; |
| 46 | |
| 47 | if ($search_in_gallery) { |
| 48 | $attch_id = searchExistingImage($img_url, $postId, $search_logic, 'images', $data, $logger); |
| 49 | |
| 50 | if ($attch_id) { |
| 51 | // Attach media to current post if it's currently unattached. |
| 52 | $attch = get_post($attch_id); |
| 53 | |
| 54 | if (empty($attch->post_parent)) { |
| 55 | wp_update_post( |
| 56 | array( |
| 57 | 'ID' => $attch_id, |
| 58 | 'post_parent' => $postId |
| 59 | ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | return $attch_id; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Search for existing image in /files folder. |
| 68 | if ($search_in_files) { |
| 69 | $downloadFiles = "no"; |
| 70 | $fileName = wp_all_import_basename(wp_parse_url(trim($img_url), PHP_URL_PATH)); |
| 71 | } |
| 72 | |
| 73 | if (!self::isImage($img_url)) { |
| 74 | $attach_id = PMXI_API::upload_image($postId, $img_url, $downloadFiles, $logger, true, $fileName, 'files', false, $data['articleData'], $data); |
| 75 | } else { |
| 76 | $attach_id = PMXI_API::upload_image($postId, $img_url, $downloadFiles, $logger, true, $fileName, 'images', false, $data['articleData'], $data); |
| 77 | } |
| 78 | |
| 79 | return $attach_id; |
| 80 | } |
| 81 | } |
| 82 |