Skip to content

Commit

Permalink
fix order search showing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sultann committed Aug 3, 2023
1 parent a88fcca commit 4a4c398
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 30 deletions.
64 changes: 64 additions & 0 deletions assets/js/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Licensed under the GPLv2+ license.
*/
/* global jQuery, wc_serial_numbers_vars */

(function ($, window) {
'use strict';

Expand Down Expand Up @@ -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));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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+",
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
178 changes: 176 additions & 2 deletions src/Admin/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : '';

}

/**
Expand All @@ -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 ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
Expand Down
1 change: 0 additions & 1 deletion src/Admin/ListTables/KeysTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ public function process_bulk_actions( $doaction ) {
case 'reset_activations':
$key->reset_activations();
break;
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/ListTables/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function order_dropdown() {
<label for="filter-by-order-id" class="screen-reader-text">
<?php esc_html_e( 'Filter by order', 'wc-serial-numbers' ); ?>
</label>
<select class="wcsn_search_order" name="order_id" id="filter-by-order-id">
<select class="wcsn_search_order" name="order_id" id="filter-by-order-id" data-placeholder="<?php esc_attr_e( 'Filter by order', 'wc-serial-numbers' ); ?>">
<?php if ( ! empty( $order ) ) : ?>
<option selected="selected" value="<?php echo esc_attr( $order->get_id() ); ?>">
<?php echo esc_html( $order->get_formatted_billing_full_name() ); ?>
Expand Down
Loading

0 comments on commit 4a4c398

Please sign in to comment.