Skip to content

Commit

Permalink
Merge pull request #1872 from Codeinwp/feat/live-search-hook
Browse files Browse the repository at this point in the history
Load Live Search deps via WordPress Action Hook
  • Loading branch information
HardeepAsrani authored Sep 25, 2023
2 parents 9788022 + 1c9a6b3 commit 92f1ba8
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions plugins/otter-pro/inc/plugins/class-live-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Live_Search {
*/
public function init() {
add_filter( 'render_block', array( $this, 'render_blocks' ), 10, 2 );
add_action( 'otter_load_live_search_deps', array( $this, 'load_deps' ) );
}

/**
Expand All @@ -39,6 +40,44 @@ public function render_blocks( $block_content, $block ) {
return $block_content;
}

do_action( 'otter_load_live_search_deps' );

$post_types_data = '';
if ( isset( $block['attrs']['otterSearchQuery']['post_type'] ) ) {
$post_types_data = 'data-post-types=' . wp_json_encode( $block['attrs']['otterSearchQuery']['post_type'] );
}

// Insert hidden fields to filter core's search results.
$query_params_markup = '';
if ( isset( $block['attrs']['otterSearchQuery'] ) && count( $block['attrs']['otterSearchQuery'] ) > 0 ) {
foreach ( $block['attrs']['otterSearchQuery'] as $param => $value ) {
$query_params_markup .= sprintf(
'<input type="hidden" name="o_%s" value="%s" />',
esc_attr( $param ),
esc_attr( implode( ',', $value ) )
);
}
}

$block_content = substr( $block_content, 0, strpos( $block_content, '</form>' ) ) . $query_params_markup . substr( $block_content, strpos( $block_content, '</form>' ) );
return '<div class="o-live-search"' . $post_types_data . '>' . $block_content . '</div>';
}

/**
* Load the live search dependencies.
*
* @return void
*
* @static
*/
public static function load_deps() {

$has_license = in_array( License::get_license_type(), array( 2, 3 ) ) || ( License::has_active_license() && isset( License::get_license_data()->otter_pro ) );

if ( ! $has_license ) {
return;
}

$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/live-search.asset.php';
wp_enqueue_script(
'otter-live-search',
Expand All @@ -64,26 +103,6 @@ public function render_blocks( $block_content, $block ) {

$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/live-search-style.asset.php';
wp_enqueue_style( 'otter-live-search-style', OTTER_BLOCKS_URL . 'build/blocks/live-search-style.css', $asset_file['dependencies'], $asset_file['version'] );

$post_types_data = '';
if ( isset( $block['attrs']['otterSearchQuery']['post_type'] ) ) {
$post_types_data = 'data-post-types=' . wp_json_encode( $block['attrs']['otterSearchQuery']['post_type'] );
}

// Insert hidden fields to filter core's search results.
$query_params_markup = '';
if ( isset( $block['attrs']['otterSearchQuery'] ) && count( $block['attrs']['otterSearchQuery'] ) > 0 ) {
foreach ( $block['attrs']['otterSearchQuery'] as $param => $value ) {
$query_params_markup .= sprintf(
'<input type="hidden" name="o_%s" value="%s" />',
esc_attr( $param ),
esc_attr( implode( ',', $value ) )
);
}
}

$block_content = substr( $block_content, 0, strpos( $block_content, '</form>' ) ) . $query_params_markup . substr( $block_content, strpos( $block_content, '</form>' ) );
return '<div class="o-live-search"' . $post_types_data . '>' . $block_content . '</div>';
}

/**
Expand Down

0 comments on commit 92f1ba8

Please sign in to comment.