PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.2.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.2.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / importer / batch-process / helpers / class-cartflows-importer-image.php

class-cartflows-importer-image.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.2.0, at classes/importer/batch-process/helpers/class-cartflows-importer-image.php

261 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Image Importer
4 *
5 * => How to use?
6 *
7 * $image = array(
8 * 'url' => '<image-url>',
9 * 'id' => '<image-id>',
10 * );
11 *
12 * $downloaded_image = CartFlows_Import_Image::get_instance()->import( $image );
13 *
14 * @package CartFlows
15 * @since 1.1.1
16 */
17
18 // Exit if accessed directly.
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 if ( ! class_exists( 'CartFlows_Import_Image' ) ) :
24
25 /**
26 * CartFlows Importer
27 *
28 * @since 1.1.1
29 */
30 class CartFlows_Import_Image {
31
32 /**
33 * Instance
34 *
35 * @since 1.1.1
36 * @var object Class object.
37 * @access private
38 */
39 private static $instance;
40
41 /**
42 * Images IDs
43 *
44 * @var array The Array of already image IDs.
45 * @since 1.1.1
46 */
47 private $already_imported_ids = array();
48
49 /**
50 * Initiator
51 *
52 * @since 1.1.1
53 * @return object initialized object of class.
54 */
55 public static function get_instance() {
56 if ( ! isset( self::$instance ) ) {
57 self::$instance = new self();
58 }
59 return self::$instance;
60 }
61
62 /**
63 * Constructor
64 *
65 * @since 1.1.1
66 */
67 public function __construct() {
68
69 if ( ! function_exists( 'WP_Filesystem' ) ) {
70 require_once ABSPATH . 'wp-admin/includes/file.php';
71 }
72
73 WP_Filesystem();
74 }
75
76 /**
77 * Process Image Download
78 *
79 * @since 1.1.1
80 * @param array $attachments Attachment array.
81 * @return array Attachment array.
82 */
83 public function process( $attachments ) {
84
85 $downloaded_images = array();
86
87 foreach ( $attachments as $key => $attachment ) {
88 $downloaded_images[] = $this->import( $attachment );
89 }
90
91 return $downloaded_images;
92 }
93
94 /**
95 * Get Hash Image.
96 *
97 * @since 1.1.1
98 * @param string $attachment_url Attachment URL.
99 * @return string Hash string.
100 */
101 private function get_hash_image( $attachment_url ) {
102 return sha1( $attachment_url );
103 }
104
105 /**
106 * Get Saved Image.
107 *
108 * @since 1.1.1
109 * @param string $attachment Attachment Data.
110 * @return string Hash string.
111 */
112 private function get_saved_image( $attachment ) {
113
114 if ( apply_filters( 'cartflows_image_importer_skip_image', false, $attachment ) ) {
115 wcf()->logger->import_log( 'IMAGE - SKIP Image - {from filter} - ' . $attachment['url'] . ' - Filter name `cartflows_image_importer_skip_image`.' );
116 return array(
117 'status' => true,
118 'attachment' => $attachment,
119 );
120 }
121
122 global $wpdb;
123
124 // 1. Is already imported in Batch Import Process?
125 $post_id = $wpdb->get_var(
126 $wpdb->prepare(
127 'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
128 WHERE `meta_key` = \'_cartflows_image_hash\'
129 AND `meta_value` = %s
130 ;',
131 $this->get_hash_image( $attachment['url'] )
132 )
133 );
134
135 // 2. Is image already imported though XML?
136 if ( empty( $post_id ) ) {
137
138 // Get file name without extension.
139 // To check it exist in attachment.
140 $filename = basename( $attachment['url'] );
141
142 $post_id = $wpdb->get_var(
143 $wpdb->prepare(
144 "SELECT post_id FROM {$wpdb->postmeta}
145 WHERE meta_key = '_wp_attached_file'
146 AND meta_value LIKE %s",
147 '%' . $filename . '%'
148 )
149 );
150
151 wcf()->logger->import_log( 'IMAGE - SKIP Image {already imported from xml} - ' . $attachment['url'] );
152 }
153
154 if ( $post_id ) {
155 $new_attachment = array(
156 'id' => $post_id,
157 'url' => wp_get_attachment_url( $post_id ),
158 );
159 $this->already_imported_ids[] = $post_id;
160
161 return array(
162 'status' => true,
163 'attachment' => $new_attachment,
164 );
165 }
166
167 return array(
168 'status' => false,
169 'attachment' => $attachment,
170 );
171 }
172
173 /**
174 * Import Image
175 *
176 * @since 1.1.1
177 * @param array $attachment Attachment array.
178 * @return array Attachment array.
179 */
180 public function import( $attachment ) {
181
182 wcf()->logger->import_log( 'Source - ' . $attachment['url'] );
183 $saved_image = $this->get_saved_image( $attachment );
184 wcf()->logger->import_log( 'Log - ' . wp_json_encode( $saved_image['attachment'] ) );
185
186 if ( $saved_image['status'] ) {
187 return $saved_image['attachment'];
188 }
189
190 $file_content = wp_remote_retrieve_body(
191 wp_safe_remote_get(
192 $attachment['url'],
193 array(
194 'timeout' => '60', //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
195 'sslverify' => false,
196 )
197 )
198 );
199
200 // Empty file content?
201 if ( empty( $file_content ) ) {
202
203 wcf()->logger->import_log( 'IMAGE - FAIL Image {Error: Failed wp_remote_retrieve_body} - ' . $attachment['url'] );
204 return $attachment;
205 }
206
207 // Extract the file name and extension from the URL.
208 $filename = basename( $attachment['url'] );
209
210 $upload = wp_upload_bits(
211 $filename,
212 null,
213 $file_content
214 );
215
216 wcf()->logger->import_log( $filename );
217 wcf()->logger->import_log( wp_json_encode( $upload ) );
218
219 $post = array(
220 'post_title' => $filename,
221 'guid' => $upload['url'],
222 );
223 wcf()->logger->import_log( wp_json_encode( $post ) );
224
225 $info = wp_check_filetype( $upload['file'] );
226 if ( $info ) {
227 $post['post_mime_type'] = $info['type'];
228 } else {
229 // For now just return the origin attachment.
230 return $attachment;
231 }
232
233 $post_id = wp_insert_attachment( $post, $upload['file'] );
234 wp_update_attachment_metadata(
235 $post_id,
236 wp_generate_attachment_metadata( $post_id, $upload['file'] )
237 );
238 update_post_meta( $post_id, '_cartflows_image_hash', $this->get_hash_image( $attachment['url'] ) );
239 // update_post_meta( $post_id, '_cartflows_original_url', $attachment['url'] );
240
241 $new_attachment = array(
242 'id' => $post_id,
243 'url' => $upload['url'],
244 );
245
246 wcf()->logger->import_log( 'IMAGE - SUCCESS Image {Imported} - ' . $new_attachment['url'] );
247
248 $this->already_imported_ids[] = $post_id;
249
250 return $new_attachment;
251 }
252
253 }
254
255 /**
256 * Kicking this off by calling 'get_instance()' method
257 */
258 CartFlows_Import_Image::get_instance();
259
260 endif;
261