-
Notifications
You must be signed in to change notification settings - Fork 7
/
RCCWP_CustomField.php
executable file
·474 lines (410 loc) · 14.7 KB
/
RCCWP_CustomField.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* @package FlutterDatabaseObjects
*/
/**
* Create/Edit/Delete custom fields
* @package FlutterDatabaseObjects
*/
class RCCWP_CustomField
{
/**
* Create a new custom field
*
* @param id $customGroupId the id of the group that will contain the field
* @param string $name the name of the field, the name is used to uniquely identify the field
* when retrieving its value.
* @param string $label the label of the field, the label is displayed beside the field
* in Write tab.
* @param integer $order the order of the field when it is displayed in
* the Write tab.
* @param integer $required_field whether this field is a required field. Required fields
* doesn't allow users to save a post if they are null.
*
* @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
* @param array $options array of strings that represent the list of the field if
* its type is list.
* @param array $default_value array of strings that represent default value(s) of
* of the field if its type is list.
* @param array $properties an array containing extra properties of the field.
* @return the new field id
*/
function Create($customGroupId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null,$duplicate)
{
global $wpdb;
$name = stripslashes(stripslashes($name));
$name = addslashes($name);
$label = stripslashes(stripslashes($label));
$label = addslashes($label);
$sql = sprintf(
"INSERT INTO " . RC_CWP_TABLE_GROUP_FIELDS .
" (group_id, name, description, display_order, required_field, type, CSS, duplicate) values (%d, %s, %s, %d, %d, %d, %s, %d)",
$customGroupId,
RC_Format::TextToSql($name),
RC_Format::TextToSql($label),
$order,
$required_field,
$type,
"'".$_POST['custom-field-css']."'",
$duplicate
);
$wpdb->query($sql);
$customFieldId = $wpdb->insert_id;
$field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
if ($field_type->has_options == "true")
{
if (!is_array($options)) {
$options = stripslashes($options);
$options = explode("\n", $options);
}
array_walk($options, array(RC_Format, TrimArrayValues));
$options = addslashes(serialize($options));
if (!is_array($default_value)) {
$default_value = stripslashes($default_value);
$default_value = explode("\n", $default_value);
}
array_walk($default_value, array(RC_Format, TrimArrayValues));
$default_value = addslashes(serialize($default_value));
$sql = sprintf(
"INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
" (custom_field_id, options, default_option) values (%d, %s, %s)",
$customFieldId,
RC_Format::TextToSql($options),
RC_Format::TextToSql($default_value)
);
$wpdb->query($sql);
}
if ($field_type->has_properties == "true")
{
$sql = sprintf(
"INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
" (custom_field_id, properties) values (%d, %s)",
$customFieldId,
RC_Format::TextToSql(serialize($properties))
);
$wpdb->query($sql);
}
return $customFieldId;
}
/**
* Delete a field
*
* @param integer $customFieldId field id
*/
function Delete($customFieldId = null)
{
global $wpdb;
$customField = RCCWP_CustomField::Get($customFieldId);
$sql = sprintf(
"DELETE FROM " . RC_CWP_TABLE_GROUP_FIELDS .
" WHERE id = %d",
$customFieldId
);
$wpdb->query($sql);
if ($customField->has_options == "true")
{
$sql = sprintf(
"DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
" WHERE custom_field_id = %d",
$customFieldId
);
$wpdb->query($sql);
}
}
/**
* Get the field information including properties, options and default value(s)
*
* @param integer $customFieldId field id
* @return an object containing information about fields. The object contains
* 3 objects: properties, options and default_value
*/
function Get($customFieldId)
{
global $wpdb;
$sql = "SELECT cf.group_id, cf.id, cf.name, cf.CSS, tt.id AS type_id, tt.name AS type, cf.description, cf.display_order, cf.required_field, co.options, co.default_option AS default_value, tt.has_options, cp.properties, tt.has_properties, tt.allow_multiple_values, duplicate FROM " . RC_CWP_TABLE_GROUP_FIELDS .
" cf LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" .
" LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" .
" JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" .
" WHERE cf.id = " . $customFieldId;
$results = $wpdb->get_row($sql);
$results->options = unserialize($results->options);
$results->properties = unserialize($results->properties);
$results->default_value = unserialize($results->default_value);
return $results;
}
/**
* Retrievies information about a specified type
*
* @param integer $customFieldTypeId the type id, if null, a list of all types will be returned
* @return a list/object containing information about the specified type. The information
* includes id, name, description, has_options, has_properties, and
* allow_multiple_values (whether fields of that type can have more than one default value)
*/
function GetCustomFieldTypes($customFieldTypeId = null)
{
global $wpdb;
if (isset($customFieldTypeId))
{
$sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES .
" WHERE id = " . (int)$customFieldTypeId;
$results = $wpdb->get_row($sql);
}
else
{
$sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES;
$results = $wpdb->get_results($sql);
if (!isset($results))
$results = array();
}
return $results;
}
/**
* Retrieves the value of a custom field for a specified post
*
* @param integer $postId post id
* @param string $customFieldName field name
* @return a string containing field value
*/
/*function GetCustomFieldValue($postId, $customFieldName)
{
// TODO Get custom field id
$customFieldId = 1;
// TODO Given customFieldID, $groupIndex and $fieldIndex get meta_id
$fieldMetaID = 1;
// use get_post_meta_by_id to get meta value
return get_post_meta($postId, $customFieldName, true);
}*/
function GetMetaID($postId, $customFieldName, $groupIndex=1, $fieldIndex=1){
global $wpdb;
// Given $postId, $customFieldName, $groupIndex and $fieldIndex get meta_id
return $wpdb->get_var("SELECT id FROM " . RC_CWP_TABLE_POST_META .
" WHERE field_name = '$customFieldName' AND group_count = $groupIndex ".
" AND field_count = $fieldIndex AND post_id = $postId" );
}
/**
* Retrieves the value of a custom field for a specified post
*
* @param boolean $single
* @param integer $postId
* @param string $customFieldName
* @param integer $groupIndex
* @param integer $fieldIndex
* @return a
*/
function GetCustomFieldValues($single, $postId, $customFieldName, $groupIndex=1, $fieldIndex=1)
{
global $wpdb;
$fieldMetaID = RCCWP_CustomField::GetMetaID($postId, $customFieldName, $groupIndex, $fieldIndex);
// for backward compatability, if no accociated row was found, use old method
if (!$fieldMetaID){
//$customFieldName = $wpdb->get_var("SELECT name FROM ". RC_CWP_TABLE_GROUP_FIELDS .
// " WHERE id = '$customFieldId'");
return get_post_meta($postId, $customFieldName, $single);
}
// Get meta value
$mid = (int) $fieldMetaID;
$meta = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
if (!$single) return unserialize($meta->meta_value);
return $meta->meta_value;
}
/**
* Get number of group duplicates given field name. The function returns 1
* if there are no duplicates (just he original group), 2 if there is one
* duplicate and so on.
*
* @param integer $postId post id
* @param integer $fieldID the name of any field in the group
* @return number of groups
*/
function GetFieldGroupDuplicates($postId, $fieldName){
global $wpdb;
return $wpdb->get_var("SELECT count(DISTINCT group_count) FROM " . RC_CWP_TABLE_POST_META .
" WHERE field_name = '$fieldName' AND post_id = $postId");
}
/**
* Get number of group duplicates given field name. The function returns 1
* if there are no duplicates (just he original group), 2 if there is one
* duplicate and so on.
*
* @param integer $postId post id
* @param integer $fieldID the name of any field in the group
* @return number of groups
*/
function GetFieldDuplicates($postId, $fieldName, $groupIndex){
global $wpdb;
return $wpdb->get_var("SELECT count(DISTINCT field_count) FROM " . RC_CWP_TABLE_POST_META .
" WHERE field_name = '$fieldName' AND post_id = $postId AND group_count = $groupIndex");
}
/**
* Get field duplicates
*
*
*/
function GetFieldsOrder($postId,$fieldName){
global $wpdb;
$tmp = $wpdb->get_col(
"SELECT field_count FROM ".RC_CWP_TABLE_POST_META." WHERE field_name = '{$fieldName}' AND post_id = {$postId} GROUP BY field_count ORDER BY field_count ASC"
);
return $tmp;
}
/**
* Get the order of group duplicates given the field name. The function returns a
* array with the orden
*
* @param integer $postId post id
* @param integer $fieldID the name of any field in the group
* @return order of one group
*/
function GetOrderDuplicates($postId,$fieldName){
global $wpdb;
$tmp = $wpdb->get_col(
"SELECT group_count FROM ".RC_CWP_TABLE_POST_META." WHERE field_name = '{$fieldName}' AND post_id = {$postId} GROUP BY group_count ORDER BY order_id asc"
);
//the order start to 1 and the arrays start to 0
//then i just sum one element in each array key for
//the order and the array keys be the same
$order = array();
foreach($tmp as $key => $value){
$order[$key+1] = $value;
}
return $order;
}
/**
* Retrieves the id of a custom field given field name for the current post.
*
* @param string $customFieldName
* @return custom field id
*/
function GetIDByName($customFieldName)
{
global $wpdb, $post;
// Get Panel ID
$customWritePanelId = get_post_meta($post->ID, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
if (empty($customWritePanelId)) return false;
$customFieldId = $wpdb->get_var("SELECT cf.id FROM ". RC_CWP_TABLE_GROUP_FIELDS . " cf" .
" WHERE cf.name = '$customFieldName' AND ".
" cf.group_id in (SELECT mg.id FROM ". RC_CWP_TABLE_PANEL_GROUPS . " mg ".
" WHERE mg.panel_id = $customWritePanelId)");
return $customFieldId;
}
/**
* @access private
*/
function GetDefaultCustomFieldType()
{
return 'Textbox';
}
/**
* Updates the properties of a custom field.
*
* @param integer $customFieldId the id of the field to be updated
* @param string $name the name of the field, the name is used to uniquely identify the field
* when retrieving its value.
* @param string $label the label of the field, the label is displayed beside the field
* in Write tab.
* @param integer $order the order of the field when it is displayed in
* the Write tab.
* @param integer $required_field whether this field is a required field. Required fields
* doesn't allow users to save a post if they are null.
* @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
* @param array $options array of strings that represent the list of the field if
* its type is list.
* @param array $default_value array of strings that represent default value(s) of
* of the field if its type is list.
* @param array $properties an array containing extra properties of the field.
*/
function Update($customFieldId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null, $duplicate)
{
global $wpdb;
$oldCustomField = RCCWP_CustomField::Get($customFieldId);
if ($oldCustomField->name != $name)
{
$sql = sprintf(
"UPDATE $wpdb->postmeta" .
" SET meta_key = %s" .
" WHERE meta_key = %s",
RC_Format::TextToSql($name),
RC_Format::TextToSql($oldCustomField->name)
);
$wpdb->query($sql);
}
$sql = sprintf(
"UPDATE " . RC_CWP_TABLE_GROUP_FIELDS .
" SET name = %s" .
" , description = %s" .
" , display_order = %d" .
" , required_field = %d" .
" , type = %d" .
" , CSS = '%s'" .
" , duplicate = %d" .
" WHERE id = %d",
RC_Format::TextToSql($name),
RC_Format::TextToSql($label),
$order,
$required_field,
$type,
$_POST['custom-field-css'],
$duplicate,
$customFieldId
);
$wpdb->query($sql);
$field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
if ($field_type->has_options == "true")
{
if (!is_array($options)) {
$options = stripslashes($options);
$options = explode("\n", $options);
}
array_walk($options, array(RC_Format, TrimArrayValues));
$options = addslashes(serialize($options));
if (!is_array($default_value)) {
$default_value = stripslashes($default_value);
$default_value = explode("\n", $default_value);
}
array_walk($default_value, array(RC_Format, TrimArrayValues));
$default_value = addslashes(serialize($default_value));
$sql = sprintf(
"INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
" (custom_field_id, options, default_option) values (%d, %s, %s)" .
" ON DUPLICATE KEY UPDATE options = %s, default_option = %s",
$customFieldId,
RC_Format::TextToSql($options),
RC_Format::TextToSql($default_value),
RC_Format::TextToSql($options),
RC_Format::TextToSql($default_value)
);
$wpdb->query($sql);
}
else
{
$sql = sprintf(
"DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS .
" WHERE custom_field_id = %d",
$customFieldId
);
$wpdb->query($sql);
}
if ($field_type->has_properties == "true")
{
$sql = sprintf(
"INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
" (custom_field_id, properties) values (%d, %s)" .
" ON DUPLICATE KEY UPDATE properties = %s",
$customFieldId,
RC_Format::TextToSql(serialize($properties)),
RC_Format::TextToSql(serialize($properties))
);
$wpdb->query($sql);
}
else
{
$sql = sprintf(
"DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES .
" WHERE custom_field_id = %d",
$customFieldId
);
$wpdb->query($sql);
}
}
}
?>