This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
/
mangapress-options.php
executable file
·313 lines (280 loc) · 11.3 KB
/
mangapress-options.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
<?php
/**
* MangaPress_Options class. Controls registering, fields, and sanitizing routines for
* plugin options
*
* @package MangaPress_Next\MangaPress_Options
* @author Jess Green <jgreen at psy-dreamer.com>
* @version $Id$
* @license GPL
*/
final class MangaPress_Options
{
const OPTIONS_GROUP_NAME = 'mangapress_options';
/**
* Get default options
* @return array
*/
public static function get_default_options()
{
$options = self::options_fields();
$defaults = [];
foreach ($options as $section => $section_params) {
$defaults[$section] = [];
foreach ($section_params['fields'] as $option_name => $option_params) {
if (!isset($option_params['default'])) continue;
$defaults[$section][$option_name] = $option_params['default'];
}
}
return $defaults;
}
/**
* MangaPress_Options constructor.
*/
public function __construct()
{
add_action('admin_init', array(__CLASS__, 'admin_init'));
}
/**
* Initialize options
*/
public static function admin_init()
{
if (defined('DOING_AJAX') && DOING_AJAX)
return;
register_setting(
self::OPTIONS_GROUP_NAME,
self::OPTIONS_GROUP_NAME,
array(__CLASS__, 'sanitize_options')
);
self::register_sections();
self::register_fields();
}
/**
* Register option sections
*/
private static function register_sections()
{
$sections = self::options_sections();
foreach ($sections as $section => $params) {
add_settings_section(
self::OPTIONS_GROUP_NAME . "-{$section}",
$params['title'],
array(__CLASS__, 'section_output_cb'),
self::OPTIONS_GROUP_NAME . "-{$section}"
);
}
}
private static function register_fields()
{
$field_sections = self::options_fields();
foreach ($field_sections as $section => $section_params) {
foreach ($section_params['fields'] as $field_name => $field_params) {
add_settings_field(
"{$section}-options-{$field_params['id']}",
$field_params['title'],
$field_params['callback'],
"mangapress_options-{$section}",
"mangapress_options-{$section}",
array_merge(array('name' => $field_name, 'section' => $section), $field_params)
);
}
}
}
/**
* Output settings section
* @param array $params Parameters passed from add_settings_section
*/
public static function section_output_cb($params)
{
$section_id = explode('-', $params['id'])[1];
$sections = self::options_sections();
echo "<p>{$sections[$section_id]['description']}</p>";
}
/**
* Helper function for creating default options fields.
*
* @return array
*/
public static function options_fields()
{
/*
* Section
* |_ Option
* |_ Option Setting
*/
$options = array(
'basic' => array(
'title' => __('Basic Options', MP_DOMAIN),
'description' => __('This section sets the “Latest-” and “Comic Archive” pages, number of comics per page, and grouping comics together by category.', MP_DOMAIN),
'fields' => [
'group_comics' => array(
'id' => 'group-comics',
'type' => 'checkbox',
'title' => __('Group Comics', MP_DOMAIN),
'valid' => 'boolean',
'description' => __('Group comics by category. This option will ignore the parent category, and group according to the child-category.', MP_DOMAIN),
'default' => false,
'callback' => array(__CLASS__, 'settings_field_cb'),
),
'group_by_parent' => array(
'id' => 'group-by-parent',
'type' => 'checkbox',
'title' => __('Use Parent Category', MP_DOMAIN),
'valid' => 'boolean',
'description' => __('Group comics by top-most parent category. Use this option if you have sub-categories but want your navigation to function using the parent category.', MP_DOMAIN),
'default' => false,
'callback' => array(__CLASS__, 'settings_field_cb'),
),
'latestcomic_page' => array(
'id' => 'latest-comic-page',
'type' => 'select',
'title' => __('Latest Comic Page', MP_DOMAIN),
'value' => array(
'no_val' => __('Select a Page', MP_DOMAIN),
),
'valid' => 'array',
'default' => 0,
'callback' => array(__CLASS__, 'basic_page_dropdowns_cb'),
),
'comicarchive_page' => array(
'id' => 'archive-page',
'type' => 'select',
'title' => __('Comic Archive Page', MP_DOMAIN),
'value' => array(
'no_val' => __('Select a Page', MP_DOMAIN),
),
'valid' => 'array',
'default' => 0,
'callback' => array(__CLASS__, 'basic_page_dropdowns_cb'),
),
]
),
'comic_page' => array(
'title' => __('Comic Page Options', MP_DOMAIN),
'description' => __('Handles image sizing options for comic pages. Thumbnail support may need to be enabled for some features to work properly. If page- or thumbnail sizes are changed, then a plugin such as Regenerate Thumbnails may be used to create the new thumbnails.', MP_DOMAIN),
'fields' => [
'generate_comic_page' => array(
'id' => 'generate-page',
'type' => 'checkbox',
'title' => __('Generate Comic Page', MP_DOMAIN),
'description' => __('Generate a comic page based on values below.', MP_DOMAIN),
'valid' => 'boolean',
'default' => 1,
'callback' => array(__CLASS__, 'settings_field_cb'),
),
'comic_page_width' => array(
'id' => 'page-width',
'type' => 'text',
'title' => __('Comic Page Width', MP_DOMAIN),
'valid' => '/[0-9]/',
'default' => 600,
'callback' => array(__CLASS__, 'settings_field_cb'),
),
'comic_page_height' => array(
'id' => 'page-height',
'type' => 'text',
'title' => __('Comic Page Height', MP_DOMAIN),
'valid' => '/[0-9]/',
'default' => 1000,
'callback' => array(__CLASS__, 'settings_field_cb'),
),
]
),
'nav' => array(
'title' => __('Navigation Options', MP_DOMAIN),
'description' => __('Options for comic navigation. Whether to have navigation automatically inserted on comic pages, or to enable/disable default comic navigation CSS.', MP_DOMAIN),
'fields' => [
'nav_css' => array(
'id' => 'navigation-css',
'title' => __('Navigation CSS', MP_DOMAIN),
'description' => __('Defaults to Custom CSS. Use Default CSS if you do not wish to create a child-theme.', MP_DOMAIN),
'type' => 'select',
'value' => array(
'custom_css' => __('Custom CSS', MP_DOMAIN),
'default_css' => __('Default CSS', MP_DOMAIN),
),
'valid' => 'array',
'default' => 'custom_css',
'callback' => array(__CLASS__, 'settings_field_cb'),
),
'display_css' => array(
'id' => 'display',
'title' => false,
'callback' => array(__CLASS__, 'navigation_css_display_cb'),
)
],
),
);
/**
* mangapress_options_fields
* This filter modify Manga+Press' option fields
*
* @param array $options Manga+Press options array to be modified
* @return array
*/
return apply_filters('mangapress_options_fields', $options);
}
/**
* Helper function for setting default options sections.
*
* @return array
*/
public static function options_sections()
{
$sections = [];
$options = self::options_fields();
foreach ($options as $section => $section_params) {
$sections[$section] = [
'title' => $section_params['title'],
'description' => $section_params['description'],
];
}
/**
* mangapress_options_sections
* This filter modify Manga+Press' option sections
*
* @param array $sections Manga+Press sections array to be modified
* @return array
*/
return apply_filters('mangapress_options_sections', $sections);
}
/**
* Outputs a settings field
* @param array $params Parameters used to output field
*/
public static function settings_field_cb($params)
{
$type = $params['type'];
$callback = "MangaPress\Fields\\{$type}_field_cb";
if (function_exists($callback)) {
$callback($params);
}
}
public static function navigation_css_display_cb()
{
}
public static function basic_page_dropdowns_cb($params)
{
$description = \MangaPress\Fields\output_description($params);
$pages = get_posts(['post_type' => 'page', 'post_status' => 'publish']);
$select = "<select>\r\n";
$select .= "\t<option value='{" . key($params['value']) . "}'>{$params['value']['no_val']}</option>\r\n";
foreach ($pages as $page) {
// TODO sanitize post_name and post_title
$select .= "\t<option value='{$page->post_name}'>{$page->post_title}</option>\r\n";
}
$select .= "</select>\r\n";
echo $select . $description;
}
/**
* Sanitize options before saving to database
*
* @param array $options Array of options to be sanitized
* @return array
*/
public static function sanitize_options($options)
{
return $options;
}
}