| 1 |
<?php |
| 2 |
/** |
| 3 |
* Builder Plugin Compatibility Code |
| 4 |
* |
| 5 |
* @package Themify_Builder |
| 6 |
* @subpackage Themify_Builder/classes |
| 7 |
*/ |
| 8 |
|
| 9 |
class Themify_Builder_Plugin_Compat_WooCommerce { |
| 10 |
|
| 11 |
static function init() { |
| 12 |
$description_hook = themify_get( 'setting-product_description_type','long',true ); |
| 13 |
if ( ! is_admin() ) { |
| 14 |
$description_hook = $description_hook==='long' || !$description_hook? 'the_content' : 'woocommerce_short_description'; |
| 15 |
add_filter( $description_hook, array( __CLASS__, 'single_product_builder_content') ); |
| 16 |
if('woocommerce_short_description'===$description_hook){ |
| 17 |
add_action( 'woocommerce_variable_add_to_cart', array( __CLASS__, 'remove_builder_content_variation' ) ); |
| 18 |
} else { |
| 19 |
/* prevent static content from showing in short description */ |
| 20 |
add_filter( 'woocommerce_short_description', [ 'Themify_Builder', 'clear_static_content' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
// Single Variations Plugin compatibility |
| 24 |
if( class_exists( 'Iconic_WSSV_Query',false ) ) { |
| 25 |
add_filter( 'pre_get_posts', array( __CLASS__, 'add_variations_to_product_query' ), 50, 1 ); |
| 26 |
} |
| 27 |
} |
| 28 |
elseif('short'===$description_hook){ |
| 29 |
global $pagenow; |
| 30 |
if(($pagenow === 'post-new.php' && isset($_GET['post_type']) && 'product'===$_GET['post_type']) || ('post.php' === $pagenow && isset($_GET['post']) && 'product'===get_post_type( $_GET['post']))){ |
| 31 |
add_filter( 'themify_builder_active_vars', array( __CLASS__, 'short_desc_builder_badge' ) ); |
| 32 |
} |
| 33 |
} |
| 34 |
add_action( 'woocommerce_archive_description', array( __CLASS__, 'wc_builder_shop_page' ), 11 ); |
| 35 |
add_action( 'woocommerce_before_template_part', array( __CLASS__, 'before_woocommerce_templates' ) ); |
| 36 |
add_action( 'woocommerce_after_template_part', array( __CLASS__, 'after_woocommerce_templates' ) ); |
| 37 |
add_filter( 'woocommerce_short_description', array( __CLASS__, 'fix_static_content_on_shop_page' ) ); |
| 38 |
add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'woocommerce_product_tabs' ) ); |
| 39 |
|
| 40 |
/** |
| 41 |
* disable Builder on Shop page |
| 42 |
* self::wc_builder_shop_page() handles this |
| 43 |
*/ |
| 44 |
add_action( 'woocommerce_before_main_content', array( __CLASS__, 'woocommerce_before_main_content' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* Localize a value to add builder static content to short description |
| 49 |
* */ |
| 50 |
public static function short_desc_builder_badge(array $vars ):array { |
| 51 |
$vars['short_badge'] = true; |
| 52 |
return $vars; |
| 53 |
} |
| 54 |
|
| 55 |
public static function woocommerce_before_main_content() { |
| 56 |
if ( themify_is_shop() ) { |
| 57 |
Themify_Builder::reset_builder_query( 'reset' ); |
| 58 |
add_action( 'woocommerce_after_main_content', array( __CLASS__, 'woocommerce_after_main_content' ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static function woocommerce_after_main_content() { |
| 63 |
Themify_Builder::reset_builder_query( 'restore' ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Remove builder content filter from variation short description |
| 68 |
*/ |
| 69 |
public static function remove_builder_content_variation() { |
| 70 |
global $post; |
| 71 |
if ( $post->post_type === 'product' && is_product()) { |
| 72 |
remove_filter( 'woocommerce_short_description', array( __CLASS__, 'single_product_builder_content') ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Show builder on Shop page. |
| 78 |
* |
| 79 |
* @access public |
| 80 |
*/ |
| 81 |
public static function wc_builder_shop_page() { |
| 82 |
if ( themify_is_shop() ) { |
| 83 |
echo self::show_builder_content( Themify_Builder_Model::get_ID() ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Avoid render buider content in WooCommerce content |
| 89 |
*/ |
| 90 |
|
| 91 |
public static function before_woocommerce_templates() { |
| 92 |
if( Themify_Builder_Model::is_front_builder_activate() ) { |
| 93 |
remove_filter( 'the_content', array( 'Themify_Builder', 'builder_show_on_front'), 11 ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static function after_woocommerce_templates() { |
| 98 |
if( Themify_Builder_Model::is_front_builder_activate() ) { |
| 99 |
add_filter( 'the_content', array( 'Themify_Builder', 'builder_show_on_front' ), 11 ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Removes Builder static content from Shop page |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public static function fix_static_content_on_shop_page(string $content='' ):string { |
| 109 |
if ( is_post_type_archive( 'product' ) ) { |
| 110 |
$content = ThemifyBuilder_Data_Manager::update_static_content_string( '', $content ); |
| 111 |
} |
| 112 |
|
| 113 |
return $content; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Ensure "Description" product tab is visible on frontend even if there are no description, |
| 118 |
* so that Builder frontend editor can be used. |
| 119 |
* |
| 120 |
* Hooked to "woocommerce_product_tabs" |
| 121 |
*/ |
| 122 |
public static function woocommerce_product_tabs(array $tabs ):array { |
| 123 |
if ( ! isset( $tabs['description'] ) && Themify_Builder_Model::is_frontend_editor_page()&& is_singular( 'product' ) ) { |
| 124 |
$tabs['description'] = array( |
| 125 |
'title' => __( 'Description', 'themify' ), |
| 126 |
'priority' => 10, |
| 127 |
'callback' => 'woocommerce_product_description_tab', |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
return $tabs; |
| 132 |
} |
| 133 |
|
| 134 |
private static function show_builder_content(int $id,string $content=''):string{ |
| 135 |
return Themify_Builder::render( $id, $content ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Render builder content for Single products |
| 140 |
* |
| 141 |
* @access public |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public static function single_product_builder_content(string $content='' ):string { |
| 145 |
global $post; |
| 146 |
if ( isset( $post->post_type ) && $post->post_type === 'product' && is_product()) { |
| 147 |
$content = self::show_builder_content( $post->ID, $content ); |
| 148 |
} |
| 149 |
|
| 150 |
return $content; |
| 151 |
} |
| 152 |
|
| 153 |
public static function add_variations_to_product_query($q){ |
| 154 |
if ('product' !== $q->get('post_type') && !$q->is_search ) { |
| 155 |
return $q; |
| 156 |
} |
| 157 |
|
| 158 |
// Add product variations to the query |
| 159 |
$post_type = (array) $q->get( 'post_type' ); |
| 160 |
$post_type[] = 'product_variation'; |
| 161 |
if ( ! in_array( 'product', $post_type,true ) ) { |
| 162 |
$post_type[] = 'product'; |
| 163 |
} |
| 164 |
$q->set( 'post_type', array_filter( $post_type ) ); |
| 165 |
|
| 166 |
// Don't get variations with unpublished parents |
| 167 |
$unpublished_variable_product_ids = Iconic_WSSV_Query::get_unpublished_variable_product_ids(); |
| 168 |
if ( ! empty( $unpublished_variable_product_ids ) ) { |
| 169 |
$q->set( 'post_parent__not_in', array_merge( (array) $q->get( 'post_parent__not_in' ), $unpublished_variable_product_ids ) ); |
| 170 |
} |
| 171 |
|
| 172 |
// Don't get variations with missing parents :( |
| 173 |
$variation_ids_with_missing_parent = Iconic_WSSV_Query::get_variation_ids_with_missing_parent(); |
| 174 |
if ( ! empty( $variation_ids_with_missing_parent ) ) { |
| 175 |
$q->set( 'post__not_in', array_merge( (array) $q->get( 'post__not_in' ), $variation_ids_with_missing_parent ) ); |
| 176 |
} |
| 177 |
// update the tax query to include our variations |
| 178 |
$q->set( 'tax_query', Iconic_WSSV_Query::update_tax_query( (array) $q->get( 'tax_query' ) ) ); |
| 179 |
|
| 180 |
return $q; |
| 181 |
} |
| 182 |
} |