| 1 |
<?php |
| 2 |
/** |
| 3 |
* Necessary functions of the plugin. |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Get global options value. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
* |
| 15 |
* @param string $option_name Option name. |
| 16 |
* @param null $default Default value. |
| 17 |
* @param null $override_for Override global options for product list/product details page. Accepted values are: pl, sp |
| 18 |
* |
| 19 |
* @return string|null |
| 20 |
*/ |
| 21 |
function swatchly_get_option( $option_name = '', $default = null, $override_for = null ) { |
| 22 |
$options = get_option( 'swatchly_options' ); |
| 23 |
|
| 24 |
$global_general_setting_fields = array( |
| 25 |
'enable_swatches', |
| 26 |
'auto_convert_dropdowns_to_label', |
| 27 |
'swatch_width', |
| 28 |
'swatch_height', |
| 29 |
'tooltip', |
| 30 |
'show_swatch_image_in_tooltip', |
| 31 |
'tooltip_image_size', |
| 32 |
'shape_style', |
| 33 |
'enable_shape_inset', |
| 34 |
'shape_inset_size', |
| 35 |
'deselect_on_click', |
| 36 |
'show_selected_attribute_name', |
| 37 |
'disabled_attribute_type', |
| 38 |
'disable_out_of_stock', |
| 39 |
'enable_out_of_stock_swatch_selection', |
| 40 |
); |
| 41 |
|
| 42 |
if($override_for == 'sp' && isset($options['sp_override_global']) && $options['sp_override_global']){ |
| 43 |
$opt_name = 'sp_'. $option_name; |
| 44 |
|
| 45 |
if(in_array($option_name, $global_general_setting_fields)){ |
| 46 |
$option_name = $opt_name; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if($override_for == 'pl' && isset($options['pl_override_global']) && $options['pl_override_global']){ |
| 51 |
$opt_name = 'pl_'. $option_name; |
| 52 |
|
| 53 |
if(in_array($option_name, $global_general_setting_fields)){ |
| 54 |
$option_name = $opt_name; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return isset( $options[$option_name] ) ? $options[$option_name] : $default; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get product meta options value. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
* @param int $product_id Product ID. |
| 67 |
* @param string $option_name Option name. |
| 68 |
* @param null $default Default value. |
| 69 |
* |
| 70 |
* @return string|null |
| 71 |
*/ |
| 72 |
function swatchly_get_post_meta( $product_id, $taxonomy, $option_name = '', $default = '') { |
| 73 |
// product override |
| 74 |
$product_meta = get_post_meta( $product_id, '_swatchly_product_meta', true ); |
| 75 |
|
| 76 |
$meta_value = isset( $product_meta[$taxonomy][$option_name] ) && $product_meta[$taxonomy][$option_name] ? $product_meta[$taxonomy][$option_name] : $default; |
| 77 |
|
| 78 |
return $meta_value; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get image sizes. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
function swatchly_get_image_sizes() { |
| 89 |
global $_wp_additional_image_sizes; |
| 90 |
|
| 91 |
$image_sizes = array(); |
| 92 |
$default_image_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); |
| 93 |
foreach ( $default_image_sizes as $size ) { |
| 94 |
$image_sizes[$size]['width'] = intval( get_option( "{$size}_size_w") ); |
| 95 |
$image_sizes[$size]['height'] = intval( get_option( "{$size}_size_h") ); |
| 96 |
$image_sizes[$size]['crop'] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false; |
| 97 |
} |
| 98 |
|
| 99 |
if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ){ |
| 100 |
$image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes ); |
| 101 |
} |
| 102 |
|
| 103 |
return array_keys($image_sizes); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get swatch type by taxonomy name |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* |
| 111 |
* @param string $taxonomy Taxonomy name. |
| 112 |
* @param null $product_id Product id. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
function swatchly_get_swatch_type( $taxonomy, $product_id = null ) { |
| 117 |
$swatch_type = 'select'; |
| 118 |
|
| 119 |
// txonomy override |
| 120 |
if( taxonomy_exists( $taxonomy ) ){ |
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
$attr = substr( $taxonomy, 3 ); |
| 124 |
$attr = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s", $attr ) ); //phpcs:ignore |
| 125 |
$swatch_type = isset($attr->attribute_type) ? $attr->attribute_type : ''; |
| 126 |
|
| 127 |
if(is_admin()){ |
| 128 |
return $swatch_type; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// product override |
| 133 |
if( $product_id ){ |
| 134 |
$product_meta = get_post_meta( $product_id, '_swatchly_product_meta', true ); |
| 135 |
$auto_convert_dropdowns_to_label = isset($product_meta['auto_convert_dropdowns_to_label']) ? $product_meta['auto_convert_dropdowns_to_label'] : ''; |
| 136 |
$swatch_type = isset( $product_meta[$taxonomy]['swatch_type'] ) && $product_meta[$taxonomy]['swatch_type'] ? $product_meta[$taxonomy]['swatch_type'] : $swatch_type; |
| 137 |
} |
| 138 |
|
| 139 |
return $swatch_type; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the directory name of the current theme regardless of the child theme. |
| 144 |
* |
| 145 |
* @return The directory name of the theme's "stylesheet" files, inside the theme root. |
| 146 |
*/ |
| 147 |
function swatchly_get_current_theme_directory(){ |
| 148 |
$current_theme_dir = ''; |
| 149 |
$current_theme = wp_get_theme(); |
| 150 |
if( $current_theme->exists() && $current_theme->parent() ){ |
| 151 |
$parent_theme = $current_theme->parent(); |
| 152 |
|
| 153 |
if( $parent_theme->exists() ){ |
| 154 |
$current_theme_dir = $parent_theme->get_stylesheet(); |
| 155 |
} |
| 156 |
} elseif( $current_theme->exists() ) { |
| 157 |
$current_theme_dir = $current_theme->get_stylesheet(); |
| 158 |
} |
| 159 |
|
| 160 |
return $current_theme_dir; |
| 161 |
} |