-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharad_fields_class.php
122 lines (108 loc) · 3.39 KB
/
arad_fields_class.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
<?php
require('arad_form_class.php');
if(!class_exists(arad_fields_class)){
class arad_fields_class{
public static $metabox_data;
private static $metabox_count;
public static $arad_dir;
public function __construct(){
self::$metabox_data = array();
self::$metabox_count = 0;
}
private function generateDefaultValue(){
self::$metabox_data['meta'][self::$metabox_count]['id'] = 'arad_m'.md5(self::$metabox_count);
self::$metabox_data['meta'][self::$metabox_count]['title'] = 'Arad';
self::$metabox_data['meta'][self::$metabox_count]['screen'] = null;
self::$metabox_data['meta'][self::$metabox_count]['context'] = 'advanced';
self::$metabox_data['meta'][self::$metabox_count]['priority'] = 'high';
}
public function addMetaBox($metabox_input){
if(is_array($metabox_input)){
self::generateDefaultValue();
foreach($metabox_input as $key => $value){
if($key === 'fields'){
self::$metabox_data['fields'][self::$metabox_count] = $value;
}else{
self::$metabox_data['meta'][self::$metabox_count][$key] = $value;
}
}
self::$metabox_count++;
}
}
public function createMetaBox(){
if(is_admin()) {
wp_register_script('arad-scripts', self::$arad_dir.'/js/arad-scripts.js', array('jquery'), '1.0.0', true);
wp_register_style('arad-styles', self::$arad_dir.'/css/arad-styles.css', array(), '1.0.0');
wp_enqueue_style('arad-styles');
wp_enqueue_script('arad-scripts');
add_action('load-post.php', array(__CLASS__, 'initMetaBox'));
add_action('load-post-new.php', array(__CLASS__, 'initMetaBox'));
}
}
public function initMetaBox(){
add_action('add_meta_boxes', array(__CLASS__, 'generateMetaBox'));
add_action('save_post', array(__CLASS__, 'saveMetaBox' ), 10, 2 );
}
public function generateMetaBox(){
for($i=0; $i < self::$metabox_count; $i++){
add_meta_box(
self::$metabox_data['meta'][$i]['id'],
self::$metabox_data['meta'][$i]['title'],
array(__CLASS__, 'generateForm'),
self::$metabox_data['meta'][$i]['screen'],
self::$metabox_data['meta'][$i]['context'],
self::$metabox_data['meta'][$i]['priority'],
self::$metabox_data['fields'][$i]);
}
}
public function generateForm($post, $fields_array){
$post_id = $post->ID;
arad_form_class::generateForm($post_id, $fields_array);
}
public function saveMetaBox($post_id, $post){
/**
* Check is autosave
*
*/
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
/**
* Check user permissions
*
*/
if('page' == $_POST['post_type']){
if(!current_user_can('edit_page', $post_id)){
return;
}
}elseif(!current_user_can('edit_post', $post_id)){
return;
}
/**
* Validate nonce every metabox
*
*/
for($i=0; $i<self::$metabox_count; $i++){
if(!arad_form_class::validateNonce(self::$metabox_data['meta'][$i]['id'])){
return;
}
}
/**
* Save every fields
*
*/
for($i=0; $i<self::$metabox_count; $i++){
foreach(self::$metabox_data['fields'][$i] as $field){
$old_val = get_post_meta($post_id, $field['name'], true);
$new_val = sanitize_text_field(nl2br($_POST[$field['name']]));
if(isset($new_val)){
update_post_meta($post_id, $field['name'], $new_val);
}elseif(isset($old_val) && empty($new_val)){
delete_post_meta($post_id, $field['name'], $old_val);
}
}
}
}
}
new arad_fields_class;
}