| @@ -1,0 +1,401 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { | |
| 14 | + | |
| 15 | + die( 'Do. Or do not. There is no try.' ); | |
| 16 | +} | |
| 17 | + | |
| 18 | +if ( ! class_exists( 'WpssoUtilUnits' ) ) { // Just in case. | |
| 19 | + | |
| 20 | + require_once WPSSO_PLUGINDIR . 'lib/util-units.php'; | |
| 21 | +} | |
| 22 | + | |
| 23 | +if ( ! class_exists( 'WpssoUtilWooCommerce' ) ) { | |
| 24 | + | |
| 25 | + class WpssoUtilWooCommerce { | |
| 26 | + | |
| 27 | + private $p; // Wpsso class object. | |
| 28 | + | |
| 29 | + private $fee_cost = ''; // Package cost for the [fee] shortcode. | |
| 30 | + | |
| 31 | + /* | |
| 32 | + * Instantiated by WpssoUtil->__construct(). | |
| 33 | + */ | |
| 34 | + public function __construct( &$plugin ) { | |
| 35 | + | |
| 36 | + $this->p =& $plugin; | |
| 37 | + | |
| 38 | + if ( $this->p->debug->enabled ) { | |
| 39 | + | |
| 40 | + $this->p->debug->mark(); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + /* | |
| 45 | + * Returns true for a variable product, false for a simple product. | |
| 46 | + */ | |
| 47 | + public function is_mod_variable( $mod ) { | |
| 48 | + | |
| 49 | + static $local_cache = array(); | |
| 50 | + | |
| 51 | + if ( $mod[ 'is_post' ] ) { // Just in case. | |
| 52 | + | |
| 53 | + if ( isset( $local_cache[ $mod[ 'id' ] ] ) ) { | |
| 54 | + | |
| 55 | + return $local_cache[ $mod[ 'id' ] ]; | |
| 56 | + | |
| 57 | + } elseif ( $product = $this->get_product( $mod[ 'id' ] ) ) { | |
| 58 | + | |
| 59 | + return $local_cache[ $mod[ 'id' ] ] = $this->is_product_variable( $product ); | |
| 60 | + | |
| 61 | + } else return $local_cache[ $mod[ 'id' ] ] = false; | |
| 62 | + } | |
| 63 | + | |
| 64 | + return false; | |
| 65 | + } | |
| 66 | + | |
| 67 | + public function is_product_variable( $product ) { | |
| 68 | + | |
| 69 | + if ( $product instanceof WC_Product ) { // Just in case. | |
| 70 | + | |
| 71 | + if ( 'variable' === $this->get_product_type( $product ) ) { | |
| 72 | + | |
| 73 | + return true; | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + return false; | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function is_product( $product ) { | |
| 81 | + | |
| 82 | + if ( $product instanceof WC_Product ) { | |
| 83 | + | |
| 84 | + return true; | |
| 85 | + } | |
| 86 | + | |
| 87 | + return false; | |
| 88 | + } | |
| 89 | + | |
| 90 | + public function get_product( $product_id ) { | |
| 91 | + | |
| 92 | + global $woocommerce; | |
| 93 | + | |
| 94 | + $product = false; | |
| 95 | + | |
| 96 | + if ( isset( $woocommerce->product_factory ) && is_callable( array( $woocommerce->product_factory, 'get_product' ) ) ) { | |
| 97 | + | |
| 98 | + $product = $woocommerce->product_factory->get_product( $product_id ); | |
| 99 | + | |
| 100 | + } elseif ( class_exists( 'WC_Product' ) ) { | |
| 101 | + | |
| 102 | + $product = new WC_Product( $product_id ); | |
| 103 | + | |
| 104 | + } elseif ( $this->p->debug->enabled ) { | |
| 105 | + | |
| 106 | + $this->p->debug->log( 'no method or class to get product' ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + return apply_filters( 'wpsso_wc_get_product_object', $product, $product_id ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + /* | |
| 113 | + * Returns product id from product object. | |
| 114 | + */ | |
| 115 | + public function get_product_id( $product ) { | |
| 116 | + | |
| 117 | + $product_id = 0; | |
| 118 | + | |
| 119 | + if ( is_callable( array( $product, 'get_id' ) ) ) { | |
| 120 | + | |
| 121 | + $product_id = $product->get_id(); | |
| 122 | + | |
| 123 | + } elseif ( isset( $product->id ) ) { | |
| 124 | + | |
| 125 | + $product_id = $product->id; | |
| 126 | + | |
| 127 | + } elseif ( $this->p->debug->enabled ) { | |
| 128 | + | |
| 129 | + $this->p->debug->log( 'no method or property to get product id' ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $product_id; | |
| 133 | + } | |
| 134 | + | |
| 135 | + public function get_product_type( $product ) { | |
| 136 | + | |
| 137 | + $product_type = ''; | |
| 138 | + | |
| 139 | + if ( is_callable( array( $product, 'get_type' ) ) ) { | |
| 140 | + | |
| 141 | + $product_type = $product->get_type(); // Returns 'simple', 'variable', etc. | |
| 142 | + | |
| 143 | + } elseif ( isset( $product->product_type ) ) { | |
| 144 | + | |
| 145 | + $product_type = $product->product_type; | |
| 146 | + | |
| 147 | + } elseif ( $this->p->debug->enabled ) { | |
| 148 | + | |
| 149 | + $this->p->debug->log( 'no method or property to get product type' ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + return $product_type; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /* | |
| 156 | + * Format the WooCommerce meta data as WordPress meta data. | |
| 157 | + */ | |
| 158 | + public function get_product_wp_meta( $product ) { | |
| 159 | + | |
| 160 | + $var_wp_meta = array(); | |
| 161 | + | |
| 162 | + if ( is_callable( array( $product, 'get_meta_data' ) ) ) { // Just in case. | |
| 163 | + | |
| 164 | + $wc_var_meta = $product->get_meta_data(); | |
| 165 | + | |
| 166 | + if ( is_array( $wc_var_meta ) ) { // Just in case. | |
| 167 | + | |
| 168 | + foreach ( $wc_var_meta as $md_obj ) { // WC_Meta_Data object. | |
| 169 | + | |
| 170 | + if ( ! empty( $md_obj->key ) ) { | |
| 171 | + | |
| 172 | + $var_wp_meta[ $md_obj->key ] = array( $md_obj->value ); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + } | |
| 176 | + } | |
| 177 | + | |
| 178 | + return $var_wp_meta; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /* | |
| 182 | + * Similar to the WooCommerce method, except it does not exclude out of stock variations. | |
| 183 | + * | |
| 184 | + * See WpssoIntegEcomWooCommerce->filter_og_seed(). | |
| 185 | + */ | |
| 186 | + public function get_available_variations( $product ) { | |
| 187 | + | |
| 188 | + $available_variations = array(); | |
| 189 | + | |
| 190 | + if ( 'variable' !== $this->get_product_type( $product ) ) { // Nothing to do. | |
| 191 | + | |
| 192 | + return $available_variations; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $product_children = $product->get_children(); | |
| 196 | + | |
| 197 | + foreach ( $product_children as $child_id ) { | |
| 198 | + | |
| 199 | + $variation = wc_get_product( $child_id ); | |
| 200 | + | |
| 201 | + if ( ! $variation ) { | |
| 202 | + | |
| 203 | + if ( $this->p->debug->enabled ) { | |
| 204 | + | |
| 205 | + $this->p->debug->log( 'product child id ' . $child_id . ' is empty' ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + continue; | |
| 209 | + | |
| 210 | + } elseif ( ! $variation->exists() ) { | |
| 211 | + | |
| 212 | + if ( $this->p->debug->enabled ) { | |
| 213 | + | |
| 214 | + $this->p->debug->log( 'product child id ' . $child_id . ' does not exist' ); | |
| 215 | + } | |
| 216 | + | |
| 217 | + continue; | |
| 218 | + | |
| 219 | + } elseif ( 'variation' !== $this->get_product_type( $variation ) ) { // Just in case. | |
| 220 | + | |
| 221 | + if ( $this->p->debug->enabled ) { | |
| 222 | + | |
| 223 | + $this->p->debug->log( 'product child id ' . $child_id . ' type is not a variation' ); | |
| 224 | + } | |
| 225 | + | |
| 226 | + continue; | |
| 227 | + | |
| 228 | + } elseif ( ! $variation->variation_is_visible() ) { | |
| 229 | + | |
| 230 | + if ( $this->p->debug->enabled ) { | |
| 231 | + | |
| 232 | + $this->p->debug->log( 'product child id ' . $child_id . ' variation is not visible' ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + continue; | |
| 236 | + } | |
| 237 | + | |
| 238 | + /* | |
| 239 | + * Returns an array of data for a variation. | |
| 240 | + * | |
| 241 | + * Applies the 'woocommerce_available_variation' filter. | |
| 242 | + */ | |
| 243 | + $available_variations[] = $product->get_available_variation( $variation ); | |
| 244 | + } | |
| 245 | + | |
| 246 | + return $available_variations; | |
| 247 | + } | |
| 248 | + | |
| 249 | + /* | |
| 250 | + * Returns the variation product object or false if not a visible, active, and purchasable variation. | |
| 251 | + */ | |
| 252 | + public function get_variation_product( $variation ) { | |
| 253 | + | |
| 254 | + $product = false; | |
| 255 | + | |
| 256 | + if ( ! is_array( $variation ) ) { | |
| 257 | + | |
| 258 | + return false; // Stop here. | |
| 259 | + | |
| 260 | + } elseif ( empty( $variation[ 'variation_id' ] ) ) { | |
| 261 | + | |
| 262 | + if ( $this->p->debug->enabled ) { | |
| 263 | + | |
| 264 | + $this->p->debug->log( 'exiting early: variation id is empty' ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + return false; // Stop here. | |
| 268 | + | |
| 269 | + } elseif ( empty( $variation[ 'variation_is_visible' ] ) ) { | |
| 270 | + | |
| 271 | + if ( $this->p->debug->enabled ) { | |
| 272 | + | |
| 273 | + $this->p->debug->log( 'exiting early: variation is not visible' ); | |
| 274 | + } | |
| 275 | + | |
| 276 | + return false; // Stop here. | |
| 277 | + | |
| 278 | + } elseif ( empty( $variation[ 'variation_is_active' ] ) ) { | |
| 279 | + | |
| 280 | + if ( $this->p->debug->enabled ) { | |
| 281 | + | |
| 282 | + $this->p->debug->log( 'exiting early: variation is not active' ); | |
| 283 | + } | |
| 284 | + | |
| 285 | + return false; // Stop here. | |
| 286 | + | |
| 287 | + } elseif ( empty( $variation[ 'is_purchasable' ] ) ) { | |
| 288 | + | |
| 289 | + if ( $this->p->debug->enabled ) { | |
| 290 | + | |
| 291 | + $this->p->debug->log( 'exiting early: variation is not purchaseable' ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + return false; // Stop here. | |
| 295 | + } | |
| 296 | + | |
| 297 | + $product = $this->get_product( $variation[ 'variation_id' ] ); | |
| 298 | + | |
| 299 | + if ( false === $product ) { | |
| 300 | + | |
| 301 | + if ( $this->p->debug->enabled ) { | |
| 302 | + | |
| 303 | + $this->p->debug->log( 'exiting early: no product for variation id ' . $variation[ 'variation_id' ] ); | |
| 304 | + } | |
| 305 | + } | |
| 306 | + | |
| 307 | + return $product; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /* | |
| 311 | + * Check if a simple product, variable product or any of its variations, has a meta data value. | |
| 312 | + */ | |
| 313 | + public function has_meta( $product, $meta_key ) { | |
| 314 | + | |
| 315 | + static $local_cache = array(); | |
| 316 | + | |
| 317 | + $product_id = $this->get_product_id( $product ); | |
| 318 | + | |
| 319 | + if ( isset( $local_cache[ $product_id ][ $meta_key ] ) ) { // Already checked. | |
| 320 | + | |
| 321 | + return $local_cache[ $product_id ][ $meta_key ]; | |
| 322 | + } | |
| 323 | + | |
| 324 | + $meta_val = $product->get_meta( $meta_key, $single = true ); | |
| 325 | + | |
| 326 | + if ( '' !== $meta_val ) { | |
| 327 | + | |
| 328 | + return $local_cache[ $product_id ][ $meta_key ] = true; | |
| 329 | + } | |
| 330 | + | |
| 331 | + $available_vars = $this->get_available_variations( $product ); // Always returns an array. | |
| 332 | + | |
| 333 | + foreach ( $available_vars as $num => $variation ) { | |
| 334 | + | |
| 335 | + $var_id = $variation[ 'variation_id' ]; | |
| 336 | + | |
| 337 | + if ( $var_obj = $this->get_product( $var_id ) ) { | |
| 338 | + | |
| 339 | + $meta_val = $var_obj->get_meta( $meta_key, $single = true ); | |
| 340 | + | |
| 341 | + if ( '' !== $meta_val ) { | |
| 342 | + | |
| 343 | + return $local_cache[ $product_id ][ $meta_key ] = true; | |
| 344 | + } | |
| 345 | + } | |
| 346 | + } | |
| 347 | + | |
| 348 | + return $local_cache[ $product_id ][ $meta_key ] = false; | |
| 349 | + } | |
| 350 | + | |
| 351 | + /* | |
| 352 | + * Deprecatd on 2022/12/28. | |
| 353 | + */ | |
| 354 | + public static function get_dimension_label( $key = '' ) { | |
| 355 | + | |
| 356 | + _deprecated_function( __METHOD__ . '()', '2022/12/28', $replacement = 'WpssoUtilUnits::get_dimension_label()' ); // Deprecation message. | |
| 357 | + | |
| 358 | + return WpssoUtilUnits::get_dimension_label( $key ); | |
| 359 | + } | |
| 360 | + | |
| 361 | + /* | |
| 362 | + * Deprecatd on 2022/12/28. | |
| 363 | + */ | |
| 364 | + public static function get_dimension( $value, $to, $from = '' ) { | |
| 365 | + | |
| 366 | + _deprecated_function( __METHOD__ . '()', '2022/12/28', $replacement = 'WpssoUtilUnits::get_dimension_convert()' ); // Deprecation message. | |
| 367 | + | |
| 368 | + return WpssoUtilUnits::get_dimension_convert( $value, $to, $from ); | |
| 369 | + } | |
| 370 | + | |
| 371 | + /* | |
| 372 | + * Deprecatd on 2022/12/28. | |
| 373 | + */ | |
| 374 | + public static function get_fluid_volume_label( $key = '' ) { | |
| 375 | + | |
| 376 | + _deprecated_function( __METHOD__ . '()', '2022/12/28', $replacement = 'WpssoUtilUnits::get_fluid_volume_label()' ); // Deprecation message. | |
| 377 | + | |
| 378 | + return WpssoUtilUnits::get_fluid_volume_label( $key ); | |
| 379 | + } | |
| 380 | + | |
| 381 | + /* | |
| 382 | + * Deprecatd on 2022/12/28. | |
| 383 | + */ | |
| 384 | + public static function get_fluid_volume_units() { | |
| 385 | + | |
| 386 | + _deprecated_function( __METHOD__ . '()', '2022/12/28', $replacement = 'WpssoUtilUnits::get_fluid_volume_units()' ); // Deprecation message. | |
| 387 | + | |
| 388 | + return WpssoUtilUnits::get_fluid_volume_units(); | |
| 389 | + } | |
| 390 | + | |
| 391 | + /* | |
| 392 | + * Deprecatd on 2022/12/28. | |
| 393 | + */ | |
| 394 | + public static function get_fluid_volume( $value, $to, $from = '' ) { | |
| 395 | + | |
| 396 | + _deprecated_function( __METHOD__ . '()', '2022/12/28', $replacement = 'WpssoUtilUnits::get_fluid_volume_convert()' ); // Deprecation message. | |
| 397 | + | |
| 398 | + return WpssoUtilUnits::get_fluid_volume_convert( $value, $to, $from ); | |
| 399 | + } | |
| 400 | + } | |
| 401 | +} | |