-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-post-type-pasta.php
executable file
·120 lines (108 loc) · 3.95 KB
/
custom-post-type-pasta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
* Register custom taxonomy wp_pizzeria_pizza for pizzas
*/
Class WP_Pizzeria_Pasta extends CPT_Factory {
protected $post_type = 'wp_pizzeria_pasta';
protected function __construct() {
parent::construct( $this );
}
public function register_post_type() {
//setup labels
$labels = array(
'name' => esc_html__( 'Pasta', 'wp_pizzeria' ),
'singular_name' => esc_html__( 'Pasta', 'wp_pizzeria' ),
'add_new' => esc_html__( 'Add Pasta', 'wp_pizzeria' ),
'add_new_item' => esc_html__( 'Add Pasta', 'wp_pizzeria' ),
'edit_item' => esc_html__( 'Edit Pasta', 'wp_pizzeria' ),
'new_item' => esc_html__( 'New Pasta', 'wp_pizzeria' ),
'all_items' => esc_html__( 'Pasta', 'wp_pizzeria' ),
'view_item' => esc_html__( 'View Pasta', 'wp_pizzeria' ),
'search_items' => esc_html__( 'Search Pasta', 'wp_pizzeria' ),
'not_found' => esc_html__( 'No pasta found', 'wp_pizzeria' ),
'not_found_in_trash' => esc_html__( 'No pasta in the trash', 'wp_pizzeria' ),
'parent_item_colon' => '',
'menu_name' => esc_html__( 'Pasta', 'wp_pizzeria' )
);
//register custom post type using before declared labels
register_post_type( $this->post_type, array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'rewrite' => array( 'slug' => 'pasta', 'with_front' => true ),
'query_var' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'wp_pizzeria_pasta_category' ),
'register_meta_box_cb' => array( $this, 'custom_box' )
) );
}
/* Custom Meta boxes */
public function custom_box() {
if ( true === isset( $_GET['post'] ) ) {
$post_id = absint( $_GET['post'] );
} elseif ( true === isset( $_POST['post_ID'] ) ) {
$post_id = absint( $_POST['post_ID'] );
}
if (
( true === isset( $post_id ) && $this->post_type === get_post_type( $post_id ) )
|| ( true === isset( $_GET['post_type'] ) && $this->post_type === $_GET['post_type'] )
) {
remove_meta_box( 'pageparentdiv', $this->post_type, 'side' );
add_meta_box(
'wp_pizzeria_pasta_price_custom_box',
esc_html__( 'Pasta price', 'wp_pizzeria' ),
array( $this, 'inner_custom_box' ),
$this->post_type,
'side',
'core'
);
add_meta_box(
'wp_pizzeria_number_custom_box',
esc_html__( 'Pasta menu number', 'wp_pizzeria' ),
array( $this, 'number_inner_custom_box' ),
$this->post_type,
'side',
'core'
);
}
}
public function inner_custom_box( $post ) {
$price = get_post_meta( $post->ID, '_wp_pizzeria_price', true );
if ( false === $price ) {
$price = '';
}
$pizzeria_settings = $this::get_pizzeria_settings();
?>
<p>
<label for="pasta_price"><?php _e( 'Price', 'wp_pizzeria' ); ?></label>
<?php if ( true === array_key_exists( 'currency', $pizzeria_settings )
&& true === array_key_exists( 'currency_pos', $pizzeria_settings )
&& 'before' === $pizzeria_settings['currency_pos']
) {
echo esc_html( $pizzeria_settings['currency'] );
} ?>
<input type="text" id="pasta_price" name="pasta_price" value="<?php echo $price; ?>" />
<?php
if ( true === array_key_exists( 'currency', $pizzeria_settings )
&& ( false === array_key_exists( 'currency_pos', $pizzeria_settings )
|| 'after' === $pizzeria_settings['currency_pos'] )
) {
echo esc_html( $pizzeria_settings['currency'] );
}
?>
</p>
<?php
}
/* Save custom meta boxes content */
public function save_postdata( $post_id ) {
if ( false === $this->can_save( $post_id ) ) {
return false;
}
if ( true === isset( $_POST['pasta_price'] ) ) {
update_post_meta( $post_id, '_wp_pizzeria_price', intval( $_POST['pasta_price'] ) );
}
}
}