-
Notifications
You must be signed in to change notification settings - Fork 25
/
RCCWP_Post.php
249 lines (205 loc) · 7.29 KB
/
RCCWP_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* When a posts is saved this class is called for check if exists a write panel with custom fields
* if exists then this processes all the custom fields and save his values in the database
*/
class RCCWP_Post {
/**
* This function is called when a post is saves
*/
public static function SaveCustomFields($postId){
global $flag;
if($flag == 0){
//with this the save_post action don't will be execute twice
$flag = 1;
//security
if(!wp_verify_nonce(@$_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel'))
return $postId;
//the user can edit posts?
if (!(current_user_can('edit_posts', $postId) || current_user_can('edit_published_pages', $postId))){
return $postId;
}
RCCWP_Post::SetCustomWritePanel($postId);
RCCWP_Post::PrepareFieldsValues($postId);
RCCWP_Post::SetMetaValues($postId);
return $postId;
}
}
/**
* Attach a custom write panel to the current post by saving the custom write panel id
* as a meta value for the post
*
* @param integer $postId
*/
public static function SetCustomWritePanel($postId) {
if (array_key_exists('rc-cwp-change-custom-write-panel-id', $_POST)) {
$customWritePanelId = (int)$_POST['rc-cwp-change-custom-write-panel-id'];
} else {
$customWritePanelId = (int)$_POST['rc-cwp-custom-write-panel-id'];
}
if (isset($customWritePanelId)) {
if (!empty($customWritePanelId) && $customWritePanelId != "-1") {
update_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId);
} else {
delete_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY);
}
}
}
/**
* Save all custom field values meta values for the post, this function assumes that
* $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}]
* contains the value of the field named {FIELD_NAME}
*
* @param integer $postId
* @return void
*/
public static function SetMetaValues($postId){
global $wpdb;
$customWritePanelId = (int)$_POST['rc-cwp-custom-write-panel-id'];
//delete file
if(!empty($_POST['magicfields_remove_files'])){
$files = preg_split('/\|\|\|/', $_POST['magicfields_remove_files']);
foreach($files as $file){
do_action('mf_before_delete_file',$file);
@unlink(MF_FILES_PATH.$file);
}
}
if(empty($_POST['magicfields'])){
return true;
}
$customfields = $_POST['magicfields'];
if ( $the_post = wp_is_post_revision($postId))
$postId = $the_post;
if (!empty($customWritePanelId)) {
// --- Delete old values
foreach($customfields as $name => $field){
delete_post_meta($postId,$name);
}
$wpdb->delete( MF_TABLE_POST_META, array( 'post_id' => $postId ), array( '%d' ) );
//Creating the new values
//Iterating the custom fields
foreach($customfields as $name => $groups){
$groups_index = 1;
//Iterating the groups
foreach($groups as $group_id => $fields){
$index = 1;
//Iterating the duplicates
foreach($fields as $value){
// Adding field value meta data
add_post_meta($postId, $name, $value);
$fieldMetaID = $wpdb->insert_id;
$wpdb->insert(
MF_TABLE_POST_META,
array(
'id' => $fieldMetaID,
'field_name' => $name,
'group_count' => $groups_index,
'field_count' => $index,
'post_id' => $postId,
'order_id' => $groups_index
),
array(
'%d',
'%s',
'%d',
'%d',
'%d',
'%d',
)
);
//pre save value
do_action('mf_presave',$fieldMetaID,$name,$groups_index,$index,$postId,$value,$customWritePanelId);
$index++;
}
$groups_index++;
}
}
if( MF_GET_CACHE_IS_ON ) {
purge_cache_dir( MF_GET_CACHE_DIR.$postId );
}
}
}
/**
* This function prepares some custom fields before saving it. It reads $_REQUEST and:
* 1. Adds params to photos uploaded (Image field)
* 2. Formats dates (Date Field)
*
* @param integer postId
*/
public static function PrepareFieldsValues($postId) {
global $wpdb;
// Format Dates
if( isset( $_REQUEST['rc_cwp_meta_date'])){
foreach( $_REQUEST['rc_cwp_meta_date'] as $meta_name ) {
$metaDate = strtotime($_POST[$meta_name]);
$formatted_date = date('Y-m-d',$metaDate);
$_POST[$meta_name] = $formatted_date;
}
}
}
/**
* Get a custom write panel by reading $_REQUEST['custom-write-panel-id'] or the
* To see whether $_GET['post'] has a custom write panel associated to it.
*
* @return Custom Write Panel as an object, returns null if there is no write panels.
*/
public static function GetCustomWritePanel() {
global $wpdb;
if (isset($_GET['post'])) {
$customWritePanelId = get_post_meta((int)$_GET['post'], RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
if (empty($customWritePanelId)) {
if( array_key_exists( 'custom-write-panel-id', $_REQUEST ) ) {
$customWritePanelId = (int)($_REQUEST['custom-write-panel-id']);
} else {
$customWritePanelId = 0;
}
}
}
else if (function_exists('icl_t') && isset($_GET['trid']) ) {
$t_name = $wpdb->prefix . "icl_translations";
$sql = $wpdb->prepare( "SELECT element_id FROM $t_name WHERE element_type='post' AND trid = %d", array( $_GET['trid'] ) );
$element_id = $wpdb->get_col($sql);
$customWritePanelId = get_post_meta((int)$element_id, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
if (empty($customWritePanelId)) {
if( array_key_exists( 'custom-write-panel-id', $_REQUEST ) ) {
$customWritePanelId = (int)($_REQUEST['custom-write-panel-id']);
} else {
$customWritePanelId = 0;
}
}
} else if (isset($_REQUEST['custom-write-panel-id'])) {
$customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
}
$customWritePanel = FALSE;
if (isset($customWritePanelId)) {
include_once('RCCWP_Application.php');
$customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
}
return $customWritePanel;
}
/**
* This Method is Executed when a post is deleted
* @param integer $postId
*/
function DeletePostMetaData($postId) {
global $wpdb;
//only delete images and postmeta fields with write panels
if(count(get_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY))){
$sql = $wpdb->prepare( "SELECT wp_pm.meta_value FROM " . MF_TABLE_POST_META . " mf_pm, " . MF_TABLE_GROUP_FIELDS . " mf_cf, $wpdb->postmeta wp_pm
WHERE mf_pm.field_name = mf_cf.name AND mf_cf.type = 9 AND mf_pm.post_id = %d AND wp_pm.meta_id = mf_pm.id", array( $postId ) );
$images = $wpdb->get_results($sql);
foreach($images as $image) {
if($image->meta_value != '') {
$tmp = sprintf('%s%s',MF_FILES_PATH,$image->meta_value);
do_action('mf_before_delete_file',$image->meta_value);
@unlink($tmp);
}
}
//delete all data of postmeta (WP and MF)
$sql = $wpdb->prepare( "DELETE a,b from $wpdb->postmeta a INNER JOIN " . MF_TABLE_POST_META . " b WHERE a.meta_id = b.id AND a.post_id = %d", array( $postId ) );
$wpdb->query($query);
$sql_mf = $wpdb->prepare( "DELETE from " . MF_TABLE_POST_META . " WHERE post_id = %d", array( $postId ) );
$wpdb->query($sql_mf);
}
}
}