forked from WPGov/telegram-bot
/
custom-post-types.php
371 lines (186 loc) · 8.14 KB
/
custom-post-types.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
add_action( 'init', 'telegram_subscribers_cpt', 0 );
function telegram_subscribers_cpt() {
$labels = array(
'name' => _x( 'Subscribers', 'Post Type General Name', 'telegram-bot' ),
'singular_name' => _x( 'Subscriber', 'Post Type Singular Name', 'telegram-bot' ),
'menu_name' => __( 'Telegram', 'telegram-bot' ),
'name_admin_bar' => __( 'Subscribers', 'telegram-bot' ),
'parent_item_colon' => __( 'Parent Item:', 'telegram-bot' ),
'all_items' => __( 'All Items', 'telegram-bot' ),
'add_new_item' => __( 'Add New Item', 'telegram-bot' ),
'add_new' => __( 'Add New', 'telegram-bot' ),
'new_item' => __( 'New Item', 'telegram-bot' ),
'edit_item' => __( 'Edit Item', 'telegram-bot' ),
'update_item' => __( 'Update Item', 'telegram-bot' ),
'view_item' => __( 'View Item', 'telegram-bot' ),
'search_items' => __( 'Search Item', 'telegram-bot' ),
'not_found' => __( 'Not found', 'telegram-bot' ),
'not_found_in_trash' => __( 'Not found in Trash', 'telegram-bot' )
);
$args = array(
'label' => __( 'Subscriber', 'telegram-bot' ),
'description' => __( 'Post Type Description', 'telegram-bot' ),
'labels' => $labels,
'taxonomies' => array(
'telegram_groups'
),
'supports' => array(
'title',
'editor',
'custom-fields'
),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false
)
);
register_post_type( 'telegram_subscribers', $args );
//TELEGRAM GROUPS
$labels = array(
'name' => _x( 'Subscribers', 'Post Type General Name', 'telegram-bot' ),
'singular_name' => _x( 'Subscriber', 'Post Type Singular Name', 'telegram-bot' ),
'menu_name' => __( 'Telegram', 'telegram-bot' ),
'name_admin_bar' => __( 'Subscribers', 'telegram-bot' ),
'parent_item_colon' => __( 'Parent Item:', 'telegram-bot' ),
'all_items' => __( 'All Items', 'telegram-bot' ),
'add_new_item' => __( 'Add New Item', 'telegram-bot' ),
'add_new' => __( 'Add New', 'telegram-bot' ),
'new_item' => __( 'New Item', 'telegram-bot' ),
'edit_item' => __( 'Edit Item', 'telegram-bot' ),
'update_item' => __( 'Update Item', 'telegram-bot' ),
'view_item' => __( 'View Item', 'telegram-bot' ),
'search_items' => __( 'Search Item', 'telegram-bot' ),
'not_found' => __( 'Not found', 'telegram-bot' ),
'not_found_in_trash' => __( 'Not found in Trash', 'telegram-bot' )
);
$args = array(
'label' => __( 'Groups', 'telegram-bot' ),
'description' => __( 'Post Type Description', 'telegram-bot' ),
'labels' => $labels,
'taxonomies' => array(
'telegram_groups'
),
'supports' => array(
'title',
'editor',
'custom-fields'
),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false
)
);
register_post_type( 'telegram_groups', $args );
$labels = array(
'name' => _x( 'Commands', 'Post Type General Name', 'telegram-bot' ),
'singular_name' => _x( 'Command', 'Post Type Singular Name', 'telegram-bot' ),
'menu_name' => __( 'Commands', 'telegram-bot' ),
'name_admin_bar' => __( 'Commands', 'telegram-bot' ),
'parent_item_colon' => __( 'Parent Item:', 'telegram-bot' ),
'all_items' => __( 'All Commands', 'telegram-bot' ),
'add_new_item' => __( 'Add New Command', 'telegram-bot' ),
'add_new' => __( 'Add New', 'telegram-bot' ),
'new_item' => __( 'New Command', 'telegram-bot' ),
'edit_item' => __( 'Edit Command', 'telegram-bot' ),
'update_item' => __( 'Update Command', 'telegram-bot' ),
'view_item' => __( 'View Command', 'telegram-bot' ),
'search_items' => __( 'Search Command', 'telegram-bot' ),
'not_found' => __( 'Not found', 'telegram-bot' ),
'not_found_in_trash' => __( 'Not found in Trash', 'telegram-bot' )
);
$args = array(
'label' => __( 'Command', 'telegram-bot' ),
'description' => __( 'Post Type Description', 'telegram-bot' ),
'labels' => $labels,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page'
);
register_post_type( 'telegram_commands', $args );
}
function telegram_commands_meta_box() {
$screens = array( 'telegram_commands' );
foreach ( $screens as $screen ) {
add_meta_box(
'telegram_command_extra',
__( 'Extra', 'telegram-bot' ),
'telegram_command_extra_callback',
$screen,
'side',
'core'
);
}
}
add_action( 'add_meta_boxes', 'telegram_commands_meta_box' );
function telegram_command_extra_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'telegram_command_extra_save_meta_box_data', 'telegram_command_extra_nonce' );
echo '<tr valign="top"><td>';
echo '<label for="telegram_keyboard_template">';
_e( 'Keyboard Template', 'telegram_keyboard_template' );
echo '</label> ';
echo '<input type="text" id="telegram_kt" name="telegram_kt" value="' . esc_attr( get_post_meta( $post->ID, 'telegram_kt', true ) ) . '" size="25" />';
echo '<br><small>Example: <b>1,2,3;4,5,6;Text</b></small><br><br><input type="checkbox" name="telegram_otk" id="telegram_otk" value="1"';
checked( '1', get_post_meta( $post->ID, 'telegram_otk', true ) );
echo ' /> <label for="telegram_otk">' . __( 'One-Time Keyboard', 'telegram-bot' ) . '</label>';
}
function telegram_command_extra_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['telegram_command_extra_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['telegram_command_extra_nonce'], 'telegram_command_extra_save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'telegram_commands' == $_POST['post_type'] && current_user_can( 'edit_post', $post_id ) ) {
if ( isset( $_POST['telegram_kt'] ) ) {
update_post_meta( $post_id, 'telegram_kt', sanitize_text_field( $_POST['telegram_kt'] ) );
}
if ( isset( $_POST['telegram_otk'] ) ) {
update_post_meta( $post_id, 'telegram_otk', 1 );
} else {
update_post_meta( $post_id, 'telegram_otk', 0 );
}
}
}
add_action( 'save_post', 'telegram_command_extra_save_meta_box_data' );