| 1 |
<?php |
| 2 |
/** |
| 3 |
* Polylang Integration for Whols |
| 4 |
* |
| 5 |
* Handles synchronization of Whols metadata when products are translated |
| 6 |
* using Polylang for WooCommerce |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Whols\Admin; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Polylang_Integration |
| 17 |
* |
| 18 |
* Ensures Whols plugin metadata is properly synchronized when products |
| 19 |
* are duplicated/translated using Polylang for WooCommerce |
| 20 |
*/ |
| 21 |
class Polylang_Integration { |
| 22 |
|
| 23 |
/** |
| 24 |
* List of Whols meta keys that should be synchronized for the free version |
| 25 |
*/ |
| 26 |
private $whols_meta_keys = [ |
| 27 |
'_whols_price_type_1_properties', |
| 28 |
'_whols_price_type_2_properties', |
| 29 |
'_whols_product_visibility', |
| 30 |
'_whols_mark_this_product_as_wholesale_only', |
| 31 |
]; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initialize the integration |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
// Check if Polylang and Polylang for WooCommerce are active |
| 38 |
if ( ! $this->is_polylang_active() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$this->init_hooks(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if Polylang plugins are active |
| 47 |
*/ |
| 48 |
private function is_polylang_active() { |
| 49 |
return function_exists( 'pll_get_post' ) && class_exists( 'Polylang_Woocommerce' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Initialize hooks for Polylang integration |
| 54 |
*/ |
| 55 |
private function init_hooks() { |
| 56 |
|
| 57 |
// Register meta keys for Polylang synchronization for products |
| 58 |
add_filter( 'pllwc_copy_post_metas', array( $this, 'add_whols_meta_to_copy_list' ), 10, 1 ); |
| 59 |
|
| 60 |
// Register meta keys for variations (if variations exist) |
| 61 |
add_filter( 'pllwc_copy_variation_metas', array( $this, 'add_whols_variation_meta_to_copy_list' ), 10, 1 ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add Whols meta keys to the list of metas to copy |
| 66 |
* |
| 67 |
* @param array|mixed $metas Array of meta keys or other value |
| 68 |
* @return array|mixed Modified array of meta keys or original value |
| 69 |
*/ |
| 70 |
public function add_whols_meta_to_copy_list( $metas ) { |
| 71 |
// Ensure $metas is an array before merging |
| 72 |
if ( ! is_array( $metas ) ) { |
| 73 |
return $metas; |
| 74 |
} |
| 75 |
$whols_meta_keys = []; |
| 76 |
foreach ( $this->whols_meta_keys as $meta_key ) { |
| 77 |
$whols_meta_keys[$meta_key] = $meta_key; |
| 78 |
} |
| 79 |
return array_merge( $metas, $whols_meta_keys ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add Whols variation meta keys to the list of metas to copy |
| 84 |
* |
| 85 |
* @param array|mixed $metas Array of meta keys or other value |
| 86 |
* @return array|mixed Modified array of meta keys or original value |
| 87 |
*/ |
| 88 |
public function add_whols_variation_meta_to_copy_list( $metas ) { |
| 89 |
// Ensure $metas is an array before merging |
| 90 |
if ( ! is_array( $metas ) ) { |
| 91 |
return $metas; |
| 92 |
} |
| 93 |
|
| 94 |
// Variation-specific meta keys for free version |
| 95 |
$variation_meta_keys = [ |
| 96 |
'_whols_price_type_1_properties', |
| 97 |
'_whols_price_type_2_properties', |
| 98 |
]; |
| 99 |
|
| 100 |
$whols_variation_meta = []; |
| 101 |
foreach ( $variation_meta_keys as $meta_key ) { |
| 102 |
$whols_variation_meta[$meta_key] = $meta_key; |
| 103 |
} |
| 104 |
|
| 105 |
return array_merge( $metas, $whols_variation_meta ); |
| 106 |
} |
| 107 |
} |