| 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 |
/** |
| 12 |
* Gate for the Elementor editor's select2 lookups. |
| 13 |
* |
| 14 |
* Both endpoints are registered for every logged-in user and carried neither a nonce nor |
| 15 |
* a capability check, so any subscriber — or a cross-site request riding an editor's |
| 16 |
* session — could list post titles and terms, unpublished ones included. They exist only |
| 17 |
* for the editor, so they require what the editor requires. |
| 18 |
* |
| 19 |
* @since 2.3.4 |
| 20 |
* |
| 21 |
* @return void Ends the request when refused. |
| 22 |
*/ |
| 23 |
private function verify_select2_request() { |
| 24 |
if ( ! check_ajax_referer( 'better_payment_select2', 'security', false ) || ! current_user_can( 'edit_posts' ) ) { |
| 25 |
wp_send_json_error( [], 403 ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function select2_ajax_posts_filter_autocomplete() { |
| 30 |
$this->verify_select2_request(); |
| 31 |
|
| 32 |
$post_type = 'post'; |
| 33 |
$source_name = 'post_type'; |
| 34 |
|
| 35 |
if ( !empty( $_GET[ 'post_type' ] ) ) { |
| 36 |
$post_type = sanitize_text_field( $_GET[ 'post_type' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( !empty( $_GET[ 'source_name' ] ) ) { |
| 40 |
$source_name = sanitize_text_field( $_GET[ 'source_name' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
$search = !empty( $_GET[ 'term' ] ) ? sanitize_text_field( $_GET[ 'term' ] ) : ''; |
| 44 |
$results = $post_list = []; |
| 45 |
switch($source_name){ |
| 46 |
case 'taxonomy': |
| 47 |
$args = [ |
| 48 |
'hide_empty' => false, |
| 49 |
'orderby' => 'name', |
| 50 |
'order' => 'ASC', |
| 51 |
'search' => $search, |
| 52 |
'number' => '5', |
| 53 |
]; |
| 54 |
|
| 55 |
if ( $post_type !== 'all' ) { |
| 56 |
$args['taxonomy'] = $post_type; |
| 57 |
} |
| 58 |
|
| 59 |
$post_list = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 60 |
break; |
| 61 |
case 'user': |
| 62 |
if ( ! current_user_can( 'list_users' ) ) { |
| 63 |
$post_list = []; |
| 64 |
break; |
| 65 |
} |
| 66 |
|
| 67 |
$users = []; |
| 68 |
|
| 69 |
foreach ( get_users( [ 'search' => "*{$search}*" ] ) as $user ) { |
| 70 |
$user_id = $user->ID; |
| 71 |
$user_name = $user->display_name; |
| 72 |
$users[ $user_id ] = $user_name; |
| 73 |
} |
| 74 |
|
| 75 |
$post_list = $users; |
| 76 |
break; |
| 77 |
default: |
| 78 |
if ( $post_type === 'fluent-products' && function_exists('fluentCart') ) { |
| 79 |
$post_list = $this->get_fluentcart_products( $search ); |
| 80 |
} else { |
| 81 |
$post_list = $this->get_query_post_list( $post_type, 10, $search ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ( !empty( $post_list ) ) { |
| 86 |
foreach ( $post_list as $key => $item ) { |
| 87 |
$results[] = [ 'text' => $item, 'id' => $key ]; |
| 88 |
} |
| 89 |
} |
| 90 |
wp_send_json( [ 'results' => $results ] ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Select2 Ajax Get Posts Value Titles |
| 95 |
* get selected value to show elementor editor panel in select2 ajax search box |
| 96 |
* |
| 97 |
* @access public |
| 98 |
* @return void |
| 99 |
* @since 1.0.0 |
| 100 |
*/ |
| 101 |
public function select2_ajax_get_posts_value_titles() { |
| 102 |
$this->verify_select2_request(); |
| 103 |
|
| 104 |
if ( empty( $_POST['id'] ) ) { |
| 105 |
wp_send_json_error( [] ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( empty( array_filter($_POST['id']) ) ) { |
| 109 |
wp_send_json_error( [] ); |
| 110 |
} |
| 111 |
$ids = array_map('intval', $_POST['id']); |
| 112 |
$source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( $_POST['source_name'] ) : ''; |
| 113 |
|
| 114 |
switch ( $source_name ) { |
| 115 |
case 'taxonomy': |
| 116 |
$args = [ |
| 117 |
'hide_empty' => false, |
| 118 |
'orderby' => 'name', |
| 119 |
'order' => 'ASC', |
| 120 |
'include' => implode( ',', $ids ), |
| 121 |
]; |
| 122 |
|
| 123 |
if ( $_POST['post_type'] !== 'all' ) { |
| 124 |
$args['taxonomy'] = sanitize_text_field( $_POST[ 'post_type' ] ); |
| 125 |
} |
| 126 |
|
| 127 |
$response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 128 |
break; |
| 129 |
case 'user': |
| 130 |
$users = []; |
| 131 |
|
| 132 |
foreach ( get_users( [ 'include' => $ids ] ) as $user ) { |
| 133 |
$user_id = $user->ID; |
| 134 |
$user_name = $user->display_name; |
| 135 |
$users[ $user_id ] = $user_name; |
| 136 |
} |
| 137 |
|
| 138 |
$response = $users; |
| 139 |
break; |
| 140 |
default: |
| 141 |
$post_info = get_posts( [ 'post_type' => sanitize_text_field( $_POST['post_type'] ), 'include' => implode( ',', $ids ) ] ); |
| 142 |
$response = wp_list_pluck( $post_info, 'post_title', 'ID' ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! empty( $response ) ) { |
| 146 |
wp_send_json_success( [ 'results' => $response ] ); |
| 147 |
} else { |
| 148 |
wp_send_json_error( [] ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Add elementor category |
| 154 |
* |
| 155 |
* @since v1.0.0 |
| 156 |
*/ |
| 157 |
public function register_widget_categories( $elements_manager ) { |
| 158 |
$elements_manager->add_category( |
| 159 |
'better-payment', |
| 160 |
[ |
| 161 |
'title' => __( 'Better Payment', 'better-payment' ), |
| 162 |
'icon' => 'font', |
| 163 |
], 1 ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get FluentCart products for select2 dropdown |
| 168 |
* |
| 169 |
* @param string $search Search term |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
private function get_fluentcart_products( $search = '' ) { |
| 173 |
if ( ! function_exists('fluentCart') || ! class_exists( '\FluentCart\App\Models\Product' ) ) { |
| 174 |
return []; |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! is_user_logged_in() || ! current_user_can('edit_posts') ) { |
| 178 |
return []; |
| 179 |
} |
| 180 |
|
| 181 |
try { |
| 182 |
$product_model = new \FluentCart\App\Models\Product(); |
| 183 |
|
| 184 |
$query = $product_model->newQuery() |
| 185 |
->where('post_status', 'publish') |
| 186 |
->limit(10); |
| 187 |
|
| 188 |
|
| 189 |
if ( ! empty( $search ) ) { |
| 190 |
$query->where('post_title', 'LIKE', '%' . sanitize_text_field( $search ) . '%'); |
| 191 |
} |
| 192 |
|
| 193 |
$products = $query->get(); |
| 194 |
|
| 195 |
$product_list = []; |
| 196 |
if ( ! empty( $products ) ) { |
| 197 |
foreach ( $products as $product ) { |
| 198 |
$product_list[ $product->ID ] = $product->post_title; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $product_list; |
| 203 |
} catch ( \Exception $e ) { |
| 204 |
return []; |
| 205 |
} |
| 206 |
} |
| 207 |
} |