| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageHub; |
| 4 |
|
| 5 |
use ImageHub\Utils; |
| 6 |
|
| 7 |
|
| 8 |
class MediaModal |
| 9 |
{ |
| 10 |
|
| 11 |
protected $settings; |
| 12 |
|
| 13 |
public function __construct($settings) |
| 14 |
{ |
| 15 |
$this->settings = $settings; |
| 16 |
|
| 17 |
|
| 18 |
if (get_option('image_hub_enable_media_modal', true)) { |
| 19 |
add_action('admin_enqueue_scripts', array($this, 'image_hub_enqueue_media_library_scripts')); |
| 20 |
add_action('elementor/editor/after_enqueue_styles', array($this, 'image_hub_enqueue_media_library_scripts')); |
| 21 |
// add_action('print_media_templates', array($this, 'image_hub_create_media_library_root')); |
| 22 |
add_action('media_upload_image_hub_tab', array($this, 'image_hub_media_upload_handler')); |
| 23 |
|
| 24 |
add_action('wp_ajax_image_hub_download_image_to_library', array($this, 'image_hub_download_to_library')); |
| 25 |
|
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function image_hub_enqueue_media_library_scripts($id = '') |
| 34 |
{ |
| 35 |
if (is_admin()) { |
| 36 |
Utils::enque_image_hub_script('media-modal', false); |
| 37 |
|
| 38 |
wp_localize_script('image-hub-media-modal', 'image_hub_settings', $this->settings); |
| 39 |
|
| 40 |
Utils::localize_props('image-hub-media-modal'); |
| 41 |
|
| 42 |
wp_enqueue_style( |
| 43 |
'image-hub-style', |
| 44 |
IMAGE_HUB_ROOT_URL . '/assets/css/index.css', |
| 45 |
array(), |
| 46 |
Utils::get_version() |
| 47 |
); |
| 48 |
wp_enqueue_style('wp-components'); |
| 49 |
wp_set_script_translations('image-hub-media-modal', 'image-hub'); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Handles the AJAX request to download an image into the library. |
| 55 |
*/ |
| 56 |
public function image_hub_download_to_library() |
| 57 |
{ |
| 58 |
if ( |
| 59 |
!isset($_POST['nonce']) || |
| 60 |
!wp_verify_nonce(sanitize_key(wp_unslash($_POST['nonce'])), 'image_hub_nonce') |
| 61 |
) { |
| 62 |
wp_send_json_error('Nonce verification failed.'); |
| 63 |
} |
| 64 |
|
| 65 |
// call the callback url to notify provider that image is downloaded (unsplash terms) |
| 66 |
//https://help.unsplash.com/en/articles/2511258-guideline-triggering-a-download |
| 67 |
if (isset($_POST['downloadImageCallbackUrl']) && !empty($_POST['downloadImageCallbackUrl'])) { |
| 68 |
$callback_url = esc_url_raw(wp_unslash($_POST['downloadImageCallbackUrl'])); |
| 69 |
|
| 70 |
if (filter_var($callback_url, FILTER_VALIDATE_URL)) { |
| 71 |
$response = wp_remote_get($callback_url); |
| 72 |
if (is_wp_error($response)) { |
| 73 |
wp_send_json_error('Failed to fetch data from callback URL. ' . $callback_url); |
| 74 |
} |
| 75 |
} else { |
| 76 |
// error_log('Invalid callback URL format: ' . $_POST['downloadImageCallbackUrl']); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$image_url = isset($_POST['image_url']) ? esc_url_raw(wp_unslash($_POST['image_url'])) : ''; |
| 81 |
|
| 82 |
if (isset($_POST['provider']) && $_POST['provider'] === 'unsplash') { |
| 83 |
$max_width = get_option('image_hub_max_image_width'); |
| 84 |
$max_height = get_option('image_hub_max_image_height'); |
| 85 |
//here add query param of width and height to the image url |
| 86 |
if ($max_height || $max_width) |
| 87 |
if ($max_height > 0 && $max_width > 0) { |
| 88 |
$image_url = add_query_arg(['w' => $max_width, 'h' => $max_height], $image_url); |
| 89 |
} else if ($max_height > 0) { |
| 90 |
$image_url = add_query_arg(['h' => $max_height], $image_url); |
| 91 |
} else if ($max_width > 0) { |
| 92 |
$image_url = add_query_arg(['w' => $max_width], $image_url); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (empty($image_url)) { |
| 97 |
wp_send_json_error('No valid image URL provided.'); |
| 98 |
} |
| 99 |
|
| 100 |
$parsed_url = wp_parse_url($image_url); |
| 101 |
|
| 102 |
if (empty($parsed_url['host'])) { |
| 103 |
wp_send_json_error('Malformed image URL.'); |
| 104 |
} |
| 105 |
|
| 106 |
$host_parts = explode('.', $parsed_url['host']); |
| 107 |
|
| 108 |
// Sanitize all expected fields |
| 109 |
$data = [ |
| 110 |
'title' => sanitize_text_field(wp_unslash($_POST['title'] ?? '')), |
| 111 |
'name' => sanitize_text_field(wp_unslash($_POST['name'] ?? '')), |
| 112 |
'description' => sanitize_textarea_field(wp_unslash($_POST['description'] ?? '')), |
| 113 |
'alt' => sanitize_text_field(wp_unslash($_POST['alt'] ?? '')), |
| 114 |
'provider' => sanitize_text_field(wp_unslash($_POST['provider'] ?? '')), |
| 115 |
'link' => esc_url_raw(wp_unslash($_POST['link'] ?? '')), |
| 116 |
'username' => sanitize_text_field(wp_unslash($_POST['username'] ?? '')) |
| 117 |
]; |
| 118 |
|
| 119 |
$data['caption'] = Utils::create_attribution($data['provider'], $data['link'], $data['username']); |
| 120 |
|
| 121 |
// Download image |
| 122 |
$image_id = Utils::download_image($image_url, $data['description']); |
| 123 |
if (is_wp_error($image_id)) { |
| 124 |
wp_send_json_error($image_id->get_error_message()); |
| 125 |
} |
| 126 |
|
| 127 |
$file_path = get_attached_file($image_id); |
| 128 |
if (!$file_path) { |
| 129 |
wp_send_json_error('Failed to retrieve file path.'); |
| 130 |
} |
| 131 |
|
| 132 |
$new_file_path = Utils::rename_image($file_path, $data['name'], $image_id); |
| 133 |
|
| 134 |
if ($data['provider'] !== 'giphy') { |
| 135 |
Utils::resize_image($new_file_path); |
| 136 |
} |
| 137 |
|
| 138 |
Utils::update_attachment_data($image_id, $data, $new_file_path); |
| 139 |
|
| 140 |
wp_send_json_success([ |
| 141 |
'message' => 'Image successfully downloaded and added to media library!', |
| 142 |
'attachment' => [ |
| 143 |
'id' => $image_id, |
| 144 |
'url' => wp_get_attachment_url($image_id), |
| 145 |
'edit_url' => get_edit_post_link($image_id), |
| 146 |
'alt' => $data['alt'], |
| 147 |
'caption' => $data['caption'], |
| 148 |
'description' => $data['description'] |
| 149 |
], |
| 150 |
]); |
| 151 |
} |
| 152 |
} |
| 153 |
|