Completed
Pull Request — master (#9826)
by Mike
10:01
created

instance_settings_screen()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 33
rs 6.7273
cc 7
eloc 20
nc 16
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 12.

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.

Loading history...
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
	public static function zone_methods_screen( $zone_id ) {
34
		$wc_shipping      = WC_Shipping      ::instance();
35
		$zone             = WC_Shipping_Zones::get_zone( $zone_id );
36
		$shipping_methods = $wc_shipping ->get_shipping_methods();
0 ignored issues
show
Unused Code introduced by
$shipping_methods is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
38
		if ( ! $zone ) {
39
			wp_die( __( 'Zone does not exist!', 'woocommerce' ) );
40
		}
41
42
		wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
43
            'methods'                 => $zone->get_shipping_methods(),
44
			'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
45
			'strings'                 => array(
46
				'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
47
				'save_failed'             => __( 'Your changes were not saved. Please retry.', 'woocommerce' )
48
			),
49
		) );
50
		wp_enqueue_script( 'wc-shipping-zone-methods' );
51
52
		include_once( 'views/html-admin-page-shipping-zone-methods.php' );
53
	}
54
55
	public static function zones_screen() {
56
		$allowed_countries = WC()->countries->get_allowed_countries();
0 ignored issues
show
Unused Code introduced by
$allowed_countries is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
        $continents        = WC()->countries->get_continents();
0 ignored issues
show
Unused Code introduced by
$continents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
59
		wp_localize_script( 'wc-shipping-zones', 'shippingZonesLocalizeScript', array(
60
            'zones'         => WC_Shipping_Zones::get_zones(),
61
            'default_zone'  => array(
62
				'zone_id'    => 0,
63
				'zone_name'  => '',
64
				'zone_order' => null,
65
			),
66
			'wc_shipping_zones_nonce'  => wp_create_nonce( 'wc_shipping_zones_nonce' ),
67
			'strings'       => array(
68
				'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
69
				'save_failed'             => __( 'Your changes were not saved. Please retry.', 'woocommerce' )
70
			),
71
		) );
72
		wp_enqueue_script( 'wc-shipping-zones' );
73
74
		include_once( 'views/html-admin-page-shipping-zones.php' );
75
	}
76
77
	public static function instance_settings_screen( $instance_id ) {
78
		$zone            = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
79
		$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
80
81
		if ( ! $shipping_method ) {
82
			wp_die( __( 'Invalid shipping method!', 'woocommerce' ) );
83
		}
84
		if ( ! $zone ) {
85
			wp_die( __( 'Zone does not exist!', 'woocommerce' ) );
86
		}
87
88
		if ( ! empty( $_POST['save_method'] ) ) {
89
90
			if ( empty( $_POST['woocommerce_save_method_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_save_method_nonce'], 'woocommerce_save_method' )) {
91
				echo '<div class="updated error"><p>' . __( 'Edit failed. Please try again.', 'woocommerce' ) . '</p></div>';
92
93
			} elseif ( $shipping_method->process_instance_options() ) {
94
95
				echo '<div class="updated success"><p>' . __( 'Your settings have been saved.', 'woocommerce' ) . '</p></div>';
96
			}
97
		}
98
99
		?>
100
		<div class="wrap woocommerce">
101
			<h1><?php echo esc_html( $shipping_method->get_method_title() ); ?> <small class="wc-admin-breadcrumb">&lt; <a href=""<?php echo esc_url( admin_url( 'admin.php?page=shipping&zone_id=' . absint( $zone->get_zone_id() ) ) ); ?>"><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>)</a> &lt; <a href="<?php echo esc_url( admin_url( 'admin.php?page=shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a></small></h1>
102
			<form id="add-method" method="post">
103
				<?php $shipping_method->instance_options(); ?>
104
				<p class="submit"><input type="submit" class="button button-primary" name="save_method" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" /></p>
105
				<?php wp_nonce_field( 'woocommerce_save_method', 'woocommerce_save_method_nonce' ); ?>
106
			</form>
107
		</div>
108
		<?php
109
	}
110
}
111