Skip to content

Commit 8789723

Browse files
authored
packaged version 4.25.0 (#335)
1 parent 68a805c commit 8789723

23 files changed

+451
-38
lines changed

CHANGELOG.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [4.25.0]
4+
5+
### Added
6+
- Added ability to control images import. It adds the ability to choose between serving images from external CDN, skip import completely, or serving images as usual. In order to do that you should go to Bigcommerce > Settings > Product Sync > Images Import. You are able to choose between 3 options:
7+
- Full images import - the default behavior. During the sync with Bigcommerce, each image will be downloaded and stored in WordPress locally. All images will be served from the WordPress environment
8+
- Import only images URL - a new option that allows to retrieve only images URIs from Bigcommerce and serve images with that URIs. Images won't be stored in WordPress locally and will be loaded from an external source(Bigcommerce CDN)
9+
- Disable images import - completely disable images import. No images won't be added to the WordPress environment during the sync process. Images from Bigcommerce CDN won't be loaded as well. Despite that, you are still able to set a featured image on the product and it will be displayed on the frontend
10+
311
## [4.24.0]
412

513
### Added
@@ -1696,7 +1704,8 @@
16961704
in fact, reset postdata, so far as Gutenberg 3.2.0 is concerned.
16971705

16981706

1699-
[4.23.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.23.0...4.24.0
1707+
[4.25.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.24.0...4.25.0
1708+
[4.24.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.23.0...4.24.0
17001709
[4.23.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.22.0...4.23.0
17011710
[4.22.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.21.1...4.22.0
17021711
[4.21.0]: https://github.com/bigcommerce/bigcommerce-for-wordpress/compare/4.20.1...4.21.0

bigcommerce.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: BigCommerce for WordPress
44
Description: Scale your ecommerce business with WordPress on the front-end and BigCommerce on the back end. Free up server resources from things like catalog management, processing payments, and managing fulfillment logistics.
55
Author: BigCommerce
6-
Version: 4.24.0
6+
Version: 4.25.0
77
Author URI: https://www.bigcommerce.com/wordpress
88
Requires PHP: 7.4.0
99
Text Domain: bigcommerce

build-timestamp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?php
2-
define('BIGCOMMERCE_ASSETS_BUILD_TIMESTAMP', '5.08.02.15.2022');
2+
define('BIGCOMMERCE_ASSETS_BUILD_TIMESTAMP', '5.01.03.07.2022');

readme.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: bigcommerce, moderntribe, jbrinley, becomevocal, vincentlistrani,
33
Tags: ecommerce, online store, sell online, storefront, retail, online shop, bigcommerce, big commerce, e-commerce, physical products, buy buttons, commerce, shopping cart, checkout, cart, shop, headless commerce, shipping, payments, fulfillment
44
Requires at least: 5.2
55
Tested up to: 5.8.1
6-
Stable tag: 4.24.0
6+
Stable tag: 4.25.0
77
Requires PHP: 7.4.0
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html

src/BigCommerce/Container/Image.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BigCommerce\Container;
4+
5+
use BigCommerce\Import\Image_Importer;
6+
use Pimple\Container;
7+
8+
class Image extends Provider {
9+
10+
public function register( Container $container ) {
11+
12+
add_filter( 'bigcommerce/import/product/import_images', $this->create_callback( 'images_import_full_disabled', function ( ) {
13+
return Image_Importer::is_image_import_allowed();
14+
} ), 10, 0 );
15+
16+
add_filter( 'wp_get_attachment_image', $this->create_callback( 'handle_attachment_via_cdn', function ( $html, $thumb_id ) {
17+
$bigcommerce_id = get_post_meta( $thumb_id, 'bigcommerce_id', true );
18+
19+
if ( empty( $bigcommerce_id ) ) {
20+
return $html;
21+
}
22+
23+
if ( ! Image_Importer::should_load_from_cdn() ) {
24+
return $html;
25+
}
26+
27+
$src = get_post_meta( $thumb_id, Image_Importer::URL_THUMB, true );
28+
29+
if ( empty( $src ) ) {
30+
return $html;
31+
}
32+
33+
$html = preg_replace( '/src="[^"]*"/', 'src="' . $src . '"', $html );
34+
$html = preg_replace( '/srcset="[^"]*"/', '', $html );
35+
36+
return $html;
37+
} ), 10, 2 );
38+
}
39+
40+
}

src/BigCommerce/Import/Image_Importer.php

+142-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,42 @@
55

66
use BigCommerce\Assets\Theme\Image_Sizes;
77
use BigCommerce\Logging\Error_Log;
8+
use BigCommerce\Settings\Sections\Import;
89

910
/**
1011
* Class Image_Importer
1112
*
1213
* Imports an image from a URL and attaches it to a post
1314
*/
1415
class Image_Importer {
15-
const SOURCE_URL = 'bigcommerce_source_url';
16+
const SOURCE_URL = 'bigcommerce_source_url';
17+
const URL_ZOOM = 'url_zoom';
18+
const URL_STD = 'url_standard';
19+
const URL_THUMB = 'url_thumbnail';
20+
const URL_TINY = 'url_tiny';
21+
const FULL_IMAGE_IMPORT = 'bigcommerce_allow_full_image_import';
22+
const CDN_IMAGE_IMPORT = 'bigcommerce_allow_cdn_image_import';
23+
const DISABLE_IMAGE_IMPORT = 'bigcommerce_disable_image_import';
24+
25+
const MIMES = [
26+
IMAGETYPE_GIF => 'image/gif',
27+
IMAGETYPE_JPEG => 'image/jpg',
28+
IMAGETYPE_PNG => 'image/png',
29+
IMAGETYPE_SWF => 'image/swf',
30+
IMAGETYPE_PSD => 'image/psd',
31+
IMAGETYPE_BMP => 'image/bmp',
32+
IMAGETYPE_TIFF_II => 'image/tiff',
33+
IMAGETYPE_TIFF_MM => 'image/tiff',
34+
IMAGETYPE_JPC => 'image/jpc',
35+
IMAGETYPE_JP2 => 'image/jp2',
36+
IMAGETYPE_JPX => 'image/jpx',
37+
IMAGETYPE_JB2 => 'image/jb2',
38+
IMAGETYPE_SWC => 'image/swc',
39+
IMAGETYPE_IFF => 'image/iff',
40+
IMAGETYPE_WBMP => 'image/wbmp',
41+
IMAGETYPE_XBM => 'image/xbm',
42+
IMAGETYPE_ICO => 'image/ico',
43+
];
1644

1745
private $image_url;
1846
private $attach_to_post_id;
@@ -24,6 +52,11 @@ public function __construct( $image_url, $attach_to_post_id = 0 ) {
2452

2553
public function import() {
2654
$this->require_files();
55+
56+
if ( self::should_load_from_cdn() ) {
57+
return $this->process_cdn_items();
58+
}
59+
2760
$tmp = download_url( $this->image_url );
2861
if ( is_wp_error( $tmp ) ) {
2962
do_action( 'bigcommerce/import/log', Error_Log::NOTICE, __( 'Failed to download image', 'bigcommerce' ), [
@@ -66,4 +99,112 @@ private function require_files() {
6699
require_once( ABSPATH . 'wp-admin/includes/media.php' );
67100
require_once( ABSPATH . 'wp-admin/includes/image.php' );
68101
}
102+
103+
/**
104+
* Check if images serving from BigCommerce CDN is enabled
105+
*
106+
* @return bool
107+
*/
108+
public static function should_load_from_cdn() {
109+
return get_option( Import::ENABLE_IMAGE_IMPORT ) === self::CDN_IMAGE_IMPORT;
110+
}
111+
112+
/**
113+
* Check whether image import is enabled
114+
* @return bool
115+
*/
116+
public static function is_image_import_allowed() {
117+
return get_option( Import::ENABLE_IMAGE_IMPORT ) !== self::DISABLE_IMAGE_IMPORT;
118+
}
119+
120+
/**
121+
* Is featured image exists only on WP side
122+
*
123+
* @param int $post_id
124+
*
125+
* @return bool
126+
*/
127+
public static function has_local_featured_image( $post_id = 0 ) {
128+
if ( empty( $post_id ) ) {
129+
return false;
130+
}
131+
132+
$thumb_id = get_post_thumbnail_id( $post_id );
133+
134+
if ( empty( $thumb_id ) ) {
135+
return false;
136+
}
137+
138+
$thumbnail_bc_id = get_post_meta( $thumb_id, 'bigcommerce_id', true );
139+
140+
// Local images doesn't have BC id
141+
if ( ! empty( $thumbnail_bc_id ) ) {
142+
return false;
143+
}
144+
145+
return true;
146+
}
147+
148+
/**
149+
* @return false|int|\WP_Error
150+
*/
151+
private function process_cdn_items() {
152+
$path = parse_url( $this->image_url, PHP_URL_PATH );
153+
$name = basename( $path );
154+
155+
$attachment = [
156+
'guid' => $this->image_url,
157+
'post_title' => $name,
158+
'post_status' => 'inherit',
159+
'post_mime_type' => $this->get_image_mime_type(),
160+
'post_type' => 'attachment',
161+
];
162+
163+
try {
164+
$image_id = wp_insert_attachment( $attachment, $name, $this->attach_to_post_id );
165+
166+
if ( is_wp_error( $image_id ) || empty( $image_id ) ) {
167+
return false;
168+
}
169+
170+
update_post_meta( $image_id, self::SOURCE_URL, $this->image_url );
171+
172+
return $image_id;
173+
} catch ( \Exception $exception ) {
174+
do_action( 'bigcommerce/import/log', Error_Log::NOTICE, __( 'Failed to save CDN image', 'bigcommerce' ), [
175+
'url' => $this->image_url,
176+
'error' => $exception->getMessage(),
177+
] );
178+
179+
return false;
180+
}
181+
}
182+
183+
/**
184+
* Get image mime type
185+
*
186+
* @return string
187+
*/
188+
private function get_image_mime_type() {
189+
try {
190+
if ( empty( $this->image_url ) ) {
191+
return '';
192+
}
193+
194+
$image_type = exif_imagetype( $this->image_url );
195+
196+
if ( array_key_exists( $image_type, self::MIMES ) ) {
197+
return self::MIMES[ $image_type ];
198+
}
199+
200+
return '';
201+
} catch ( \Exception $exception ) {
202+
do_action( 'bigcommerce/import/log', Error_Log::NOTICE, __( 'Failed to get CDN image mime type', 'bigcommerce' ), [
203+
'url' => $this->image_url,
204+
'error' => $exception->getMessage(),
205+
] );
206+
207+
return '';
208+
}
209+
}
69210
}

src/BigCommerce/Import/Importers/Products/Product_Builder.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function build_post_array() {
7575
}
7676

7777
private function get_post_title() {
78-
$title = $this->listing->getName() ?: $this->product->getName();
78+
$title = $this->listing->getName() ?: $this->product->getName();
7979

8080
return $this->sanitize_title( $title );
8181
}
@@ -253,6 +253,7 @@ public function build_images( $parent_id ) {
253253
}
254254

255255
$images = $this->product[ 'images' ];
256+
256257
usort( $images, function ( $a, $b ) {
257258
if ( $a[ 'sort_order' ] == $b[ 'sort_order' ] ) {
258259
return 0;
@@ -279,12 +280,12 @@ public function build_images( $parent_id ) {
279280
if ( ! empty( $existing ) ) {
280281
$post_id = reset( $existing );
281282
} else {
282-
$importer = new Image_Importer( $image[ 'url_zoom' ], $parent_id );
283+
$importer = new Image_Importer( $image[ Image_Importer::URL_ZOOM ], $parent_id );
283284
$post_id = $importer->import();
284285
}
285286
if ( ! empty( $post_id ) ) {
286287
update_post_meta( $post_id, 'bigcommerce_id', $image[ 'id' ] );
287-
foreach ( [ 'url_zoom', 'url_standard', 'url_thumbnail', 'url_tiny' ] as $key ) {
288+
foreach ( [ Image_Importer::URL_ZOOM, Image_Importer::URL_STD, Image_Importer::URL_THUMB, Image_Importer::URL_TINY ] as $key ) {
288289
$derivative_url = $image[ $key ];
289290
if ( $derivative_url ) {
290291
update_post_meta( $post_id, $key, $derivative_url );

src/BigCommerce/Import/Importers/Products/Product_Saver.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BigCommerce\Api\v3\Api\CatalogApi;
66
use BigCommerce\Api\v3\Model;
7+
use BigCommerce\Import\Image_Importer;
78
use BigCommerce\Import\Import_Strategy;
89
use BigCommerce\Post_Types\Product\Product;
910
use BigCommerce\Taxonomies\Availability\Availability;
@@ -204,12 +205,15 @@ protected function save_terms( Product_Builder $builder ) {
204205
* @return void
205206
*/
206207
protected function save_images( Product_Builder $builder ) {
207-
$images = $builder->build_images( $this->post_id );
208-
if ( array_key_exists( 'thumbnail', $images ) ) {
208+
$images = $builder->build_images( $this->post_id );
209+
$is_local_featured = Image_Importer::has_local_featured_image( $this->post_id );
210+
211+
if ( array_key_exists( 'thumbnail', $images ) && ! $is_local_featured ) {
209212
update_post_meta( $this->post_id, '_thumbnail_id', $images['thumbnail'] );
210-
} else {
213+
} elseif ( ! $is_local_featured ) {
211214
delete_post_meta( $this->post_id, '_thumbnail_id' );
212215
}
216+
213217
if ( array_key_exists( 'gallery', $images ) ) {
214218
update_post_meta( $this->post_id, Product::GALLERY_META_KEY, $images['gallery'] );
215219
} else {

src/BigCommerce/Plugin.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace BigCommerce;
55

66
class Plugin {
7-
const VERSION = '4.24.0';
7+
const VERSION = '4.25.0';
88

99
protected static $_instance;
1010

@@ -82,6 +82,7 @@ private function load_service_providers() {
8282
$this->providers[ 'webhooks' ] = new Container\Webhooks();
8383
$this->providers[ 'util' ] = new Container\Util();
8484
$this->providers[ 'banners' ] = new Container\Banners();
85+
$this->providers[ 'images' ] = new Container\Image();
8586

8687

8788
/**

src/BigCommerce/Post_Types/Product/Admin_List.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BigCommerce\Post_Types\Product;
44

55
use BigCommerce\Assets\Theme\Image_Sizes;
6+
use BigCommerce\Import\Image_Importer;
67
use Pimple\Container;
78
use Symfony\Component\DomCrawler\Image;
89

@@ -65,9 +66,20 @@ public function get_bigcommerce_product_id_value( $column_name, $post_ID ) {
6566
* @action manage_bigcommerce_product_posts_custom_column for BC product ID
6667
*/
6768
public function get_bigcommerce_product_thumbnail_value( $column_name, $post_ID ) {
68-
if ( $column_name == self::COLUMN_PRODUCT_THUMB ) {
69-
$product_thumbnail = get_the_post_thumbnail( $post_ID, Image_Sizes::BC_THUMB);
70-
echo $product_thumbnail ;
69+
if ( $column_name !== self::COLUMN_PRODUCT_THUMB ) {
70+
return;
7171
}
72+
if ( Image_Importer::should_load_from_cdn() ) {
73+
$thumb_cdn = Product::get_thumb_from_cdn( $post_ID );
74+
}
75+
76+
if ( ! empty( $thumb_cdn ) ) {
77+
echo $thumb_cdn;
78+
79+
return;
80+
}
81+
82+
$product_thumbnail = get_the_post_thumbnail( $post_ID, Image_Sizes::BC_THUMB);
83+
echo $product_thumbnail ;
7284
}
7385
}

0 commit comments

Comments
 (0)