1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Shipping Zones Admin Page |
4
|
|
|
* |
5
|
|
|
* @author WooThemes |
6
|
|
|
* @category Admin |
7
|
|
|
* @package WooCommerce/Admin |
8
|
|
|
* @version 2.5.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
12
|
|
|
exit; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* WC_Admin_Shipping_Zones Class. |
17
|
|
|
*/ |
18
|
|
|
class WC_Admin_Shipping_Zones { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Handles output of the reports page in admin. |
22
|
|
|
*/ |
23
|
|
|
public static function output() { |
24
|
|
|
if ( isset( $_REQUEST['zone_id'] ) ) { |
25
|
|
|
self::zone_methods_screen( absint( $_REQUEST['zone_id'] ) ); |
26
|
|
|
} elseif ( isset( $_REQUEST['instance_id'] ) ) { |
27
|
|
|
self::instance_settings_screen( absint( $_REQUEST['instance_id'] ) ); |
28
|
|
|
} else { |
29
|
|
|
self::zones_screen(); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Show method for a zone |
35
|
|
|
* @param int $zone_id |
36
|
|
|
*/ |
37
|
|
|
public static function zone_methods_screen( $zone_id ) { |
38
|
|
|
$wc_shipping = WC_Shipping ::instance(); |
39
|
|
|
$zone = WC_Shipping_Zones::get_zone( $zone_id ); |
40
|
|
|
$shipping_methods = $wc_shipping ->get_shipping_methods(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if ( ! $zone ) { |
43
|
|
|
wp_die( __( 'Zone does not exist!', 'woocommerce' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array( |
47
|
|
|
'methods' => $zone->get_shipping_methods(), |
48
|
|
|
'zone_id' => $zone->get_zone_id(), |
49
|
|
|
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ), |
50
|
|
|
'strings' => array( |
51
|
|
|
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ), |
52
|
|
|
'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ), |
53
|
|
|
'add_method_failed' => __( 'Shipping method could not be added. Please retry.', 'woocommerce' ), |
54
|
|
|
'yes' => __( 'Yes', 'woocommerce' ), |
55
|
|
|
), |
56
|
|
|
) ); |
57
|
|
|
wp_enqueue_script( 'wc-shipping-zone-methods' ); |
58
|
|
|
|
59
|
|
|
include_once( 'views/html-admin-page-shipping-zone-methods.php' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Show zones |
64
|
|
|
*/ |
65
|
|
|
public static function zones_screen() { |
66
|
|
|
$allowed_countries = WC()->countries->get_allowed_countries(); |
|
|
|
|
67
|
|
|
$continents = WC()->countries->get_continents(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
wp_localize_script( 'wc-shipping-zones', 'shippingZonesLocalizeScript', array( |
70
|
|
|
'zones' => WC_Shipping_Zones::get_zones(), |
71
|
|
|
'default_zone' => array( |
72
|
|
|
'zone_id' => 0, |
73
|
|
|
'zone_name' => '', |
74
|
|
|
'zone_order' => null, |
75
|
|
|
), |
76
|
|
|
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ), |
77
|
|
|
'strings' => array( |
78
|
|
|
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ), |
79
|
|
|
'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ) |
80
|
|
|
), |
81
|
|
|
) ); |
82
|
|
|
wp_enqueue_script( 'wc-shipping-zones' ); |
83
|
|
|
|
84
|
|
|
include_once( 'views/html-admin-page-shipping-zones.php' ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Show instance settings |
89
|
|
|
* @param int $instance_id |
90
|
|
|
*/ |
91
|
|
|
public static function instance_settings_screen( $instance_id ) { |
92
|
|
|
$zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id ); |
93
|
|
|
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id ); |
94
|
|
|
|
95
|
|
|
if ( ! $shipping_method ) { |
96
|
|
|
wp_die( __( 'Invalid shipping method!', 'woocommerce' ) ); |
97
|
|
|
} |
98
|
|
|
if ( ! $zone ) { |
99
|
|
|
wp_die( __( 'Zone does not exist!', 'woocommerce' ) ); |
100
|
|
|
} |
101
|
|
|
if ( ! $shipping_method->has_settings() ) { |
102
|
|
|
wp_die( __( 'This shipping method does not have any settings to configure.', 'woocommerce' ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ( ! empty( $_POST['save_method'] ) ) { |
106
|
|
|
|
107
|
|
|
if ( empty( $_POST['woocommerce_save_method_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_save_method_nonce'], 'woocommerce_save_method' )) { |
108
|
|
|
echo '<div class="updated error"><p>' . __( 'Edit failed. Please try again.', 'woocommerce' ) . '</p></div>'; |
109
|
|
|
|
110
|
|
|
} elseif ( $shipping_method->process_admin_options() ) { |
111
|
|
|
echo '<div class="updated success"><p>' . __( 'Your settings have been saved.', 'woocommerce' ) . '</p></div>'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$shipping_method->display_errors(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
?> |
118
|
|
|
<div class="wrap woocommerce"> |
119
|
|
|
<h1><?php echo esc_html( $shipping_method->get_method_title() ); ?> <small class="wc-admin-breadcrumb">< <a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping&zone_id=' . absint( $zone->get_zone_id() ) ) ); ?>"><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>)</a> < <a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a></small></h1> |
120
|
|
|
<form id="add-method" method="post"> |
121
|
|
|
<?php $shipping_method->admin_options(); ?> |
122
|
|
|
<p class="submit"><input type="submit" class="button button-primary" name="save_method" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" /></p> |
123
|
|
|
<?php wp_nonce_field( 'woocommerce_save_method', 'woocommerce_save_method_nonce' ); ?> |
124
|
|
|
</form> |
125
|
|
|
</div> |
126
|
|
|
<?php |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.