PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.15
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.15
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 / batch-process / helpers / class-cartflows-importer-image.php

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

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