-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-attribute-descriptions.php
150 lines (95 loc) · 4.52 KB
/
woocommerce-attribute-descriptions.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Plugin Name: WooCommerce Attribute Descriptions
* Description: Extends WooCommerce and adds customisable descriptions to Product Attributes on Variable Products
* Version: 1.0.0
* Author: Creative Little Dots
* Author URI: http://creativelittledots.co.uk
*
* Text Domain: woocommerce-attribute-descriptions
* Domain Path: /languages/
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
*/
class WC_Attribute_Descriptions {
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
add_action('woocommerce_after_edit_attribute_fields', array($this, 'add_attributes_description_field_to_screen') );
add_action('woocommerce_attribute_updated', array($this, 'save_attribute_description'), 10 );
add_filter('woocommerce_attribute_label', array($this, 'display_attribute_description'), 10, 3);
add_action('woocommerce_after_main_content', array($this, 'display_attribute_modal') );
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts') );
}
public function plugin_url() {
return plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) );
}
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
public function add_attributes_description_field_to_screen() {
$attribute_id = absint( $_GET['edit'] );
$attribute_descriptions = get_option('attribute_descriptions') ? get_option('attribute_descriptions') : array();
$attribute_description = isset($_POST['attribute_description']) ? $_POST['attribute_description'] : (isset($attribute_descriptions[$attribute_id]) ? $attribute_descriptions[$attribute_id] : '');
include('admin/views/attribute-description.php');
}
public function sanitize_output($buffer) {
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\\1'
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
public function save_attribute_description($attribute_id) {
$attribute_descriptions = get_option('attribute_descriptions') ? get_option('attribute_descriptions') : array();
$attribute_descriptions[$attribute_id] = stripslashes( $_POST['attribute_description'] );
update_option('attribute_descriptions', $attribute_descriptions);
}
public function display_attribute_description($label, $name, $product) {
if( ! is_product() || is_admin() )
return $label;
global $wpdb;
$attribute_id = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_id FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
$attribute_descriptions = get_option('attribute_descriptions') ? get_option('attribute_descriptions') : array();
if(isset($attribute_descriptions[$attribute_id]) && $attribute_descriptions[$attribute_id]) {
$attribute_description = $attribute_descriptions[$attribute_id];
$label = wc_get_template_html( 'single-product/attribute-description.php', array(
'label' => $label,
'attribute_description' => $attribute_description,
'name' => $name,
'product' => $product,
), '', $this->plugin_path() . '/templates/' );
}
return $label;
}
public function display_attribute_modal() {
global $product;
wc_get_template( 'single-product/attribute-description-modal.php', array(
'message' => '',
), '', $this->plugin_path() . '/templates/' );
}
public function enqueue_scripts() {
if( is_product() ) {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script('woocommerce_attribute_descriptions', $this->plugin_url() . '/assets/js/woocommerce-attribute-descriptions' . $suffix . '.js', array('jquery'), '1.0.0', true);
wp_enqueue_style('woocommerce_attribute_descriptions', $this->plugin_url() . '/assets/css/woocommerce-attribute-descriptions.css', array(), '1.0.0');
}
}
}
function WC_Attribute_Descriptions() {
return WC_Attribute_Descriptions::instance();
}
$GLOBALS['WC_Attribute_Descriptions'] = WC_Attribute_Descriptions();