PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 1.0.13
WDesignKit – AI Templates, Widget Builder & MCP Workflow v1.0.13
2.6.6 2.6.5 2.6.4 2.6.3 2.6.2 2.6.1 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5.0 2.4.0 2.3.3 2.3.2 2.3.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 All 128 releases
wdesignkit / includes / admin / class-wdkit-import-images.php

class-wdkit-import-images.php in WDesignKit – AI Templates, Widget Builder & MCP Workflow 1.0.13, at includes/admin/class-wdkit-import-images.php

172 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is used when importing media files.
4 *
5 * @link https://posimyth.com/
6 * @since 1.0.0
7 *
8 * @package Wdesignkit
9 */
10
11 /**Exit if accessed directly.*/
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 if ( ! class_exists( 'Wdkit_Import_Images' ) ) {
17
18 /**
19 * Template Import Images Here
20 *
21 * @since 1.0.0
22 */
23 class Wdkit_Import_Images {
24
25 /**
26 * Replaced images IDs.
27 *
28 * The old attachment ID and the new attachment ID generated after the import.
29 *
30 * @since 1.0.0
31 * @access private
32 *
33 * @var array
34 */
35 private static $new_image_ids = array();
36
37 /**
38 * Get attachment url image hash sha1.
39 *
40 * Retrieve the sha1 hash of the image URL.
41 *
42 * @since 1.0.0
43 * @access private
44 *
45 * @param string $attachment_url The attachment URL.
46 */
47 private static function get_attachment_url_hash_image( $attachment_url ) {
48 return sha1( $attachment_url );
49 }
50
51 /**
52 * Media Import image.
53 *
54 * Import a single image from a remote server, upload the image WordPress
55 * uploads folder, create a new attachment in the database and updates the
56 * attachment metadata.
57 *
58 * @since 1.0.0
59 * @access public
60 *
61 * @param array $attachment The attachment.
62 */
63 public static function wdkit_Import_media( $attachment ) {
64 $stored_image = self::get_store_image_saved( $attachment );
65
66 if ( $stored_image ) {
67 return $stored_image;
68 }
69
70 // Extract the file name and extension from the url.
71 $file_name = basename( $attachment['url'] );
72
73 $file_content = wp_remote_retrieve_body( wp_safe_remote_get( $attachment['url'] ) );
74
75 if ( empty( $file_content ) ) {
76 return false;
77 }
78
79 $upload_data = wp_upload_bits( $file_name, null, $file_content );
80
81 $post_image = array(
82 'post_title' => $file_name,
83 'guid' => $upload_data['url'],
84 );
85
86 $info = wp_check_filetype( $upload_data['file'] );
87 if ( ! empty( $info ) ) {
88 $post_image['post_mime_type'] = $info['type'];
89 } else {
90 return $attachment;
91 }
92
93 $post_id = wp_insert_attachment( $post_image, $upload_data['file'] );
94
95 // On REST requests.
96 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
97 require_once ABSPATH . '/wp-admin/includes/image.php';
98 }
99
100 if ( ! function_exists( 'wp_read_video_metadata' ) ) {
101 require_once ABSPATH . '/wp-admin/includes/media.php';
102 }
103
104 wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload_data['file'] ) );
105 update_post_meta( $post_id, 'tpgb_source_image_key', self::get_attachment_url_hash_image( $attachment['url'] ) );
106
107 $new_attachment_img = array(
108 'id' => $post_id,
109 'url' => $upload_data['url'],
110 );
111
112 if ( isset( $attachment['id'] ) ) {
113 self::$new_image_ids[ $attachment['id'] ] = $new_attachment_img;
114 }
115
116 return $new_attachment_img;
117 }
118
119 /**
120 * Get store saved image.
121 *
122 * Retrieve new image ID, if the image has a new ID after the import.
123 *
124 * @since 1.0.0
125 * @access private
126 *
127 * @param array $attachment The attachment.
128 */
129 private static function get_store_image_saved( $attachment ) {
130 global $wpdb;
131
132 if ( isset( $attachment['id'] ) && isset( self::$new_image_ids[ $attachment['id'] ] ) ) {
133 return self::$new_image_ids[ $attachment['id'] ];
134 }
135
136 $post_id = $wpdb->get_var(
137 $wpdb->prepare( 'SELECT `post_id` FROM `' . $wpdb->postmeta . '` WHERE `meta_key` = \'tpgb_source_image_key\' AND `meta_value` = %s;', self::get_attachment_url_hash_image( $attachment['url'] ) )
138 );
139
140 if ( ! empty( $post_id ) ) {
141 $new_attachment_img = array(
142 'id' => $post_id,
143 'url' => wp_get_attachment_url( $post_id ),
144 );
145
146 if ( isset( $attachment['id'] ) ) {
147 self::$new_image_ids[ $attachment['id'] ] = $new_attachment_img;
148 }
149
150 return $new_attachment_img;
151 }
152
153 return false;
154 }
155
156 /**
157 * Import images Constructor.
158 *
159 * @since 1.0.0
160 * @access public
161 */
162 public function __construct() {
163 if ( ! function_exists( 'WP_Filesystem' ) ) {
164 require_once ABSPATH . 'wp-admin/includes/file.php';
165 }
166
167 WP_Filesystem();
168 }
169 }
170
171 }
172