-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmometasettings.php
107 lines (93 loc) · 3.18 KB
/
dmometasettings.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
<?php
class dmometasettings
{
public $file;
public $plugin;
public $settings;
public $namespace;
public function __construct($file, $namespace)
{
$this->file = $file;
$this->plugin = plugin_basename($file);
$this->namespace = $namespace;
$this->settings = (object)[];
$this->set_defaults();
}
/**
* Set default options settings for plugin
*/
public function set_defaults()
{
if (!get_option('dxw_members_only_list_content')) {
add_option('dxw_members_only_list_content', '');
}
if (!get_option('dxw_members_only_redirect')) {
add_option('dxw_members_only_redirect', '/wp-login.php?redirect_to=%return_path%');
}
if (!get_option('dxw_members_only_redirect_root')) {
add_option('dxw_members_only_redirect_root', '/wp-login.php?redirect_to=%return_path%');
}
if (!get_option('dxw_members_only_max_age')) {
add_option('dxw_members_only_max_age', 0);
}
if (!get_option('dxw_members_only_max_age_static')) {
add_option('dxw_members_only_max_age_static', 0);
}
if (!get_option('dxw_members_only_max_age_public')) {
add_option('dxw_members_only_max_age_public', 86400);
}
}
/**
* Wrapper for adding settings with the WP Settings API
*
* @param string $title Name of the setting
* @param array $options Setting options array
* @param string $callback Callback function
*/
public function add_settings($title, $options, $callback)
{
$this->settings->title = $title;
$this->settings->options = $options;
$this->settings->callback = $callback;
add_filter('plugin_action_links', [$this, 'plugin_action_links'], 10, 2);
add_action('admin_menu', [$this, 'admin_menu']);
add_filter('whitelist_options', [$this, 'whitelist_options']);
}
/**
* Insert action links for plugin
*
* @param array $links Existing action links for the plugin
* @param string $file File to link to in the actions
* @return array New action links
*/
public function plugin_action_links($links, $file)
{
if (dirname($file) === dirname($this->plugin)) {
array_unshift($links, '<a href="'.get_admin_url(null, 'options-general.php?page='.$this->plugin).'">'.__('Settings', 'dxwmembersonly').'</a>');
}
return $links;
}
/**
* Create admin menu for plugin
*
* @return void
*/
public function admin_menu()
{
add_options_page($this->settings->title, $this->settings->title, 'edit_users', $this->file, $this->settings->callback);
}
/**
* Prefix options with namespace
*
* @param array $whitelist_options Options to whitelist
* @return array Whitelisted options
*/
public function whitelist_options($whitelist_options)
{
$whitelist_options[$this->namespace] = [];
foreach ($this->settings->options as $opt) {
$whitelist_options[$this->namespace][] = $this->namespace.'_'.$opt;
}
return $whitelist_options;
}
}