-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexi_ajax_post.php
140 lines (114 loc) · 5.56 KB
/
flexi_ajax_post.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
<?php
//Process ajax form submitted by [flexi-form] shortcode
add_action("wp_ajax_flexi_ajax_post", "flexi_ajax_post");
add_action("wp_ajax_nopriv_flexi_ajax_post", "flexi_ajax_post");
//Upload all images from frontend
function flexi_ajax_post()
{
if (
!isset($_POST['flexi-nonce'])
|| !wp_verify_nonce($_POST['flexi-nonce'], 'flexi-nonce')
) {
exit('The form is not valid');
}
// A default response holder, which will have data for sending back to our js file
$response = array(
'error' => false,
'msg' => 'No Message',
);
// Example for creating an response with error information, to know in our js file
// about the error and behave accordingly, like adding error message to the form with JS
if (trim($_POST['upload_type']) == '') {
$response['error'] = true;
$response['error_message'] = 'Improper form fields. Ajax cannot continue.';
// Exit here, for not processing further because of the error
exit(wp_json_encode($response));
}
$attr = flexi_default_args('');
$detail_layout = $attr['detail_layout'];
$edit_page = $attr['edit_page'];
$title = $attr['user-submitted-title'];
$content = $attr['content'];
$category = $attr['category'];
$tags = $attr['tags'];
$upload_type = $attr['upload_type'];
$post_taxonomy = $attr['taxonomy'];
$tag_taxonomy = $attr['tag_taxonomy'];
$edit = $attr['edit'];
$flexi_id = $attr['flexi_id'];
$type = $attr['type'];
$url = $attr['user-submitted-url'];
$unique = $attr['unique'];
if ('flexi' == $upload_type) {
$files = array();
if (isset($_FILES['user-submitted-image'])) {
$files = $_FILES['user-submitted-image'];
}
if ("false" == $edit) {
//Insert new post
if ('url' == $attr['type']) {
//flexi_log($attr);
$result = flexi_submit_url($title, $url, $content, $category, $detail_layout, $tags, $edit_page, $unique);
} else {
$result = flexi_submit($title, $files, $content, $category, $detail_layout, $tags, $edit_page, $unique);
}
$post_id = false;
if (isset($result['id'])) {
$post_id = $result['id'];
}
$error = false;
if (isset($result['error'])) {
$error = array_filter(array_unique($result['error']));
}
//$response['msg'] = $msg . ' ';
if ($post_id) {
do_action("flexi_submit_complete", $post_id);
$response['type'] = "success";
if (flexi_get_option('publish', 'flexi_form_settings', 1) == 1) {
$response['msg'] = "<div class='flexi_alert-box flexi_success'>" . __('Submission completed', 'flexi') . "</div> " . '' . flexi_get_error($result) . '' . flexi_post_toolbar_grid($post_id, true);
} else {
$response['msg'] = "<div class='flexi_alert-box flexi_warning'>" . __('Your submission is under review', 'flexi') . "</div>" . '' . flexi_get_error($result) . '' . flexi_post_toolbar_grid($post_id, true);
}
} else {
$err = '';
$reindex_array = array_values(array_filter($result['error']));
$msg = "";
for ($x = 0; $x < count($reindex_array); $x++) {
// $err .= $reindex_array[$x] . " ";
$msg .= "<div class='flexi_alert-box flexi_error'>" . flexi_error_code($reindex_array[$x]) . '</div>';
}
$response['msg'] = $msg . ' ' . flexi_post_toolbar_grid('', true);
//flexi_log($reindex_array);
/*
if (in_array('file-type', $result['error'])) {
$response['msg'] = "<div class='flexi_error'>" . __('Check your file type', 'flexi') . " " . __('Submission failed', 'flexi') . "</div>";
} else if (in_array('required-category', $result['error'])) {
$response['msg'] = "<div class='flexi_error'>" . __('Category is not specified', 'flexi') . " " . __('Submission failed', 'flexi') . "</div>";
} else {
$response['msg'] = "<div class='flexi_error'>" . __('Submission failed', 'flexi') . "<br>" . __('Error message: ') . $err . "</div>";
}
*/
}
} else {
//Update old post
$result = flexi_update_post($flexi_id, $title, $files, $content, $category, $tags);
if ($flexi_id) {
do_action("flexi_submit_update", $flexi_id);
$response['type'] = "success";
if (flexi_get_option('publish', 'flexi_form_settings', 1) == 1) {
$response['msg'] = "<div class='flexi_alert-box flexi_success'>" . __('Successfully updated', 'flexi') . "</div>" . flexi_post_toolbar_grid($flexi_id, true);
} else {
$response['msg'] = "<div class='flexi_alert-box flexi_warning'>" . __('Your modification is under review.', 'flexi') . "</div>" . flexi_post_toolbar_grid($flexi_id, true);
}
} else {
$response['msg'] = "<div class='flexi_alert-box flexi_error'>" . __('Submission failed', 'flexi') . "</div>" . flexi_post_toolbar_grid('', false);
}
}
} else {
$result['error'][] = "Upload Type Not Supported. Check your form parameters.";
}
// Don't forget to exit at the end of processing
$data = wp_json_encode($response);
echo $data;
die();
}