| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
/** |
| 6 |
* Filters component class. |
| 7 |
* |
| 8 |
* @since 1.1.0 |
| 9 |
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
| 10 |
*/ |
| 11 |
class Sellkit_Admin_Filters { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class instance. |
| 15 |
* |
| 16 |
* @since 1.1.0 |
| 17 |
* @var Sellkit_Admin_Filters |
| 18 |
*/ |
| 19 |
private static $instance = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get a class instance. |
| 23 |
* |
| 24 |
* @since 1.1.0 |
| 25 |
* |
| 26 |
* @return Sellkit_Admin_Filters Class instance. |
| 27 |
*/ |
| 28 |
public static function get_instance() { |
| 29 |
if ( null === self::$instance ) { |
| 30 |
self::$instance = new self(); |
| 31 |
} |
| 32 |
|
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor. |
| 38 |
* |
| 39 |
* @since 1.1.0 |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
add_action( 'wp_ajax_sellkit_filters_options', [ $this, 'get_options' ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get Conditions options |
| 47 |
* |
| 48 |
* @since 1.1.0 |
| 49 |
*/ |
| 50 |
public function get_options() { |
| 51 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 52 |
|
| 53 |
$filter_type = sellkit_htmlspecialchars( INPUT_GET, 'filter_type' ); |
| 54 |
$input_value = sellkit_htmlspecialchars( INPUT_GET, 'input_value' ); |
| 55 |
|
| 56 |
$options = call_user_func( [ $this, "get_filtered_{$filter_type}" ], $input_value ); |
| 57 |
|
| 58 |
wp_send_json_success( $options ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get Filtered products. |
| 63 |
* |
| 64 |
* @since 1.1.0 |
| 65 |
* @param string $input_value Input value. |
| 66 |
*/ |
| 67 |
private function get_filtered_products( $input_value ) { |
| 68 |
$filtered_products = []; |
| 69 |
$args = [ |
| 70 |
'post_type' => 'product', |
| 71 |
'post_status' => 'any', |
| 72 |
's' => $input_value, |
| 73 |
]; |
| 74 |
|
| 75 |
$query = new WP_Query( $args ); |
| 76 |
|
| 77 |
if ( $query->have_posts() ) { |
| 78 |
while ( $query->have_posts() ) { |
| 79 |
$query->the_post(); |
| 80 |
$filtered_products[] = [ |
| 81 |
'label' => html_entity_decode( get_the_title() ), |
| 82 |
'value' => get_the_ID(), |
| 83 |
]; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $filtered_products; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get Filtered products. |
| 92 |
* |
| 93 |
* @since 1.1.0 |
| 94 |
* @param string $input_value Input value. |
| 95 |
*/ |
| 96 |
private function get_filtered_tags( $input_value ) { |
| 97 |
$filtered_tags = []; |
| 98 |
|
| 99 |
$terms = get_terms( [ |
| 100 |
'taxonomy' => 'product_tag', |
| 101 |
'hide_empty' => false, |
| 102 |
'search' => $input_value, |
| 103 |
] ); |
| 104 |
|
| 105 |
foreach ( $terms as $tag ) { |
| 106 |
$filtered_tags[] = [ |
| 107 |
'label' => html_entity_decode( $tag->name ), |
| 108 |
'value' => $tag->term_id, |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
return $filtered_tags; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get Filtered products. |
| 117 |
* |
| 118 |
* @since 1.1.0 |
| 119 |
* @param string $input_value Input value. |
| 120 |
*/ |
| 121 |
private function get_filtered_categories( $input_value ) { |
| 122 |
$filtered_tags = []; |
| 123 |
|
| 124 |
$terms = get_terms( [ |
| 125 |
'taxonomy' => 'product_cat', |
| 126 |
'hide_empty' => false, |
| 127 |
'search' => $input_value, |
| 128 |
] ); |
| 129 |
|
| 130 |
foreach ( $terms as $tag ) { |
| 131 |
$filtered_tags[] = [ |
| 132 |
'label' => htmlspecialchars_decode( $tag->name ), |
| 133 |
'value' => $tag->term_id, |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
return $filtered_tags; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
Sellkit_Admin_Filters::get_instance(); |
| 142 |
|