forked from hunk/Magic-Fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCCWP_CustomWritePanel.php
652 lines (544 loc) · 26.8 KB
/
RCCWP_CustomWritePanel.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
/**
* In this Class can be found it the methods for work with Write Panels.
*/
class RCCWP_CustomWritePanel {
/**
* Get all Write Panels.
*
* @return array of objects containing all write panels. Each object contains
* id, name, description, display_order, capability_name, type, always_show
*/
public static function GetCustomWritePanels($include_global = FALSE) {
global $wpdb;
$sql = "SELECT id, name, description, display_order, capability_name, type, single FROM " . MF_TABLE_PANELS;
if (!$include_global) { // fix to exclude the global panel from general lists
$sql .= " WHERE name <> '_Global' ";
}
$sql .= " ORDER BY display_order ASC";
$results = $wpdb->get_results($sql);
if (!isset($results))
$results = array();
return $results;
}
/**
* Assign a specified write panel to a role.
*
* @param integer $customWritePanelId panel id
* @param string $roleName role name (see roles in wordpress)
*/
public static function AssignToRole($customWritePanelId, $roleName) {
$customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
$capabilityName = $customWritePanel->capability_name;
$role = get_role($roleName);
$role->add_cap($capabilityName);
}
/**
* Create a new write panel.
*
* @param string $name write panel name
* @param string $description write panel description
* @param array $standardFields a list of standard fields ids that are to be displayed in
* in the panel. Use $STANDARD_FIELDS defined in MF_Constant.php
* @param array $categories array of category ids that are checked by default when the user
* opens Write tab for that panel.
* @param integer $display_order the order of the panel in Magic Fields > Write Panels tab
* @param string $type 'post' or 'page'
* @param boolean $createDefaultGroup indicates whether to create a default group.
* @return the id of the write panel
*/
function Create($name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1, $type = FALSE, $createDefaultGroup=true,$single_post = 0, $default_theme_page = NULL, $default_parent_page = NULL, $expanded = 0) {
include_once('RC_Format.php');
global $wpdb;
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($description, ENT_QUOTES, 'UTF-8');
$capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name);
if (!$type) $type = filter_var($_POST['radPostPage'], FILTER_SANITIZE_SPECIAL_CHARS);
$type = htmlspecialchars($type, ENT_QUOTES, 'UTF-8');
$capabilityName = htmlspecialchars($capabilityName, ENT_QUOTES, 'UTF-8');
$sql = $wpdb->prepare(
"INSERT INTO " . MF_TABLE_PANELS .
" (name, description, display_order, capability_name, type,single,expanded)" .
" values" .
" (%s, %s, %d, %s, %s,%d, %d)",
array(
$name,
$description,
$display_order,
$capabilityName,
$type,
$single_post,
$expanded
)
);
$wpdb->query($sql);
$customWritePanelId = $wpdb->insert_id;
if ( !isset($categories) )
$categories = array();
foreach ($categories as $cat_id) {
$sql = $wpdb->prepare( "INSERT INTO " . MF_TABLE_PANEL_CATEGORY . " (panel_id, cat_id) values (%d, '%s')", array( $customWritePanelId, $cat_id ) );
$wpdb->query($sql);
}
if (!isset($standardFields))
$standardFields = array();
foreach ($standardFields as $standard_field_id) {
$sql = $wpdb->prepare( "INSERT INTO " . MF_TABLE_PANEL_STANDARD_FIELD . " (panel_id, standard_field_id) values (%d, %d)", array( $customWritePanelId, $standard_field_id ) );
$wpdb->query($sql);
}
// Create default group
if ($createDefaultGroup) {
include_once('RCCWP_CustomGroup.php');
RCCWP_CustomGroup::Create($customWritePanelId, '__default', false, false);
}
if($default_theme_page) {
$theme_key="t_".$name;
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (meta_key, meta_value) VALUES (%s, %s)", array( $theme_key, $default_theme_page ) );
$wpdb->query($sql);
}
if($default_parent_page && $default_parent_page >= 0){
$parent_key="p_".$name;
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (meta_key, meta_value) VALUES (%s, %s)", array( $parent_key, $default_parent_page ) );
$wpdb->query($sql);
}
RCCWP_CustomWritePanel::AssignToRole($customWritePanelId, 'administrator');
return $customWritePanelId;
}
/**
* Delete a write panel without deleting its modules
*
* @param integer $customWritePanelId write panel id
*/
function Delete($customWritePanelId = null) {
if (isset($customWritePanelId)) {
global $wpdb;
$customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
$wpdb->delete( MF_TABLE_PANELS, array( 'id' => $customWritePanel->id ), array( '%d' ) );
$wpdb->delete( MF_TABLE_PANEL_CATEGORY, array( 'panel_id' => $customWritePanel->id ), array( '%d' ) );
$wpdb->delete( MF_TABLE_PANEL_STANDARD_FIELD, array( 'panel_id' => $customWritePanel->id ), array( '%d' ) );
$wpdb->delete( MF_TABLE_PANEL_GROUPS, array( 'panel_id' => $customWritePanel->id ), array( '%d' ) );
}
}
/**
* Get the properties of a write panel
*
* @param unknown_type $customWritePanelId
* @return an object containing the properties of the write panel which are
* id, name, description, display_order, capability_name, type
*/
public static function Get($customWritePanelId) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT id, name, description, display_order, capability_name, type,single, expanded FROM " . MF_TABLE_PANELS .
" WHERE id = %d", array( $customWritePanelId ) );
$results = $wpdb->get_row($sql);
return $results;
}
/**
* Gets a write panel id based on write panel name.
*
* @param string $name
* @return the write panel id
*/
function GetIdByName($name) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT id FROM ".MF_TABLE_PANELS." WHERE name = %s", array( $name ) );
return $wpdb->get_var( $sql );
}
/**
* Get the properties of a write panel
*
* @param unknown_type $customWritePanelId
* @return an object containing the properties of the write panel which are
* id, name, description, display_order, capability_name, type
*/
function GetThemePage($customWritePanelName) {
global $wpdb;
$sql = "SELECT meta_value FROM " . $wpdb->postmeta .
" WHERE meta_key = 't_".$customWritePanelName."' AND post_id = 0" ;
$sql = $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = 0", array( "t_".$customWritePanelName ) );
$results = $wpdb->get_row($sql);
if($results) return $results->meta_value;
return false;
}
/**
* Get the properties of a write panel
*
* @param unknown_type $customWritePanelId
* @return an object containing the properties of the write panel which are
* id, name, description, display_order, capability_name, type
*/
public static function GetParentPage($customWritePanelName) {
global $wpdb;
$sql = "SELECT meta_value FROM " . $wpdb->postmeta .
" WHERE meta_key = 'p_".$customWritePanelName."' AND post_id = 0" ;
$sql = $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = 0", array( "p_".$customWritePanelName ) );
$results = $wpdb->get_row($sql);
if($results) return $results->meta_value;
return FALSE;
}
/**
* Get a list of the ids of the categories assigned to a write panel
*
* @param integer $customWritePanelId write panel id
* @return array of ids
*/
public static function GetAssignedCategoryIds($customWritePanelId) {
$results = RCCWP_CustomWritePanel::GetAssignedCategories($customWritePanelId);
$ids = array();
foreach ($results as $r)
{
$ids[] = $r->cat_id;
}
return $ids;
}
/**
* Get a list of categories assigned to a write panel
*
* @param integer $customWritePanelId write panel id
* @return array of objects, each object contains cat_id and cat_name
*/
public static function GetAssignedCategories($customWritePanelId) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT cat_id FROM " . MF_TABLE_PANEL_CATEGORY . " WHERE panel_id = %d", array( $customWritePanelId ) );
$results = $wpdb->get_results($sql);
if (!isset($results))
$results = array();
return $results;
}
/**
* Create a capability name for a write panel given its name. This function is
* copied from WP's sanitize_title_with_dashes($title) (formatting.php)
*
* @param string $customWritePanelName panel name
* @return string capability name
*/
public static function GetCapabilityName($customWritePanelName) {
// copied from WP's sanitize_title_with_dashes($title) (formatting.php)
$capabilityName = strip_tags($customWritePanelName);
// Preserve escaped octets.
$capabilityName = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $capabilityName);
// Remove percent signs that are not part of an octet.
$capabilityName = str_replace('%', '', $capabilityName);
// Restore octets.
$capabilityName = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $capabilityName);
$capabilityName = remove_accents($capabilityName);
if (seems_utf8($capabilityName))
{
if (function_exists('mb_strtolower'))
{
$capabilityName = mb_strtolower($capabilityName, 'UTF-8');
}
$capabilityName = utf8_uri_encode($capabilityName, 200);
}
$capabilityName = strtolower($capabilityName);
$capabilityName = preg_replace('/&.+?;/', '', $capabilityName); // kill entities
$capabilityName = preg_replace('/[^%a-z0-9 _-]/', '', $capabilityName);
$capabilityName = preg_replace('/\s+/', '_', $capabilityName);
$capabilityName = preg_replace('|-+|', '_', $capabilityName);
$capabilityName = trim($capabilityName, '_');
return $capabilityName;
}
/**
* Get a list of the standard fields of a the write panel
*
* @param integer $customWritePanelId panel id
* @return array of ids of the standard fields (see $STANDARD_FIELDS defined in MF_Constant.php)
*/
public static function GetStandardFields($customWritePanelId) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT standard_field_id FROM " . MF_TABLE_PANEL_STANDARD_FIELD . " WHERE panel_id = %d", array( $customWritePanelId ) );
$results = $wpdb->get_col($sql);
if (!isset($results))
$results = array();
return $results;
}
/**
* Updates the properties of a write panel
*
* @param integer $customWritePanelId panel id
* @param string $name write panel name
* @param string $description write panel description
* @param array $standardFields a list of standard fields ids that are to be displayed in
* in the panel. Use $STANDARD_FIELDS defined in MF_Constant.php
* @param array $categories array of category ids that are checked by default when the user
* opens Write tab for that panel.
* @param integer $display_order the order of the panel in Magic Fields > Write Panels tab
* @param string $type 'post' or 'page'
*/
public static function Update($customWritePanelId, $name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1, $type = FALSE, $createDefaultGroup=true,$single_post = 0, $default_theme_page = NULL, $default_parent_page = NULL, $expanded = 0) {
include_once('RC_Format.php');
global $wpdb;
$capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name);
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($description, ENT_QUOTES, 'UTF-8');
$sql = $wpdb->prepare(
"UPDATE " . MF_TABLE_PANELS .
" SET name = %s" .
" , description = %s" .
" , display_order = %d" .
" , capability_name = %s" .
" , type = %s" .
" , single = %s" .
" , expanded = %d" .
" where id = %d",
array(
$name,
$description,
$display_order,
$capabilityName,
filter_var($_POST['radPostPage'], FILTER_SANITIZE_SPECIAL_CHARS),
$single_post,
$expanded,
$customWritePanelId
)
);
$wpdb->query($sql);
if (!isset($categories) || empty($categories)) {
$wpdb->delete( MF_TABLE_PANEL_CATEGORY, array( 'panel_id' => $customWritePanelId ), array( '%d' ) );
} else {
$wpdb->delete( MF_TABLE_PANEL_CATEGORY, array( 'panel_id' => $customWritePanelId ), array( '%d' ) );
foreach($categories as $cat_id){
$wpdb->insert(
MF_TABLE_PANEL_CATEGORY,
array(
'panel_id' => $customWritePanelId,
'cat_id' => $cat_id
),
array(
'%d',
'%s'
)
);
}
}
if (!isset($standardFields) || empty($standardFields)) {
$wpdb->delete( MF_TABLE_PANEL_STANDARD_FIELD, array( 'panel_id' => $customWritePanelId ), array( '%d' ) );
} else {
$currentStandardFieldIds = array();
$currentStandardFieldIds = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId);
$keepStandardFieldIds = array_intersect($currentStandardFieldIds, $standardFields);
$deleteStandardFieldIds = array_diff($currentStandardFieldIds, $keepStandardFieldIds);
$insertStandardFieldIds = array_diff($standardFields, $keepStandardFieldIds);
foreach ($insertStandardFieldIds as $standard_field_id) {
$wpdb->insert(
MF_TABLE_PANEL_STANDARD_FIELD,
array(
'panel_id' => $customWritePanelId,
'standard_field_id' => $standard_field_id
),
array(
'%d',
'%d'
)
);
}
if (!empty($deleteStandardFieldIds)) {
$ids = implode(',', $deleteStandardFieldIds);
$sql = $wpdb->prepare( "DELETE FROM " . MF_TABLE_PANEL_STANDARD_FIELD .
" WHERE panel_id = %d" .
" AND standard_field_id IN ($ids)", array( $customWritePanelId ) );
$wpdb->query($sql);
}
}
if($default_theme_page) {
$theme_key="t_".$name;
//check if exist template in postmeta
$check_template = $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", array( $theme_key ) );
$query_template= $wpdb->query($check_template);
if($query_template) {
$sql = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_key = %s AND post_id = %s ", array( $default_theme_page, $theme_key, "0" ) );
} else {
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (meta_key, meta_value) VALUES (%s, %s)", array( $theme_key, $default_theme_page ) );
}
$wpdb->query($sql);
}
if($default_parent_page && $default_parent_page >= 0){
$parent_key="p_".$name;
//check if exist parent in postmeta
$check_parent = $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", array( $parent_key ) );
$query_parent = $wpdb->query($check_parent);
if($query_parent){
$sql = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_key = %s AND post_id = %s", array( $default_parent_page, $parent_key, "0") );
}else{
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (meta_key, meta_value) VALUES (%s, %s)", array( $parent_key, $default_parent_page ) );
}
$wpdb->query($sql);
} elseif ($default_parent_page == -1) {
delete_post_meta(0, "p_".$name);
}
}
/**
* Retrieves the groups of a module
*
* @param integer $customWriteModuleId module id
* @return array of objects representing basic information of the group,
* each object contains id, name and module_id
*/
public static function GetCustomGroups($customWritePanelId, $orderby = "name") {
global $wpdb;
$sql = $wpdb->prepare( "SELECT * FROM " . MF_TABLE_PANEL_GROUPS .
" WHERE panel_id = %d " .
" OR panel_id IN (SELECT id FROM " . MF_TABLE_PANELS . " WHERE name = '_Global' ) " .
" ORDER BY %s", array( $customWritePanelId, $orderby ) );
$results =$wpdb->get_results($sql);
if (!isset($results))
$results = array();
return $results;
}
/**
* Import a write panel given the file path.
* @param string $panelFilePath the full path of the panel file
* @param string $writePanelName the write panel name, if this value if false, the function will
* use the pnl filename as the write panel name. The default value is false
* @param boolean $overwrite whether to overwrite existing panels with the same name
* @return the panel id, or false in case of error.
*/
function Import($panelFilePath, $writePanelName = false, $overwrite = false) {
global $wpdb;
include_once('RCCWP_CustomGroup.php');
include_once('RCCWP_CustomField.php');
include_once('RCCWP_Application.php');
if (!$writePanelName)
//use filename
$writePanelName = basename($panelFilePath, ".pnl");
if ($writePanelName == '') return false;
$writePanelID = RCCWP_CustomWritePanel::GetIdByName($writePanelName);
if ($writePanelID && !$overwrite) {
// Append a number if the panel already exists,
$i = 2;
$temp_name = $writePanelName . "_1";
while (RCCWP_CustomWritePanel::GetIdByName($temp_name)){
$temp_name = $writePanelName. "_" . $i++;
}
$writePanelName = $temp_name;
}
// Unserialize file
$imported_data = unserialize(file_get_contents($panelFilePath));
$types_results = RCCWP_CustomField::GetCustomFieldTypes();
$types = array();
foreach($types_results as $types_result) {
$types[$types_result->name] = $types_result->id;
}
// Prepare categories list
$assignedCategories = array();
if(is_array($imported_data['panel']->assignedCategories)) {
foreach($imported_data['panel']->assignedCategories as $cat_name){
wp_create_category($cat_name);
$assignedCategories[] = $cat_name;
}
}
//Create write panel
if($writePanelID && $overwrite) {
RCCWP_CustomWritePanel::Update($existingPanelId, $writePanelName, $imported_data['panel']->description, $imported_data['panel']->standardFieldsIDs, $assignedCategories,$imported_data['panel']->display_order, $imported_data['panel']->type, false,$imported_data['panel']->single,$imported_data['panel']->theme, $imported_data['panel']->parent_page);
foreach (RCCWP_CustomWritePanel::GetCustomGroups($writePanelID) as $group) {
RCCWP_CustomGroup::Delete($group->id);
}
} else {
$writePanelID = RCCWP_CustomWritePanel::Create($writePanelName, $imported_data['panel']->description, $imported_data['panel']->standardFieldsIDs, $assignedCategories,$imported_data['panel']->display_order, $imported_data['panel']->type, false,$imported_data['panel']->single,$imported_data['panel']->theme, $imported_data['panel']->parent_page);
}
if(is_array($imported_data['fields'])){
foreach($imported_data['fields'] as $groupName => $group){
// For backward compatability
if (!isset($group->fields)) {
$newGroup->fields = $group;
$group = $newGroup;
}
// Import group
$groupID = RCCWP_CustomGroup::Create($writePanelID, $groupName, $group->duplicate, $group->at_right);
// Import group fields
foreach ($group->fields as $field){
$fieldOptions = @implode("\n", $field->options);
$fieldDefault = @implode("\n", $field->default_value);
if ($field->type == "Related Type") {
$field->properties["panel_id"] = RCCWP_CustomWritePanel::GetIdByName($field->properties["panel_name"]);
unset($field->properties["panel_name"]);
}
RCCWP_CustomField::Create($groupID, $field->name, $field->description, $field->display_order, $field->required_field, $types[$field->type], $fieldOptions, $fieldDefault, $field->properties, $field->duplicate,$field->help_text);
}
}
}
return $writePanelID;
}
/**
* Export a write panel to file
*
* @param integer $panelID
* @param string $exportedFilename the full path of the file to which the panel will be exported
*/
function Export($panelID){
include_once('RCCWP_CustomGroup.php');
include_once('RCCWP_CustomField.php');
$exported_data = array();
$writePanel = RCCWP_CustomWritePanel::Get($panelID);
$writePanel->standardFieldsIDs = RCCWP_CustomWritePanel::GetStandardFields($panelID);
$writePanel->assignedCategories = array();
$writePanel->theme = RCCWP_CustomWritePanel::GetThemePage($writePanel->name);
$writePanel->parent_page = RCCWP_CustomWritePanel::GetParentPage($writePanel->name);
$assignedCategories = RCCWP_CustomWritePanel::GetAssignedCategories($panelID);
foreach($assignedCategories as $assignedCategory) {
$writePanel->assignedCategories[] = $assignedCategory->cat_id;
}
$moduleGroups = RCCWP_CustomWritePanel::GetCustomGroups($panelID);
foreach( $moduleGroups as $moduleGroup) {
$fields = RCCWP_CustomGroup::GetCustomFields($moduleGroup->id);
foreach ($fields as $field) {
if ($field->type == "Related Type") {
$tmp = RCCWP_CustomWritePanel::Get($field->properties["panel_id"]);
$field->properties["panel_name"] = $tmp->name;
unset($field->properties["panel_id"]);
}
}
$groupFields[$moduleGroup->name]->fields = $fields;
$groupFields[$moduleGroup->name]->duplicate = $moduleGroup->duplicate;
$groupFields[$moduleGroup->name]->at_right = $moduleGroup->at_right;
}
$exported_data['panel'] = $writePanel;
$exported_data['fields'] = $groupFields;
return serialize($exported_data);
}
/**
* Return the name of the write panel giving the post_id
*
* @param integer $post_id
* @return string
*/
function GetWritePanelName($post_id) {
global $wpdb;
if ($the_post = wp_is_post_revision($post_id)) {
$post_id = $the_post;
}
//getting the panel id
$sql = $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %s AND meta_key = %s", array( $post_id, '_mf_write_panel_id' ) );
$panel_id = $wpdb->get_var($sql);
if(empty($panel_id)) {
return false;
}
//Getting the write panel name using the id
$properties = RCCWP_CustomWritePanel::Get($panel_id);
return $properties->name;
}
public static function GetCountPstWritePanel($write_panel_id) {
global $wpdb;
$user = wp_get_current_user();
$sql = $wpdb->prepare( "SELECT COUNT(DISTINCT(p.ID)) AS num_posts, p.post_status FROM $wpdb->posts p JOIN $wpdb->postmeta pm ON p.id = pm.post_id WHERE meta_key = %s AND meta_value = %s GROUP BY p.post_status", array( "_mf_write_panel_id", $write_panel_id ) );
$count = $wpdb->get_results( $sql, ARRAY_A );
$stats = array( 'publish' => 0, 'private' => 0, 'draft' => 0, 'pending' => 0, 'future' => 0, 'trash' => 0 );
foreach( (array) $count as $row_num => $row ) {
$stats[$row['post_status']] = $row['num_posts'];
}
$stats = (object) $stats;
return $stats;
}
public static function GetCountPostNotWritePanel($type) {
global $wpdb;
$user = wp_get_current_user();
$sql = $wpdb->prepare( "SELECT COUNT(DISTINCT(p.ID)) AS num_posts, p.post_status FROM $wpdb->posts p WHERE p.post_type = %s AND 0 = (SELECT COUNT(*) FROM $wpdb->postmeta pm WHERE p.id = pm.post_id AND meta_key = %s ) GROUP BY p.post_status", array( $type, "_mf_write_panel_id" ) );
$count = $wpdb->get_results( $sql, ARRAY_A );
$stats = array( 'publish' => 0, 'private' => 0, 'draft' => 0, 'pending' => 0, 'future' => 0, 'trash' => 0 );
foreach( (array) $count as $row_num => $row ) {
$stats[$row['post_status']] = $row['num_posts'];
}
$stats['auto-draft'] = 0;
$stats = (object) $stats;
return $stats;
}
}