PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.41
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.41
3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 2.44 All 141 releases
photonic / Admin / Options_Manager.php

Options_Manager.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 2.41, at Admin/Options_Manager.php

1,007 lines 35.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Photonic_Plugin\Admin;
3
4 use Photonic_Plugin\Core\Photonic;
5
6 use Photonic_Plugin\Options\Defaults;
7 use Photonic_Plugin\Options\Flickr;
8 use Photonic_Plugin\Options\Generic;
9 use Photonic_Plugin\Options\Google;
10 use Photonic_Plugin\Options\Instagram;
11 use Photonic_Plugin\Options\Lightbox;
12 use Photonic_Plugin\Options\SmugMug;
13 use Photonic_Plugin\Options\Zenfolio;
14
15 require_once('Admin_Page.php');
16
17 class Options_Manager extends Admin_Page {
18 var $options, $tab, $tab_options, $reverse_options, $shown_options, $option_defaults, $allowed_values, $hidden_options, $nested_options, $displayed_sections;
19 var $option_structure, $previous_displayed_section, $file, $tab_name, $core;
20
21 /**
22 * Options_Manager constructor.
23 * @param $file
24 * @param Photonic $core
25 */
26 function __construct($file, $core) {
27 global $photonic_setup_options;
28 $options_page_array = [
29 'Generic.php' => Generic::get_instance()->get_options(),
30 'Flickr.php' => Flickr::get_instance()->get_options(),
31 'Google.php' => Google::get_instance()->get_options(),
32 'SmugMug.php' => SmugMug::get_instance()->get_options(),
33 'Zenfolio.php' => Zenfolio::get_instance()->get_options(),
34 'Instagram.php' => Instagram::get_instance()->get_options(),
35 'Lightbox.php' => Lightbox::get_instance()->get_options(),
36 ];
37
38 $tab_name_array = [
39 'Generic.php' => 'Generic Options',
40 'Flickr.php' => 'Flickr Options',
41 'Google.php' => 'Google Photos Options',
42 'SmugMug.php' => 'SmugMug Options',
43 'Zenfolio.php' => 'Zenfolio Options',
44 'Instagram.php' => 'Instagram Options',
45 'Lightbox.php' => 'Lightbox Options',
46 ];
47
48 $this->core = $core;
49 $this->file = $file;
50 $this->tab = 'Generic.php';
51 if (isset($_REQUEST['tab']) && array_key_exists($_REQUEST['tab'], $tab_name_array)) {
52 $this->tab = $_REQUEST['tab'];
53 }
54
55 $this->tab_options = $options_page_array[$this->tab];
56 $this->tab_name = $tab_name_array[$this->tab];
57 $this->options = $photonic_setup_options;
58 $this->reverse_options = [];
59 $this->nested_options = [];
60 $this->displayed_sections = 0;
61 $this->option_structure = $this->get_option_structure();
62
63 $all_options = get_option('photonic_options');
64 if (!isset($all_options)) {
65 $this->hidden_options = [];
66 }
67 else {
68 $this->hidden_options = $all_options;
69 }
70
71 foreach ($this->tab_options as $option) {
72 if (isset($option['id'])) {
73 $this->shown_options[] = $option['id'];
74 if (isset($this->hidden_options[$option['id']])) unset($this->hidden_options[$option['id']]);
75 }
76 }
77
78 $defaults = Defaults::get_options();
79 foreach ($photonic_setup_options as $option) {
80 if (isset($option['category']) && !isset($this->nested_options[$option['category']])) {
81 $this->nested_options[$option['category']] = [];
82 }
83
84 if (isset($option['id'])) {
85 $this->reverse_options[$option['id']] = $option['type'];
86
87 $this->option_defaults[$option['id']] = $defaults[$option['id']];
88 $option['std'] = $defaults[$option['id']];
89
90 if (isset($option['options'])) {
91 $this->allowed_values[$option['id']] = $option['options'];
92 }
93 if (isset($option['grouping'])) {
94 if (!isset($this->nested_options[$option['grouping']])) {
95 $this->nested_options[$option['grouping']] = [];
96 }
97 $this->nested_options[$option['grouping']][] = $option['id'];
98 }
99 }
100 }
101 }
102
103 function render_content() {
104 $saved_options = get_option('photonic_options');
105 if (isset($saved_options) && !empty($saved_options)) {
106 $generated_css = $this->core->generate_css(false);
107 update_option('photonic_css', $generated_css);
108 if (!empty($saved_options['css_in_file'])) {
109 $this->save_css_to_file($generated_css);
110 }
111 }
112 ?>
113 <div class="photonic-tabbed-options">
114 <div class="photonic-header-nav">
115 <div class="photonic-header-nav-top fix">
116 </div>
117 <div class="photonic-options-header-bar fix">
118 <h2 class='nav-tab-wrapper'>
119 <a class='nav-tab <?php if ($this->tab == 'Generic.php') echo 'nav-tab-active'; ?>' id='photonic-options-generic' href='?page=photonic-options-manager&amp;tab=Generic.php'><span class="icon">&nbsp;</span> Generic Options</a>
120 <a class='nav-tab <?php if ($this->tab == 'Flickr.php') echo 'nav-tab-active'; ?>' id='photonic-options-flickr' href='?page=photonic-options-manager&amp;tab=Flickr.php'><span class="icon">&nbsp;</span> Flickr</a>
121 <a class='nav-tab <?php if ($this->tab == 'SmugMug.php') echo 'nav-tab-active'; ?>' id='photonic-options-smugmug' href='?page=photonic-options-manager&amp;tab=SmugMug.php'><span class="icon">&nbsp;</span> SmugMug</a>
122 <a class='nav-tab <?php if ($this->tab == 'Google.php') echo 'nav-tab-active'; ?>' id='photonic-options-google' href='?page=photonic-options-manager&amp;tab=Google.php'><span class="icon">&nbsp;</span> Google Photos</a>
123 <a class='nav-tab <?php if ($this->tab == 'Zenfolio.php') echo 'nav-tab-active'; ?>' id='photonic-options-zenfolio' href='?page=photonic-options-manager&amp;tab=Zenfolio.php'><span class="icon">&nbsp;</span> Zenfolio</a>
124 <a class='nav-tab <?php if ($this->tab == 'Instagram.php') echo 'nav-tab-active'; ?>' id='photonic-options-instagram' href='?page=photonic-options-manager&amp;tab=Instagram.php'><span class="icon">&nbsp;</span> Instagram</a>
125 <a class='nav-tab <?php if ($this->tab == 'Lightbox.php') echo 'nav-tab-active'; ?>' id='photonic-options-lightbox' href='?page=photonic-options-manager&amp;tab=Lightbox.php'><span class="icon">&nbsp;</span> Lightboxes</a>
126 </h2>
127 </div>
128 </div>
129 <?php
130 $option_structure = $this->get_option_structure();
131 $group = substr($this->tab, 0, stripos($this->tab, '.'));
132
133 echo "<div class='photonic-options photonic-options-$group' id='photonic-options'>";
134 echo "<ul class='photonic-section-tabs'>";
135 foreach ($option_structure as $l1_slug => $l1) {
136 echo "<li><a href='#$l1_slug'>" . $l1['name'] . "</a></li>\n";
137 }
138 echo "</ul>";
139
140 do_settings_sections($this->file);
141
142 $last_option = end($option_structure);
143 $last_slug = key($option_structure);
144 $this->show_buttons($last_slug, $last_option);
145
146 echo "</form>\n";
147 echo "</div><!-- /photonic-options-panel -->\n";
148
149 echo "</div><!-- /#photonic-options -->\n";
150 ?>
151 </div><!-- /#photonic-tabbed-options -->
152 <?php
153 }
154
155 function show_buttons($slug, $option) {
156 if (!isset($option['buttons']) || ($option['buttons'] != 'no-buttons' && $option['buttons'] != 'special-buttons')) {
157 echo "<div class=\"photonic-button-bar photonic-button-bar-{$slug}\">\n";
158 echo "<input name=\"photonic_options[submit-{$slug}]\" type='submit' value=\"Save page &ldquo;".esc_attr($option['name'])."&rdquo;\" class=\"button button-primary\" />\n";
159 echo "<input name=\"photonic_options[submit-{$slug}]\" type='submit' value=\"Reset page &ldquo;".esc_attr($option['name'])."&rdquo;\" class=\"button\" />\n";
160 echo "<input name=\"photonic_options[submit-{$slug}]\" type='submit' value=\"Delete all options\" class=\"button\" />\n";
161 echo "</div><!-- photonic-button-bar -->\n";
162 }
163 }
164
165 function init() {
166 foreach ($this->option_structure as $slug => $option) {
167 if (!in_array($slug, Defaults::get_options_pages())) {
168 wp_die("Invalid option section: $slug");
169 }
170 // Since we are not including this file on all admin pages due to the size and associated load, register_setting cannot be done here.
171 // It is instead done via Admin_Menu, which is loaded for all admin pages.
172 //register_setting('photonic_options-'.$slug, 'photonic_options', [&$this, 'validate_options']);
173
174 add_settings_section($slug, "", [&$this, "create_settings_section"], $this->file);
175 $this->add_settings_fields($this->file);
176 }
177 }
178
179 function validate_options($options) {
180 foreach ($options as $option => $option_value) {
181 if (isset($this->reverse_options[$option])) {
182 //Sanitize options
183 switch ($this->reverse_options[$option]) {
184 // For all text type of options make sure that the eventual text is properly escaped.
185 case "text":
186 case "textarea":
187 case "color-picker":
188 case "background":
189 case "border":
190 $options[$option] = esc_attr($option_value);
191 break;
192
193 case "select":
194 case "radio":
195 if (isset($this->allowed_values[$option])) {
196 if (!array_key_exists($option_value, $this->allowed_values[$option])) {
197 $options[$option] = $this->option_defaults[$option];
198 }
199 }
200 break;
201
202 case "multi-select":
203 $selections = explode(',', $option_value);
204 $final_selections = [];
205 foreach ($selections as $selection) {
206 if (array_key_exists($selection, $this->allowed_values[$option])) {
207 $final_selections[] = $selection;
208 }
209 }
210 $options[$option] = implode(',', $final_selections);
211 break;
212
213 case "sortable-list":
214 $selections = explode(',', $option_value);
215 $final_selections = [];
216 $master_list = $this->option_defaults[$option]; // Sortable lists don't have their values in ['options']
217 foreach ($selections as $selection) {
218 if (array_key_exists($selection, $master_list)) {
219 $final_selections[] = $selection;
220 }
221 }
222 $options[$option] = implode(',', $final_selections);
223 break;
224
225 case "checkbox":
226 if (!in_array($option_value, ['on', 'off', 'true', 'false']) && isset($this->option_defaults[$option])) {
227 $options[$option] = $this->option_defaults[$option];
228 }
229 break;
230 }
231 }
232 }
233
234 /* The Settings API does an update_option($option, $value), overwriting the $photonic_options array with the values on THIS page
235 * This is problematic because all options are stored in a single array, but are displayed on different options pages.
236 * Hence the overwrite kills the options from the other pages.
237 * So this is a workaround to include the options from other pages as hidden fields on this page, so that the array gets properly updated.
238 * The alternative would be to separate options for each page, but that would cause a migration headache for current users.
239 */
240 $current_options = array_keys($options);
241 if (isset($this->hidden_options) && is_array($this->hidden_options)) {
242 foreach ($this->hidden_options as $hidden_option => $hidden_value) {
243 if (strlen($hidden_option) >= 7 && (substr($hidden_option, 0, 7) == 'submit-' || substr($hidden_option, 0, 6) == 'reset-') || in_array($hidden_option, $current_options)) {
244 continue;
245 }
246 $options[$hidden_option] = esc_attr($hidden_value);
247 }
248 }
249
250 foreach ($this->nested_options as $section => $children) {
251 if (isset($options['submit-'.$section])) {
252 $options['last-set-section'] = $section;
253 if (substr($options['submit-'.$section], 0, 9) == 'Save page' || substr($options['submit-'.$section], 0, 10) == 'Reset page') {
254 global $photonic_options;
255 foreach ($this->nested_options as $inner_section => $inner_children) {
256 if ($inner_section != $section) {
257 foreach ($inner_children as $inner_child) {
258 if (isset($photonic_options[$inner_child]) && !in_array($inner_child, $current_options)) {
259 $options[$inner_child] = $photonic_options[$inner_child];
260 }
261 }
262 }
263 }
264
265 if (substr($options['submit-'.$section], 0, 10) == 'Reset page') {
266 unset($options['submit-'.$section]);
267 // This is a reset for an individual section. So we will unset the child fields.
268 foreach ($children as $child) {
269 unset($options[$child]);
270 }
271 }
272 unset($options['submit-'.$section]);
273 }
274 else if (substr($options['submit-'.$section], 0, 6) == 'Delete') {
275 return;
276 }
277 break;
278 }
279 }
280 return $options;
281 }
282
283 function get_option_structure() {
284 if (isset($this->option_structure)) {
285 return $this->option_structure;
286 }
287 $options = $this->tab_options;
288 $option_structure = [];
289 foreach ($options as $value) {
290 switch ($value['type']) {
291 case "title":
292 case "section":
293 $option_structure[$value['category']] = [];
294 $option_structure[$value['category']]['slug'] = $value['category'];
295 $option_structure[$value['category']]['name'] = $value['name'];
296 $option_structure[$value['category']]['children'] = [];
297
298 if (isset($value['help'])) $option_structure[$value['category']]['help'] = $value['help'];
299 if (isset($value['buttons'])) $option_structure[$value['category']]['buttons'] = $value['buttons'];
300 break;
301 default:
302 if (isset($value['id'])) {
303 $option_structure[$value['grouping']]['children'][$value['id']] = $value['name'];
304 }
305 }
306 }
307 return $option_structure;
308 }
309
310 function add_settings_fields($page) {
311 $ctr = 0;
312 foreach ($this->tab_options as $value) {
313 $ctr++;
314 switch ($value['type']) {
315 case "blurb";
316 add_settings_field($value['grouping'].'-'.$ctr, $value['name'], [&$this, "create_section_for_blurb"], $page, $value['grouping'], $value);
317 break;
318
319 case "text";
320 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_text"], $page, $value['grouping'], $value);
321 break;
322
323 case "textarea";
324 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_textarea"], $page, $value['grouping'], $value);
325 break;
326
327 case "select":
328 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_select"], $page, $value['grouping'], $value);
329 break;
330
331 case "multi-select":
332 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_multi_select"], $page, $value['grouping'], $value);
333 break;
334
335 case "radio":
336 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_radio"], $page, $value['grouping'], $value);
337 break;
338
339 case "checkbox":
340 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_checkbox"], $page, $value['grouping'], $value);
341 break;
342
343 case "border":
344 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_border"], $page, $value['grouping'], $value);
345 break;
346
347 case "background":
348 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_background"], $page, $value['grouping'], $value);
349 break;
350
351 case "padding":
352 add_settings_field($value['id'], $value['name'], [&$this, "create_section_for_padding"], $page, $value['grouping'], $value);
353 break;
354 }
355 }
356 }
357
358 function create_section_for_radio($value) {
359 global $photonic_options;
360 $defaults = Defaults::get_options();
361 $this->create_opening_tag($value);
362 foreach ($value['options'] as $option_value => $option_text) {
363 $option_value = stripslashes($option_value);
364 if (isset($photonic_options[$value['id']])) {
365 $checked = checked(stripslashes($photonic_options[$value['id']]), $option_value, false);
366 }
367 else {
368 $checked = checked($defaults[$value['id']], $option_value, false);
369 }
370 echo '<div class="photonic-radio"><label><input type="radio" name="photonic_options['.$value['id'].']" value="'.$option_value.'" '.$checked."/>".$option_text."</label></div>\n";
371 }
372 $this->create_closing_tag($value);
373 }
374
375 function create_section_for_text($value) {
376 global $photonic_options;
377 $defaults = Defaults::get_options();
378 $this->create_opening_tag($value);
379 if (!isset($photonic_options[$value['id']])) {
380 $text = $defaults[$value['id']];
381 }
382 else {
383 $text = $photonic_options[$value['id']];
384 $text = stripslashes($text);
385 $text = esc_attr($text);
386 }
387
388 echo '<input type="text" name="photonic_options['.$value['id'].']" value="'.$text.'" />'."\n";
389 if (isset($value['hint'])) {
390 echo "<em> &laquo; ".$value['hint']."<br /></em>\n";
391 }
392 $this->create_closing_tag($value);
393 }
394
395 function create_section_for_textarea($value) {
396 global $photonic_options;
397 $defaults = Defaults::get_options();
398 $this->create_opening_tag($value);
399 echo '<textarea name="photonic_options['.$value['id'].']" cols="" rows="">'."\n";
400 if (isset($photonic_options[$value['id']]) && $photonic_options[$value['id']] != "") {
401 $text = stripslashes($photonic_options[$value['id']]);
402 $text = esc_attr($text);
403 echo $text;
404 }
405 else {
406 echo $defaults[$value['id']];
407 }
408 echo '</textarea>';
409 if (isset($value['hint'])) {
410 echo " &laquo; ".$value['hint']."<br />\n";
411 }
412 $this->create_closing_tag($value);
413 }
414
415 function create_section_for_select($value) {
416 global $photonic_options;
417 $defaults = Defaults::get_options();
418 $this->create_opening_tag($value);
419 echo '<select name="photonic_options['.$value['id'].']">'."\n";
420 foreach ($value['options'] as $option_value => $option_text) {
421 echo "<option ";
422 if (isset($photonic_options[$value['id']])) {
423 selected($photonic_options[$value['id']], $option_value);
424 }
425 else {
426 selected($defaults[$value['id']], $option_value);
427 }
428 echo " value='$option_value' >".$option_text."</option>\n";
429 }
430 echo "</select>\n";
431 $this->create_closing_tag($value);
432 }
433
434 function create_section_for_multi_select($value) {
435 global $photonic_options;
436 $defaults = Defaults::get_options();
437 $this->create_opening_tag($value);
438 echo '<div class="photonic-checklist">'."\n";
439 echo '<ul class="photonic-checklist" id="'.$value['id'].'-chk" >'."\n";
440 if (isset($defaults[$value['id']])) {
441 $consolidated_value = $defaults[$value['id']];
442 }
443 if (isset($photonic_options[$value['id']])) {
444 $consolidated_value = $photonic_options[$value['id']];
445 }
446 if (!isset($consolidated_value)) {
447 $consolidated_value = "";
448 }
449 $consolidated_value = trim($consolidated_value);
450 $exploded = [];
451 if ($consolidated_value != '') {
452 $exploded = explode(',', $consolidated_value);
453 }
454
455 foreach ($value['options'] as $option_value => $option_list) {
456 $checked = " ";
457 if ($consolidated_value) {
458 foreach ($exploded as $checked_value) {
459 $checked = checked($checked_value, $option_value, false);
460 if (trim($checked) != '') {
461 break;
462 }
463 }
464 }
465 echo "<li>\n";
466 $depth = 0;
467 if (isset($option_list['depth'])) {
468 $depth = $option_list['depth'];
469 }
470 echo '<label><input type="checkbox" name="'.$value['id']."_".$option_value.'" value="true" '.$checked.' class="depth-'.($depth+1).' photonic-options-checkbox-'.$value['id'].'" data-photonic-selection-for="'.$value['id'].'" data-photonic-value="'.$option_value.'" />'.$option_list['title']."</label>\n";
471 echo "</li>\n";
472 }
473 echo "</ul>\n";
474
475 if (isset($photonic_options[$value['id']])) {
476 $set_value = $photonic_options[$value['id']];
477 }
478 else if (isset($defaults[$value['id']])) {
479 $set_value = $defaults[$value['id']];
480 }
481 else {
482 $set_value = "";
483 }
484 echo '<input type="hidden" name="photonic_options['.$value['id'].']" id="'.$value['id'].'" value="'.$set_value.'"/>'."\n";
485 echo "</div>\n";
486 $this->create_closing_tag($value);
487 }
488
489 function create_settings_section($section) {
490 $option_structure = $this->option_structure;
491 if ($this->displayed_sections != 0) {
492 $this->show_buttons($this->previous_displayed_section, $option_structure[$this->previous_displayed_section]);
493 echo "</form>\n";
494 echo "</div><!-- /photonic-options-panel -->\n";
495 }
496
497 echo "<div id='{$section['id']}' class='photonic-options-panel'> \n";
498 echo "<form method=\"post\" action=\"options.php\" id=\"photonic-options-form-{$section['id']}\" class='photonic-options-form'>\n";
499 echo '<h3>' . $option_structure[$section['id']]['name'] . "</h3>\n";
500
501 /*
502 * We store all options in one array, but display them across multiple pages. Hence we need the following hack.
503 * We are registering the same setting across multiple pages, hence we need to pass the "page" parameter to options.php.
504 * Otherwise options.php returns an error saying "Options page not found"
505 */
506 echo "<input type='hidden' name='page' value='" . esc_attr($_REQUEST['page']) . "' />\n";
507 if (!isset($_REQUEST['tab'])) {
508 $tab = 'theme-options-intro.php';
509 }
510 else {
511 $tab = esc_attr($_REQUEST['tab']);
512 }
513 echo "<input type='hidden' name='tab' value='" . $tab . "' />\n";
514
515 settings_fields("photonic_options-{$section['id']}");
516 $this->displayed_sections++;
517 $this->previous_displayed_section = $section['id'];
518 }
519
520 function create_section_for_blurb($value) {
521 $this->create_opening_tag($value);
522 $this->create_closing_tag($value);
523 }
524
525 /**
526 * Renders an option whose type is "checkbox". Invoked by add_settings_field.
527 *
528 * @param $value
529 * @return void
530 */
531 function create_section_for_checkbox($value) {
532 global $photonic_options;
533 $checked = '';
534 if (isset($photonic_options[$value['id']])) {
535 $checked = checked(stripslashes($photonic_options[$value['id']]), 'on', false);
536 }
537 $this->create_opening_tag($value);
538 echo '<label><input type="checkbox" name="photonic_options['.$value['id'].']" '.$checked."/>{$value['desc']}</label>\n";
539 $this->create_closing_tag($value);
540 }
541
542 /**
543 * Renders an option whose type is "border". Invoked by add_settings_field.
544 *
545 * @param $value
546 * @return void
547 */
548 function create_section_for_border($value) {
549 global $photonic_options;
550 $defaults = Defaults::get_options();
551 $this->create_opening_tag($value);
552 $original = $defaults[$value['id']];
553 if (!isset($photonic_options[$value['id']])) {
554 $default = $defaults[$value['id']];
555 $default_txt = "";
556 foreach ($default as $edge => $edge_val) {
557 $default_txt .= $edge.'::';
558 foreach ($edge_val as $opt => $opt_val) {
559 $default_txt .= $opt . "=" . $opt_val . ";";
560 }
561 $default_txt .= "||";
562 }
563 }
564 else {
565 $default_txt = $photonic_options[$value['id']];
566 $default = $default_txt;
567 $edge_array = explode('||', $default);
568 $default = [];
569 if (is_array($edge_array)) {
570 foreach ($edge_array as $edge_vals) {
571 if (trim($edge_vals) != '') {
572 $edge_val_array = explode('::', $edge_vals);
573 if (is_array($edge_val_array) && count($edge_val_array) > 1) {
574 $vals = explode(';', $edge_val_array[1]);
575 $default[$edge_val_array[0]] = [];
576 foreach ($vals as $val) {
577 $pair = explode("=", $val);
578 if (isset($pair[0]) && isset($pair[1])) {
579 $default[$edge_val_array[0]][$pair[0]] = $pair[1];
580 }
581 else if (isset($pair[0]) && !isset($pair[1])) {
582 $default[$edge_val_array[0]][$pair[0]] = "";
583 }
584 }
585 }
586 }
587 }
588 }
589 }
590 $edges = ['top' => 'Top', 'right' => 'Right', 'bottom' => 'Bottom', 'left' => 'Left'];
591 $styles = ["none" => "No border",
592 "hidden" => "Hidden",
593 "dotted" => "Dotted",
594 "dashed" => "Dashed",
595 "solid" => "Solid",
596 "double" => "Double",
597 "grove" => "Groove",
598 "ridge" => "Ridge",
599 "inset" => "Inset",
600 "outset" => "Outset"];
601
602 $border_width_units = ["px" => "Pixels (px)", "em" => "Em"];
603
604 foreach ($value['options'] as $option_value => $option_text) {
605 if (isset($photonic_options[$value['id']])) {
606 $checked = checked($photonic_options[$value['id']], $option_value, false);
607 }
608 else {
609 $checked = checked($defaults[$value['id']], $option_value, false);
610 }
611 echo '<div class="photonic-radio"><input type="radio" name="'.$value['id'].'" value="'.$option_value.'" '.$checked."/>".$option_text."</div>\n";
612 }
613 ?>
614 <div class='photonic-border-options'>
615 <p>For any edge set style to "No Border" if you don't want a border.</p>
616 <table class='opt-sub-table-5'>
617 <col class='opt-sub-table-col-51'/>
618 <col class='opt-sub-table-col-5'/>
619 <col class='opt-sub-table-col-5'/>
620 <col class='opt-sub-table-col-5'/>
621 <col class='opt-sub-table-col-5'/>
622
623 <tr>
624 <th scope="col">&nbsp;</th>
625 <th scope="col">Border Style</th>
626 <th scope="col">Color</th>
627 <th scope="col">Border Width</th>
628 <th scope="col">Border Width Units</th>
629 </tr>
630
631 <?php
632 foreach ($edges as $edge => $edge_text) {
633 ?>
634 <tr>
635 <th scope="row"><?php echo $edge_text; ?></th>
636 <td valign='top'>
637 <select name="<?php echo $value['id'].'-'.$edge; ?>-style" id="<?php echo $value['id'].'-'.$edge; ?>-style" >
638 <?php
639 foreach ($styles as $option_value => $option_text) {
640 echo "<option ";
641 if (isset($default[$edge]) && isset($default[$edge]['style'])) {
642 selected($default[$edge]['style'], $option_value);
643 }
644 echo " value='$option_value' >".$option_text."</option>\n";
645 }
646 ?>
647 </select>
648 </td>
649
650 <td valign='top'>
651 <div class="color-picker-group">
652 <input type="radio" name="<?php echo $value['id'].'-'.$edge; ?>-colortype" value="transparent" <?php checked($default[$edge]['colortype'], 'transparent'); ?> /> Transparent / No color<br/>
653 <input type="radio" name="<?php echo $value['id'].'-'.$edge; ?>-colortype" value="custom" <?php checked($default[$edge]['colortype'], 'custom'); ?>/> Custom
654 <input type="text" id="<?php echo $value['id'].'-'.$edge; ?>-color" name="<?php echo $value['id']; ?>-color" value="<?php echo $default[$edge]['color']; ?>" data-photonic-default-color="<?php echo $original[$edge]['color']; ?>" class="color" /><br />
655 Default: <span> <?php echo $original[$edge]['color']; ?> </span>
656 </div>
657 </td>
658
659 <td valign='top'>
660 <input type="text" id="<?php echo $value['id'].'-'.$edge; ?>-border-width" name="<?php echo $value['id'].'-'.$edge; ?>-border-width" value="<?php echo $default[$edge]['border-width']; ?>" /><br />
661 </td>
662
663 <td valign='top'>
664 <select name="<?php echo $value['id'].'-'.$edge; ?>-border-width-type" id="<?php echo $value['id'].'-'.$edge; ?>-border-width-type" >
665 <?php
666 foreach ($border_width_units as $option_value => $option_text) {
667 echo "<option ";
668 selected($default[$edge]['border-width-type'], $option_value);
669 echo " value='$option_value' >".$option_text."</option>\n";
670 }
671 ?>
672 </select>
673 </td>
674 </tr>
675 <?php
676 }
677 ?>
678 </table>
679 <input type='hidden' id="<?php echo $value['id']; ?>" name="photonic_options[<?php echo $value['id']; ?>]" value="<?php echo $default_txt; ?>" />
680 </div>
681 <?php
682 $this->create_closing_tag($value);
683 }
684
685 /**
686 * Renders an option whose type is "background". Invoked by add_settings_field.
687 *
688 * @param $value
689 * @return void
690 */
691 function create_section_for_background($value) {
692 global $photonic_options;
693 $defaults = Defaults::get_options();
694
695 $this->create_opening_tag($value);
696 $original = $defaults[$value['id']];
697 if (!isset($photonic_options[$value['id']])) {
698 $default = $defaults[$value['id']];
699 $default_txt = "";
700 foreach ($defaults[$value['id']] as $opt => $opt_val) {
701 $default_txt .= $opt."=".$opt_val.";";
702 }
703 }
704 else {
705 $default_txt = $photonic_options[$value['id']];
706 $default = $default_txt;
707 $vals = explode(";", $default);
708 $default = [];
709 foreach ($vals as $val) {
710 $pair = explode("=", $val);
711 if (isset($pair[0]) && isset($pair[1])) {
712 $default[$pair[0]] = $pair[1];
713 }
714 else if (isset($pair[0]) && !isset($pair[1])) {
715 $default[$pair[0]] = "";
716 }
717 }
718 }
719 $repeats = ["repeat" => "Repeat horizontally and vertically",
720 "repeat-x" => "Repeat horizontally only",
721 "repeat-y" => "Repeat vertically only",
722 "no-repeat" => "Do not repeat"];
723
724 $positions = ["top left" => "Top left",
725 "top center" => "Top center",
726 "top right" => "Top right",
727 "center left" => "Center left",
728 "center center" => "Middle of the page",
729 "center right" => "Center right",
730 "bottom left" => "Bottom left",
731 "bottom center" => "Bottom center",
732 "bottom right" => "Bottom right"];
733
734 foreach ($value['options'] as $option_value => $option_text) {
735 if (isset($photonic_options[$value['id']])) {
736 $checked = checked($photonic_options[$value['id']], $option_value, false);
737 }
738 else {
739 $checked = checked($defaults[$value['id']], $option_value, false);
740 }
741 echo '<div class="photonic-radio"><input type="radio" name="'.$value['id'].'" value="'.$option_value.'" '.$checked."/>".$option_text."</div>\n";
742 }
743 ?>
744 <div class='photonic-background-options'>
745 <table class='opt-sub-table'>
746 <colgroup>
747 <col class='opt-sub-table-cols'/>
748 <col class='opt-sub-table-cols'/>
749 </colgroup>
750 <tr>
751 <td valign='top'>
752 <div class="color-picker-group">
753 <strong>Background Color:</strong><br />
754 <label><input type="radio" name="<?php echo $value['id']; ?>-colortype" value="transparent" <?php checked($default['colortype'], 'transparent'); ?> /> Transparent / No color</label><br/>
755 <label><input type="radio" name="<?php echo $value['id']; ?>-colortype" value="custom" <?php checked($default['colortype'], 'custom'); ?>/> Custom</label>
756 <input type="text" id="<?php echo $value['id']; ?>-bgcolor" name="<?php echo $value['id']; ?>-bgcolor" value="<?php echo $default['color']; ?>" data-photonic-default-color="<?php echo $original['color']; ?>" class="color" /><br />
757 Default: <span> <?php echo $original['color']; ?> </span>
758 </div>
759 </td>
760 <td valign='top'>
761 <strong>Image URL:</strong><br />
762 <?php $this->display_upload_field($default['image'], $value['id']."-bgimg", $value['id']."-bgimg"); ?>
763 </td>
764 </tr>
765
766 <tr>
767 <td valign='top'>
768 <strong>Image Position:</strong><br />
769 <select name="<?php echo $value['id']; ?>-position" id="<?php echo $value['id']; ?>-position" >
770 <?php
771 foreach ($positions as $option_value => $option_text) {
772 echo "<option ";
773 selected($default['position'], $option_value);
774 echo " value='$option_value' >".$option_text."</option>\n";
775 }
776 ?>
777 </select>
778 </td>
779
780 <td valign='top'>
781 <strong>Image Repeat:</strong><br />
782 <select name="<?php echo $value['id']; ?>-repeat" id="<?php echo $value['id']; ?>-repeat" >
783 <?php
784 foreach ($repeats as $option_value => $option_text) {
785 echo "<option ";
786 selected($default['repeat'], $option_value);
787 echo " value='$option_value' >".$option_text."</option>\n";
788 }
789 ?>
790 </select>
791 </td>
792 </tr>
793 <tr>
794 <td valign='top' colspan='2'>
795 <div class='slider'>
796 <p>
797 <strong>Layer Transparency (not for IE):</strong>
798 <select id="<?php echo $value['id']; ?>-trans" name="<?php echo $value['id']; ?>-trans">
799 <?php
800 for ($i = 0; $i <= 100; $i++) {
801 echo "<option ";
802 selected($default['trans'], $i);
803 echo " value='$i' >".$i."</option>\n";
804 }
805 ?>
806 </select>
807 </p>
808 </div>
809 </td>
810 </tr>
811 </table>
812 <input type='hidden' id="<?php echo $value['id']; ?>" name="photonic_options[<?php echo $value['id']; ?>]" value="<?php echo $default_txt; ?>" />
813 </div>
814 <?php
815 $this->create_closing_tag($value);
816 }
817
818 /**
819 * Renders an option whose type is "background". Invoked by add_settings_field.
820 *
821 * @param $value
822 * @return void
823 */
824 function create_section_for_padding($value) {
825 global $photonic_options;
826 $defaults = Defaults::get_options();
827 $this->create_opening_tag($value);
828 if (!isset($photonic_options[$value['id']])) {
829 $default = $defaults[$value['id']];
830 $default_txt = "";
831 foreach ($default as $edge => $edge_val) {
832 $default_txt .= $edge.'::';
833 foreach ($edge_val as $opt => $opt_val) {
834 $default_txt .= $opt . "=" . $opt_val . ";";
835 }
836 $default_txt .= "||";
837 }
838 }
839 else {
840 $default_txt = $photonic_options[$value['id']];
841 $default = $default_txt;
842 $edge_array = explode('||', $default);
843 $default = [];
844 if (is_array($edge_array)) {
845 foreach ($edge_array as $edge_vals) {
846 if (trim($edge_vals) != '') {
847 $edge_val_array = explode('::', $edge_vals);
848 if (is_array($edge_val_array) && count($edge_val_array) > 1) {
849 $vals = explode(';', $edge_val_array[1]);
850 $default[$edge_val_array[0]] = [];
851 foreach ($vals as $val) {
852 $pair = explode("=", $val);
853 if (isset($pair[0]) && isset($pair[1])) {
854 $default[$edge_val_array[0]][$pair[0]] = $pair[1];
855 }
856 else if (isset($pair[0]) && !isset($pair[1])) {
857 $default[$edge_val_array[0]][$pair[0]] = "";
858 }
859 }
860 }
861 }
862 }
863 }
864 }
865 $edges = ['top' => 'Top', 'right' => 'Right', 'bottom' => 'Bottom', 'left' => 'Left'];
866 $padding_units = ["px" => "Pixels (px)", "em" => "Em"];
867
868 foreach ($value['options'] as $option_value => $option_text) {
869 if (isset($photonic_options[$value['id']])) {
870 $checked = checked($photonic_options[$value['id']], $option_value, false);
871 }
872 else {
873 $checked = checked($defaults[$value['id']], $option_value, false);
874 }
875 echo '<div class="photonic-radio"><input type="radio" name="'.$value['id'].'" value="'.$option_value.'" '.$checked."/>".$option_text."</div>\n";
876 }
877 ?>
878 <div class='photonic-padding-options'>
879 <table class='opt-sub-table-5'>
880 <col class='opt-sub-table-col-51'/>
881 <col class='opt-sub-table-col-5'/>
882 <col class='opt-sub-table-col-5'/>
883
884 <tr>
885 <th scope="col">&nbsp;</th>
886 <th scope="col">Padding</th>
887 <th scope="col">Padding Units</th>
888 </tr>
889
890 <?php
891 foreach ($edges as $edge => $edge_text) {
892 ?>
893 <tr>
894 <th scope="row"><?php echo $edge_text; ?></th>
895 <td valign='top'>
896 <input type="text" id="<?php echo $value['id'].'-'.$edge; ?>-padding" name="<?php echo $value['id'].'-'.$edge; ?>-padding" value="<?php echo $default[$edge]['padding']; ?>" /><br />
897 </td>
898
899 <td valign='top'>
900 <select name="<?php echo $value['id'].'-'.$edge; ?>-padding-type" id="<?php echo $value['id'].'-'.$edge; ?>-padding-type" >
901 <?php
902 foreach ($padding_units as $option_value => $option_text) {
903 echo "<option ";
904 selected($default[$edge]['padding-type'], $option_value);
905 echo " value='$option_value' >".$option_text."</option>\n";
906 }
907 ?>
908 </select>
909 </td>
910 </tr>
911 <?php
912 }
913 ?>
914 </table>
915 <input type='hidden' id="<?php echo $value['id']; ?>" name="photonic_options[<?php echo $value['id']; ?>]" value="<?php echo $default_txt; ?>" />
916 </div>
917 <?php
918 $this->create_closing_tag($value);
919 }
920
921 /**
922 * Creates the opening markup for each option.
923 *
924 * @param $value
925 * @return void
926 */
927 function create_opening_tag($value) {
928 echo "<div class='photonic-section fix'>\n";
929 if (isset($value['desc']) && $value['type'] != 'checkbox') {
930 echo $value['desc']."<br />";
931 }
932 if (isset($value['note'])) {
933 echo "<span class=\"note\">".$value['note']."</span><br />";
934 }
935 }
936
937 /**
938 * Creates the closing markup for each option.
939 *
940 * @param $value
941 * @return void
942 */
943 function create_closing_tag($value) {
944 echo "</div><!-- photonic-section -->\n";
945 }
946
947 /**
948 * This method displays an upload field and button. This has been separated from the create_section_for_upload method,
949 * because this is used by the create_section_for_background as well.
950 *
951 * @param $upload
952 * @param $id
953 * @param $name
954 * @param null $hint
955 * @return void
956 */
957 function display_upload_field($upload, $id, $name, $hint = null) {
958 echo '<input type="text" name="'.$name.'" id="'.$id.'" value="'.$upload.'" />'."\n";
959 if ($hint != null) {
960 echo "<em> &laquo; ".$hint."<br /></em>\n";
961 }
962 }
963
964 /**
965 * Save generated options to a file. This uses the WP_Filesystem to validate the credentials of the user attempting to save options.
966 *
967 * @param $custom_css
968 * @return bool
969 */
970 function save_css_to_file($custom_css) {
971 if(!isset($_GET['settings-updated'])) {
972 return false;
973 }
974
975 $url = wp_nonce_url('admin.php?page=photonic-options-manager');
976 if (false === ($creds = request_filesystem_credentials($url, '', false, false))) {
977 return true;
978 }
979
980 if (!WP_Filesystem($creds)) {
981 request_filesystem_credentials($url, '', true, false);
982 return true;
983 }
984
985 /** @var $wp_filesystem \WP_Filesystem_Base */
986 global $wp_filesystem;
987 if (!is_dir(PHOTONIC_UPLOAD_DIR)) {
988 if (!$wp_filesystem->mkdir(PHOTONIC_UPLOAD_DIR)) {
989 echo "<div class='error'><p>Failed to create directory ".PHOTONIC_UPLOAD_DIR.". Please check your folder permissions.</p></div>";
990 return false;
991 }
992 }
993
994 $filename = trailingslashit(PHOTONIC_UPLOAD_DIR).'custom-styles.css';
995
996 if (empty($custom_css)) {
997 return false;
998 }
999
1000 if (!$wp_filesystem->put_contents($filename, $custom_css, FS_CHMOD_FILE)) {
1001 echo "<div class='error'><p>Failed to save file $filename. Please check your folder permissions.</p></div>";
1002 return false;
1003 }
1004 return true;
1005 }
1006 }
1007