| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing; |
| 2 |
|
| 3 |
use Exception; |
| 4 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 5 |
use WC_Customer; |
| 6 |
use WP_Term; |
| 7 |
use WP_User_Query; |
| 8 |
|
| 9 |
class LookupService { |
| 10 |
|
| 11 |
use ServiceContainerTrait; |
| 12 |
|
| 13 |
const CATEGORIES_SEARCH_ACTION = 'woocommerce_json_search_tpt_categories'; |
| 14 |
const TAGS_SEARCH_ACTION = 'woocommerce_json_search_tpt_tags'; |
| 15 |
const BRANDS_SEARCH_ACTION = 'woocommerce_json_search_tpt_brands'; |
| 16 |
const CUSTOMERS_SEARCH_ACTION = 'woocommerce_json_search_tpt_customers'; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
add_action( 'wp_ajax_' . self::CATEGORIES_SEARCH_ACTION, array( $this, 'categoriesSearchHandler' ) ); |
| 20 |
add_action( 'wp_ajax_' . self::TAGS_SEARCH_ACTION, array( $this, 'tagsSearchHandler' ) ); |
| 21 |
add_action( 'wp_ajax_' . self::BRANDS_SEARCH_ACTION, array( $this, 'brandsSearchHandler' ) ); |
| 22 |
add_action( 'wp_ajax_' . self::CUSTOMERS_SEARCH_ACTION, array( $this, 'customersSearchHandler' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
public function customersSearchHandler() { |
| 26 |
|
| 27 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 28 |
wp_send_json( array() ); |
| 29 |
} |
| 30 |
|
| 31 |
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false; |
| 32 |
|
| 33 |
if ( $term ) { |
| 34 |
$wp_user_query = new WP_User_Query( |
| 35 |
array( |
| 36 |
'search' => '*' . $term . '*', |
| 37 |
'search_columns' => array( |
| 38 |
'user_login', |
| 39 |
'user_nicename', |
| 40 |
'user_email', |
| 41 |
), |
| 42 |
'fields' => 'ID', |
| 43 |
) ); |
| 44 |
|
| 45 |
$users = $wp_user_query->get_results(); |
| 46 |
|
| 47 |
if ( $users ) { |
| 48 |
$_users = array(); |
| 49 |
|
| 50 |
foreach ( $users as $userId ) { |
| 51 |
try { |
| 52 |
$customer = new WC_Customer( $userId ); |
| 53 |
} catch ( Exception $e ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $customer instanceof WC_Customer ) { |
| 58 |
$_users[ $userId ] = Formatter::formatCustomerString( $customer ); |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
wp_send_json( $_users ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
wp_send_json( array() ); |
| 68 |
} |
| 69 |
|
| 70 |
public function categoriesSearchHandler() { |
| 71 |
|
| 72 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 73 |
wp_send_json( array() ); |
| 74 |
} |
| 75 |
|
| 76 |
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false; |
| 77 |
|
| 78 |
if ( $term ) { |
| 79 |
$args = array( |
| 80 |
'taxonomy' => array( 'product_cat' ), |
| 81 |
'orderby' => 'id', |
| 82 |
'order' => 'ASC', |
| 83 |
'limit' => 5, |
| 84 |
'hide_empty' => false, |
| 85 |
'fields' => 'all', |
| 86 |
'name__like' => $term |
| 87 |
); |
| 88 |
|
| 89 |
$terms = get_terms( $args ); |
| 90 |
|
| 91 |
if ( $terms ) { |
| 92 |
$_terms = array(); |
| 93 |
|
| 94 |
foreach ( $terms as $term ) { |
| 95 |
if ( $term instanceof WP_Term ) { |
| 96 |
$_terms[ $term->term_id ] = self::getCategoryLabel( $term ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
wp_send_json( $_terms ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
wp_send_json( array() ); |
| 105 |
} |
| 106 |
|
| 107 |
public function tagsSearchHandler() { |
| 108 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 109 |
wp_send_json( array() ); |
| 110 |
} |
| 111 |
|
| 112 |
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false; |
| 113 |
|
| 114 |
if ( $term ) { |
| 115 |
$args = array( |
| 116 |
'taxonomy' => array( 'product_tag' ), |
| 117 |
'orderby' => 'id', |
| 118 |
'order' => 'ASC', |
| 119 |
'limit' => 5, |
| 120 |
'hide_empty' => false, |
| 121 |
'fields' => 'all', |
| 122 |
'name__like' => $term |
| 123 |
); |
| 124 |
|
| 125 |
$terms = get_terms( $args ); |
| 126 |
|
| 127 |
if ( $terms && ! is_wp_error( $terms ) ) { |
| 128 |
$_terms = array(); |
| 129 |
|
| 130 |
foreach ( $terms as $term ) { |
| 131 |
if ( $term instanceof WP_Term ) { |
| 132 |
$_terms[ $term->term_id ] = $term->name; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
wp_send_json( $_terms ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
wp_send_json( array() ); |
| 141 |
} |
| 142 |
|
| 143 |
public function brandsSearchHandler() { |
| 144 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 145 |
wp_send_json( array() ); |
| 146 |
} |
| 147 |
|
| 148 |
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : false; |
| 149 |
|
| 150 |
if ( $term ) { |
| 151 |
if ( taxonomy_exists( 'product_brand' ) ) { |
| 152 |
$args = array( |
| 153 |
'taxonomy' => array( 'product_brand' ), |
| 154 |
'orderby' => 'id', |
| 155 |
'order' => 'ASC', |
| 156 |
'limit' => 5, |
| 157 |
'hide_empty' => false, |
| 158 |
'fields' => 'all', |
| 159 |
'name__like' => $term |
| 160 |
); |
| 161 |
|
| 162 |
$terms = get_terms( $args ); |
| 163 |
|
| 164 |
if ( $terms && ! is_wp_error( $terms ) ) { |
| 165 |
$_terms = array(); |
| 166 |
|
| 167 |
foreach ( $terms as $term ) { |
| 168 |
if ( $term instanceof WP_Term ) { |
| 169 |
$_terms[ $term->term_id ] = $term->name; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
wp_send_json( $_terms ); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
wp_send_json( array() ); |
| 179 |
} |
| 180 |
|
| 181 |
public static function getCategoryLabel( WP_Term $category ): string { |
| 182 |
$parentTermName = ''; |
| 183 |
|
| 184 |
if ( $category->parent ) { |
| 185 |
$parentTerm = get_term( $category->parent ); |
| 186 |
|
| 187 |
if ( $parentTerm ) { |
| 188 |
$parentTermName = ' (' . $parentTerm->name . ')'; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return $category->name . $parentTermName; |
| 193 |
} |
| 194 |
} |
| 195 |
|