| @@ -7,9 +7,29 @@ | ||
| 7 | 7 | } // Exit if accessed directly |
| 8 | 8 | |
| 9 | 9 | trait ElementorHelper { |
| 10 | 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 | + | |
| 11 | 29 | public function select2_ajax_posts_filter_autocomplete() { |
| 30 | + $this->verify_select2_request(); | |
| 31 | + | |
| 12 | 32 | $post_type = 'post'; |
| 13 | 33 | $source_name = 'post_type'; |
| 14 | 34 | |
| 15 | 35 | if ( !empty( $_GET[ 'post_type' ] ) ) { |
| @@ -37,10 +57,30 @@ | ||
| 37 | 57 | } |
| 38 | 58 | |
| 39 | 59 | $post_list = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 40 | 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; | |
| 41 | 77 | default: |
| 42 | - $post_list = $this->get_query_post_list( $post_type, 10, $search ); | |
| 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 | + } | |
| 43 | 83 | } |
| 44 | 84 | |
| 45 | 85 | if ( !empty( $post_list ) ) { |
| 46 | 86 | foreach ( $post_list as $key => $item ) { |
| @@ -49,19 +89,28 @@ | ||
| 49 | 89 | } |
| 50 | 90 | wp_send_json( [ 'results' => $results ] ); |
| 51 | 91 | } |
| 52 | 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 | + */ | |
| 53 | 101 | public function select2_ajax_get_posts_value_titles() { |
| 54 | - | |
| 55 | - if ( empty( $_POST[ 'id' ] ) ) { | |
| 102 | + $this->verify_select2_request(); | |
| 103 | + | |
| 104 | + if ( empty( $_POST['id'] ) ) { | |
| 56 | 105 | wp_send_json_error( [] ); |
| 57 | 106 | } |
| 58 | 107 | |
| 59 | - if ( empty( array_filter($_POST[ 'id' ]) ) ) { | |
| 108 | + if ( empty( array_filter($_POST['id']) ) ) { | |
| 60 | 109 | wp_send_json_error( [] ); |
| 61 | 110 | } |
| 62 | - $ids = array_map('intval',$_POST[ 'id' ]); | |
| 63 | - $source_name = !empty( $_POST[ 'source_name' ] ) ? sanitize_text_field( $_POST[ 'source_name' ] ) : ''; | |
| 111 | + $ids = array_map('intval', $_POST['id']); | |
| 112 | + $source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( $_POST['source_name'] ) : ''; | |
| 64 | 113 | |
| 65 | 114 | switch ( $source_name ) { |
| 66 | 115 | case 'taxonomy': |
| 67 | 116 | $args = [ |
| @@ -70,22 +119,89 @@ | ||
| 70 | 119 | 'order' => 'ASC', |
| 71 | 120 | 'include' => implode( ',', $ids ), |
| 72 | 121 | ]; |
| 73 | 122 | |
| 74 | - if ( $_POST[ 'post_type' ] !== 'all' ) { | |
| 123 | + if ( $_POST['post_type'] !== 'all' ) { | |
| 75 | 124 | $args['taxonomy'] = sanitize_text_field( $_POST[ 'post_type' ] ); |
| 76 | 125 | } |
| 77 | 126 | |
| 78 | 127 | $response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' ); |
| 79 | 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; | |
| 80 | 140 | default: |
| 81 | - $post_info = get_posts( [ 'post_type' => sanitize_text_field( $_POST[ 'post_type' ] ), 'include' => implode( ',', $ids ) ] ); | |
| 141 | + $post_info = get_posts( [ 'post_type' => sanitize_text_field( $_POST['post_type'] ), 'include' => implode( ',', $ids ) ] ); | |
| 82 | 142 | $response = wp_list_pluck( $post_info, 'post_title', 'ID' ); |
| 83 | 143 | } |
| 84 | 144 | |
| 85 | - if ( !empty( $response ) ) { | |
| 145 | + if ( ! empty( $response ) ) { | |
| 86 | 146 | wp_send_json_success( [ 'results' => $response ] ); |
| 87 | 147 | } else { |
| 88 | 148 | wp_send_json_error( [] ); |
| 89 | 149 | } |
| 90 | 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 | + } | |
| 91 | 207 | } |