This repository was archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.php
More file actions
182 lines (148 loc) · 4.36 KB
/
Options.php
File metadata and controls
182 lines (148 loc) · 4.36 KB
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
<?php
/**
* Class BPFOptions
* Plugin options
*/
class BPFOptions
{
/**
* @var array Fields that can be searched
*/
protected $custom_fields = array();
/**
* @var array Current options (only those related to the plugin)
*/
public $options = array();
/**
* @param $fields
*/
public function __construct($fields)
{
$this->custom_fields = $fields;
$this->options = get_option('bpf_settings');
}
/**
* Add the options page to the wp-admin menu
*/
function bpf_add_admin_menu()
{
add_menu_page('Bit Post Filter', 'Bit Post Filter', 'manage_options', 'bit_post_filter', array($this, 'bpf_options_page'));
}
/**
* Initialize the options page
*/
function bpf_settings_init()
{
register_setting('pluginPage', 'bpf_settings');
/**
* Add searchable fields section
*/
add_settings_section(
'bpf_pluginPage_section',
__('Searchable fields', 'wordpress'),
array($this, 'bpf_settings_section_callback'),
'pluginPage'
);
/**
* Loop through all custom fields and assign them as checkboxes
*/
foreach ($this->custom_fields as $field => $values) {
add_settings_field(
BPFUtils::slugify($field),
$field,
array($this, 'bpf_checkbox_field_render'),
'pluginPage',
'bpf_pluginPage_section',
array(BPFUtils::slugify($field), $values)
);
}
/**
* Assign the pages for the result page selection
*/
$pages = get_pages();
$opts = array('plugin-page' => 'Bit Post Filter Custom Page (default)');
$opts['category-page'] = 'Theme category page';
foreach ($pages as $page) {
$opts[$page->ID] = $page->post_title;
}
add_settings_field(
'results-page',
'Results page',
array($this, 'bpf_select_field_render'),
'pluginPage',
'bpf_pluginPage_section',
array('name' => 'results-page', 'values' => $opts)
);
}
/**
* Displays the description of the options section
*/
function bpf_settings_section_callback()
{
echo __('Select the custom fields that you want to be searchable. After adding a new custom field to a post,
you need to activate it as searchable here before it showing in the filter widget', 'wordpress');
}
/**
* Render checkboxes
* @param array $options
*/
function bpf_checkbox_field_render($options = array())
{
?>
<input type='checkbox'
name='bpf_settings[<?php echo $options[0]; ?>]' <?php checked($this->options[$options[0]], 1); ?> value='1'>
Values: <b><?php echo implode(', ', $options[1]); ?></b>
<?php
}
/**
* Render select boxes
* @param array $options
*/
function bpf_select_field_render($options = array())
{
?>
<select name="bpf_settings[<?php echo $options['name']; ?>]">
<?php
foreach ($options['values'] as $val => $display) {
echo '<option value="' . $val . '" ' . ($this->options[$options['name']] == $val ? 'selected' : '') . '>' . $display . '</option>';
}
?>
</select>
If you chose a custom page to display the result, you need to create a custom template in your theme or use the
shortcode <code>[bit_post_filter_results]</code> to be able to display the results
<?php
}
/**
* Render the options page
*/
function bpf_options_page()
{
?>
<form action='options.php' method='post'>
<h2>Bit Post Filter</h2>
<?php
settings_fields('pluginPage');
do_settings_sections('pluginPage');
submit_button();
?>
</form>
<?php
}
/**
* Get a list of all enabled custom fields
* @return array
*/
public function get_enabled()
{
return $this->options;
}
/**
* Returns true if the specified $slug is enabled
* @param $slug
* @return bool
*/
public function is_enabled($slug)
{
return ($this->options[$slug] == 1);
}
}