|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Meta boxes |
|
4
|
|
|
* |
|
5
|
|
|
* @package PrintCenter\Admin\SSI_Products\MetaBoxes |
|
6
|
|
|
* @since 1.0.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
// Exit if accessed directly |
|
11
|
|
|
if( ! defined( 'ABSPATH' ) ) { |
|
12
|
|
|
exit; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Register meta boxes |
|
18
|
|
|
* |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
function printcenter_add_ssi_product_meta_boxes() { |
|
23
|
|
|
add_meta_box( 'ssi-product-details', __( 'Details', 'printcenter' ), 'printcenter_render_ssi_product_details_meta_box', 'ssi_product', 'side', 'default' ); |
|
24
|
|
|
} |
|
25
|
|
|
add_action( 'add_meta_boxes', 'printcenter_add_ssi_product_meta_boxes' ); |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Render details meta box |
|
30
|
|
|
* |
|
31
|
|
|
* @since 1.0.0 |
|
32
|
|
|
* @global object $post The post we are editing |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
function printcenter_render_ssi_product_details_meta_box() { |
|
36
|
|
|
global $post; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$ssi_sku = get_post_meta( $post->ID, '_ssi_sku', true ); |
|
39
|
|
|
?> |
|
40
|
|
|
<p> |
|
41
|
|
|
<label for="_ssi_sku"><?php _e( 'SSI SKU:', 'printcenter' ); ?></label> |
|
42
|
|
|
<input type="text" class="widefat" id="_ssi_sku" name="_ssi_sku" value="<?php echo esc_attr( stripslashes( $ssi_sku ) ); ?>" /> |
|
43
|
|
|
</p> |
|
44
|
|
|
<?php |
|
45
|
|
|
// Allow extension of the meta box |
|
46
|
|
|
do_action( 'printcenter_ssi_product_details_meta_box_fields', $post->ID ); |
|
47
|
|
|
|
|
48
|
|
|
wp_nonce_field( basename( __FILE__ ), 'printcenter_ssi_product_details_meta_box_nonce' ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Save post meta when the save_post action is called |
|
54
|
|
|
* |
|
55
|
|
|
* @since 1.0.0 |
|
56
|
|
|
* @param int $post_id The ID of the post we are saving |
|
57
|
|
|
* @global object $post The post we are saving |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
function printcenter_ssi_product_meta_box_save( $post_id ) { |
|
61
|
|
|
global $post; |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// Don't process if nonce can't be validated |
|
64
|
|
|
if( ! isset( $_POST['printcenter_ssi_product_details_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['printcenter_ssi_product_details_meta_box_nonce'], basename( __FILE__ ) ) ) return $post_id; |
|
65
|
|
|
|
|
66
|
|
|
// Don't process if this is an autosave |
|
67
|
|
|
if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) return $post_id; |
|
68
|
|
|
|
|
69
|
|
|
// Don't process if this is a revision |
|
70
|
|
|
if( isset( $post->post_type ) && $post->post_type == 'revision' ) return $post_id; |
|
71
|
|
|
|
|
72
|
|
|
// The default fields that get saved |
|
73
|
|
|
$fields = apply_filters( 'printcenter_ssi_product_meta_box_fields_save', array( |
|
74
|
|
|
'_ssi_sku' |
|
75
|
|
|
) ); |
|
76
|
|
|
|
|
77
|
|
|
foreach( $fields as $field ) { |
|
78
|
|
|
if( isset( $_POST[ $field ] ) ) { |
|
79
|
|
|
if( is_string( $_POST[ $field ] ) ) { |
|
80
|
|
|
$new = esc_attr( $_POST[ $field ] ); |
|
81
|
|
|
} else { |
|
82
|
|
|
$new = $_POST[ $field ]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$new = apply_filters( 'printcenter_ssi_product_meta_box_save_' . $field, $new ); |
|
86
|
|
|
|
|
87
|
|
|
update_post_meta( $post_id, $field, $new ); |
|
88
|
|
|
} else { |
|
89
|
|
|
delete_post_meta( $post_id, $field ); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
add_action( 'save_post', 'printcenter_ssi_product_meta_box_save' ); |
|
94
|
|
|
|
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.