| 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 |
case 'user': |
| 42 |
if ( ! current_user_can( 'list_users' ) ) { |
| 43 |
$post_list = []; |
| 44 |
break; |
| 45 |
} |
| 46 |
|
| 47 |
$users = []; |
| 48 |
|
| 49 |
foreach ( get_users( [ 'search' => "*{$search}*" ] ) as $user ) { |
| 50 |
$user_id = $user->ID; |
| 51 |
$user_name = $user->display_name; |
| 52 |
$users[ $user_id ] = $user_name; |
| 53 |
} |
| 54 |
|
| 55 |
$post_list = $users; |
| 56 |
break; |
| 57 |
default: |
| 58 |
if ( $post_type === 'fluent-products' && function_exists('fluentCart') ) { |
| 59 |
$post_list = $this->get_fluentcart_products( $search ); |
| 60 |
} else { |
| 61 |
$post_list = $this->get_query_post_list( $post_type, 10, $search ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( !empty( $post_list ) ) { |
| 66 |
foreach ( $post_list as $key => $item ) { |
| 67 |
$results[] = [ 'text' => $item, 'id' => $key ]; |
| 68 |
} |
| 69 |
} |
| 70 |
wp_send_json( [ 'results' => $results ] ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Select2 Ajax Get Posts Value Titles |
| 75 |
* get selected value to show elementor editor panel in select2 ajax search box |
| 76 |
* |
| 77 |
* @access public |
| 78 |
* @return void |
| 79 |
* @since 1.0.0 |
| 80 |
*/ |
| 81 |
public function select2_ajax_get_posts_value_titles() { |
| 82 |
|
| 83 |
if ( empty( $_POST['id'] ) ) { |
| 84 |
wp_send_json_error( [] ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( empty( array_filter($_POST['id']) ) ) { |
| 88 |
wp_send_json_error( [] ); |
| 89 |
} |
| 90 |
$ids = array_map('intval', $_POST['id']); |
| 91 |
$source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( $_POST['source_name'] ) : ''; |
| 92 |
|
| 93 |
switch ( $source_name ) { |
| 94 |
case 'taxonomy': |
| 95 |
$args = [ |
| 96 |
'hide_empty' => false, |
| 97 |
'orderby' => 'name', |
| 98 |
'order' => 'ASC', |
| 99 |
'include' => implode( ',', $ids ), |
| 100 |
]; |
| 101 |
|
| 102 |
if ( $_POST['post_type'] !== 'all' ) { |
| 103 |
$args['taxonomy'] = sanitize_text_field( $_POST[ 'post_type' ] ); |
| 104 |
} |
| 105 |
|
| 106 |
$response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 107 |
break; |
| 108 |
case 'user': |
| 109 |
$users = []; |
| 110 |
|
| 111 |
foreach ( get_users( [ 'include' => $ids ] ) as $user ) { |
| 112 |
$user_id = $user->ID; |
| 113 |
$user_name = $user->display_name; |
| 114 |
$users[ $user_id ] = $user_name; |
| 115 |
} |
| 116 |
|
| 117 |
$response = $users; |
| 118 |
break; |
| 119 |
default: |
| 120 |
$post_info = get_posts( [ 'post_type' => sanitize_text_field( $_POST['post_type'] ), 'include' => implode( ',', $ids ) ] ); |
| 121 |
$response = wp_list_pluck( $post_info, 'post_title', 'ID' ); |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! empty( $response ) ) { |
| 125 |
wp_send_json_success( [ 'results' => $response ] ); |
| 126 |
} else { |
| 127 |
wp_send_json_error( [] ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Add elementor category |
| 133 |
* |
| 134 |
* @since v1.0.0 |
| 135 |
*/ |
| 136 |
public function register_widget_categories( $elements_manager ) { |
| 137 |
$elements_manager->add_category( |
| 138 |
'better-payment', |
| 139 |
[ |
| 140 |
'title' => __( 'Better Payment', 'better-payment' ), |
| 141 |
'icon' => 'font', |
| 142 |
], 1 ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get FluentCart products for select2 dropdown |
| 147 |
* |
| 148 |
* @param string $search Search term |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
private function get_fluentcart_products( $search = '' ) { |
| 152 |
if ( ! function_exists('fluentCart') || ! class_exists( '\FluentCart\App\Models\Product' ) ) { |
| 153 |
return []; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! is_user_logged_in() || ! current_user_can('edit_posts') ) { |
| 157 |
return []; |
| 158 |
} |
| 159 |
|
| 160 |
try { |
| 161 |
$product_model = new \FluentCart\App\Models\Product(); |
| 162 |
|
| 163 |
$query = $product_model->newQuery() |
| 164 |
->where('post_status', 'publish') |
| 165 |
->limit(10); |
| 166 |
|
| 167 |
|
| 168 |
if ( ! empty( $search ) ) { |
| 169 |
$query->where('post_title', 'LIKE', '%' . sanitize_text_field( $search ) . '%'); |
| 170 |
} |
| 171 |
|
| 172 |
$products = $query->get(); |
| 173 |
|
| 174 |
$product_list = []; |
| 175 |
if ( ! empty( $products ) ) { |
| 176 |
foreach ( $products as $product ) { |
| 177 |
$product_list[ $product->ID ] = $product->post_title; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return $product_list; |
| 182 |
} catch ( \Exception $e ) { |
| 183 |
return []; |
| 184 |
} |
| 185 |
} |
| 186 |
} |