/
love_porinite.php
185 lines (149 loc) · 4.52 KB
/
love_porinite.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
<?php
// bringing CMB2 form to frontend
############################################
function love_porinite(){
$args = array(
'public' => true,
'label' => 'Parinite',
'taxonomies' => array('category', 'post_tag')
);
register_post_type('parinite', $args);
}
add_action('cmb2_init', 'front_end_metabox');
function front_end_metabox(){
$prefix = '_frontend_';
$cmb = new_cmb2_box(array(
'id' => $prefix.'form',
'title' => __('Post Info', 'sexy'),
'object_types' => array('parinite'),
'save_fields' => false,
));
$cmb->add_field(array(
'name' => __('Name', 'sexy'),
'id' => $prefix.'name',
'type' => 'text',
// 'repeatable' => true
));
$cmb->add_field(array(
'name' => __('Email', 'sexy'),
'id' => $prefix.'email',
'type' => 'text_email',
// 'repeatable' => true
));
$cmb->add_field(array(
'name' => __('Phone Number', 'sexy'),
'id' => $prefix.'phone',
'type' => 'text',
// 'repeatable' => true
));
}
// end of the function front_end_metabox
add_action('cmb2_init', 'love_porinite');
function get_parinite(){
$metabox_id = '_frontend_form';
$object_id = 'fake-object-id';
return cmb2_get_metabox($metabox_id, $object_id);
}
function parinite_shortcode($atts = array()){
$cmb = get_parinite();
$post_types = $cmb->prop('object_types');
$user_id = get_current_user_id();
$atts = shortcode_atts(array(
'post_author' => $user_id ? $user_id : 1,
'post_status' => 'pending',
'post_type' => reset( $post_types ),
), $atts, 'cmb-frontend-form');
foreach ($atts as $key => $value) {
$cmb->add_hidden_field(array(
'id' => "atts[$key]",
'type' => 'hidden',
'default' => $value,
));
}
$output = '';
// if error
if (($error = $cmb->prop('submission_error')) && is_wp_error($error)) {
$output .='<h3>'. sprintf(__('There was an error in the submission: %s', 'sexy'),'<strong>'.$error-> get_error_message().'</strong>').'</h3>';
}
// if succes
if(isset($_GET['post_submitted']) && ($post = get_post(absint($_GET['post_submitted'])))){
$name = get_post_meta($post->ID, '_frontend_name', 1);
$name = $name ? ' '.$name : '';
$output .='<h3>'. sprintf(__('Thank you%s, your new post has been submitted and is pending review by a site administrator .', 'sexy'), esc_html( $name )).'</h3>';
}
$output .= cmb2_get_metabox_form( $cmb, 'fake-object-id', array(
'save_button' => __('Submit Post', 'sexy')
));
return $output;
}
add_shortcode('cmb-frontend-form', 'parinite_shortcode');
function parinite_handle(){
if(empty($_POST) || ! isset($_POST['submit-cmb'], $_POST['object_id'])
){
return false;
}
// above line is important speciall [submit-cmb] don't mix it with
// [submit_cmb] and also [object_id]
$cmb = get_parinite();
$post_data = array();
// get our shortcode
if(isset($_POST['atts'])){
foreach ( (array) $_POST['atts'] as $key => $value) {
$post_data[$key] = sanitize_text_field($value);
}
unset( $_POST['atts'] );
}
// security nonce
if(
! isset($_POST[$cmb->nonce()])
|| ! wp_verify_nonce($_POST[$cmb->nonce()], $cmb->nonce())
){
return $cmb->prop('submission_error', new WP_Error('security_fail', __('security check failed')));
}
// check phone number submitted
if(
empty($_POST['_frontend_phone'])
){
return $cmb->prop('submission_error',
new WP_Error(
'post_data_missing', __('New post requires a title')
)
);
}
// phone number is not default title
if(
$cmb->get_field('_frontend_phone')->default() == $_POST['_frontend_phone']
){
return $cmb->prop(
'submission_error', new WP_Error(
'post_data_missing', __('Please enter a post title.')
)
);
}
// fetch sanitize value
$sanitize_values = $cmb->get_sanitized_values($_POST);
$post_data['post_title'] = $sanitize_values['_frontend_phone'];
unset($sanitize_values['_frontend_phone']);
$post_data['post_email'] = $sanitize_values['_frontend_email'];
unset($sanitize_values['_frontend_email']);
$new_submission_id = wp_insert_post($post_data, true);
if ( is_wp_error( $new_submission_id ) ) {
return $cmb->prop( 'submission_error', $new_submission_id );
}
unset( $post_data['post_type'] );
unset( $post_data['post_status'] );
foreach ($sanitize_values as $key => $value) {
if(is_array($value)){
$value = array_filter($value);
if(! empty($value)){
update_post_meta($new_submission_id, $key, $value);
}
else{
update_post_meta($new_submission_id, $key, $value);
}
}
}
wp_redirect(esc_url_raw( add_query_arg('post_submitted', $new_submission_id)));
exit;
}
add_action('cmb2_after_init', 'parinite_handle');