From 4a4c398a08c81932645c691fc571794f9ab8d46d Mon Sep 17 00:00:00 2001 From: Sultan Nasir Uddin Date: Thu, 3 Aug 2023 11:12:46 +0600 Subject: [PATCH] fix order search showing errors --- assets/js/admin-script.js | 64 ++++++++++ package.json | 2 +- readme.txt | 5 +- src/Admin/Actions.php | 178 +++++++++++++++++++++++++- src/Admin/Admin.php | 1 + src/Admin/ListTables/KeysTable.php | 1 - src/Admin/ListTables/ListTable.php | 2 +- src/Admin/Menus.php | 33 +++-- src/Admin/Notices.php | 2 +- src/Admin/Views/html-add-key.php | 192 ++++++++++++++++++++++++++++- src/Admin/Views/html-edit-key.php | 16 +-- src/Ajax.php | 4 +- 12 files changed, 470 insertions(+), 30 deletions(-) diff --git a/assets/js/admin-script.js b/assets/js/admin-script.js index 9d7e3bb7..e6e02cd8 100644 --- a/assets/js/admin-script.js +++ b/assets/js/admin-script.js @@ -6,6 +6,7 @@ * Licensed under the GPLv2+ license. */ /* global jQuery, wc_serial_numbers_vars */ + (function ($, window) { 'use strict'; @@ -159,6 +160,69 @@ minDate: new Date() }); } + if (typeof typeof $.fn.select2 !== 'undefined') { + $(':input.wcsn-select2').filter(':not(.enhanced)').each(function () { + var $element = $(this); + var select2_args = { + allowClear: $element.data('allow_clear') && !$element.prop('multiple') || true, + placeholder: $element.data('placeholder') || $element.attr('placeholder') || '', + minimumInputLength: $element.data('minimum_input_length') ? $element.data('minimum_input_length') : 0, + ajax: { + url: wc_serial_numbers_vars.ajaxurl, + dataType: 'json', + delay: 250, + method: 'POST', + data: function (params) { + return { + term: params.term, + action: $element.data('action'), + type: $element.data('type'), + _wpnonce: $element.data('nonce')||wc_serial_numbers_vars.ajax_nonce, + exclude: $element.data('exclude'), + include: $element.data('include'), + limit: $element.data('limit'), + page: params.page || 1, + }; + }, + processResults: function (data) { + data.page = data.page || 1; + return data; + }, + cache: true + } + } + + // if data action is set then use ajax. + if (!$element.data('action')) { + delete select2_args.ajax; + } + + $element.select2(select2_args).addClass('enhanced'); + }); + } + + // Add key form. + $('#wcsn-add-key-form :input[name="status"]').on('change', function () { + var $this = $(this); + var $form = $this.closest('form'); + var $customer = $form.find(':input[name="customer_id"]'); + var $order = $form.find(':input[name="order_id"]'); + var value = $(this).is(':checked') ? $(this).val() : ''; + if (!value) { + return false; + } + + if ($this.val() === 'create_order') { + $customer.prop('required', true).closest('tr').show(); + $order.prop('required', false).closest('tr').hide(); + } else if ($this.val() === 'existing_order') { + $customer.prop('required', false).closest('tr').hide(); + $order.prop('required', true).closest('tr').show(); + }else { + $customer.prop('required', false).closest('tr').hide(); + $order.prop('required', false).closest('tr').hide(); + } + }).trigger('change'); }); }(jQuery, window)); diff --git a/package.json b/package.json index 4272b2e6..a13f00b0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "wc-serial-numbers", "title": "Serial Numbers for WooCommerce", - "version": "1.5.4", + "version": "1.5.6", "description": "The best WooCommerce extension to sell license & serial keys, gift cards and other secret numbers!", "homepage": "https://pluginever.com/plugins/wc-serial-numbers/", "license": "GPL-3.0+", diff --git a/readme.txt b/readme.txt index 627bda80..68754a41 100644 --- a/readme.txt +++ b/readme.txt @@ -209,11 +209,14 @@ Serial Numbers for WooCommerce has a dedicated page for Software API. [You can l == Changelog == += 1.5.6 (2 Aug 2023) = +* Enhance: Key edit page UI improvement. + = 1.5.4 (18 Jun 2023) = * Enhance: Labels and notice texts. * Fix: Sequential pointers with manual assigning not working properly. -* Fix: Reports filter not working. +* Fix: Reports filter not working. = 1.5.3 (14 Jun 2023) = * Enhance: Added bulk reset action for serial keys. diff --git a/src/Admin/Actions.php b/src/Admin/Actions.php index 56ae02a1..5b194ccb 100644 --- a/src/Admin/Actions.php +++ b/src/Admin/Actions.php @@ -20,7 +20,181 @@ class Actions { * @since 1.0.0 */ public function __construct() { - add_action( 'admin_post_wc_serial_numbers_edit_key', array( __CLASS__, 'handle_edit_key' ) ); + add_action( 'wp_ajax_wcsn_ajax_search', array( __CLASS__, 'handle_ajax_search' ) ); + add_action( 'admin_post_wcsn_add_key', array( __CLASS__, 'handle_add_key' ) ); + add_action( 'admin_post_wcsn_edit_key', array( __CLASS__, 'handle_edit_key' ) ); + } + + /** + * Handle ajax search. + * + * @since 1.0.0 + * @return void + */ + public static function handle_ajax_search() { + check_ajax_referer( 'wcsn_ajax_search' ); + $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; + $term = isset( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : ''; + $limit = isset( $_POST['limit'] ) ? absint( $_POST['limit'] ) : 20; + $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; + $results = array(); + $total = 0; + $offset = ( $page - 1 ) * $limit; + + switch ( $type ) { + case 'product': + $args = array_merge( + wcsn_get_products_query_args(), + array( + 'paged' => $page, + 'posts_per_page' => $limit, + 's' => $term, + 'fields' => 'ids', + ) + ); + // if the term is numeric then search by product id. + if ( is_numeric( $term ) ) { + $args['post__in'] = array( $term ); + unset( $args['s'] ); + } + + $the_query = new \WP_Query( $args ); + $product_ids = $the_query->get_posts(); + $total = $the_query->found_posts; + foreach ( $product_ids as $product_id ) { + $product = wc_get_product( $product_id ); + + if ( ! $product ) { + continue; + } + + $text = sprintf( + '(#%1$s) %2$s', + $product->get_id(), + wp_strip_all_tags( $product->get_formatted_name() ) + ); + + $results[] = array( + 'id' => $product->get_id(), + 'text' => $text, + ); + } + break; + case 'order': + $args = array( + 'paged' => $page, + 'posts_per_page' => $limit, + 's' => $term, + 'fields' => 'ids', + 'post_type' => 'shop_order', + 'post_status' => array_keys( wc_get_order_statuses() ), + ); + // if the term is numeric then search by order id. + if ( is_numeric( $term ) ) { + $args['post__in'] = array( $term ); + unset( $args['s'] ); + } + + $the_query = new \WP_Query( $args ); + $order_ids = $the_query->get_posts(); + $total = $the_query->found_posts; + foreach ( $order_ids as $order_id ) { + $order = wc_get_order( $order_id ); + + if ( ! $order ) { + continue; + } + + $text = sprintf( + '(#%1$s) %2$s - %3$s', + $order->get_id(), + wp_strip_all_tags( $order->get_formatted_billing_full_name() ), + wp_strip_all_tags( wc_format_datetime( $order->get_date_created() ) ) + ); + + $results[] = array( + 'id' => $order->get_id(), + 'text' => $text, + ); + } + break; + + case 'customer': + case 'user': + // query wp users. + $args = array( + 'paged' => $page, + 'number' => $limit, + 'search' => '*' . $term . '*', + 'search_columns' => array( 'user_login', 'user_email', 'user_nicename' ), + 'fields' => 'ID', + ); + // if the term is numeric then search by user id. + if ( is_numeric( $term ) ) { + $args['include'] = array( $term ); + unset( $args['search'] ); + } + + $user_query = new \WP_User_Query( $args ); + $user_ids = $user_query->get_results(); + $total = $user_query->get_total(); + + foreach ( $user_ids as $user_id ) { + $user = get_user_by( 'id', $user_id ); + + if ( ! $user ) { + continue; + } + + $text = sprintf( + '(#%1$s) %2$s - %3$s', + $user->ID, + wp_strip_all_tags( $user->display_name ), + wp_strip_all_tags( $user->user_email ) + ); + + $results[] = array( + 'id' => $user->ID, + 'text' => $text, + ); + } + + break; + } + + wp_send_json( + array( + 'page' => $page, + 'results' => $results, + 'pagination' => array( + 'more' => $total > ( $offset + $limit ), + ), + ) + ); + } + + /** + * Handle add key. + * + * @since 1.0.0 + * @return void + */ + public static function handle_add_key() { + check_admin_referer( 'wcsn_add_key' ); + $data = wc_clean( wp_unslash( $_POST ) ); + $key = Key::insert( $data ); + if ( is_wp_error( $key ) ) { + WCSN()->add_notice( $key->get_error_message(), 'error' ); + // redirect to referrer. + wp_safe_redirect( wp_get_referer() ); + exit(); + } + // Adding manually so let's enable to product and set the source. + $product_id = $key->get_product_id(); + update_post_meta( $product_id, '_is_serial_number', 'yes' ); + update_post_meta( $product_id, '_serial_key_source', 'custom_source' ); + $status = isset( $data['status'] ) ? $data['status'] : ''; + } /** @@ -30,7 +204,7 @@ public function __construct() { * @return void */ public static function handle_edit_key() { - check_admin_referer( 'wc_serial_numbers_edit_key' ); + check_admin_referer( 'wcsn_edit_key' ); $data = wc_clean( wp_unslash( $_POST ) ); $key = Key::insert( $data ); if ( is_wp_error( $key ) ) { diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index ef7d9ebd..bdbe4a71 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -70,6 +70,7 @@ public function enqueue_scripts( $hook ) { 'copied' => __( 'Copied', 'wc-serial-numbers' ), ), 'search_nonce' => wp_create_nonce( 'wc_serial_numbers_search_nonce' ), + 'ajax_nonce' => wp_create_nonce( 'wcsn_ajax_search' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'apiurl' => site_url( '?wc-api=serial-numbers-api' ), ) diff --git a/src/Admin/ListTables/KeysTable.php b/src/Admin/ListTables/KeysTable.php index bd770a75..401cb9fe 100644 --- a/src/Admin/ListTables/KeysTable.php +++ b/src/Admin/ListTables/KeysTable.php @@ -278,7 +278,6 @@ public function process_bulk_actions( $doaction ) { case 'reset_activations': $key->reset_activations(); break; - break; } } diff --git a/src/Admin/ListTables/ListTable.php b/src/Admin/ListTables/ListTable.php index 8a946b86..f42e3ba7 100644 --- a/src/Admin/ListTables/ListTable.php +++ b/src/Admin/ListTables/ListTable.php @@ -138,7 +138,7 @@ public function order_dropdown() { -