load_widgets();
$available_widgets = $dashboard->get_widgets();
// If there is no widget with such name, do not continue.
if ( ! in_array( $widget_id, array_keys( $available_widgets ) ) ) {
wp_send_json_error( __( 'That widget is not available', ATUM_TEXT_DOMAIN ) );
}
$widget = $available_widgets[ $widget_id ];
if ( ! is_a( $widget, '\Atum\Components\AtumWidget' ) ) {
wp_die( esc_attr__( 'Invalid widget', ATUM_TEXT_DOMAIN ) );
}
ob_start();
$grid_item_settings = $dashboard->get_widget_grid_item_defaults( $widget_id );
$dashboard->add_widget( $widget, $grid_item_settings, TRUE );
$default_widgets_layout = Dashboard::get_default_widgets_layout();
$widget_data = array(
'layout' => $default_widgets_layout[ $widget_id ],
'widget' => ob_get_clean(),
);
wp_send_json_success( $widget_data );
}
/**
* Sort the videos within the Videos Widget
*
* @package Dashboard
* @subpackage Videos Widget
*
* @since 1.4.0
*/
public function videos_widget_sorting() {
check_ajax_referer( 'atum-dashboard-widgets', 'token' );
if ( empty( $_POST['sortby'] ) ) {
wp_die( - 1 );
}
ob_start();
Helpers::load_view( 'widgets/videos', Videos::get_filtered_videos( esc_attr( $_POST['sortby'] ) ) );
wp_die( ob_get_clean() ); // WPCS: XSS ok.
}
/**
* Filter values within current stock values widget
*
* @package Dashboard
* @subpackage Current Stock Values Widget
*
* @since 1.5.0
*/
public function current_stock_values() {
check_ajax_referer( 'atum-dashboard-widgets', 'token' );
$current_stock_values = WidgetHelpers::get_items_in_stock( $_POST['categorySelected'], $_POST['productTypeSelected'] );
wp_send_json_success( compact( 'current_stock_values' ) );
}
/**
* Change the Statistics widget chart data
*
* @package Dashboard
* @subpackage Statistics Widget
*
* @since 1.4.0
*/
public function statistics_widget_chart() {
check_ajax_referer( 'atum-dashboard-widgets', 'token' );
if ( empty( $_POST['chart_data'] ) || empty( $_POST['chart_period'] ) ) {
wp_send_json_error();
}
$chart_data = esc_attr( $_POST['chart_data'] );
$chart_period = esc_attr( $_POST['chart_period'] );
switch ( $chart_data ) {
case 'sales':
$dataset = WidgetHelpers::get_sales_chart_data( $chart_period );
$legends = array(
'value' => __( 'Sales', ATUM_TEXT_DOMAIN ),
'products' => __( 'Products', ATUM_TEXT_DOMAIN ),
);
break;
case 'lost-sales':
$dataset = WidgetHelpers::get_sales_chart_data( $chart_period, [ 'lost_sales' ] );
$legends = array(
'value' => __( 'Lost Sales', ATUM_TEXT_DOMAIN ),
'products' => __( 'Products', ATUM_TEXT_DOMAIN ),
);
break;
case 'promo-sales':
$dataset = WidgetHelpers::get_promo_sales_chart_data( $chart_period );
$legends = array(
'value' => __( 'Sales', ATUM_TEXT_DOMAIN ),
'products' => __( 'Products', ATUM_TEXT_DOMAIN ),
);
break;
case 'orders':
$dataset = WidgetHelpers::get_orders_chart_data( $chart_period );
$legends = array(
'value' => __( 'Value', ATUM_TEXT_DOMAIN ),
'products' => __( 'Orders', ATUM_TEXT_DOMAIN ),
);
break;
default:
wp_send_json_error();
break;
}
if ( strpos( $chart_period, 'year' ) !== FALSE ) {
$period = 'month';
}
elseif ( strpos( $chart_period, 'month' ) !== FALSE ) {
$period = 'monthDay';
}
else {
$period = 'weekDay';
}
wp_send_json_success( compact( 'dataset', 'period', 'legends' ) );
}
/**
* Loads the Stock Central ListTable class and calls ajax_response method
*
* @package Stock Central
*
* @since 0.0.1
*/
public function fetch_stock_central_list() {
check_ajax_referer( 'atum-list-table-nonce', 'token' );
$args = array(
'per_page' => ! empty( $_REQUEST['per_page'] ) ? absint( $_REQUEST['per_page'] ) : Helpers::get_option( 'posts_per_page', Settings::DEFAULT_POSTS_PER_PAGE ),
'show_cb' => ! empty( $_REQUEST['show_cb'] ) ? (bool) $_REQUEST['show_cb'] : FALSE,
'show_controlled' => ! empty( $_REQUEST['show_controlled'] ) ? (bool) $_REQUEST['show_controlled'] : FALSE,
'screen' => esc_attr( $_REQUEST['screen'] ),
);
do_action( 'atum/ajax/stock_central_list/before_fetch_list' );
if ( ! empty( $_REQUEST['view'] ) && 'all_stock' === $_REQUEST['view'] ) {
$_REQUEST['view'] = '';
}
$namespace = '\Atum\StockCentral\Lists';
$list_class = $args['show_controlled'] ? "$namespace\ListTable" : "$namespace\UncontrolledListTable";
/**
* Variable deifinition
*
* @var ListTable $list
*/
$list = new $list_class( $args );
$list->ajax_response();
}
/**
* Loads the Inbound Stock ListTable class and calls ajax_response method
*
* @package Inbound Stock
*
* @since 1.3.0
*/
public function fetch_inbound_stock_list() {
check_ajax_referer( 'atum-list-table-nonce', 'token' );
$args = array(
'per_page' => ! empty( $_REQUEST['per_page'] ) ? absint( $_REQUEST['per_page'] ) : Helpers::get_option( 'posts_per_page', Settings::DEFAULT_POSTS_PER_PAGE ),
'screen' => $_REQUEST['screen'],
);
do_action( 'atum/ajax/inbound_stock/before_fetch_list' );
$list = new InboundStockListTable( $args );
$list->ajax_response();
}
/**
* Triggered when clicking the rating footer
*
* @package Main
*
* @since 1.2.0
*/
public function rated() {
update_option( 'atum_admin_footer_text_rated', 1 );
wp_die();
}
/**
* Update the meta values for the edited ListTable columns
*
* @package ATUM List Tables
*
* @since 1.1.2
*/
public function update_list_data() {
check_ajax_referer( 'atum-list-table-nonce', 'token' );
if ( empty( $_POST['data'] ) ) {
wp_send_json_error( __( 'Error saving the table data.', ATUM_TEXT_DOMAIN ) );
}
try {
// Disable cache to avoid saving the wrong data.
$was_cache_disabled = AtumCache::is_cache_disabled();
if ( ! $was_cache_disabled ) {
AtumCache::disable_cache();
}
$data = json_decode( stripslashes( $_POST['data'] ), TRUE );
if ( empty( $data ) ) {
wp_send_json_error( __( 'Error saving the table data.', ATUM_TEXT_DOMAIN ) );
}
$data = apply_filters( 'atum/ajax/before_update_product_meta', $data );
foreach ( $data as $product_id => &$product_meta ) {
Helpers::update_product_data( $product_id, $product_meta );
}
// If the first edit notice was already shown, save it as user meta.
if ( ! empty( $_POST['first_edit_key'] ) ) {
update_user_meta( get_current_user_id(), esc_attr( $_POST['first_edit_key'] ), 1 );
}
do_action( 'atum/ajax/after_update_list_data', $data );
if ( ! $was_cache_disabled ) {
AtumCache::enable_cache();
}
wp_send_json_success( __( 'Data saved.', ATUM_TEXT_DOMAIN ) );
} catch ( \Exception $e ) {
wp_send_json_error( $e->getMessage() );
}
}
/**
* Apply actions in bulk to the selected ListTable rows
*
* @package ATUM List Tables
*
* @since 1.4.1
*/
public function apply_bulk_action() {
check_ajax_referer( 'atum-list-table-nonce', 'token' );
if ( empty( $_POST['ids'] ) ) {
wp_send_json_error( __( 'No Items Selected.', ATUM_TEXT_DOMAIN ) );
}
if ( empty( $_POST['bulk_action'] ) ) {
wp_send_json_error( __( 'Invalid bulk action.', ATUM_TEXT_DOMAIN ) );
}
$ids = array_map( 'absint', $_POST['ids'] );
switch ( $_POST['bulk_action'] ) {
case 'uncontrol_stock':
foreach ( $ids as $id ) {
Helpers::update_atum_control( $id, 'disable' );
}
break;
case 'control_stock':
foreach ( $ids as $id ) {
Helpers::update_atum_control( $id );
}
break;
case 'unmanage_stock':
foreach ( $ids as $id ) {
Helpers::update_wc_manage_stock( $id, 'disable' );
}
break;
case 'manage_stock':
foreach ( $ids as $id ) {
Helpers::update_wc_manage_stock( $id );
}
break;
}
wp_send_json_success( __( 'Action applied to the selected products successfully.', ATUM_TEXT_DOMAIN ) );
}
/**
* Control all the shop products at once from the List Tables' button
*
* @package ATUM List Tables
*
* @since 1.4.10
*/
public function control_all_products() {
check_ajax_referer( 'atum-control-all-products-nonce', 'token' );
$this->change_status_meta( Globals::ATUM_CONTROL_STOCK_KEY, 'yes' );
}
/**
* Validate an addon license key through API
*
* @package Add-ons
*
* @since 1.2.0
*/
public function validate_license() {
$this->check_license_post_data();
$addon_name = esc_attr( $_POST['addon'] );
$key = esc_attr( $_POST['key'] );
if ( ! $addon_name || ! $key ) {
wp_send_json_error( __( 'An error occurred, please try again later.', ATUM_TEXT_DOMAIN ) );
}
$error_message = __( 'This license is not valid.', ATUM_TEXT_DOMAIN );
// Validate the license through API.
$response = Addons::check_license( $addon_name, $key );
if ( is_wp_error( $response ) ) {
wp_send_json_error( __( 'ATUM API error', ATUM_TEXT_DOMAIN ) );
}
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
switch ( $license_data->license ) {
case 'valid':
// Save the valid license.
Addons::update_key( $addon_name, array(
'key' => $key,
'status' => 'valid',
) );
// Delete status transient.
Addons::delete_status_transient( $addon_name );
wp_send_json_success( __( 'Your add-on license was saved.', ATUM_TEXT_DOMAIN ) );
break;
case 'inactive':
case 'site_inactive':
Addons::update_key( $addon_name, array(
'key' => $key,
'status' => 'inactive',
) );
// Delete status transient.
Addons::delete_status_transient( $addon_name );
if ( $license_data->activations_left < 1 ) {
wp_send_json_error( __( "You've reached your license activation limit for this add-on.
Please contact the Stock Management Labs support team.", ATUM_TEXT_DOMAIN ) );
}
$licenses_after_activation = $license_data->activations_left - 1;
wp_send_json( array(
'success' => 'activate',
'data' => sprintf(
/* translators: the number of remaininig licenses */
_n(
'Your license is valid.
After the activation you will have %s remaining license.
Please, click the button to activate.',
'Your license is valid.
After the activation you will have %s remaining licenses.
Please, click the button to activate.',
$licenses_after_activation,
ATUM_TEXT_DOMAIN
),
$licenses_after_activation
),
) );
break;
case 'expired':
$error_message = sprintf(
/* translators: the expiration date */
__( 'Your license key expired on %s.', ATUM_TEXT_DOMAIN ),
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
);
break;
case 'disabled':
$error_message = __( 'This license has been disabled', ATUM_TEXT_DOMAIN );
break;
}
Addons::update_key( $addon_name, array(
'key' => $key,
'status' => 'invalid',
) );
// Delete status transient.
Addons::delete_status_transient( $addon_name );
wp_send_json_error( $error_message );
}
/**
* First check before validating|activating|deactivating an addon license
*
* @package Add-ons
*
* @since 1.2.0
*/
private function check_license_post_data() {
check_ajax_referer( ATUM_PREFIX . 'manage_license', 'token' );
if ( empty( $_POST['addon'] ) ) {
wp_send_json_error( __( 'No addon name provided', ATUM_TEXT_DOMAIN ) );
}
if ( empty( $_POST['key'] ) ) {
wp_send_json_error( __( 'Please enter a valid addon license key', ATUM_TEXT_DOMAIN ) );
}
}
/**
* Activate an addon license key through API
*
* @package Add-ons
*
* @since 1.2.0
*/
public function activate_license() {
$this->check_license_post_data();
$addon_name = esc_attr( $_POST['addon'] );
$key = esc_attr( $_POST['key'] );
$default_error = __( 'An error occurred, please try again later.', ATUM_TEXT_DOMAIN );
if ( ! $addon_name || ! $key ) {
wp_send_json_error( $default_error );
}
$response = Addons::activate_license( $addon_name, $key );
// Make sure the response came back okay.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$message = is_wp_error( $response ) ? $response->get_error_message() : $default_error;
}
else {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( FALSE === $license_data->success ) {
switch ( $license_data->error ) {
case 'expired':
$message = sprintf(
/* translators: the expiration date */
__( 'Your license key expired on %s.', ATUM_TEXT_DOMAIN ),
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
);
break;
case 'revoked':
$message = __( 'Your license key has been disabled.', ATUM_TEXT_DOMAIN );
break;
case 'missing':
$message = __( 'Invalid license.', ATUM_TEXT_DOMAIN );
break;
case 'invalid':
case 'site_inactive':
$message = __( 'Your license is not active for this URL.', ATUM_TEXT_DOMAIN );
break;
case 'item_name_mismatch':
/* translators: the add-on name */
$message = sprintf( __( 'This appears to be an invalid license key for %s.', ATUM_TEXT_DOMAIN ), $addon_name );
break;
case 'no_activations_left':
$message = __( 'Your license key has reached its activation limit.', ATUM_TEXT_DOMAIN );
break;
default:
$message = $default_error;
break;
}
}
}
if ( ! empty( $message ) ) {
wp_send_json_error( $message );
}
// Update the key in database.
if ( ! empty( $license_data ) ) {
Addons::update_key( $addon_name, array(
'key' => $key,
'status' => $license_data->license,
) );
// Delete status transient.
Addons::delete_status_transient( $addon_name );
if ( 'valid' === $license_data->license ) {
wp_send_json_success( __( 'Your license has been activated.', ATUM_TEXT_DOMAIN ) );
}
}
wp_send_json_error( $default_error );
}
/**
* Deactivate an addon license key through API
*
* @package Add-ons
*
* @since 1.2.0
*/
public function deactivate_license() {
$this->check_license_post_data();
$addon_name = esc_attr( $_POST['addon'] );
$key = esc_attr( $_POST['key'] );
$default_error = __( 'An error occurred, please try again later.', ATUM_TEXT_DOMAIN );
if ( ! $addon_name || ! $key ) {
wp_send_json_error( $default_error );
}
$response = Addons::deactivate_license( $addon_name, $key );
// Make sure the response came back okay.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$message = ( is_wp_error( $response ) ) ? $response->get_error_message() : $default_error;
wp_send_json_error( $message );
}
// Decode the license data.
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
// $license_data->license will be either "deactivated" or "failed" or "limit_reached".
if ( 'deactivated' === $license_data->license ) {
// Update the key status.
Addons::update_key( $addon_name, array(
'key' => $key,
'status' => 'inactive',
) );
// Delete status transient.
Addons::delete_status_transient( $addon_name );
wp_send_json_success( __( 'Your license has been deactivated.', ATUM_TEXT_DOMAIN ) );
}
elseif ( 'limit_reached' === $license_data->license ) {
wp_send_json_error( sprintf(
/* translators: first one is the Ticksy link and the second is the link closing tag */
__( "You've reached the limit of allowed deactivations for this license. Please %1\$sopen a support ticket%2\$s to request the deactivation.", ATUM_TEXT_DOMAIN ),
'',
''
) );
}
wp_send_json_error( $default_error );
}
/**
* Install an addon from the addons page
*
* @package Add-ons
*
* @since 1.2.0
*/
public function install_addon() {
$this->check_license_post_data();
$addon_name = esc_attr( $_POST['addon'] );
$addon_slug = esc_attr( $_POST['slug'] );
$key = esc_attr( $_POST['key'] );
$default_error = __( 'An error occurred, please try again later.', ATUM_TEXT_DOMAIN );
if ( ! $addon_name || ! $addon_slug || ! $key ) {
wp_send_json_error( $default_error );
}
$response = Addons::get_version( $addon_name, $key, '0.0' );
// Make sure the response came back okay.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$message = ( is_wp_error( $response ) ) ? $response->get_error_message() : $default_error;
wp_send_json_error( $message );
}
// Decode the license data.
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( $license_data->download_link ) {
Addons::delete_status_transient( $addon_name );
/* @noinspection PhpUnhandledExceptionInspection */
$result = Addons::install_addon( $addon_name, $addon_slug, $license_data->download_link );
wp_send_json( $result );
}
wp_send_json_error( $default_error );
}
/**
* Dismiss the ATUM notices
*
* @package Helpers
*
* @since 1.4.4
*/
public function dismiss_notice() {
check_ajax_referer( 'dismiss-atum-notice', 'token' );
if ( ! empty( $_POST['key'] ) ) {
Helpers::dismiss_notice( esc_attr( $_POST['key'] ) );
}
wp_die();
}
/**
* If the site is not using the new tables, use the legacy methods
*
* @since 1.5.0
* @deprecated Only for backwards compatibility and will be removed in a future version.
*/
use AjaxLegacyTrait;
/**
* Seach for products from enhanced selects
*
* @package ATUM Orders
*
* @since 1.3.7
*/
public function search_products() {
/**
* If the site is not using the new tables, use the legacy method
*
* @since 1.5.0
* @deprecated Only for backwards compatibility and will be removed in a future version.
*/
if ( ! Helpers::is_using_new_wc_tables() ) {
$this->search_products_legacy();
return;
}
check_ajax_referer( 'search-products', 'security' );
ob_start();
$term = stripslashes( $_GET['term'] );
if ( empty( $term ) ) {
wp_die();
}
global $wpdb;
$like_term = '%%' . $wpdb->esc_like( $term ) . '%%';
$post_types = apply_filters( 'atum/ajax/search_products/searched_post_types', [ 'product', 'product_variation' ] );
$post_statuses = current_user_can( 'edit_private_products' ) ? [ 'private', 'publish' ] : [ 'publish' ];
$meta_join = $meta_where = array();
$type_where = '';
// Search by SKU.
$meta_join[] = "LEFT JOIN {$wpdb->prefix}wc_products wcd ON posts.ID = wcd.product_id";
$meta_where[] = $wpdb->prepare( 'OR wcd.sku LIKE %s', $like_term );
// Search by Supplier SKU.
$atum_data_table = $wpdb->prefix . Globals::ATUM_PRODUCT_DATA_TABLE;
$meta_join[] = "LEFT JOIN $atum_data_table apd ON posts.ID = apd.product_id";
$meta_where[] = $wpdb->prepare( 'OR apd.supplier_sku LIKE %s', $like_term );
// Exclude variable products from results.
$excluded_types = (array) apply_filters( 'atum/ajax/search_products/excluded_product_types', array_diff( Globals::get_inheritable_product_types(), [ 'grouped', 'bundle' ] ) );
if ( ! empty( $excluded_types ) ) {
$type_where = "AND posts.ID NOT IN (
SELECT wpd1.product_id FROM {$wpdb->prefix}wc_products wpd1
WHERE wpd1.type IN ('" . implode( "','", $excluded_types ) . "')
)";
}
$query_select = "SELECT DISTINCT posts.ID FROM $wpdb->posts posts " . implode( "\n", $meta_join ) . ' ';
$where_clause = $wpdb->prepare( '
WHERE (
posts.post_title LIKE %s
OR posts.post_content LIKE %s
' . implode( "\n", $meta_where ) . "
)
AND posts.post_type IN ('" . implode( "','", $post_types ) . "')
AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "')
" . $type_where . ' ',
$like_term,
$like_term
); // WPCS: unprepared SQL ok.
$query_select = apply_filters( 'atum/product_levels/ajax/search_products/select', $query_select );
$where_clause = apply_filters( 'atum/product_levels/ajax/search_products/where', $where_clause );
$query = "$query_select $where_clause
ORDER BY posts.post_parent ASC, posts.post_title ASC";
$product_ids = $wpdb->get_col( $query ); // WPCS: unprepared SQL ok.
if ( is_numeric( $term ) ) {
$post_id = absint( $term );
$post_type = get_post_type( $post_id );
if ( 'product_variation' === $post_type ) {
$product_ids[] = $post_id;
}
elseif ( 'product' === $post_type ) {
$product_ids[] = $post_id;
}
$product_ids[] = wp_get_post_parent_id( $post_id );
}
$ids = wp_parse_id_list( $product_ids );
if ( ! empty( $_GET['exclude'] ) ) {
$ids = array_diff( $ids, (array) $_GET['exclude'] );
}
$included = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) $_GET['include'] ) : array();
$url = wp_parse_url( wp_get_referer() );
parse_str( $url['query'], $url_query );
if ( ! empty( $url_query['post'] ) ) {
/**
* Variable definition
*
* @var PurchaseOrder $po
*/
$po = Helpers::get_atum_order_model( absint( $url_query['post'] ) );
// The Purchase Orders only should allow products from the current PO's supplier (if such PO only allows 1 supplier).
if ( is_a( $po, '\Atum\PurchaseOrders\Models\PurchaseOrder' ) && ! $po->has_multiple_suppliers() ) {
$supplier_products = apply_filters( 'atum/ajax/search_products/included_search_products', Suppliers::get_supplier_products( $po->get_supplier( 'id' ), [ 'product', 'product_variation' ], FALSE ) );
// If the PO supplier has no linked products, it must return an empty array.
if ( empty( $supplier_products ) ) {
$ids = $included = array();
}
else {
$included = array_merge( $included, $supplier_products );
}
}
}
if ( ! empty( $included ) ) {
$ids = array_intersect( $ids, $included );
}
if ( ! empty( $_GET['limit'] ) ) {
$ids = array_slice( $ids, 0, absint( $_GET['limit'] ) );
}
$product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_editable' );
$products = array();
foreach ( $product_objects as $product_object ) {
/**
* Variable definition
*
* @var \WC_Product $product_object
*/
$products[ $product_object->get_id() ] = rawurldecode( $product_object->get_formatted_name() );
}
wp_send_json( apply_filters( 'atum/ajax/search_products/json_search_found_products', $products ) );
}
/**
* Seach for WooCommerce orders from enhanced selects
*
* @package ATUM Orders
*
* @since 1.2.4
*/
public function search_wc_orders() {
check_ajax_referer( 'search-products', 'security' );
ob_start();
$order_id = absint( $_GET['term'] );
if ( empty( $order_id ) ) {
wp_die();
}
// Get all the orders with IDs starting with the provided number.
global $wpdb;
$max_results = absint( apply_filters( 'atum/ajax/search_wc_orders/max_results', 10 ) );
$query = $wpdb->prepare(
"SELECT DISTINCT ID from {$wpdb->posts} WHERE post_type = 'shop_order'
AND post_status IN ('" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "')
AND ID LIKE %s LIMIT %d",
"$order_id%",
$max_results
); // WPCS: unprepared SQL ok.
$order_ids = $wpdb->get_col( $query ); // WPCS: unprepared SQL ok.
if ( empty( $order_ids ) ) {
wp_die();
}
$order_results = array();
foreach ( $order_ids as $order_id ) {
$order_results[ $order_id ] = __( 'Order #', ATUM_TEXT_DOMAIN ) . $order_id;
}
wp_send_json( $order_results );
}
/**
* Seach for Suppliers from enhanced selects
*
* @package Suppliers
*
* @since 1.2.9
*/
public function search_suppliers() {
check_ajax_referer( 'search-products', 'security' );
global $wpdb;
ob_start();
$where = '';
if ( is_numeric( $_GET['term'] ) ) {
$supplier_id = absint( $_GET['term'] );
$where = "AND ID LIKE $supplier_id";
}
elseif ( ! empty( $_GET['term'] ) ) {
$supplier_name = $wpdb->esc_like( $_GET['term'] );
$where = "AND post_title LIKE '%%{$supplier_name}%%'";
}
else {
wp_die();
}
// Get all the orders with IDs starting with the provided number.
$max_results = absint( apply_filters( 'atum/ajax/search_suppliers/max_results', 10 ) );
$post_statuses = AtumCapabilities::current_user_can( 'edit_private_suppliers' ) ? [ 'private', 'publish' ] : [ 'publish' ];
$query = $wpdb->prepare(
"SELECT DISTINCT ID, post_title from $wpdb->posts
WHERE post_type = %s $where
AND post_status IN ('" . implode( "','", $post_statuses ) . "')
LIMIT %d",
Suppliers::POST_TYPE,
$max_results
); // WPCS: unprepared SQL ok.
$suppliers = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
if ( empty( $suppliers ) ) {
wp_die();
}
$supplier_results = array();
foreach ( $suppliers as $supplier ) {
$supplier_results[ $supplier->ID ] = $supplier->post_title;
}
wp_send_json( $supplier_results );
}
/**
* Add a note to an ATUM Order
*
* @package ATUM Orders
*
* @since 1.2.4
*/
public function add_atum_order_note() {
check_ajax_referer( 'add-atum-order-note', 'security' );
if ( ! AtumCapabilities::current_user_can( 'create_order_notes' ) ) {
wp_die( -1 );
}
$post_id = absint( $_POST['post_id'] );
$note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) );
if ( $post_id ) {
$atum_order = Helpers::get_atum_order_model( $post_id );
if ( ! is_wp_error( $atum_order ) ) {
$comment_id = $atum_order->add_note( $note );
?>