PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.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 / class-cartflows-importer-core.php

class-cartflows-importer-core.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.0, at classes/class-cartflows-importer-core.php

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