-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpt_factory.php
160 lines (136 loc) · 5.38 KB
/
cpt_factory.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
abstract class CPT_Factory {
private static $_instances = array();
public static function getInstance() {
$class = get_called_class();
if (!isset(self::$_instances[$class])) {
self::$_instances[$class] = new $class();
}
return self::$_instances[$class];
}
static private function throw_singleton_error() {
return new WP_Error( 'direct_access_prohibit', __( 'This class is a singleton. Use ::getInstance instead of trying to create new object.', 'wp_pizzeria' ) );
}
private function __clone() {
return self::throw_singleton_error();
}
private function __wakeup() {
return self::throw_singleton_error();
}
abstract public function save_postdata( $post_id );
protected static function construct( $obj ) {
add_action( 'init', array( $obj, 'register_post_type' ), 10, 0 );
add_action( 'save_post', array( $obj, 'save_postdata' ), 10, 1 );
add_action( 'wp_insert_post_data', array( $obj, 'save_menu_number' ), 99, 2 );
add_action( 'dashboard_glance_items', array( $obj, 'add_counts' ), 10, 0 );
add_filter( "manage_edit-{$obj->post_type}_columns", array( $obj, 'edit_columns' ), 10, 1 );
add_action( "manage_{$obj->post_type}_posts_custom_column", array( $obj, 'manage_columns' ), 10, 2 );
}
public function add_counts() {
if ( false === post_type_exists( $this->post_type ) ) {
return;
}
$post_type_obj = get_post_type_object( $this->post_type );
$num_posts = wp_count_posts( $this->post_type );
$num = number_format_i18n( $num_posts->publish );
$text = _n( $post_type_obj->labels->singular_name, $post_type_obj->labels->name, intval( $num_posts->publish ) );
if ( true === current_user_can( 'edit_posts' ) ) {
$edit_url = add_query_arg( array( 'post_type' => $this->post_type ), admin_url( 'edit.php' ) );
$text = sprintf( '<a href="%s">%d %s</a>', $edit_url, $num, $text );
}
echo sprintf( '<li class="t %s">%s</li>', esc_attr( $this->post_type ), esc_html( $text ) );
}
public function edit_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'menu_number' => strip_tags( esc_html__( '#', 'wp_pizzeria' ) ),
'title' => strip_tags( esc_html__( 'Title' ) ),
'category' => strip_tags( esc_html__( 'Category', 'wp_pizzeria' ) ),
'price' => strip_tags( esc_html__( 'Price', 'wp_pizzeria' ) ),
'date' => strip_tags( esc_html__( 'Date', 'wp_pizzeria' ) ),
);
return $columns;
}
public function manage_columns( $column, $post_id ) {
global $post;
switch ( $column ) {
case 'menu_number' :
global $wpdb;
$menu_id = $wpdb->get_var( $wpdb->prepare( "SELECT menu_order FROM $wpdb->posts WHERE ID = %d LIMIT 1", intval( $post_id ) ) );
//TODO: check return value
echo intval( $menu_id );
break;
case 'category' :
$terms = get_the_terms( $post_id, $this->post_type . '_category' );
if ( false === empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) {
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url(
add_query_arg( array(
'post_type' => rawurlencode( $post->post_type ),
$this->post_type . '_category' => rawurlencode( $term->slug )
) , 'edit.php' )
),
esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $this->post_type . '_category', 'display' ) )
);
}
echo join( ', ', $out );
} else {
esc_html_e( 'No Categories', 'wp_pizzeria' );
}
break;
case 'price' :
$pizzeria_settings = maybe_unserialize( get_option( 'wp_pizzeria_settings' ) );
if ( false !== get_post_meta( $post_id, '_wp_pizzeria_price', true ) ) {
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'] );
}
echo esc_html( get_post_meta( $post_id, '_wp_pizzeria_price', true ) );
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'] );
}
} else {
echo '';
}
break;
default :
break;
}
}
protected function can_save( $post_id ) {
if ( true === defined( 'DOING_AUTOSAVE' ) && true === constant( 'DOING_AUTOSAVE' ) ) {
return false;
}
if ( $this->post_type !== get_post_type( $post_id ) ) {
return false;
}
if ( false === current_user_can( 'edit_post', $post_id ) ) {
return false;
}
return true;
}
public static function get_pizzeria_settings() {
return (array) maybe_unserialize( get_option( 'wp_pizzeria_settings' ) );
}
public function number_inner_custom_box( $post ) {
$number = $post->menu_order;
$post_type_obj = get_post_type_object( $this->post_type );
$label = sprintf( __( '%s menu number' ), $post_type_obj->labels->singular_name );
echo sprintf( '<label for="wp_pizzeria_number">%s</label><input type="text" name="wp_pizzeria_number" value="%d"/>', esc_html( $label ), intval( $number ) );
}
function save_menu_number( $data, $postarr ) {
if ( false !== $this->can_save( $postarr['ID'] ) ) {
if ( true === isset( $_POST['wp_pizzeria_number'] ) ) {
$data['menu_order'] = intval( $_POST['wp_pizzeria_number'] );
}
}
return $data;
}
}