-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwoocommerce-swatches-main.php
135 lines (99 loc) · 5.04 KB
/
woocommerce-swatches-main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class WC_SwatchesPlugin {
private $product_attribute_images;
public function __construct() {
define( 'WC_SWATCHES_VERSION', '3.0.6' );
require 'woocommerce-swatches-template-functions.php';
require 'classes/class-wc-swatch-term.php';
require 'classes/class-wc-swatch-product-term.php';
require 'classes/class-wc-swatches-product-attribute-images.php';
require 'classes/class-wc-ex-product-data-tab.php';
require 'classes/class-wc-swatches-product-data-tab.php';
require 'classes/class-wc-swatch-attribute-configuration.php';
require 'classes/class-wc-swatches-ajax-handler.php';
add_action( 'init', array( &$this, 'on_init' ) );
add_action( 'wc_quick_view_enqueue_scripts', array( $this, 'on_enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( &$this, 'on_enqueue_scripts' ) );
add_action( 'admin_head', array( &$this, 'on_enqueue_scripts' ) );
$this->product_attribute_images = new WC_Swatches_Product_Attribute_Images( 'swatches_id', 'swatches_image_size' );
$this->product_data_tab = new WC_Swatches_Product_Data_Tab();
//Swatch Image Size Settings
add_filter( 'woocommerce_catalog_settings', array(
&$this,
'swatches_image_size_setting'
) ); // pre WC 2.1
add_filter( 'woocommerce_product_settings', array(
&$this,
'swatches_image_size_setting'
) ); // WC 2.1+
add_filter( 'woocommerce_get_image_size_swatches', array( $this, 'get_image_size_swatches' ) );
}
public function on_init() {
$image_size = get_option( 'swatches_image_size', array() );
$size = array();
$size['width'] = isset( $image_size['width'] ) && ! empty( $image_size['width'] ) ? $image_size['width'] : '32';
$size['height'] = isset( $image_size['height'] ) && ! empty( $image_size['height'] ) ? $image_size['height'] : '32';
$size['crop'] = isset( $image_size['crop'] ) ? $image_size['crop'] : 1;
$image_size = apply_filters( 'woocommerce_get_image_size_swatches_image_size', $size );
add_image_size( 'swatches_image_size', apply_filters( 'woocommerce_swatches_size_width_default', $image_size['width'] ), apply_filters( 'woocommerce_swatches_size_height_default', $image_size['height'] ), $image_size['crop'] );
}
public function on_enqueue_scripts() {
global $pagenow;
if ( ! is_admin() ) {
wp_enqueue_style( 'swatches-and-photos', $this->plugin_url() . '/assets/css/swatches-and-photos.css', array(), WC_SWATCHES_VERSION );
wp_enqueue_script( 'swatches-and-photos', $this->plugin_url() . '/assets/js/swatches-and-photos.js', array( 'jquery' ), WC_SWATCHES_VERSION, true );
$data = array(
'ajax_url' => admin_url( 'admin-ajax.php' )
);
wp_localize_script( 'swatches-and-photos', 'wc_swatches_params', $data );
}
if ( is_admin() && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' || 'edit-tags.php' ) ) {
wp_enqueue_media();
wp_enqueue_style( 'swatches-and-photos', $this->plugin_url() . '/assets/css/swatches-and-photos.css' );
wp_enqueue_script( 'swatches-and-photos-admin', $this->plugin_url() . '/assets/js/swatches-and-photos-admin.js', array( 'jquery' ), '1.0', true );
wp_enqueue_style( 'colourpicker', $this->plugin_url() . '/assets/css/colorpicker.css' );
wp_enqueue_script( 'colourpicker', $this->plugin_url() . '/assets/js/colorpicker.js', array( 'jquery' ) );
$data = array(
'placeholder_img_src' => apply_filters( 'woocommerce_placeholder_img_src', WC()->plugin_url() . '/assets/images/placeholder.png' )
);
wp_localize_script( 'swatches-and-photos-admin', 'wc_swatches_params', $data );
}
}
public function plugin_url() {
return untrailingslashit( plugin_dir_url( __FILE__ ) );
}
public function plugin_dir() {
return plugin_dir_path( __FILE__ );
}
public function swatches_image_size_setting( $settings ) {
$setting = array(
'name' => __( 'Swatches and Photos', 'wc_swatches_and_photos' ),
'desc' => __( 'The default size for color swatches and photos.', 'wc_swatches_and_photos' ),
'id' => 'swatches_image_size',
'css' => '',
'type' => 'image_width',
'std' => '32',
'desc_tip' => true,
'default' => array(
'crop' => 1,
'width' => 32,
'height' => 32
)
);
$index = count( $settings ) - 1;
$settings[ $index + 1 ] = $settings[ $index ];
$settings[ $index ] = $setting;
return $settings;
}
public function get_image_size_swatches( $size ) {
$image_size = get_option( 'swatches_image_size', array() );
$size = array();
$size['width'] = isset( $image_size['width'] ) && ! empty( $image_size['width'] ) ? $image_size['width'] : '32';
$size['height'] = isset( $image_size['height'] ) && ! empty( $image_size['height'] ) ? $image_size['height'] : '32';
$size['crop'] = isset( $image_size['crop'] ) && $image_size['crop'] ? 1 : 0;
$image_size = apply_filters( 'woocommerce_get_image_size_swatches_image_size', $size );
//Need to remove the filter because woocommerce will disable the input field.
remove_filter( 'woocommerce_get_image_size_swatches', array( $this, 'get_image_size_swatches' ) );
return $image_size;
}
}