| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\WordPress\Settings; |
| 4 |
|
| 5 |
use Depicter\WordPress\Settings\Options\Checkbox; |
| 6 |
use Depicter\WordPress\Settings\Options\Choices; |
| 7 |
use Depicter\WordPress\Settings\Options\CodeEditor; |
| 8 |
use Depicter\WordPress\Settings\Options\Select; |
| 9 |
use Depicter\WordPress\Settings\Options\SelectMultiple; |
| 10 |
use Depicter\WordPress\Settings\Options\Text; |
| 11 |
use Depicter\WordPress\Settings\Options\Textarea; |
| 12 |
use Depicter\WordPress\Settings\Options\WPEditor; |
| 13 |
use Jeffreyvr\WPSettings\WPSettings; |
| 14 |
|
| 15 |
class Settings extends WPSettings |
| 16 |
{ |
| 17 |
|
| 18 |
public $prefix = 'depicter_'; |
| 19 |
|
| 20 |
public function __construct( $title, $slug = null, $prefix = 'depicter_') |
| 21 |
{ |
| 22 |
$this->prefix = $prefix; |
| 23 |
add_filter( 'wp_settings_option_type_map', [ $this, 'change_options_handler' ] ); |
| 24 |
|
| 25 |
parent::__construct( $title, $slug ); |
| 26 |
} |
| 27 |
|
| 28 |
public function change_options_handler( $types ) |
| 29 |
{ |
| 30 |
return [ |
| 31 |
'text' => Text::class, |
| 32 |
'checkbox' => Checkbox::class, |
| 33 |
'choices' => Choices::class, |
| 34 |
'textarea' => Textarea::class, |
| 35 |
'wp-editor' => WPEditor::class, |
| 36 |
'code-editor' => CodeEditor::class, |
| 37 |
'select' => Select::class, |
| 38 |
'select-multiple' => SelectMultiple::class |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_url() |
| 43 |
{ |
| 44 |
if ($this->parent_slug) { |
| 45 |
return \add_query_arg('page', $this->slug, \admin_url("admin.php?page=" . $this->parent_slug)); |
| 46 |
} |
| 47 |
|
| 48 |
return \admin_url("admin.php?page=$this->slug"); |
| 49 |
} |
| 50 |
|
| 51 |
public function save() |
| 52 |
{ |
| 53 |
if (! isset($_POST['wp_settings_trigger'])) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if (! current_user_can($this->capability)) { |
| 58 |
wp_die(__('What do you think you are doing?')); |
| 59 |
} |
| 60 |
|
| 61 |
if (! isset($_POST[$this->option_name]) && !isset($_POST['wp_settings_submitted'])) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$new_options = apply_filters('wp_settings_new_options', $_POST[$this->option_name] ?? [] ); |
| 66 |
|
| 67 |
foreach ($new_options as $option => $value) { |
| 68 |
$_option = $this->find_option($option); |
| 69 |
|
| 70 |
$valid = $_option->validate($value); |
| 71 |
|
| 72 |
if (!$valid) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$value = apply_filters( "wp_settings_new_options_$option", $_option->sanitize($value), $_option ); |
| 77 |
|
| 78 |
update_option( $this->prefix . $option, $_option->sanitize( $value ) ); |
| 79 |
} |
| 80 |
|
| 81 |
// checking unchecked checkbox option |
| 82 |
foreach ($this->tabs as $tab) { |
| 83 |
foreach ($tab->sections as $section) { |
| 84 |
foreach ($section->options as $option) { |
| 85 |
if ( $option->type == 'checkbox' && !isset( $new_options[ $option->args['name' ] ] ) ) { |
| 86 |
update_option( $this->prefix . $option->args['name'], '' ); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$this->flash->set('success', __('Saved changes!')); |
| 93 |
} |
| 94 |
} |