/
content_fields.php
executable file
·120 lines (112 loc) · 3.7 KB
/
content_fields.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
<?php
class OptimizePress_Blog_Content_Fields_Module extends OptimizePress_Modules_Base {
var $wysiwygs;
function display_settings($section_name,$config=array(),$return=false,$font_options=false,$fonts_array=array()){
if(!$content_fields = $this->_check_fields($config)){
return;
}
$content_fields = $this->_prep_fields($content_fields);
$data = array(
'id' => $this->get_fieldid($section_name),
'fieldname' => $this->get_fieldname($section_name),
'section_name' => $section_name
);
$data = array_merge($data,$config);
op_tpl_assign($data);
$values = $this->get_option($section_name);
$values = is_array($values) ? $values : array();
foreach($content_fields as $name => $field){
$content_fields[$name]['value'] = isset($values[$name]) ? $values[$name] : $field['default'];
if($field['type'] != 'wysiwyg' && $font_options && !$field['disable_font']){
$content_fields[$name]['font_html'] = op_font_options_str($field['name'].' '.__('Font Options (Optional)', OP_SN),$data['fieldname'].'['.$name.']',op_get_var($fonts_array,$name,array()));
}
}
$out = $this->load_tpl('content_fields',array('content_fields'=>$content_fields));
if($return){
return $out;
}
echo $out;
}
function save_settings($section_name,$config=array(),$op,$return=false,$use_fonts=false){
if(!$content_fields = $this->_check_fields($config)){
return array();
}
//$section = is_array($section_name) ? array_pop($section_name) : $section_name;
$content_fields = $this->_prep_fields($content_fields);
$newdata = array();
$fonts = array();
$fieldid = $this->get_fieldid($section_name);
foreach($content_fields as $name => $field){
$tmp = op_get_var($op,$name);
if($use_fonts && is_array($tmp) && isset($tmp['value'])){
$newdata[$name] = $tmp['value'];
unset($tmp['value']);
$fonts[$name] = $tmp;
} else {
$newdata[$name] = stripslashes($tmp);
}
}
if($return){
if($use_fonts){
return array($newdata,$fonts);
} else {
return $newdata;
}
}
$this->update_option($section_name,$newdata);
}
function _check_fields($config=array()){
if(!isset($config['fields']) || !is_array($config['fields']) || count($config['fields']) == 0){
return false;
}
$fields = $config['fields'];
$ignores = $this->_get_ignore_fields($config);
foreach($ignores as $ignore){
if(isset($fields[$ignore])){
unset($fields[$ignore]);
}
}
return $fields;
}
function _get_ignore_fields($config=array()){
$ignore = array();
if(isset($config['ignore_fields'])){
$ignore = is_array($config['ignore_fields']) ? $config['ignore_fields'] : array_filter(explode('|',$config['ignore_fields']));
}
return $ignore;
}
function _prep_fields($fields){
if(!isset($this->wysiwygs)){
$this->wysiwygs = function_exists('wp_editor');
}
$default = array(
'type' => 'text',
'default' => '',
'disable_font' => false
);
$newfields = array();
foreach($fields as $name => $field){
if(!is_array($field)){
$field = array(
'name' => $field
);
}
$newfields[$name] = array_merge($default,$field);
if($newfields[$name]['type'] == 'wysiwyg' && !$this->wysiwygs){
$newfields[$name]['type'] = 'textarea';
}
}
return $newfields;
}
function output($section_name,$config,$options,$return=false){
if(!$content_fields = $this->_check_fields($config)){
return false;
}
$content_fields = $this->_prep_fields($content_fields);
$content = array();
foreach($content_fields as $field => $field_options){
$content[$field] = op_get_var($options,$field,op_get_var($field_options,'default'));
}
return $content;
}
}