product-settings.php ➔ printcenter_ssi_data_fields()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
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 22 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
 * Product settings
4
 *
5
 * @package     PrintCenter\Admin\Settings\Product
6
 * @since       1.0.0
7
 */
8
9
10
// Exit if accessed directly
11
if( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
16
/**
17
 * Add SSI fields to product details box
18
 *
19
 * @since       1.0.0
20
 * @return      void
21
 */
22
function printcenter_ssi_data_fields() {
23
	global $woocommerce, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
24
	?>
25
	<div class="options_group">
26
		<?php
27
		woocommerce_wp_select( array(
28
			'id'          => '_ssi_sku',
29
			'label'       => __( 'Shirt', 'printcenter' ),
30
			'description' => __( 'Choose the type of shirt this design is available for.', 'printcenter' ),
31
			'options'     => printcenter_get_shirts(),
32
		) );
33
		?>
34
	</div>
35
	<div class="options_group">
36
		<?php
37
		woocommerce_wp_select( array(
38
			'id'          => '_ssi_location',
39
			'label'       => __( 'Design Location', 'printcenter' ),
40
			'description' => __( 'Select where on the shirt the design will be printed.', 'printcenter' ),
41
			'options'     => array(
42
				'front' => __( 'Full Front', 'printcenter' ),
43
				'back'  => __( 'Full Back', 'printcenter' ),
44
				'left'  => __( 'Left Chest', 'printcenter' )
45
			),
46
		) );
47
		woocommerce_wp_select( array(
48
			'id'          => '_ssi_sizing',
49
			'label'       => __( 'Design Category', 'printcenter' ),
50
			'description' => __( 'Specify design size.', 'printcenter' ),
51
			'options'     => array(
52
				'SM'  => __( 'Small', 'printcenter' ),
53
				'REG' => __( 'Regular', 'printcenter' ),
54
				'LG'  => __( 'Large', 'printcenter' )
55
			),
56
		) );
57
		woocommerce_wp_text_input( array(
58
			'id'          => '_ssi_art',
59
			'label'       => __( 'Design Art', 'printcenter' ),
60
			'description' => __( 'Enter the URL to the printable design file.', 'printcenter' ),
61
		) );
62
		?>
63
	</div>
64
	<?php
65
}
66
add_action( 'woocommerce_product_options_general_product_data', 'printcenter_ssi_data_fields' );
67
68
69
/**
70
 * Add our custom fields to the product meta save process
71
 *
72
 * @since       1.0.0
73
 * @param       int $post_id The ID of the product we are editing
74
 * @param       object $post The WordPress post object for this product
75
 * @return      void
76
 */
77
function printcenter_ssi_data_save( $post_id, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
	$art = isset( $_POST['_ssi_art'] ) ? $_POST['_ssi_art'] : '';
79
80
	update_post_meta( $post_id, '_ssi_sku', $_POST['_ssi_sku'] );
81
	update_post_meta( $post_id, '_ssi_location', $_POST['_ssi_location'] );
82
	update_post_meta( $post_id, '_ssi_sizing', $_POST['_ssi_sizing'] );
83
	update_post_meta( $post_id, '_ssi_art', $art );
84
}
85
add_action( 'woocommerce_process_product_meta', 'printcenter_ssi_data_save', 10, 2 );