PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / trunk
Ever Compare – Products Compare Plugin for WooCommerce vtrunk
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / includes / helper-functions.php

helper-functions.php in Ever Compare – Products Compare Plugin for WooCommerce trunk, at includes/helper-functions.php

245 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3 /*
4 * Plugisn Options value
5 * return on/off
6 */
7 function ever_compare_get_option( $option, $section, $default = '' ){
8 $options = get_option( $section );
9 if ( isset( $options[$option] ) ) {
10 return $options[$option];
11 }
12 return $default;
13 }
14
15 /**
16 * [ever_compare_update_option]
17 * @param [string] $option
18 * @param [string] $section
19 * @param string $new_value
20 * @return [string]
21 */
22 function ever_compare_update_option( $section, $option_key, $new_value ){
23 $options_data = get_option( $section );
24 if ( ! is_array( $options_data ) ) {
25 $options_data = array();
26 }
27 $options_data[ $option_key ] = $new_value;
28 update_option( $section, $options_data );
29 }
30
31 /**
32 * [ever_compare_woocommerce_installed]
33 * @return [boolean]
34 */
35 function ever_compare_woocommerce_installed() {
36 return class_exists( 'WooCommerce' );
37 }
38
39 /**
40 * Get Post List
41 * return array
42 */
43 function ever_compare_get_post_list( $post_type = 'page' ){
44 $options = array();
45 $options['0'] = __('Select','ever-compare');
46 $perpage = 200;
47 $all_post = array( 'posts_per_page' => $perpage, 'post_type'=> $post_type, 'no_found_rows' => true );
48 $post_terms = get_posts( $all_post );
49 if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ){
50 foreach ( $post_terms as $term ) {
51 $options[ $term->ID ] = $term->post_title;
52 }
53 return $options;
54 }
55 }
56
57 /**
58 * [ever_compare_locate_template]
59 * @param [string] $tmp_name Template name
60 * @return [Template path]
61 */
62 function ever_compare_locate_template( $tmp_name ) {
63
64 $woo_tmp_base = function_exists('WC') ? WC()->template_path() : '';
65
66 $woo_tmp_path = $woo_tmp_base . $tmp_name; //active theme directory/woocommerce/
67 $theme_tmp_path = '/' . $tmp_name; //active theme root directory
68 $plugin_tmp_path = EVERCOMPARE_DIR . 'includes/templates/' . $tmp_name;
69
70 $located = locate_template( [ $woo_tmp_path, $theme_tmp_path ] );
71
72 if ( ! $located && file_exists( $plugin_tmp_path ) ) {
73 return apply_filters( 'evercompare_locate_template', $plugin_tmp_path, $tmp_name );
74 }
75
76 return apply_filters( 'evercompare_locate_template', $located, $tmp_name );
77 }
78
79 /**
80 * [ever_compare_get_template]
81 * @param [string] $tmp_name Template name
82 * @param [array] $args template argument array
83 * @param boolean $echo
84 * @return [void]
85 */
86 function ever_compare_get_template( $tmp_name = '', $args = null, $echo = true ) {
87 $located = ever_compare_locate_template( $tmp_name );
88
89 if ( $args && is_array( $args ) ) {
90 $evercompare_args = $args;
91 // Backward compat: if template is overridden in theme, extract for legacy support
92 $default_path = EVERCOMPARE_PATH . '/includes/templates/' . $tmp_name;
93 if ( $located !== $default_path ) {
94 extract( $args, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
95 }
96 }
97
98 if ( $echo !== true ) { ob_start(); }
99
100 // include file located.
101 include( $located );
102
103 if ( $echo !== true ) { return ob_get_clean(); }
104
105 }
106
107
108 /**
109 * Get default fields List
110 * return array
111 */
112 function ever_compare_get_default_fields(){
113 $fields = array(
114 'title' => esc_html__( 'Title', 'ever-compare' ),
115 'ratting' => esc_html__( 'Rating', 'ever-compare' ),
116 'price' => esc_html__( 'Price', 'ever-compare' ),
117 'add_to_cart' => esc_html__( 'Add To Cart', 'ever-compare' ),
118 'description' => esc_html__( 'Description', 'ever-compare' ),
119 'availability' => esc_html__( 'Availability', 'ever-compare' ),
120 'sku' => esc_html__( 'Sku', 'ever-compare' ),
121 'weight' => esc_html__( 'Weight', 'ever-compare' ),
122 'dimensions' => esc_html__( 'Dimensions', 'ever-compare' ),
123 );
124 return apply_filters( 'ever_compare_default_fields', $fields );
125 }
126
127 /**
128 * Get Fields List
129 * return array
130 */
131 function ever_compare_get_available_attributes() {
132 $attribute_list = array();
133
134 if( function_exists( 'wc_get_attribute_taxonomies' ) ) {
135 $attribute_list = wc_get_attribute_taxonomies();
136 }
137
138 $fields = ever_compare_get_default_fields();
139
140 if ( count( $attribute_list ) > 0 ) {
141 foreach ( $attribute_list as $attribute ) {
142 $fields[ 'pa_' . $attribute->attribute_name ] = $attribute->attribute_label;
143 }
144 }
145
146 return $fields;
147 }
148
149 /**
150 * [ever_compare_table_active_heading]
151 * @return [array]
152 */
153 function ever_compare_table_active_heading(){
154 $active_heading = !empty( ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) : array();
155 return $active_heading;
156 }
157
158 /**
159 * [ever_compare_table_heading]
160 * @return [array]
161 */
162 function ever_compare_table_heading(){
163 $new_list = array();
164 $field_list = count( ever_compare_table_active_heading() ) > 0 ? ever_compare_table_active_heading() : ever_compare_get_default_fields();
165 foreach ( $field_list as $key => $value ) {
166 $new_list[$key] = \EverCompare\Frontend\Manage_Compare::instance()->field_name( $key );
167 }
168 return $new_list;
169 }
170
171 /**
172 * [ever_compare_dimensions]
173 * @param [string] $key
174 * @param [string] $tab
175 * @return [String | Bool]
176 */
177 function ever_compare_dimensions( $key, $tab, $css_attr ){
178 $dimensions = !empty( ever_compare_get_option( $key, $tab ) ) ? ever_compare_get_option( $key, $tab ) : array();
179 if( !empty( $dimensions['top'] ) || !empty( $dimensions['right'] ) || !empty( $dimensions['bottom'] ) || !empty( $dimensions['left'] ) ){
180
181 $unit = ( empty( $dimensions['unit'] ) ? 'px' : $dimensions['unit'] );
182 $top = ( !empty( $dimensions['top'] ) ? $dimensions['top'] : 0 );
183 $right = ( !empty( $dimensions['right'] ) ? $dimensions['right'] : 0 );
184 $bottom = ( !empty( $dimensions['bottom'] ) ? $dimensions['bottom'] : 0 );
185 $left = ( !empty( $dimensions['left'] ) ? $dimensions['left'] : 0 );
186
187 $css_attr .= ":{$top}{$unit} {$right}{$unit} {$bottom}{$unit} {$left}{$unit}";
188 return $css_attr.';';
189
190 }else{
191 return false;
192 }
193 }
194
195 /**
196 * [ever_compare_generate_css]
197 * @return [String | Bool]
198 */
199 function ever_compare_generate_css( $key, $tab, $css_attr, $unit = '' ){
200 $field_value = !empty( ever_compare_get_option( $key, $tab ) ) ? ever_compare_get_option( $key, $tab ) : '';
201
202 if( !empty( $field_value ) ){
203 $css_attr .= ":{$field_value}{$unit}";
204 return $css_attr.';';
205 }else{
206 return false;
207 }
208
209 }
210
211 /**
212 * [ever_compare_icon_list]
213 * @return [svg]
214 */
215 function ever_compare_icon_list( $key = '' ){
216 $icon_list = [
217 'default' => '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 471.701 471.701">
218 <g class="ever-compare-refresh"><path d="M409.6,0c-9.426,0-17.067,7.641-17.067,17.067v62.344C304.667-5.656,164.478-3.386,79.411,84.479 c-40.09,41.409-62.455,96.818-62.344,154.454c0,9.426,7.641,17.067,17.067,17.067S51.2,248.359,51.2,238.933 c0.021-103.682,84.088-187.717,187.771-187.696c52.657,0.01,102.888,22.135,138.442,60.976l-75.605,25.207 c-8.954,2.979-13.799,12.652-10.82,21.606s12.652,13.799,21.606,10.82l102.4-34.133c6.99-2.328,11.697-8.88,11.674-16.247v-102.4 C426.667,7.641,419.026,0,409.6,0z"/><path d="M443.733,221.867c-9.426,0-17.067,7.641-17.067,17.067c-0.021,103.682-84.088,187.717-187.771,187.696 c-52.657-0.01-102.888-22.135-138.442-60.976l75.605-25.207c8.954-2.979,13.799-12.652,10.82-21.606 c-2.979-8.954-12.652-13.799-21.606-10.82l-102.4,34.133c-6.99,2.328-11.697,8.88-11.674,16.247v102.4 c0,9.426,7.641,17.067,17.067,17.067s17.067-7.641,17.067-17.067v-62.345c87.866,85.067,228.056,82.798,313.122-5.068 c40.09-41.409,62.455-96.818,62.344-154.454C460.8,229.508,453.159,221.867,443.733,221.867z"/></g>
219 <g class="ever-compare-check"><path d="M238.933,0C106.974,0,0,106.974,0,238.933s106.974,238.933,238.933,238.933s238.933-106.974,238.933-238.933 C477.726,107.033,370.834,0.141,238.933,0z M238.933,443.733c-113.108,0-204.8-91.692-204.8-204.8s91.692-204.8,204.8-204.8 s204.8,91.692,204.8,204.8C443.611,351.991,351.991,443.611,238.933,443.733z"/><path d="M370.046,141.534c-6.614-6.388-17.099-6.388-23.712,0v0L187.733,300.134l-56.201-56.201 c-6.548-6.78-17.353-6.967-24.132-0.419c-6.78,6.548-6.967,17.353-0.419,24.132c0.137,0.142,0.277,0.282,0.419,0.419 l68.267,68.267c6.664,6.663,17.468,6.663,24.132,0l170.667-170.667C377.014,158.886,376.826,148.082,370.046,141.534z"/></g></svg>',
220 'loading' => '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 471.701 471.701">
221 <g class="ever-compare-refresh"><path d="M409.6,0c-9.426,0-17.067,7.641-17.067,17.067v62.344C304.667-5.656,164.478-3.386,79.411,84.479 c-40.09,41.409-62.455,96.818-62.344,154.454c0,9.426,7.641,17.067,17.067,17.067S51.2,248.359,51.2,238.933 c0.021-103.682,84.088-187.717,187.771-187.696c52.657,0.01,102.888,22.135,138.442,60.976l-75.605,25.207 c-8.954,2.979-13.799,12.652-10.82,21.606s12.652,13.799,21.606,10.82l102.4-34.133c6.99-2.328,11.697-8.88,11.674-16.247v-102.4 C426.667,7.641,419.026,0,409.6,0z"/><path d="M443.733,221.867c-9.426,0-17.067,7.641-17.067,17.067c-0.021,103.682-84.088,187.717-187.771,187.696 c-52.657-0.01-102.888-22.135-138.442-60.976l75.605-25.207c8.954-2.979,13.799-12.652,10.82-21.606 c-2.979-8.954-12.652-13.799-21.606-10.82l-102.4,34.133c-6.99,2.328-11.697,8.88-11.674,16.247v102.4 c0,9.426,7.641,17.067,17.067,17.067s17.067-7.641,17.067-17.067v-62.345c87.866,85.067,228.056,82.798,313.122-5.068 c40.09-41.409,62.455-96.818,62.344-154.454C460.8,229.508,453.159,221.867,443.733,221.867z"/></g></svg>'
222 ];
223 return ( $key == '' ) ? $icon_list : $icon_list[$key];
224 }
225
226 function get_allowed_tags_for_svg () {
227 return [
228 'svg' => [
229 'xmlns' => true,
230 'width' => true,
231 'height' => true,
232 'viewbox' => true,
233 'xmlns:xlink' => true,
234 ],
235 'g' => [
236 'class' => true,
237 ],
238 'path' => [
239 'd' => true,
240 ],
241 'span' => [
242 'class' => true,
243 ],
244 ];
245 }