-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-woo-extra-product-options.php
77 lines (66 loc) · 2.47 KB
/
class-woo-extra-product-options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Compatibility Plugin Name: WooCommerce Extra Product Options
* Compatibility Plugin URI: https://codecanyon.net/item/woocommerce-extra-product-options/7908619
* Related issue: https://github.com/wpCloud/wp-stateless/issues/266
*
* Compatibility Description: Ensures compatibility with WooCommerce Extra Product Options.
*
*/
namespace WPSL\WooExtraProductOptions;
use wpCloud\StatelessMedia\Compatibility;
class WooExtraProductOptions extends Compatibility {
protected $id = 'woo-extra-product-options';
protected $title = 'WooCommerce Extra Product Options';
protected $constant = 'WP_STATELESS_COMPATIBILITY_WOO_EXTRA_PRODUCT_OPTION';
protected $description = 'Ensures compatibility with WooCommerce Extra Product Options.';
protected $plugin_file = 'woocommerce-tm-extra-product-options/tm-woo-extra-product-options.php';
/**
* @param $sm
*/
public function module_init($sm) {
add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 1);
}
/**
* Adding filter late to insure that it only runs when a product is added.
* @param $cart_item_meta
* @return mixed
*/
public function add_cart_item_data($cart_item_meta) {
add_filter('wp_handle_upload', array($this, 'wp_handle_upload'));
return $cart_item_meta;
}
/**
* upload image to GCS and return GCS link.
* @param $upload
* @return mixed
*/
public function wp_handle_upload($upload) {
$file = $upload['file'];
$url = $upload['url'];
$type = $upload['type'];
$client = ud_get_stateless_media()->get_client();
$file_path = apply_filters('wp_stateless_file_name', $file, 0);
$file_info = @getimagesize($file);
if ($file_info) {
$_metadata = array(
'width' => $file_info[0],
'height' => $file_info[1],
'object-id' => 'unknown', // we really don't know it
'source-id' => md5($file . ud_get_stateless_media()->get('sm.bucket')),
'file-hash' => md5($file)
);
}
$media = $client->add_media(apply_filters('sm:item:on_fly:before_add', array(
'use_root' => false,
'name' => $file_path,
'absolutePath' => wp_normalize_path($file),
'cacheControl' => apply_filters('sm:item:cacheControl', 'public, max-age=36000, must-revalidate', $_metadata),
'contentDisposition' => null,
'mimeType' => $type,
'metadata' => $_metadata
)));
$upload['url'] = ud_get_stateless_media()->get_gs_host() . '/' . $file_path;
return $upload;
}
}