Plugin Directory

Changeset 3035681


Ignore:
Timestamp:
02/14/2024 01:55:36 PM (2 years ago)
Author:
SmartSend
Message:

tagging version 8.1.0

Location:
smart-send-logistics
Files:
5 edited
5 copied

Legend:

Unmodified
Added
Removed
  • smart-send-logistics/tags/8.1.0/includes/class-ss-shipping-wc-order.php

    r2747067 r3035681  
    44    exit; // Exit if accessed directly
    55}
     6
     7use Automattic\WooCommerce\Utilities\OrderUtil;
     8use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
     9use WP_Post;
     10use WooCommerce\Classes\WC_Order;
    611
    712/**
     
    1722    class SS_Shipping_WC_Order
    1823    {
     24        const ADMIN_FLASH_MESSAGE_OPTION_KEY = '_ss_shipping_bulk_action_confirmation';
    1925
    2026        protected $label_prefix = 'smart-send-label-';
     
    2531        public function __construct()
    2632        {
    27 
    2833            $this->define_constants();
    2934            $this->init_hooks();
     
    4348        }
    4449
    45         /**
    46          * Init hooks
    47          */
    48         public function init_hooks()
    49         {
    50 
     50        protected function init_hooks()
     51        {
    5152            // Order page metabox actions
    52             add_action('add_meta_boxes', array($this, 'add_meta_box'), 20);
     53            add_action('add_meta_boxes', array($this, 'add_smart_send_order_meta_box'), 20);
    5354            add_action('wp_ajax_ss_shipping_generate_label', array($this, 'generate_label'));
    5455
     
    6869
    6970            // add bulk actions to the Orders screen table bulk action drop-downs
    70             add_action('admin_footer-edit.php', array($this, 'add_order_bulk_actions'));
    71 
    72             // process orders bulk actions
    73             add_action('load-edit.php', array($this, 'process_orders_bulk_actions'));
     71            $this->register_bulk_order_actions();
    7472
    7573            // display admin notices for bulk actions
    76             add_action('admin_notices', array($this, 'render_messages'));
     74            add_action('admin_notices', array($this, 'render_admin_messages'));
     75        }
     76
     77        /**
     78         * Register bulk order actions.
     79         * @see https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/
     80         * @since WordPress 4.7.0
     81         * @return void
     82         */
     83        private function register_bulk_order_actions()
     84        {
     85            $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
     86                ? 'woocommerce_page_wc-orders' // function not available wc_get_page_screen_id( 'shop-order' )
     87                : 'shop_order';
     88
     89            // An actions in the dropdown
     90            add_filter("bulk_actions-{$screen}", array($this, 'add_bulk_order_actions'));
     91
     92            // Handling the form submission
     93            add_filter("handle_bulk_actions-{$screen}", array($this, 'handle_bulk_order_actions'), 10, 3);
     94        }
     95
     96        public function add_bulk_order_actions($bulk_actions)
     97        {
     98            return array_merge($bulk_actions, $this->get_bulk_actions());
     99        }
     100
     101        /**
     102         * Processed the selected bulk action.
     103         *
     104         * Note that this function is not called if no items are selected in the table.
     105         *
     106         * @param string $sendback
     107         * @param string $doaction
     108         * @param array $items
     109         * @return string|void
     110         */
     111        public function handle_bulk_order_actions(string $sendback, string $doaction, array $items)
     112        {
     113            if (! in_array($doaction, array_keys($this->get_bulk_actions()))) {
     114                return;
     115            }
     116
     117            $array_messages = array();
     118            // $array_shipments = array();
     119            $array_messages_success = array();
     120            $array_messages_error = array();
     121            $array_shipment_ids = array();
     122
     123            if ('ss_shipping_label_bulk' === $doaction || 'ss_shipping_return_bulk' === $doaction) {
     124
     125                // Determine if the request is for a return label
     126                $return = ('ss_shipping_return_bulk' === $doaction);
     127
     128                // Trigger an admin notice to have the user manually open a print window
     129                $is_error = false;
     130                $orders_count = count($items);
     131
     132                if ($orders_count < 1) {
     133                    array_push($array_messages, array(
     134                        'message' => __('No orders selected, please select the orders to create labels for.',
     135                            'smart-send-logistics'),
     136                        'type'    => 'error',
     137                    ));
     138                } elseif ($orders_count > 5) {
     139                    array_push($array_messages, array(
     140                        'message' => __('For now it is not possible to create labels for more than 5 orders at a time.',
     141                            'smart-send-logistics'),
     142                        'type'    => 'error',
     143                    ));
     144                } else {
     145
     146                    // Ensure the selected orders have a Smart Send Shipping method
     147                    foreach ($items as $order_id) {
     148                        $order = wc_get_order($order_id);
     149
     150                        $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     151
     152                        if (!empty($ss_shipping_method_id)) {
     153
     154                            $response = $this->create_label_for_single_order_maybe_return($order_id, $return, true);
     155
     156                            foreach ($response as $key => $value) {
     157
     158                                if (isset($value['success'])) {
     159                                    array_push($array_messages_success, array(
     160                                        'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
     161                                                $order->get_order_number()) . ': '
     162                                            . (empty($value['success']->woocommerce['return']) ?
     163                                                __('Shipping label created by Smart Send',
     164                                                    'smart-send-logistics') : __('Return label created by Smart Send',
     165                                                    'smart-send-logistics'))
     166                                            . ': ' . $this->get_ss_shipping_label_link($value['success']->woocommerce['label_url'],
     167                                                !empty($value['success']->woocommerce['return'])),
     168                                        'type'    => 'success',
     169                                    ));
     170
     171                                    array_push($array_shipment_ids, array(
     172                                        'shipment_id' => $value['success']->shipment_id,
     173                                        'order_id'    => $order->get_order_number(),
     174                                    ));
     175
     176                                } else {
     177                                    // Print error message
     178                                    $message = sprintf(__('Order #%s', 'smart-send-logistics'),
     179                                            $order->get_order_number()) . ': ' . $value['error'];
     180
     181                                    array_push($array_messages_error, array(
     182                                        'message' => $message,
     183                                        'type'    => 'error',
     184                                    ));
     185                                }
     186                            }
     187
     188                        } else {
     189                            array_push($array_messages_error, array(
     190                                'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
     191                                        $order->get_order_number()) . ': ' . __('The selected order did not include a Send Smart shipping method',
     192                                        'smart-send-logistics'),
     193                                'type'    => 'error',
     194                            ));
     195                        }
     196                    }
     197
     198                    $array_combo_messages = $this->create_combo_file($array_messages_success, $array_messages_error,
     199                        $array_shipment_ids);
     200
     201                    $array_messages = array_merge($array_messages, $array_combo_messages);
     202
     203                }
     204
     205                $this->add_admin_flash_messages(get_current_user_id(), $array_messages);
     206            }
     207
     208            return $sendback;
    77209        }
    78210
     
    80212         * Add the meta box for shipment info on the order page
    81213         */
    82         public function add_meta_box()
    83         {
    84             global $woocommerce, $post;
    85             $order_id = $post->ID;
     214        public function add_smart_send_order_meta_box()
     215        {
     216            // @see https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#audit-for-order-administration-screen-functions
     217            $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
     218                ? wc_get_page_screen_id( 'shop-order' )
     219                : 'shop_order';
     220
     221            add_meta_box(
     222                'woocommerce-ss-shipping-label',
     223                __('Smart Send Shipping', 'smart-send-logistics'),
     224                array($this, 'render_smart_send_order_meta_box'),
     225                $screen,
     226                'side',
     227                'default'
     228            );
     229        }
     230
     231        /**
     232         * Render the content of the order meta box.
     233         *
     234         * @param WP_Post|WC_Order $post_or_order_object
     235         * @return void
     236         */
     237        public function render_smart_send_order_meta_box($post_or_order_object)
     238        {
     239            /** @var WC_Order $order */
     240            $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object;
     241
     242            // ... rest of the code. $post_or_order_object should not be used directly below this point´
     243
     244            $order_id = $order->ID;
     245
     246            $shipping_ss_settings = SS_SHIPPING_WC()->get_ss_shipping_settings();
    86247
    87248            $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     249
    88250            // Only display Smart Shipping (SS) meta box is SS selected as shipping method OR free shipping is set to SS method
    89             if (!empty($ss_shipping_method_id)) {
    90 
    91                 add_meta_box('woocommerce-ss-shipping-label', __('Smart Send Shipping', 'smart-send-logistics'),
    92                     array($this, 'meta_box'), 'shop_order', 'side', 'default');
    93             } else {
    94                 add_meta_box('woocommerce-ss-shipping-label', __('Smart Send Shipping', 'smart-send-logistics'),
    95                     array($this, 'meta_box_non_smart_send'), 'shop_order', 'side', 'default');
    96 
    97             }
    98         }
    99 
    100         /**
    101          * Show the meta box for shipment info on the order page
    102          *
    103          * This meta box is shown when the order does not have a Smart Send shipping method
    104          */
    105         public function meta_box_non_smart_send()
    106         {
    107             global $woocommerce, $post;
    108             $order_id = $post->ID;
    109             echo '<p>' . __('Order placed with a shipping method that is not from the Smart Send plugin',
    110                     'smart-send-logistics') . '</p>';
    111         }
    112 
    113         /**
    114          * Show the meta box for shipment info on the order page
    115          *
    116          * This meta box is shown when the order have a Smart Send shipping method
    117          */
    118         public function meta_box()
    119         {
    120             global $woocommerce, $post;
    121             $order_id = $post->ID;
    122             $order = wc_get_order($order_id);
    123 
    124             $shipping_ss_settings = SS_SHIPPING_WC()->get_ss_shipping_settings();
    125 
    126             $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     251            if (! $ss_shipping_method_id) {
     252                echo '<p>' . __('Order placed with a shipping method that is not from the Smart Send plugin',
     253                        'smart-send-logistics') . '</p>';
     254
     255                return;
     256            }
     257
    127258            $ss_shipping_method_name = SS_SHIPPING_WC()->get_shipping_method_name_from_all_shipping_method_instances($ss_shipping_method_id);
    128259
     
    316447                                    return 'postnord_commercial';
    317448                                } elseif (stripos($shipping_method_id, '_privatehome') !== false) {
    318                                     $vc_aio_options = get_post_meta($order_id, '_vc_aio_options');
     449                                    $order          = wc_get_order( $order_id );
     450                                    $vc_aio_options = $order->get_meta( '_vc_aio_options', true );
    319451                                    $flexDelivery = false;
    320452                                    $flexDeliveryOption = false;
     
    371503         */
    372504        public function filter_update_agent_meta($check, $meta_id, $meta_value, $meta_key) {
     505
    373506            if ($meta_key == 'ss_shipping_order_agent_no') {
    374507                $meta = get_metadata_by_mid( 'post', $meta_id );
     
    720853        public function save_ss_shipping_order_parcels($order_id, $parcels)
    721854        {
    722             update_post_meta($order_id, 'ss_shipping_order_parcels', $parcels);
     855            $order = wc_get_order($order_id);
     856            $order->update_meta_data('ss_shipping_order_parcels', $parcels);
     857            $order->save();
    723858        }
    724859
     
    733868        public function get_ss_shipping_order_parcels($order_id)
    734869        {
    735             return get_post_meta($order_id, 'ss_shipping_order_parcels', true);
     870            $order = wc_get_order( $order_id );
     871            return $order->get_meta( 'ss_shipping_order_parcels', true );
    736872        }
    737873
     
    747883        public function save_ss_shipping_order_agent_no($order_id, $agent_no)
    748884        {
    749             update_post_meta($order_id, 'ss_shipping_order_agent_no', $agent_no);
     885            $order = wc_get_order($order_id);
     886            $order->update_meta_data('ss_shipping_order_agent_no', $agent_no);
     887            $order->save();
    750888        }
    751889
     
    759897        public function get_ss_shipping_order_agent_no($order_id)
    760898        {
    761             // Fecth agent_no from meta field saved by Smart Send
    762             $ss_agent_number = get_post_meta($order_id, 'ss_shipping_order_agent_no', true);
     899            // Fetch agent_no from meta field saved by Smart Send
     900            $order = wc_get_order( $order_id );
     901            $ss_agent_number = $order->get_meta( 'ss_shipping_order_agent_no', true );
    763902            if ($ss_agent_number) {
    764903                // Return the agent_no found
     
    766905            } else {
    767906                // No Smart Send agent_no was found, check if the order has a vConnect agent_no
    768                 $vc_aio_meta = get_post_meta($order_id, '_vc_aio_options', true);
     907                $vc_aio_meta = $order->get_meta( '_vc_aio_options', true );
    769908                if (!empty($vc_aio_meta['addressId']['value'])) {
    770909                    return $vc_aio_meta['addressId']['value'];
     
    785924        public function save_ss_shipping_order_agent($order_id, $agent)
    786925        {
    787             update_post_meta($order_id, '_ss_shipping_order_agent', $agent);
     926            $order = wc_get_order($order_id);
     927            $order->update_meta_data('_ss_shipping_order_agent', $agent);
     928            $order->save();
    788929        }
    789930
     
    794935         */
    795936        public function delete_ss_shipping_order_agent($order_id) {
    796             delete_post_meta($order_id, '_ss_shipping_order_agent');
     937            $order = wc_get_order( $order_id );
     938            $order->delete_meta_data('_ss_shipping_order_agent');
    797939        }
    798940
     
    807949        {
    808950            // Fetch agent info from meta field saved by Smart Send
    809             $ss_agent_info = get_post_meta($order_id, '_ss_shipping_order_agent', true);
     951            $order         = wc_get_order( $order_id );
     952            $ss_agent_info = $order->get_meta( '_ss_shipping_order_agent', true );
    810953            if ($ss_agent_info) {
    811954                // Return the agent_no found
     
    813956            } else {
    814957                // No Smart Send agent_no was found, check if the order has a vConnect agent_no
    815                 $vc_aio_meta = get_post_meta($order_id, '_vc_aio_options', true);
     958                $vc_aio_meta = $order->get_meta( '_vc_aio_options', true );
    816959                if (!empty($vc_aio_meta['addressId']['value'])) {
    817960                    return (object)array(
     
    841984        public function save_ss_shipment_id_in_order_meta($order_id, $shipment_id, $return)
    842985        {
     986            $order = wc_get_order($order_id);
    843987            if ($return) {
    844                 update_post_meta($order_id, '_ss_shipping_return_label_id', $shipment_id);
     988                $order->update_meta_data('_ss_shipping_return_label_id', $shipment_id);
    845989            } else {
    846                 update_post_meta($order_id, '_ss_shipping_label_id', $shipment_id);
    847             }
     990                $order->update_meta_data('_ss_shipping_label_id', $shipment_id);
     991            }
     992            $order->save();
    848993        }
    849994
     
    8561001         * @return string URL label link
    8571002         */
    858         public function get_label_url_from_order_id($order_id, $return)
    859         {
     1003        public function get_label_url_from_order_id($order_id, $return): string
     1004        {
     1005            $order = wc_get_order( $order_id );
    8601006            if ($return) {
    861                 $shipment_id = get_post_meta($order_id, '_ss_shipping_return_label_id', true);
     1007                $shipment_id = $order->get_meta( '_ss_shipping_return_label_id', true );
    8621008            } else {
    863                 $shipment_id = get_post_meta($order_id, '_ss_shipping_label_id', true);
     1009                $shipment_id = $order->get_meta( '_ss_shipping_label_id', true );
    8641010            }
    8651011            return $this->get_label_url_from_shipment_id($shipment_id);
     
    9201066
    9211067        /**
    922          * Add Smart Send bulk actions
    923          */
    924         public function add_order_bulk_actions()
    925         {
    926             global $post_type, $post_status;
    927 
    928             if ($post_type === 'shop_order' && $post_status !== 'trash') :
    929 
    930                 ?>
    931                 <script type="text/javascript">
    932                     jQuery(document).ready(function ($) {
    933                         $('select[name^=action]').append(
    934                             <?php $index = count($actions = $this->get_bulk_actions()); ?>
    935                             <?php foreach ( $actions as $action => $name ) : ?>
    936                             $('<option>').val('<?php echo esc_js($action); ?>').text('<?php echo esc_js($name); ?>')
    937                             <?php --$index; ?>
    938                             <?php if ($index) {
    939                                 echo ',';
    940                             } ?>
    941                             <?php endforeach; ?>
    942                         );
    943                     });
    944                 </script>
    945                 <?php
    946 
    947             endif;
    948         }
    949 
    950         /**
    9511068         * Return Smart Send bulk actions
    9521069         */
    953         public function get_bulk_actions()
    954         {
    955 
    956             $shop_manager_actions = array();
    957 
    958             $shop_manager_actions = array(
     1070        private function get_bulk_actions()
     1071        {
     1072            return array(
    9591073                'ss_shipping_label_bulk'  => (SS_SHIPPING_WC()->get_demo_mode_setting() ? __('DEMO MODE',
    9601074                            'smart-send-logistics') . ': ' : '') . __('Smart Send - Generate Labels',
     
    9641078                        'smart-send-logistics'),
    9651079            );
    966 
    967             return $shop_manager_actions;
    968         }
    969 
    970         /**
    971          * Process bulk actions
    972          */
    973         public function process_orders_bulk_actions()
    974         {
    975             global $typenow;
    976             $array_messages = array('msg_user_id' => get_current_user_id());
    977             // $array_shipments = array();
    978             $array_messages_success = array();
    979             $array_messages_error = array();
    980             $array_shipment_ids = array();
    981 
    982             if ('shop_order' === $typenow) {
    983 
    984                 // Get the bulk action
    985                 $wp_list_table = _get_list_table('WP_Posts_List_Table');
    986                 $action = $wp_list_table->current_action();
    987                 $order_ids = array();
    988 
    989                 if (!$action || !array_key_exists($action, $this->get_bulk_actions())) {
    990                     return;
    991                 }
    992 
    993                 // Make sure order IDs are submitted
    994                 if (isset($_REQUEST['post'])) {
    995                     $order_ids = array_map('absint', $_REQUEST['post']);
    996                 }
    997 
    998                 $redirect_url = admin_url('edit.php?post_type=shop_order');
    999 
    1000                 if ('ss_shipping_label_bulk' === $action || 'ss_shipping_return_bulk' === $action) {
    1001 
    1002                     // Determine if the request is for a return label
    1003                     $return = ('ss_shipping_return_bulk' === $action);
    1004 
    1005                     // Trigger an admin notice to have the user manually open a print window
    1006                     $is_error = false;
    1007                     $orders_count = count($order_ids);
    1008 
    1009                     if ($orders_count < 1) {
    1010                         array_push($array_messages, array(
    1011                             'message' => __('No orders selected, please select the orders to create labels for.',
    1012                                 'smart-send-logistics'),
    1013                             'type'    => 'error',
    1014                         ));
    1015                     } elseif ($orders_count > 5) {
    1016                         array_push($array_messages, array(
    1017                             'message' => __('For now it is not possible to create labels for more than 5 orders at a time.',
    1018                                 'smart-send-logistics'),
    1019                             'type'    => 'error',
    1020                         ));
    1021                     } else {
    1022 
    1023                         // Ensure the selected orders have a Smart Send Shipping method
    1024                         foreach ($order_ids as $order_id) {
    1025                             $order = wc_get_order($order_id);
    1026 
    1027                             $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
    1028 
    1029                             if (!empty($ss_shipping_method_id)) {
    1030 
    1031                                 $response = $this->create_label_for_single_order_maybe_return($order_id, $return, true);
    1032 
    1033                                 foreach ($response as $key => $value) {
    1034 
    1035                                     if (isset($value['success'])) {
    1036                                         array_push($array_messages_success, array(
    1037                                             'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
    1038                                                     $order->get_order_number()) . ': '
    1039                                                 . (empty($value['success']->woocommerce['return']) ?
    1040                                                     __('Shipping label created by Smart Send',
    1041                                                         'smart-send-logistics') : __('Return label created by Smart Send',
    1042                                                         'smart-send-logistics'))
    1043                                                 . ': ' . $this->get_ss_shipping_label_link($value['success']->woocommerce['label_url'],
    1044                                                     !empty($value['success']->woocommerce['return'])),
    1045                                             'type'    => 'success',
    1046                                         ));
    1047 
    1048                                         array_push($array_shipment_ids, array(
    1049                                             'shipment_id' => $value['success']->shipment_id,
    1050                                             'order_id'    => $order->get_order_number(),
    1051                                         ));
    1052 
    1053                                     } else {
    1054                                         // Print error message
    1055                                         $message = sprintf(__('Order #%s', 'smart-send-logistics'),
    1056                                                 $order->get_order_number()) . ': ' . $value['error'];
    1057 
    1058                                         array_push($array_messages_error, array(
    1059                                             'message' => $message,
    1060                                             'type'    => 'error',
    1061                                         ));
    1062                                     }
    1063                                 }
    1064 
    1065                             } else {
    1066                                 array_push($array_messages_error, array(
    1067                                     'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
    1068                                             $order->get_order_number()) . ': ' . __('The selected order did not include a Send Smart shipping method',
    1069                                             'smart-send-logistics'),
    1070                                     'type'    => 'error',
    1071                                 ));
    1072                             }
    1073                         }
    1074 
    1075                         $array_combo_messages = $this->create_combo_file($array_messages_success, $array_messages_error,
    1076                             $array_shipment_ids);
    1077 
    1078                         $array_messages = array_merge($array_messages, $array_combo_messages);
    1079 
    1080                     }
    1081 
    1082                     /* @see render_messages() */
    1083                     update_option('_ss_shipping_bulk_action_confirmation', $array_messages);
    1084 
    1085                 }
    1086             }
    10871080        }
    10881081
     
    11761169
    11771170        /**
    1178          * Display messages on order view screen
    1179          */
    1180         public function render_messages($current_screen = null)
    1181         {
    1182             if (!$current_screen instanceof WP_Screen) {
     1171         * Display messages on order view screen.
     1172         */
     1173        public function render_admin_messages($current_screen = null)
     1174        {
     1175            if (! $current_screen instanceof WP_Screen) {
    11831176                $current_screen = get_current_screen();
    11841177            }
    11851178
    1186             if (isset($current_screen->id) && in_array($current_screen->id, array('shop_order', 'edit-shop_order'),
    1187                     true)) {
    1188 
    1189                 $bulk_action_message_opt = get_option('_ss_shipping_bulk_action_confirmation');
    1190 
    1191                 if (($bulk_action_message_opt) && is_array($bulk_action_message_opt)) {
    1192 
    1193                     // $user_id = key( $bulk_action_message_opt );
    1194                     // remove first element from array and verify if it is the user id
    1195                     $user_id = array_shift($bulk_action_message_opt);
    1196                     if (get_current_user_id() !== (int)$user_id) {
    1197                         return;
    1198                     }
    1199 
    1200                     foreach ($bulk_action_message_opt as $key => $value) {
    1201                         $message = wp_kses_post($value['message']);
    1202                         $type = wp_kses_post($value['type']);
    1203 
    1204                         switch ($type) {
    1205                             case 'error':
    1206                                 echo '<div class="notice notice-error"><ul><li>' . $message . '</li></ul></div>';
    1207                                 break;
    1208                             case 'success':
    1209                                 echo '<div class="notice notice-success"><ul><li><strong>' . $message . '</strong></li></ul></div>';
    1210                                 break;
    1211                             default:
    1212                                 echo '<div class="notice notice-warning"><ul><li><strong>' . $message . '</strong></li></ul></div>';
    1213                         }
    1214                     }
    1215 
    1216                     delete_option('_ss_shipping_bulk_action_confirmation');
    1217                 }
    1218             }
     1179//            if (! isset($current_screen->id) || ! in_array($current_screen->id, array('shop_order', 'edit-shop_order'), true)) {
     1180//                return;
     1181//            }
     1182
     1183            $messages = $this->get_admin_flash_messages(get_current_user_id());
     1184
     1185            if (empty($messages)) {
     1186                return;
     1187            }
     1188
     1189            // remove first element from array and verify if it is the user id
     1190            //$user_id = array_shift($bulk_action_message_opt);
     1191            //if (get_current_user_id() !== (int)$user_id) {
     1192            //    return;
     1193            //}
     1194
     1195            $this->print_admin_flash_messages($messages);
     1196
     1197            $this->set_admin_flash_messages(get_current_user_id(), []);
    12191198        }
    12201199
     
    12501229            return $weight_total;
    12511230        }
     1231
     1232        /**
     1233         * Display messages on order view screen.
     1234         *
     1235         * @see https://webprogramo.com/admin-notices-after-a-page-refresh-on-wordpress/1183/
     1236         */
     1237        protected function print_admin_flash_messages(array $messages)
     1238        {
     1239            foreach ($messages as $message) {
     1240                switch (wp_kses_post($message['type'])) {
     1241                    case 'error':
     1242                        $type = 'error';
     1243                        break;
     1244                    case 'success':
     1245                        $type = 'success';
     1246                        break;
     1247                    default:
     1248                        $type = 'warning';
     1249                }
     1250
     1251                printf('<div class="notice notice-%1$s %2$s"><p>%3$s</p></div>',
     1252                    $type, // info/warning/error/success
     1253                    ($message['dismissible'] ?? false) ? 'is-dismissible' : '',
     1254                    wp_kses_post($message['message'])
     1255                );
     1256            }
     1257        }
     1258
     1259        /**
     1260         * This is the options key used to store the flash messages.
     1261         *
     1262         * The option can contain multiple bags. This could cause race condition issues
     1263         * if multiple users are performing admin actions at the same time because they
     1264         * all read and write to the same option key.
     1265         *
     1266         * @return string
     1267         */
     1268        protected function get_admin_flash_message_option_key()
     1269        {
     1270            return self::ADMIN_FLASH_MESSAGE_OPTION_KEY;
     1271        }
     1272
     1273        protected function get_admin_flash_bags()
     1274        {
     1275            return get_option($this->get_admin_flash_message_option_key(), array());
     1276        }
     1277
     1278        protected function set_admin_flash_bags(array $bags)
     1279        {
     1280            update_option($this->get_admin_flash_message_option_key(), $bags);
     1281        }
     1282
     1283        protected function get_admin_flash_messages(string $bag)
     1284        {
     1285            return $this->get_admin_flash_bags()[$bag] ?? [];
     1286        }
     1287
     1288        /**
     1289         * @param string $bag is the option key
     1290         * @param array<array{message: string, type: string, dismissible: bool}> $messages
     1291         * @return void
     1292         */
     1293        protected function set_admin_flash_messages(string $bag, array $messages)
     1294        {
     1295            $bags = $this->get_admin_flash_bags();
     1296
     1297            $bags[$bag] = $messages;
     1298
     1299            $this->set_admin_flash_bags($bags);
     1300        }
     1301
     1302        /**
     1303         * Add a flash notice to {prefix}options table until a full page refresh is done
     1304         *
     1305         * @param string $bag is the option key
     1306         * @param string $message our notice message
     1307         * @param string $type This can be "info", "warning", "error" or "success", "warning" as default
     1308         * @param boolean $dismissible set this to TRUE to add is-dismissible functionality to your notice
     1309         * @return void
     1310         */
     1311
     1312        protected function add_admin_flash_message(string $bag, $message, $type = "warning", $dismissible = true)
     1313        {
     1314            $this->add_admin_flash_messages($bag, array(array(
     1315                'message' => $message,
     1316                'type' => $type,
     1317                'dismissible' => $dismissible
     1318            )));
     1319        }
     1320
     1321        /**
     1322         * @param string $bag is the option key
     1323         * @param array<array{message: string, type: string, dismissible: bool}> $messages
     1324         * @return void
     1325         */
     1326
     1327        protected function add_admin_flash_messages(string $bag, array $messages)
     1328        {
     1329            $bags = $this->get_admin_flash_bags();
     1330
     1331            if (! is_array($bags[$bag])) {
     1332                $bags[$bag] = [];
     1333            }
     1334
     1335            array_push( $bags[$bag], ...$messages)  ;
     1336
     1337            $this->set_admin_flash_bags($bags);
     1338        }
    12521339    }
    12531340
  • smart-send-logistics/tags/8.1.0/includes/frontend/class-ss-shipping-frontend.php

    r2226182 r3035681  
    2626        }
    2727
    28         /**
    29          * Init hooks
    30          */
    31         public function init_hooks()
     28        protected function init_hooks()
    3229        {
    3330            add_action('woocommerce_after_shipping_rate', array($this, 'display_ss_pickup_points'), 10, 2);
  • smart-send-logistics/tags/8.1.0/readme.txt

    r2948562 r3035681  
    99Requires at least: 3.0.1
    1010Tested up to: 6.3
    11 Stable tag: 8.0.27
     11Stable tag: 8.1.0
    1212License: GNU General Public License v3.0
    1313License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    201201== Changelog ==
    202202
     203= 8.1.0 =
     204* Add High-Performance Order Storage (HPOS) compatibility
     205
    203206= 8.0.27 =
    204207* Remove PostNord EMS shipping method
  • smart-send-logistics/tags/8.1.0/smart-send-logistics.php

    r2948562 r3035681  
    77 * Author URI: https://www.smartsend.io
    88 * Text Domain: smart-send-logistics
    9  * Version: 8.0.27
    10  * WC requires at least: 3.0.1
     9 * Version: 8.1.0
     10 * WC requires at least: 4.7.0
    1111 * WC tested up to: 7.9
    1212 *
     
    3535    {
    3636
    37         private $version = "8.0.27";
     37        private $version = "8.1.0";
    3838
    3939        /**
     
    9797        public function __construct()
    9898        {
     99            add_action('before_woocommerce_init', [$this, 'declaring_hpos_compatibility']);
     100
    99101            $this->define_constants();
    100102            $this->includes();
     
    117119        }
    118120
     121        public function declaring_hpos_compatibility()
     122        {
     123            if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
     124                \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
     125            }
     126        }
     127
    119128        /**
    120129         * Main Smart Send Shipping Instance.
     
    162171        }
    163172
    164         public function init_hooks()
     173        protected function init_hooks()
    165174        {
    166175            add_action('init', array($this, 'init'), 0);
  • smart-send-logistics/trunk/includes/class-ss-shipping-wc-order.php

    r2747067 r3035681  
    44    exit; // Exit if accessed directly
    55}
     6
     7use Automattic\WooCommerce\Utilities\OrderUtil;
     8use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
     9use WP_Post;
     10use WooCommerce\Classes\WC_Order;
    611
    712/**
     
    1722    class SS_Shipping_WC_Order
    1823    {
     24        const ADMIN_FLASH_MESSAGE_OPTION_KEY = '_ss_shipping_bulk_action_confirmation';
    1925
    2026        protected $label_prefix = 'smart-send-label-';
     
    2531        public function __construct()
    2632        {
    27 
    2833            $this->define_constants();
    2934            $this->init_hooks();
     
    4348        }
    4449
    45         /**
    46          * Init hooks
    47          */
    48         public function init_hooks()
    49         {
    50 
     50        protected function init_hooks()
     51        {
    5152            // Order page metabox actions
    52             add_action('add_meta_boxes', array($this, 'add_meta_box'), 20);
     53            add_action('add_meta_boxes', array($this, 'add_smart_send_order_meta_box'), 20);
    5354            add_action('wp_ajax_ss_shipping_generate_label', array($this, 'generate_label'));
    5455
     
    6869
    6970            // add bulk actions to the Orders screen table bulk action drop-downs
    70             add_action('admin_footer-edit.php', array($this, 'add_order_bulk_actions'));
    71 
    72             // process orders bulk actions
    73             add_action('load-edit.php', array($this, 'process_orders_bulk_actions'));
     71            $this->register_bulk_order_actions();
    7472
    7573            // display admin notices for bulk actions
    76             add_action('admin_notices', array($this, 'render_messages'));
     74            add_action('admin_notices', array($this, 'render_admin_messages'));
     75        }
     76
     77        /**
     78         * Register bulk order actions.
     79         * @see https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/
     80         * @since WordPress 4.7.0
     81         * @return void
     82         */
     83        private function register_bulk_order_actions()
     84        {
     85            $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
     86                ? 'woocommerce_page_wc-orders' // function not available wc_get_page_screen_id( 'shop-order' )
     87                : 'shop_order';
     88
     89            // An actions in the dropdown
     90            add_filter("bulk_actions-{$screen}", array($this, 'add_bulk_order_actions'));
     91
     92            // Handling the form submission
     93            add_filter("handle_bulk_actions-{$screen}", array($this, 'handle_bulk_order_actions'), 10, 3);
     94        }
     95
     96        public function add_bulk_order_actions($bulk_actions)
     97        {
     98            return array_merge($bulk_actions, $this->get_bulk_actions());
     99        }
     100
     101        /**
     102         * Processed the selected bulk action.
     103         *
     104         * Note that this function is not called if no items are selected in the table.
     105         *
     106         * @param string $sendback
     107         * @param string $doaction
     108         * @param array $items
     109         * @return string|void
     110         */
     111        public function handle_bulk_order_actions(string $sendback, string $doaction, array $items)
     112        {
     113            if (! in_array($doaction, array_keys($this->get_bulk_actions()))) {
     114                return;
     115            }
     116
     117            $array_messages = array();
     118            // $array_shipments = array();
     119            $array_messages_success = array();
     120            $array_messages_error = array();
     121            $array_shipment_ids = array();
     122
     123            if ('ss_shipping_label_bulk' === $doaction || 'ss_shipping_return_bulk' === $doaction) {
     124
     125                // Determine if the request is for a return label
     126                $return = ('ss_shipping_return_bulk' === $doaction);
     127
     128                // Trigger an admin notice to have the user manually open a print window
     129                $is_error = false;
     130                $orders_count = count($items);
     131
     132                if ($orders_count < 1) {
     133                    array_push($array_messages, array(
     134                        'message' => __('No orders selected, please select the orders to create labels for.',
     135                            'smart-send-logistics'),
     136                        'type'    => 'error',
     137                    ));
     138                } elseif ($orders_count > 5) {
     139                    array_push($array_messages, array(
     140                        'message' => __('For now it is not possible to create labels for more than 5 orders at a time.',
     141                            'smart-send-logistics'),
     142                        'type'    => 'error',
     143                    ));
     144                } else {
     145
     146                    // Ensure the selected orders have a Smart Send Shipping method
     147                    foreach ($items as $order_id) {
     148                        $order = wc_get_order($order_id);
     149
     150                        $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     151
     152                        if (!empty($ss_shipping_method_id)) {
     153
     154                            $response = $this->create_label_for_single_order_maybe_return($order_id, $return, true);
     155
     156                            foreach ($response as $key => $value) {
     157
     158                                if (isset($value['success'])) {
     159                                    array_push($array_messages_success, array(
     160                                        'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
     161                                                $order->get_order_number()) . ': '
     162                                            . (empty($value['success']->woocommerce['return']) ?
     163                                                __('Shipping label created by Smart Send',
     164                                                    'smart-send-logistics') : __('Return label created by Smart Send',
     165                                                    'smart-send-logistics'))
     166                                            . ': ' . $this->get_ss_shipping_label_link($value['success']->woocommerce['label_url'],
     167                                                !empty($value['success']->woocommerce['return'])),
     168                                        'type'    => 'success',
     169                                    ));
     170
     171                                    array_push($array_shipment_ids, array(
     172                                        'shipment_id' => $value['success']->shipment_id,
     173                                        'order_id'    => $order->get_order_number(),
     174                                    ));
     175
     176                                } else {
     177                                    // Print error message
     178                                    $message = sprintf(__('Order #%s', 'smart-send-logistics'),
     179                                            $order->get_order_number()) . ': ' . $value['error'];
     180
     181                                    array_push($array_messages_error, array(
     182                                        'message' => $message,
     183                                        'type'    => 'error',
     184                                    ));
     185                                }
     186                            }
     187
     188                        } else {
     189                            array_push($array_messages_error, array(
     190                                'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
     191                                        $order->get_order_number()) . ': ' . __('The selected order did not include a Send Smart shipping method',
     192                                        'smart-send-logistics'),
     193                                'type'    => 'error',
     194                            ));
     195                        }
     196                    }
     197
     198                    $array_combo_messages = $this->create_combo_file($array_messages_success, $array_messages_error,
     199                        $array_shipment_ids);
     200
     201                    $array_messages = array_merge($array_messages, $array_combo_messages);
     202
     203                }
     204
     205                $this->add_admin_flash_messages(get_current_user_id(), $array_messages);
     206            }
     207
     208            return $sendback;
    77209        }
    78210
     
    80212         * Add the meta box for shipment info on the order page
    81213         */
    82         public function add_meta_box()
    83         {
    84             global $woocommerce, $post;
    85             $order_id = $post->ID;
     214        public function add_smart_send_order_meta_box()
     215        {
     216            // @see https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#audit-for-order-administration-screen-functions
     217            $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
     218                ? wc_get_page_screen_id( 'shop-order' )
     219                : 'shop_order';
     220
     221            add_meta_box(
     222                'woocommerce-ss-shipping-label',
     223                __('Smart Send Shipping', 'smart-send-logistics'),
     224                array($this, 'render_smart_send_order_meta_box'),
     225                $screen,
     226                'side',
     227                'default'
     228            );
     229        }
     230
     231        /**
     232         * Render the content of the order meta box.
     233         *
     234         * @param WP_Post|WC_Order $post_or_order_object
     235         * @return void
     236         */
     237        public function render_smart_send_order_meta_box($post_or_order_object)
     238        {
     239            /** @var WC_Order $order */
     240            $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object;
     241
     242            // ... rest of the code. $post_or_order_object should not be used directly below this point´
     243
     244            $order_id = $order->ID;
     245
     246            $shipping_ss_settings = SS_SHIPPING_WC()->get_ss_shipping_settings();
    86247
    87248            $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     249
    88250            // Only display Smart Shipping (SS) meta box is SS selected as shipping method OR free shipping is set to SS method
    89             if (!empty($ss_shipping_method_id)) {
    90 
    91                 add_meta_box('woocommerce-ss-shipping-label', __('Smart Send Shipping', 'smart-send-logistics'),
    92                     array($this, 'meta_box'), 'shop_order', 'side', 'default');
    93             } else {
    94                 add_meta_box('woocommerce-ss-shipping-label', __('Smart Send Shipping', 'smart-send-logistics'),
    95                     array($this, 'meta_box_non_smart_send'), 'shop_order', 'side', 'default');
    96 
    97             }
    98         }
    99 
    100         /**
    101          * Show the meta box for shipment info on the order page
    102          *
    103          * This meta box is shown when the order does not have a Smart Send shipping method
    104          */
    105         public function meta_box_non_smart_send()
    106         {
    107             global $woocommerce, $post;
    108             $order_id = $post->ID;
    109             echo '<p>' . __('Order placed with a shipping method that is not from the Smart Send plugin',
    110                     'smart-send-logistics') . '</p>';
    111         }
    112 
    113         /**
    114          * Show the meta box for shipment info on the order page
    115          *
    116          * This meta box is shown when the order have a Smart Send shipping method
    117          */
    118         public function meta_box()
    119         {
    120             global $woocommerce, $post;
    121             $order_id = $post->ID;
    122             $order = wc_get_order($order_id);
    123 
    124             $shipping_ss_settings = SS_SHIPPING_WC()->get_ss_shipping_settings();
    125 
    126             $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
     251            if (! $ss_shipping_method_id) {
     252                echo '<p>' . __('Order placed with a shipping method that is not from the Smart Send plugin',
     253                        'smart-send-logistics') . '</p>';
     254
     255                return;
     256            }
     257
    127258            $ss_shipping_method_name = SS_SHIPPING_WC()->get_shipping_method_name_from_all_shipping_method_instances($ss_shipping_method_id);
    128259
     
    316447                                    return 'postnord_commercial';
    317448                                } elseif (stripos($shipping_method_id, '_privatehome') !== false) {
    318                                     $vc_aio_options = get_post_meta($order_id, '_vc_aio_options');
     449                                    $order          = wc_get_order( $order_id );
     450                                    $vc_aio_options = $order->get_meta( '_vc_aio_options', true );
    319451                                    $flexDelivery = false;
    320452                                    $flexDeliveryOption = false;
     
    371503         */
    372504        public function filter_update_agent_meta($check, $meta_id, $meta_value, $meta_key) {
     505
    373506            if ($meta_key == 'ss_shipping_order_agent_no') {
    374507                $meta = get_metadata_by_mid( 'post', $meta_id );
     
    720853        public function save_ss_shipping_order_parcels($order_id, $parcels)
    721854        {
    722             update_post_meta($order_id, 'ss_shipping_order_parcels', $parcels);
     855            $order = wc_get_order($order_id);
     856            $order->update_meta_data('ss_shipping_order_parcels', $parcels);
     857            $order->save();
    723858        }
    724859
     
    733868        public function get_ss_shipping_order_parcels($order_id)
    734869        {
    735             return get_post_meta($order_id, 'ss_shipping_order_parcels', true);
     870            $order = wc_get_order( $order_id );
     871            return $order->get_meta( 'ss_shipping_order_parcels', true );
    736872        }
    737873
     
    747883        public function save_ss_shipping_order_agent_no($order_id, $agent_no)
    748884        {
    749             update_post_meta($order_id, 'ss_shipping_order_agent_no', $agent_no);
     885            $order = wc_get_order($order_id);
     886            $order->update_meta_data('ss_shipping_order_agent_no', $agent_no);
     887            $order->save();
    750888        }
    751889
     
    759897        public function get_ss_shipping_order_agent_no($order_id)
    760898        {
    761             // Fecth agent_no from meta field saved by Smart Send
    762             $ss_agent_number = get_post_meta($order_id, 'ss_shipping_order_agent_no', true);
     899            // Fetch agent_no from meta field saved by Smart Send
     900            $order = wc_get_order( $order_id );
     901            $ss_agent_number = $order->get_meta( 'ss_shipping_order_agent_no', true );
    763902            if ($ss_agent_number) {
    764903                // Return the agent_no found
     
    766905            } else {
    767906                // No Smart Send agent_no was found, check if the order has a vConnect agent_no
    768                 $vc_aio_meta = get_post_meta($order_id, '_vc_aio_options', true);
     907                $vc_aio_meta = $order->get_meta( '_vc_aio_options', true );
    769908                if (!empty($vc_aio_meta['addressId']['value'])) {
    770909                    return $vc_aio_meta['addressId']['value'];
     
    785924        public function save_ss_shipping_order_agent($order_id, $agent)
    786925        {
    787             update_post_meta($order_id, '_ss_shipping_order_agent', $agent);
     926            $order = wc_get_order($order_id);
     927            $order->update_meta_data('_ss_shipping_order_agent', $agent);
     928            $order->save();
    788929        }
    789930
     
    794935         */
    795936        public function delete_ss_shipping_order_agent($order_id) {
    796             delete_post_meta($order_id, '_ss_shipping_order_agent');
     937            $order = wc_get_order( $order_id );
     938            $order->delete_meta_data('_ss_shipping_order_agent');
    797939        }
    798940
     
    807949        {
    808950            // Fetch agent info from meta field saved by Smart Send
    809             $ss_agent_info = get_post_meta($order_id, '_ss_shipping_order_agent', true);
     951            $order         = wc_get_order( $order_id );
     952            $ss_agent_info = $order->get_meta( '_ss_shipping_order_agent', true );
    810953            if ($ss_agent_info) {
    811954                // Return the agent_no found
     
    813956            } else {
    814957                // No Smart Send agent_no was found, check if the order has a vConnect agent_no
    815                 $vc_aio_meta = get_post_meta($order_id, '_vc_aio_options', true);
     958                $vc_aio_meta = $order->get_meta( '_vc_aio_options', true );
    816959                if (!empty($vc_aio_meta['addressId']['value'])) {
    817960                    return (object)array(
     
    841984        public function save_ss_shipment_id_in_order_meta($order_id, $shipment_id, $return)
    842985        {
     986            $order = wc_get_order($order_id);
    843987            if ($return) {
    844                 update_post_meta($order_id, '_ss_shipping_return_label_id', $shipment_id);
     988                $order->update_meta_data('_ss_shipping_return_label_id', $shipment_id);
    845989            } else {
    846                 update_post_meta($order_id, '_ss_shipping_label_id', $shipment_id);
    847             }
     990                $order->update_meta_data('_ss_shipping_label_id', $shipment_id);
     991            }
     992            $order->save();
    848993        }
    849994
     
    8561001         * @return string URL label link
    8571002         */
    858         public function get_label_url_from_order_id($order_id, $return)
    859         {
     1003        public function get_label_url_from_order_id($order_id, $return): string
     1004        {
     1005            $order = wc_get_order( $order_id );
    8601006            if ($return) {
    861                 $shipment_id = get_post_meta($order_id, '_ss_shipping_return_label_id', true);
     1007                $shipment_id = $order->get_meta( '_ss_shipping_return_label_id', true );
    8621008            } else {
    863                 $shipment_id = get_post_meta($order_id, '_ss_shipping_label_id', true);
     1009                $shipment_id = $order->get_meta( '_ss_shipping_label_id', true );
    8641010            }
    8651011            return $this->get_label_url_from_shipment_id($shipment_id);
     
    9201066
    9211067        /**
    922          * Add Smart Send bulk actions
    923          */
    924         public function add_order_bulk_actions()
    925         {
    926             global $post_type, $post_status;
    927 
    928             if ($post_type === 'shop_order' && $post_status !== 'trash') :
    929 
    930                 ?>
    931                 <script type="text/javascript">
    932                     jQuery(document).ready(function ($) {
    933                         $('select[name^=action]').append(
    934                             <?php $index = count($actions = $this->get_bulk_actions()); ?>
    935                             <?php foreach ( $actions as $action => $name ) : ?>
    936                             $('<option>').val('<?php echo esc_js($action); ?>').text('<?php echo esc_js($name); ?>')
    937                             <?php --$index; ?>
    938                             <?php if ($index) {
    939                                 echo ',';
    940                             } ?>
    941                             <?php endforeach; ?>
    942                         );
    943                     });
    944                 </script>
    945                 <?php
    946 
    947             endif;
    948         }
    949 
    950         /**
    9511068         * Return Smart Send bulk actions
    9521069         */
    953         public function get_bulk_actions()
    954         {
    955 
    956             $shop_manager_actions = array();
    957 
    958             $shop_manager_actions = array(
     1070        private function get_bulk_actions()
     1071        {
     1072            return array(
    9591073                'ss_shipping_label_bulk'  => (SS_SHIPPING_WC()->get_demo_mode_setting() ? __('DEMO MODE',
    9601074                            'smart-send-logistics') . ': ' : '') . __('Smart Send - Generate Labels',
     
    9641078                        'smart-send-logistics'),
    9651079            );
    966 
    967             return $shop_manager_actions;
    968         }
    969 
    970         /**
    971          * Process bulk actions
    972          */
    973         public function process_orders_bulk_actions()
    974         {
    975             global $typenow;
    976             $array_messages = array('msg_user_id' => get_current_user_id());
    977             // $array_shipments = array();
    978             $array_messages_success = array();
    979             $array_messages_error = array();
    980             $array_shipment_ids = array();
    981 
    982             if ('shop_order' === $typenow) {
    983 
    984                 // Get the bulk action
    985                 $wp_list_table = _get_list_table('WP_Posts_List_Table');
    986                 $action = $wp_list_table->current_action();
    987                 $order_ids = array();
    988 
    989                 if (!$action || !array_key_exists($action, $this->get_bulk_actions())) {
    990                     return;
    991                 }
    992 
    993                 // Make sure order IDs are submitted
    994                 if (isset($_REQUEST['post'])) {
    995                     $order_ids = array_map('absint', $_REQUEST['post']);
    996                 }
    997 
    998                 $redirect_url = admin_url('edit.php?post_type=shop_order');
    999 
    1000                 if ('ss_shipping_label_bulk' === $action || 'ss_shipping_return_bulk' === $action) {
    1001 
    1002                     // Determine if the request is for a return label
    1003                     $return = ('ss_shipping_return_bulk' === $action);
    1004 
    1005                     // Trigger an admin notice to have the user manually open a print window
    1006                     $is_error = false;
    1007                     $orders_count = count($order_ids);
    1008 
    1009                     if ($orders_count < 1) {
    1010                         array_push($array_messages, array(
    1011                             'message' => __('No orders selected, please select the orders to create labels for.',
    1012                                 'smart-send-logistics'),
    1013                             'type'    => 'error',
    1014                         ));
    1015                     } elseif ($orders_count > 5) {
    1016                         array_push($array_messages, array(
    1017                             'message' => __('For now it is not possible to create labels for more than 5 orders at a time.',
    1018                                 'smart-send-logistics'),
    1019                             'type'    => 'error',
    1020                         ));
    1021                     } else {
    1022 
    1023                         // Ensure the selected orders have a Smart Send Shipping method
    1024                         foreach ($order_ids as $order_id) {
    1025                             $order = wc_get_order($order_id);
    1026 
    1027                             $ss_shipping_method_id = $this->get_smart_send_method_id($order_id);
    1028 
    1029                             if (!empty($ss_shipping_method_id)) {
    1030 
    1031                                 $response = $this->create_label_for_single_order_maybe_return($order_id, $return, true);
    1032 
    1033                                 foreach ($response as $key => $value) {
    1034 
    1035                                     if (isset($value['success'])) {
    1036                                         array_push($array_messages_success, array(
    1037                                             'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
    1038                                                     $order->get_order_number()) . ': '
    1039                                                 . (empty($value['success']->woocommerce['return']) ?
    1040                                                     __('Shipping label created by Smart Send',
    1041                                                         'smart-send-logistics') : __('Return label created by Smart Send',
    1042                                                         'smart-send-logistics'))
    1043                                                 . ': ' . $this->get_ss_shipping_label_link($value['success']->woocommerce['label_url'],
    1044                                                     !empty($value['success']->woocommerce['return'])),
    1045                                             'type'    => 'success',
    1046                                         ));
    1047 
    1048                                         array_push($array_shipment_ids, array(
    1049                                             'shipment_id' => $value['success']->shipment_id,
    1050                                             'order_id'    => $order->get_order_number(),
    1051                                         ));
    1052 
    1053                                     } else {
    1054                                         // Print error message
    1055                                         $message = sprintf(__('Order #%s', 'smart-send-logistics'),
    1056                                                 $order->get_order_number()) . ': ' . $value['error'];
    1057 
    1058                                         array_push($array_messages_error, array(
    1059                                             'message' => $message,
    1060                                             'type'    => 'error',
    1061                                         ));
    1062                                     }
    1063                                 }
    1064 
    1065                             } else {
    1066                                 array_push($array_messages_error, array(
    1067                                     'message' => sprintf(__('Order #%s', 'smart-send-logistics'),
    1068                                             $order->get_order_number()) . ': ' . __('The selected order did not include a Send Smart shipping method',
    1069                                             'smart-send-logistics'),
    1070                                     'type'    => 'error',
    1071                                 ));
    1072                             }
    1073                         }
    1074 
    1075                         $array_combo_messages = $this->create_combo_file($array_messages_success, $array_messages_error,
    1076                             $array_shipment_ids);
    1077 
    1078                         $array_messages = array_merge($array_messages, $array_combo_messages);
    1079 
    1080                     }
    1081 
    1082                     /* @see render_messages() */
    1083                     update_option('_ss_shipping_bulk_action_confirmation', $array_messages);
    1084 
    1085                 }
    1086             }
    10871080        }
    10881081
     
    11761169
    11771170        /**
    1178          * Display messages on order view screen
    1179          */
    1180         public function render_messages($current_screen = null)
    1181         {
    1182             if (!$current_screen instanceof WP_Screen) {
     1171         * Display messages on order view screen.
     1172         */
     1173        public function render_admin_messages($current_screen = null)
     1174        {
     1175            if (! $current_screen instanceof WP_Screen) {
    11831176                $current_screen = get_current_screen();
    11841177            }
    11851178
    1186             if (isset($current_screen->id) && in_array($current_screen->id, array('shop_order', 'edit-shop_order'),
    1187                     true)) {
    1188 
    1189                 $bulk_action_message_opt = get_option('_ss_shipping_bulk_action_confirmation');
    1190 
    1191                 if (($bulk_action_message_opt) && is_array($bulk_action_message_opt)) {
    1192 
    1193                     // $user_id = key( $bulk_action_message_opt );
    1194                     // remove first element from array and verify if it is the user id
    1195                     $user_id = array_shift($bulk_action_message_opt);
    1196                     if (get_current_user_id() !== (int)$user_id) {
    1197                         return;
    1198                     }
    1199 
    1200                     foreach ($bulk_action_message_opt as $key => $value) {
    1201                         $message = wp_kses_post($value['message']);
    1202                         $type = wp_kses_post($value['type']);
    1203 
    1204                         switch ($type) {
    1205                             case 'error':
    1206                                 echo '<div class="notice notice-error"><ul><li>' . $message . '</li></ul></div>';
    1207                                 break;
    1208                             case 'success':
    1209                                 echo '<div class="notice notice-success"><ul><li><strong>' . $message . '</strong></li></ul></div>';
    1210                                 break;
    1211                             default:
    1212                                 echo '<div class="notice notice-warning"><ul><li><strong>' . $message . '</strong></li></ul></div>';
    1213                         }
    1214                     }
    1215 
    1216                     delete_option('_ss_shipping_bulk_action_confirmation');
    1217                 }
    1218             }
     1179//            if (! isset($current_screen->id) || ! in_array($current_screen->id, array('shop_order', 'edit-shop_order'), true)) {
     1180//                return;
     1181//            }
     1182
     1183            $messages = $this->get_admin_flash_messages(get_current_user_id());
     1184
     1185            if (empty($messages)) {
     1186                return;
     1187            }
     1188
     1189            // remove first element from array and verify if it is the user id
     1190            //$user_id = array_shift($bulk_action_message_opt);
     1191            //if (get_current_user_id() !== (int)$user_id) {
     1192            //    return;
     1193            //}
     1194
     1195            $this->print_admin_flash_messages($messages);
     1196
     1197            $this->set_admin_flash_messages(get_current_user_id(), []);
    12191198        }
    12201199
     
    12501229            return $weight_total;
    12511230        }
     1231
     1232        /**
     1233         * Display messages on order view screen.
     1234         *
     1235         * @see https://webprogramo.com/admin-notices-after-a-page-refresh-on-wordpress/1183/
     1236         */
     1237        protected function print_admin_flash_messages(array $messages)
     1238        {
     1239            foreach ($messages as $message) {
     1240                switch (wp_kses_post($message['type'])) {
     1241                    case 'error':
     1242                        $type = 'error';
     1243                        break;
     1244                    case 'success':
     1245                        $type = 'success';
     1246                        break;
     1247                    default:
     1248                        $type = 'warning';
     1249                }
     1250
     1251                printf('<div class="notice notice-%1$s %2$s"><p>%3$s</p></div>',
     1252                    $type, // info/warning/error/success
     1253                    ($message['dismissible'] ?? false) ? 'is-dismissible' : '',
     1254                    wp_kses_post($message['message'])
     1255                );
     1256            }
     1257        }
     1258
     1259        /**
     1260         * This is the options key used to store the flash messages.
     1261         *
     1262         * The option can contain multiple bags. This could cause race condition issues
     1263         * if multiple users are performing admin actions at the same time because they
     1264         * all read and write to the same option key.
     1265         *
     1266         * @return string
     1267         */
     1268        protected function get_admin_flash_message_option_key()
     1269        {
     1270            return self::ADMIN_FLASH_MESSAGE_OPTION_KEY;
     1271        }
     1272
     1273        protected function get_admin_flash_bags()
     1274        {
     1275            return get_option($this->get_admin_flash_message_option_key(), array());
     1276        }
     1277
     1278        protected function set_admin_flash_bags(array $bags)
     1279        {
     1280            update_option($this->get_admin_flash_message_option_key(), $bags);
     1281        }
     1282
     1283        protected function get_admin_flash_messages(string $bag)
     1284        {
     1285            return $this->get_admin_flash_bags()[$bag] ?? [];
     1286        }
     1287
     1288        /**
     1289         * @param string $bag is the option key
     1290         * @param array<array{message: string, type: string, dismissible: bool}> $messages
     1291         * @return void
     1292         */
     1293        protected function set_admin_flash_messages(string $bag, array $messages)
     1294        {
     1295            $bags = $this->get_admin_flash_bags();
     1296
     1297            $bags[$bag] = $messages;
     1298
     1299            $this->set_admin_flash_bags($bags);
     1300        }
     1301
     1302        /**
     1303         * Add a flash notice to {prefix}options table until a full page refresh is done
     1304         *
     1305         * @param string $bag is the option key
     1306         * @param string $message our notice message
     1307         * @param string $type This can be "info", "warning", "error" or "success", "warning" as default
     1308         * @param boolean $dismissible set this to TRUE to add is-dismissible functionality to your notice
     1309         * @return void
     1310         */
     1311
     1312        protected function add_admin_flash_message(string $bag, $message, $type = "warning", $dismissible = true)
     1313        {
     1314            $this->add_admin_flash_messages($bag, array(array(
     1315                'message' => $message,
     1316                'type' => $type,
     1317                'dismissible' => $dismissible
     1318            )));
     1319        }
     1320
     1321        /**
     1322         * @param string $bag is the option key
     1323         * @param array<array{message: string, type: string, dismissible: bool}> $messages
     1324         * @return void
     1325         */
     1326
     1327        protected function add_admin_flash_messages(string $bag, array $messages)
     1328        {
     1329            $bags = $this->get_admin_flash_bags();
     1330
     1331            if (! is_array($bags[$bag])) {
     1332                $bags[$bag] = [];
     1333            }
     1334
     1335            array_push( $bags[$bag], ...$messages)  ;
     1336
     1337            $this->set_admin_flash_bags($bags);
     1338        }
    12521339    }
    12531340
  • smart-send-logistics/trunk/includes/frontend/class-ss-shipping-frontend.php

    r2226182 r3035681  
    2626        }
    2727
    28         /**
    29          * Init hooks
    30          */
    31         public function init_hooks()
     28        protected function init_hooks()
    3229        {
    3330            add_action('woocommerce_after_shipping_rate', array($this, 'display_ss_pickup_points'), 10, 2);
  • smart-send-logistics/trunk/readme.txt

    r2948562 r3035681  
    99Requires at least: 3.0.1
    1010Tested up to: 6.3
    11 Stable tag: 8.0.27
     11Stable tag: 8.1.0
    1212License: GNU General Public License v3.0
    1313License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    201201== Changelog ==
    202202
     203= 8.1.0 =
     204* Add High-Performance Order Storage (HPOS) compatibility
     205
    203206= 8.0.27 =
    204207* Remove PostNord EMS shipping method
  • smart-send-logistics/trunk/smart-send-logistics.php

    r2948562 r3035681  
    77 * Author URI: https://www.smartsend.io
    88 * Text Domain: smart-send-logistics
    9  * Version: 8.0.27
    10  * WC requires at least: 3.0.1
     9 * Version: 8.1.0
     10 * WC requires at least: 4.7.0
    1111 * WC tested up to: 7.9
    1212 *
     
    3535    {
    3636
    37         private $version = "8.0.27";
     37        private $version = "8.1.0";
    3838
    3939        /**
     
    9797        public function __construct()
    9898        {
     99            add_action('before_woocommerce_init', [$this, 'declaring_hpos_compatibility']);
     100
    99101            $this->define_constants();
    100102            $this->includes();
     
    117119        }
    118120
     121        public function declaring_hpos_compatibility()
     122        {
     123            if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
     124                \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
     125            }
     126        }
     127
    119128        /**
    120129         * Main Smart Send Shipping Instance.
     
    162171        }
    163172
    164         public function init_hooks()
     173        protected function init_hooks()
    165174        {
    166175            add_action('init', array($this, 'init'), 0);
Note: See TracChangeset for help on using the changeset viewer.