PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.1
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.1, at classes/batch-process/helpers/class-cartflows-importer-image.php

264 lines 6.0 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 if ( apply_filters( 'cartflows_image_importer_skip_image', false, $attachment ) ) {
110
111 CartFlows_Import_Image::log( 'Download (✕) Replace (✕) - ' . $attachment['url'] );
112
113 return $attachment;
114 }
115
116 global $wpdb;
117
118 // Already imported? Then return!
119 if ( isset( $this->already_imported_ids[ $attachment['id'] ] ) ) {
120
121 CartFlows_Import_Image::log( 'Download (✓) Replace (✓) - ' . $attachment['url'] );
122
123 return $this->already_imported_ids[ $attachment['id'] ];
124 }
125
126 // 1. Is already imported in Batch Import Process?
127 $post_id = $wpdb->get_var(
128 $wpdb->prepare(
129 'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
130 WHERE `meta_key` = \'_cartflows_image_hash\'
131 AND `meta_value` = %s
132 ;',
133 $this->get_hash_image( $attachment['url'] )
134 )
135 );
136
137 // 2. Is image already imported though XML?
138 if ( empty( $post_id ) ) {
139
140 // Get file name without extension.
141 // To check it exist in attachment.
142 $filename = preg_replace( '/\\.[^.\\s]{3,4}$/', '', basename( $attachment['url'] ) );
143
144 $post_id = $wpdb->get_var(
145 $wpdb->prepare(
146 'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
147 WHERE `meta_key` = \'_wp_attached_file\'
148 AND `meta_value` LIKE %s
149 ;',
150 '%' . $filename . '%'
151 )
152 );
153
154 CartFlows_Import_Image::log( 'Download (✓) Replace (✓) - ' . $attachment['url'] );
155 }
156
157 if ( $post_id ) {
158 $new_attachment = array(
159 'id' => $post_id,
160 'url' => wp_get_attachment_url( $post_id ),
161 );
162 $this->already_imported_ids[ $attachment['id'] ] = $new_attachment;
163
164 return $new_attachment;
165 }
166
167 return false;
168 }
169
170 /**
171 * Import Image
172 *
173 * @since 1.1.1
174 * @param array $attachment Attachment array.
175 * @return array Attachment array.
176 */
177 public function import( $attachment ) {
178
179 $saved_image = $this->get_saved_image( $attachment );
180 if ( $saved_image ) {
181 return $saved_image;
182 }
183
184 $file_content = wp_remote_retrieve_body( wp_safe_remote_get( $attachment['url'] ) );
185
186 // Empty file content?
187 if ( empty( $file_content ) ) {
188
189 CartFlows_Import_Image::log( 'Download (✕) Replace (✕) - ' . $attachment['url'] );
190 CartFlows_Import_Image::log( 'Error: Failed wp_remote_retrieve_body().' );
191
192 return $attachment;
193 }
194
195 // Extract the file name and extension from the URL.
196 $filename = basename( $attachment['url'] );
197
198 $upload = wp_upload_bits(
199 $filename,
200 null,
201 $file_content
202 );
203
204 $post = array(
205 'post_title' => $filename,
206 'guid' => $upload['url'],
207 );
208
209 $info = wp_check_filetype( $upload['file'] );
210 if ( $info ) {
211 $post['post_mime_type'] = $info['type'];
212 } else {
213 // For now just return the origin attachment.
214 return $attachment;
215 }
216
217 $post_id = wp_insert_attachment( $post, $upload['file'] );
218 wp_update_attachment_metadata(
219 $post_id,
220 wp_generate_attachment_metadata( $post_id, $upload['file'] )
221 );
222 update_post_meta( $post_id, '_cartflows_image_hash', $this->get_hash_image( $attachment['url'] ) );
223
224 $new_attachment = array(
225 'id' => $post_id,
226 'url' => $upload['url'],
227 );
228
229 CartFlows_Import_Image::log( 'Download (✓) Replace (✓) - ' . $attachment['url'] );
230
231 $this->already_imported_ids[ $attachment['id'] ] = $new_attachment;
232
233 return $new_attachment;
234 }
235
236 /**
237 * Debugging Log.
238 *
239 * @since 1.1.1
240 * @param mixed $log Log data.
241 * @return void
242 */
243 public static function log( $log ) {
244
245 if ( ! WP_DEBUG_LOG ) {
246 return;
247 }
248
249 if ( is_array( $log ) || is_object( $log ) ) {
250 error_log( print_r( $log, true ) );
251 } else {
252 error_log( $log );
253 }
254 }
255
256 }
257
258 /**
259 * Initialize class object with 'get_instance()' method
260 */
261 CartFlows_Import_Image::get_instance();
262
263 endif;
264