diff --git a/aftership-woocommerce-tracking.php b/aftership-woocommerce-tracking.php index dfdabc0..77513d6 100644 --- a/aftership-woocommerce-tracking.php +++ b/aftership-woocommerce-tracking.php @@ -3,7 +3,7 @@ * Plugin Name: AfterShip Tracking - All-In-One WooCommerce Order Tracking (Free plan available) * Plugin URI: http://aftership.com/ * Description: Track orders in one place. shipment tracking, automated notifications, order lookup, branded tracking page, delivery day prediction - * Version: 1.17.6 + * Version: 1.17.7 * Author: AfterShip * Author URI: http://aftership.com * @@ -20,7 +20,7 @@ require_once( 'woo-includes/woo-functions.php' ); -define( 'AFTERSHIP_VERSION', '1.17.6' ); +define( 'AFTERSHIP_VERSION', '1.17.7' ); define( 'AFTERSHIP_PATH', dirname( __FILE__ ) ); define( 'AFTERSHIP_ASSETS_URL', plugins_url() . '/' . basename( AFTERSHIP_PATH ) ); define( 'AFTERSHIP_SCRIPT_TAGS', 'aftership_script_tags' ); @@ -216,6 +216,7 @@ public function __construct() { } // Get tracking number from order notes created by restful api like royalmail. add_action( 'woocommerce_rest_insert_order_note', array( $this->actions, 'handle_woocommerce_rest_insert_order_note' ), 10, 2 ); + add_action('woocommerce_order_note_added', array( $this->actions, 'handle_woocommerce_insert_order_note' ), 10, 2 ); add_filter( 'rest_shop_order_collection_params', array( $this->actions, 'add_collection_params' ), 10, 1 ); add_filter( 'rest_shop_coupon_collection_params', array( $this->actions, 'add_collection_params' ), 10, 1 ); diff --git a/includes/api/aftership/v1/class-am-rest-settings-controller.php b/includes/api/aftership/v1/class-am-rest-settings-controller.php index d5338fc..36d1df2 100644 --- a/includes/api/aftership/v1/class-am-rest-settings-controller.php +++ b/includes/api/aftership/v1/class-am-rest-settings-controller.php @@ -109,6 +109,11 @@ public function create_or_update_settings( WP_REST_Request $data ) { $options['enable_import_tracking'] = $data['enable_import_tracking']; } + // save notes to meta, value: 1 or -1 + if ( isset( $data['save_notes_to_meta_data'] ) && $data['save_notes_to_meta_data'] ) { + $options['save_notes_to_meta_data'] = $data['save_notes_to_meta_data']; + } + if ( isset( $data['show_orders_actions'] ) && $data['show_orders_actions'] ) { if ( '' === $this->seek_option_value( $options, 'show_orders_actions' ) ) { $options['show_orders_actions'] = $data['show_orders_actions']; diff --git a/includes/class-aftership-actions.php b/includes/class-aftership-actions.php index 8e88217..82667e9 100644 --- a/includes/class-aftership-actions.php +++ b/includes/class-aftership-actions.php @@ -1334,8 +1334,47 @@ public function handle_woocommerce_rest_insert_order_note($comment, $request) { $this->add_tracking_item( $order->get_id(), array( 'tracking_number' => $tracking['tracking_number'], 'slug' => $tracking['slug'] ) ); $order->set_date_modified( current_time( 'mysql' ) ); $order->save(); + if ($this->save_notes_to_meta_data_enabled()) { + try { + $order_id = $request['order_id']; + $order_notes = $this->get_order_notes($order_id); + $order = new WC_Order( $order_id ); + $order->update_meta_data( '_aftership_order_notes', $order_notes ); + $order->save_meta_data(); + }catch ( Exception $e) { + return; + } + } + } + + /* + * Handle order comment events send by restful api call. + */ + public function handle_woocommerce_insert_order_note($comment_id, $order) { + if ($this->save_notes_to_meta_data_enabled()) { + try { + $order_id = $order->get_id(); + $order_notes = $this->get_order_notes($order_id); + $order->update_meta_data( '_aftership_order_notes', $order_notes ); + $order->save_meta_data(); + }catch ( Exception $e) { + return; + } + } } + public function save_notes_to_meta_data_enabled() { + $options = get_option( 'aftership_option_name' ); + if ( ! $options ) { + return false; + } + $enable = isset( $options['save_notes_to_meta_data'] ) ? $options['save_notes_to_meta_data'] : -1; + if ( $enable !== 1 ) { + return false; + } + return true; + } + /** * Parse tracking from order note. */ @@ -1354,6 +1393,50 @@ private function get_tracking_from_note ($note) { $tracking['tracking_number'] = $matches[1]; return $tracking; } - return null; + $home = get_home_url(); + if (strpos($home, "99jersey.com") !== false) { + if (strpos($note, "tracking number") !== false) { + $html = stripslashes($note); + $pattern = '/]*>(.*?)<\/a>/i'; + preg_match($pattern, $html, $matches); + if (empty($matches[0])) { + return null; + } + $tracking['tracking_number'] = $matches[0]; + return $tracking; + } + } + return null; + } + + /** + * Get Order Notes + * + * @param $order_id string + * @return array + */ + private function get_order_notes( $order_id ) { + $args = array( + 'post_id' => $order_id, + 'approve' => 'approve', + 'type' => 'order_note', + ); + + remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); + $notes = get_comments( $args ); + add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); + + $order_notes = array(); + + foreach ( $notes as $note ) { + $order_notes[] = [ + "author" => $note->comment_author, + "date_created_gmt" => $note->comment_date_gmt, + "note" => $note->comment_content, + 'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ), + ]; + } + + return $order_notes; } } diff --git a/includes/class-aftership-settings.php b/includes/class-aftership-settings.php index 76978c0..a926ea5 100644 --- a/includes/class-aftership-settings.php +++ b/includes/class-aftership-settings.php @@ -137,6 +137,14 @@ public function page_init() { 'aftership_setting_section_id' ); + add_settings_field( + 'save_notes_to_meta_data', + '', + array( $this, 'save_notes_to_meta_data_callback' ), + 'aftership-setting-admin', + 'aftership_setting_section_id' + ); + add_settings_field( $this->dom_id_show_order_actions, '', @@ -177,6 +185,12 @@ public function sanitize( $input ) { } } + if ( isset( $input['save_notes_to_meta_data'] ) ) { + if ($input['save_notes_to_meta_data'] == 'on' || $input['save_notes_to_meta_data'] === true || intval($input['save_notes_to_meta_data']) === 1 ) { + $new_input['save_notes_to_meta_data'] = 1; + } + } + if ( isset( $input['show_orders_actions'] ) ) { $new_input['show_orders_actions'] = sanitize_text_field( $input['show_orders_actions'] ); } @@ -244,6 +258,16 @@ public function enable_import_tracking_callback() { ); } + /** + * Call this func before shown on pages. + */ + public function save_notes_to_meta_data_callback() { + printf( + '
Parse tracking from order notes
', + ( isset( $this->options['save_notes_to_meta_data'] ) && 1 === $this->options['save_notes_to_meta_data'] ) ? 'checked="checked"' : '' + ); + } + /** * Call this func before shown on pages. */ diff --git a/readme.txt b/readme.txt index 65038d7..639aa29 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://www.aftership.com/ Tags: woocommerce shipping,woocommerce tracking,shipment tracking,order tracking, woocommerce,track order,dhl,ups,usps,fedex,shipping,tracking,order Requires at least: 2.9 Tested up to: 6.3 -Stable tag: 1.17.6 +Stable tag: 1.17.7 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html