| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Traits; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit(); |
| 7 |
} // Exit if accessed directly |
| 8 |
|
| 9 |
trait ElementorHelper { |
| 10 |
|
| 11 |
public function select2_ajax_posts_filter_autocomplete() { |
| 12 |
$post_type = 'post'; |
| 13 |
$source_name = 'post_type'; |
| 14 |
|
| 15 |
if ( !empty( $_GET[ 'post_type' ] ) ) { |
| 16 |
$post_type = sanitize_text_field( $_GET[ 'post_type' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( !empty( $_GET[ 'source_name' ] ) ) { |
| 20 |
$source_name = sanitize_text_field( $_GET[ 'source_name' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
$search = !empty( $_GET[ 'term' ] ) ? sanitize_text_field( $_GET[ 'term' ] ) : ''; |
| 24 |
$results = $post_list = []; |
| 25 |
switch($source_name){ |
| 26 |
case 'taxonomy': |
| 27 |
$args = [ |
| 28 |
'hide_empty' => false, |
| 29 |
'orderby' => 'name', |
| 30 |
'order' => 'ASC', |
| 31 |
'search' => $search, |
| 32 |
'number' => '5', |
| 33 |
]; |
| 34 |
|
| 35 |
if ( $post_type !== 'all' ) { |
| 36 |
$args['taxonomy'] = $post_type; |
| 37 |
} |
| 38 |
|
| 39 |
$post_list = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 40 |
break; |
| 41 |
default: |
| 42 |
$post_list = $this->get_query_post_list( $post_type, 10, $search ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( !empty( $post_list ) ) { |
| 46 |
foreach ( $post_list as $key => $item ) { |
| 47 |
$results[] = [ 'text' => $item, 'id' => $key ]; |
| 48 |
} |
| 49 |
} |
| 50 |
wp_send_json( [ 'results' => $results ] ); |
| 51 |
} |
| 52 |
|
| 53 |
public function select2_ajax_get_posts_value_titles() { |
| 54 |
|
| 55 |
if ( empty( $_POST[ 'id' ] ) ) { |
| 56 |
wp_send_json_error( [] ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( empty( array_filter($_POST[ 'id' ]) ) ) { |
| 60 |
wp_send_json_error( [] ); |
| 61 |
} |
| 62 |
$ids = array_map('intval',$_POST[ 'id' ]); |
| 63 |
$source_name = !empty( $_POST[ 'source_name' ] ) ? sanitize_text_field( $_POST[ 'source_name' ] ) : ''; |
| 64 |
|
| 65 |
switch ( $source_name ) { |
| 66 |
case 'taxonomy': |
| 67 |
$args = [ |
| 68 |
'hide_empty' => false, |
| 69 |
'orderby' => 'name', |
| 70 |
'order' => 'ASC', |
| 71 |
'include' => implode( ',', $ids ), |
| 72 |
]; |
| 73 |
|
| 74 |
if ( $_POST[ 'post_type' ] !== 'all' ) { |
| 75 |
$args['taxonomy'] = sanitize_text_field( $_POST[ 'post_type' ] ); |
| 76 |
} |
| 77 |
|
| 78 |
$response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 79 |
break; |
| 80 |
default: |
| 81 |
$post_info = get_posts( [ 'post_type' => sanitize_text_field( $_POST[ 'post_type' ] ), 'include' => implode( ',', $ids ) ] ); |
| 82 |
$response = wp_list_pluck( $post_info, 'post_title', 'ID' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( !empty( $response ) ) { |
| 86 |
wp_send_json_success( [ 'results' => $response ] ); |
| 87 |
} else { |
| 88 |
wp_send_json_error( [] ); |
| 89 |
} |
| 90 |
} |
| 91 |
} |