Plugin Directory

Changeset 3292802


Ignore:
Timestamp:
05/13/2025 06:55:18 PM (10 months ago)
Author:
webikon
Message:

Added fix for export metabox

Location:
wc-dpd
Files:
101 added
3 edited

Legend:

Unmodified
Added
Removed
  • wc-dpd/trunk/includes/OrderMetabox.php

    r3255337 r3292802  
    1818    {
    1919        add_action('add_meta_boxes', [__CLASS__, 'addMetabox']);
    20         add_action('save_post', [__CLASS__, 'processExport']);
    21         add_filter('save_post', [__CLASS__, 'processReset']);
     20       
     21        // Handle form submission via dedicated admin action
     22        add_action('admin_init', [__CLASS__, 'handleFormSubmission']);
    2223    }
    2324
     
    2930
    3031        add_meta_box('dpd-export', __('DPD Export', 'wc-dpd'), [__CLASS__, 'renderMetabox'], $screen, 'side', 'core');
     32    }
     33
     34    /**
     35     * Handle direct form submissions
     36     */
     37    public static function handleFormSubmission()
     38    {
     39        // Handle the reset action
     40        if (isset($_POST[self::RESET_ACTION_KEY]) && isset($_POST['dpd_metabox_nonce'])) {
     41            if (!wp_verify_nonce($_POST['dpd_metabox_nonce'], 'dpd_metabox_save')) {
     42                return;
     43            }
     44           
     45            if (isset($_POST['order_id']) && !empty($_POST['order_id'])) {
     46                $order_id = absint($_POST['order_id']);
     47                Order::reset($order_id);
     48               
     49                $order_edit_url = admin_url('post.php?post=' . $order_id . '&action=edit');
     50                wp_safe_redirect($order_edit_url);
     51                exit;
     52            }
     53        }
     54       
     55        // Handle direct export action
     56        if (isset($_POST[self::EXPORT_ACTION_KEY]) && isset($_POST['dpd_metabox_nonce'])) {
     57            if (!wp_verify_nonce($_POST['dpd_metabox_nonce'], 'dpd_metabox_save')) {
     58                return;
     59            }
     60           
     61            if (isset($_POST['order_id']) && !empty($_POST['order_id'])) {
     62                $order_id = absint($_POST['order_id']);
     63                self::saveMetaFields($order_id);
     64                Order::export($order_id);
     65               
     66                $order_edit_url = admin_url('post.php?post=' . $order_id . '&action=edit');
     67                wp_safe_redirect($order_edit_url);
     68                exit;
     69            }
     70        }
     71    }
     72   
     73    /**
     74     * Save metabox field data
     75     *
     76     * @param int $order_id Order ID
     77     * @return bool
     78     */
     79    public static function saveMetaFields($order_id)
     80    {
     81        if (!$order_id) {
     82            return false;
     83        }
     84       
     85        $order = wc_get_order($order_id);
     86       
     87        if (!$order instanceof \WC_Order) {
     88            return false;
     89        }
     90       
     91        // Save metabox fields
     92        if (isset($_POST[Order::SHIPPING_META_KEY])) {
     93            $order->update_meta_data(Order::SHIPPING_META_KEY, sanitize_text_field($_POST[Order::SHIPPING_META_KEY]));
     94        }
     95
     96        if (isset($_POST[Order::ADDRESS_ID_META_KEY])) {
     97            $order->update_meta_data(Order::ADDRESS_ID_META_KEY, sanitize_text_field($_POST[Order::ADDRESS_ID_META_KEY]));
     98        }
     99
     100        if (isset($_POST[Order::BANK_ID_META_KEY])) {
     101            $order->update_meta_data(Order::BANK_ID_META_KEY, sanitize_text_field($_POST[Order::BANK_ID_META_KEY]));
     102        }
     103
     104        if (isset($_POST[Order::NOTIFICATION_META_KEY])) {
     105            $order->update_meta_data(Order::NOTIFICATION_META_KEY, $_POST[Order::NOTIFICATION_META_KEY] == 'on' ? 'yes' : 'no');
     106        } else {
     107            $order->update_meta_data(Order::NOTIFICATION_META_KEY, 'no');
     108        }
     109
     110        if (isset($_POST[Order::REFERENCE_1_META_KEY])) {
     111            $order->update_meta_data(Order::REFERENCE_1_META_KEY, sanitize_text_field($_POST[Order::REFERENCE_1_META_KEY]));
     112        }
     113
     114        if (isset($_POST[Order::REFERENCE_2_META_KEY])) {
     115            $order->update_meta_data(Order::REFERENCE_2_META_KEY, sanitize_text_field($_POST[Order::REFERENCE_2_META_KEY]));
     116        }
     117
     118        if (isset($_POST[Order::PACKAGE_WEIGHT_META_KEY])) {
     119            $order->update_meta_data(Order::PACKAGE_WEIGHT_META_KEY, sanitize_text_field($_POST[Order::PACKAGE_WEIGHT_META_KEY]));
     120        }
     121
     122        $order->save_meta_data();
     123       
     124        return true;
    31125    }
    32126
     
    62156            }
    63157
     158            echo '<form method="post">';
     159            wp_nonce_field('dpd_metabox_save', 'dpd_metabox_nonce');
     160            echo '<input type="hidden" name="order_id" value="' . esc_attr($order_id) . '">';
    64161            echo '<input type="submit" class="button" value="' . __('Reset', 'wc-dpd') . '" name="' . esc_attr(self::RESET_ACTION_KEY) . '">';
     162            echo '</form>';
    65163
    66164            return;
     
    95193        ?>
    96194
    97         <form method="post" name="form">
     195        <form method="post">
     196            <?php wp_nonce_field('dpd_metabox_save', 'dpd_metabox_nonce'); ?>
     197            <input type="hidden" name="order_id" value="<?php echo esc_attr($order_id); ?>">
     198           
    98199            <?php if (!empty($bank_id_options)) : ?>
    99200                <p>
     
    177278        <?php
    178279    }
    179 
    180     /**
    181      * Save data from metabox fiels
    182      *
    183      * @return void
    184      */
    185     public static function processExport($order_id)
    186     {
    187         if (!is_admin()) {
    188             return;
    189         }
    190 
    191         if (!$order_id) {
    192             return;
    193         }
    194 
    195         $order = wc_get_order($order_id);
    196 
    197         if (!$order instanceof \WC_Order) {
    198             return;
    199         }
    200 
    201         // Save metabox fields
    202         if (isset($_POST[Order::SHIPPING_META_KEY])) {
    203             $order->update_meta_data(Order::SHIPPING_META_KEY, sanitize_text_field($_POST[Order::SHIPPING_META_KEY]));
    204         }
    205 
    206         if (isset($_POST[Order::ADDRESS_ID_META_KEY])) {
    207             $order->update_meta_data(Order::ADDRESS_ID_META_KEY, sanitize_text_field($_POST[Order::ADDRESS_ID_META_KEY]));
    208         }
    209 
    210         if (isset($_POST[Order::BANK_ID_META_KEY])) {
    211             $order->update_meta_data(Order::BANK_ID_META_KEY, sanitize_text_field($_POST[Order::BANK_ID_META_KEY]));
    212         }
    213 
    214         if (isset($_POST[Order::NOTIFICATION_META_KEY])) {
    215             $order->update_meta_data(Order::NOTIFICATION_META_KEY, $_POST[Order::NOTIFICATION_META_KEY] == 'on' ? 'yes' : 'no');
    216         } else {
    217             $order->update_meta_data(Order::NOTIFICATION_META_KEY, 'no');
    218         }
    219 
    220         if (isset($_POST[Order::REFERENCE_1_META_KEY])) {
    221             $order->update_meta_data(Order::REFERENCE_1_META_KEY, sanitize_text_field($_POST[Order::REFERENCE_1_META_KEY]));
    222         }
    223 
    224         if (isset($_POST[Order::REFERENCE_2_META_KEY])) {
    225             $order->update_meta_data(Order::REFERENCE_2_META_KEY, sanitize_text_field($_POST[Order::REFERENCE_2_META_KEY]));
    226         }
    227 
    228         if (isset($_POST[Order::PACKAGE_WEIGHT_META_KEY])) {
    229             $order->update_meta_data(Order::PACKAGE_WEIGHT_META_KEY, sanitize_text_field($_POST[Order::PACKAGE_WEIGHT_META_KEY]));
    230         }
    231 
    232         $order->save_meta_data();
    233 
    234         // Process export
    235         if (isset($_POST[self::EXPORT_ACTION_KEY])) {
    236             Order::export($order_id);
    237         }
    238     }
    239 
    240     /**
    241      * Process order reset
    242      *
    243      * @param int $order_id
    244      *
    245      * @return mixed
    246      */
    247     public static function processReset($order_id)
    248     {
    249         if (!is_admin()) {
    250             return;
    251         }
    252 
    253         if (!$order_id) {
    254             return;
    255         }
    256 
    257         $order = wc_get_order($order_id);
    258 
    259         if (!$order instanceof \WC_Order) {
    260             return;
    261         }
    262 
    263         if ($order_id && isset($_POST[self::RESET_ACTION_KEY])) {
    264             Order::reset($order_id);
    265 
    266             $order_edit_url = admin_url('post.php?post=' . $order_id . '&action=edit');
    267 
    268             wp_safe_redirect($order_edit_url);
    269             exit;
    270         }
    271 
    272         return $order_id;
    273     }
    274280}
  • wc-dpd/trunk/readme.txt

    r3255337 r3292802  
    44Donate link: https://platobnebrany.sk/
    55Requires at least: 5.3
    6 Tested up to: 6.7.2
     6Tested up to: 6.8.1
    77Requires PHP: 7.4
    8 Stable tag: 8.0.1
     8Stable tag: 8.0.2
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    6262
    6363== Changelog ==
     64
     65= 8.0.2 =
     66* Fixed export metabox method
    6467
    6568= 8.0.1 =
  • wc-dpd/trunk/wc-dpd.php

    r3255337 r3292802  
    44 * Plugin Name: DPD SK for WooCommerce
    55 * Description: DPD SK plugin for WooCommerce which exports orders to the DPD through their API
    6  * Version: 8.0.1
     6 * Version: 8.0.2
    77 * Author: Webikon
    88 * Author URI: https://www.webikon.sk
     
    1212 * Domain Path: /languages
    1313 * Requires at least: 5.3
    14  * Tested up to: 6.7.2
     14 * Tested up to: 6.8.1
    1515 * Requires PHP: 7.4
    1616 * WC requires at least: 7.0
    17  * WC tested up to: 9.7.1
     17 * WC tested up to: 9.8.5
    1818 */
    1919
Note: See TracChangeset for help on using the changeset viewer.