PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.11
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.11
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.11, at classes/class-cartflows-importer-core.php

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