Skip to content

Commit

Permalink
Remove hidden product from ajax search
Browse files Browse the repository at this point in the history
Trim merchant shortcodes from product desc
  • Loading branch information
thenahidul committed Dec 20, 2024
1 parent 0b2ff8f commit 2bb7cc6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 22 deletions.
3 changes: 2 additions & 1 deletion assets/sass/modules/real-time-search/real-time-search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@
position: absolute;
top: calc(100% + 10px);
left: 0;
z-index: 99999;
width: 100%;
min-width: var(--merchant-results-box-width, 500px);
padding: 15px;
color: #212121;
background-color: #fff;
border: 1px solid #eee;
box-shadow: 0px 30px 80px -10px rgba(0, 0, 0, 0.1);
box-shadow: 0 30px 80px -10px rgba(0, 0, 0, 0.1);

&.merchant-reverse {
left: auto;
Expand Down
16 changes: 16 additions & 0 deletions inc/compatibility/class-merchant-divi-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function __construct() {

add_filter( 'merchant_module_shortcode_error_message_html', array( $this, 'shortcode_error_message' ), 10, 2 );

add_filter( 'merchant_real_time_search_product_description', array( $this, 'remove_shortcode_product_description' ) );

// Custom CSS.
add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
}
Expand All @@ -43,6 +45,17 @@ public function quick_view_description( $description ) {
return $this->et_strip_shortcodes( $description );
}

/**
* Remove Divi shortcodes from search Real-time search results product description.
*
* @param $content
*
* @return array|string|string[]|null
*/
public function remove_shortcode_product_description( $content ) {
return $this->et_strip_shortcodes( $content );
}

/**
* For some reason in edit mode it can't detect is_singular( 'product' ) or is_product() tag and shows the error message.
*
Expand Down Expand Up @@ -159,6 +172,9 @@ public function et_strip_shortcodes( $content, $truncate_post_based_shortcodes_o
'et_pb_code',
'et_pb_fullwidth_code',
'et_pb_social_media_follow_network',
'et_pb_section',
'et_pb_row',
'et_pb_row_inner',
);

// list of post-based shortcodes.
Expand Down
2 changes: 1 addition & 1 deletion inc/modules/real-time-search/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'type' => 'select',
'title' => esc_html__( 'Results description', 'merchant' ),
'options' => array(
'product-post-content' => esc_html__( 'Product Description', 'merchant' ),
'product-post-content' => esc_html__( 'Product description', 'merchant' ),
'product-short-description' => esc_html__( 'Product short description', 'merchant' ),
),
'default' => 'product-short-description',
Expand Down
93 changes: 73 additions & 20 deletions inc/modules/real-time-search/class-real-time-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ public function __construct() {
// Enqueue admin styles.
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );

// Enqueue admin scripts.
// add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );

// Localize script.
//add_filter( 'merchant_admin_localize_script', array( $this, 'localize_script' ) );

// Admin preview box.
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
}
Expand All @@ -100,10 +94,12 @@ public function __construct() {
}

// Return early if it's on admin but not in the respective module settings page.
if ( is_admin() && ! parent::is_module_settings_page() ) {
if ( is_admin() && ! wp_doing_ajax() && ! parent::is_module_settings_page() ) {
return;
}

add_filter( 'merchant_real_time_search_product_description', array( $this, 'remove_shortcode_product_description' ) );

// Enqueue styles.
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );

Expand All @@ -112,6 +108,10 @@ public function __construct() {

// Localize script.
add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) );

// Ajax handlers.
add_action( 'wp_ajax_ajax_search_callback', array( $this, 'ajax_search_callback' ) );
add_action( 'wp_ajax_nopriv_ajax_search_callback', array( $this, 'ajax_search_callback' ) );
}

/**
Expand Down Expand Up @@ -314,6 +314,15 @@ public static function ajax_search_callback() {
'order' => $order,
'orderby' => $orderby,
'post_status' => array( 'publish' ),
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array( 'exclude-from-search' ),
'operator' => 'NOT IN',
),
),
);

if ( 'price' === $orderby ) {
Expand All @@ -334,6 +343,15 @@ public static function ajax_search_callback() {
'order' => $order,
'orderby' => $orderby,
'post_status' => array( 'publish' ),
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array( 'exclude-from-search' ),
'operator' => 'NOT IN',
),
),
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
'relation' => 'OR',
Expand All @@ -352,9 +370,22 @@ public static function ajax_search_callback() {
$args['orderby'] = 'meta_value_num';
}

$qry_sku = new WP_Query( $args );
$qry->posts = array_merge( $qry->posts, $qry_sku->posts );
$qry->post_count = count( $qry->posts );
$qry_sku = new WP_Query( $args );

// Merge and remove duplicates
$merged_posts = array_merge( $qry->posts, $qry_sku->posts );
$unique_posts = array();
$product_ids = array();

foreach ( $merged_posts as $post ) {
if ( ! in_array( $post->ID, $product_ids, true ) ) {
$unique_posts[] = $post;
$product_ids[] = $post->ID;
}
}

$qry->posts = $unique_posts;
$qry->post_count = count( $unique_posts );
}

if ( $qry->have_posts() ) :
Expand Down Expand Up @@ -436,8 +467,24 @@ public static function get_ajax_search_item( $args ) {
$item_permalink = get_the_permalink( $item_post_id );
$item_image = wp_get_attachment_image( $product->get_image_id() );
$item_title = get_the_title( $item_post_id );
$description = wp_trim_words( 'product-post-content' === $desc_type ? $product->get_description() : $product->get_short_description(), $desc_length );
$price = $product->get_price_html();

/**
* `merchant_real_time_search_product_title`
*
* @since 1.11.2
*/
$item_title = apply_filters( 'merchant_real_time_search_product_title', $item_title, $product );

$description = $desc_type === 'product-post-content' ? $product->get_description() : $product->get_short_description();

/**
* `merchant_real_time_search_product_description`
*
* @since 1.11.2
*/
$description = apply_filters( 'merchant_real_time_search_product_description', $description, $product );

$price = $product->get_price_html();
} else {
$item_term_id = $args['term_id'];
$item_term = get_term( $item_term_id );
Expand All @@ -450,7 +497,6 @@ public static function get_ajax_search_item( $args ) {

ob_start();
?>

<a class="merchant-ajax-search-item merchant-ajax-search-item-<?php echo esc_attr( $args['type'] ); ?>" href="<?php echo esc_url( $item_permalink ); ?>">
<?php if ( $item_image ) : ?>
<div class="merchant-ajax-search-item-image">
Expand All @@ -460,7 +506,7 @@ public static function get_ajax_search_item( $args ) {
<div class="merchant-ajax-search-item-info">
<div class="merchant-ajax-search-item-title"><?php echo esc_html( $item_title ); ?></div>
<?php if ( $description ) : ?>
<p><?php echo esc_html( $description ); ?></p>
<p><?php echo esc_html( wp_trim_words( $description, $desc_length ) ); ?></p>
<?php endif; ?>
</div>
<?php if ( $price ) : ?>
Expand All @@ -469,18 +515,25 @@ public static function get_ajax_search_item( $args ) {
</div>
<?php endif; ?>
</a>

<?php

return ob_get_clean();
}

/**
* Remove merchant shortcodes from product description.
*
* @param $description
*
* @return array|string|string[]|null
*/
public function remove_shortcode_product_description( $content ) {
$content = preg_replace( '/\[merchant_[^]]*]/', '', $content );

return wp_strip_all_tags( $content );
}
}

// Initialize the module.
add_action( 'init', function() {
new Merchant_Real_Time_Search();
} );

// Ajax handlers.
add_action( 'wp_ajax_ajax_search_callback', array( 'Merchant_Real_Time_Search', 'ajax_search_callback' ) );
add_action( 'wp_ajax_nopriv_ajax_search_callback', array( 'Merchant_Real_Time_Search', 'ajax_search_callback' ) );

0 comments on commit 2bb7cc6

Please sign in to comment.