forked from beezwax/WP-Publish-to-Apple-News
-
Notifications
You must be signed in to change notification settings - Fork 71
/
class-admin-apple-settings.php
258 lines (226 loc) · 6.39 KB
/
class-admin-apple-settings.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
<?php
/**
* Publish to Apple News: Admin_Apple_Settings class
*
* @package Apple_News
*/
// Include dependencies.
require_once dirname( __DIR__ ) . '/includes/apple-exporter/class-settings.php';
require_once __DIR__ . '/settings/class-admin-apple-settings-section.php';
require_once __DIR__ . '/settings/class-admin-apple-settings-section-api.php';
require_once __DIR__ . '/settings/class-admin-apple-settings-section-advanced.php';
require_once __DIR__ . '/settings/class-admin-apple-settings-section-post-types.php';
require_once __DIR__ . '/settings/class-admin-apple-settings-section-developer-tools.php';
use Apple_Exporter\Settings;
/**
* This class is in charge of creating a WordPress page to manage the
* Exporter's settings class.
*/
class Admin_Apple_Settings extends Apple_News {
/**
* Keeps track of whether functionality has been initialized or not.
*
* @access private
* @var bool
*/
private static $initialized = false;
/**
* Associative array of fields and types. If not present, defaults to string.
* Possible types are: integer, color, boolean, string and options.
* If options, use an array instead of a string.
*
* @since 0.4.0
* @var array
* @access private
*/
private $field_types;
/**
* Optionally define more elaborated labels for each setting and store them
* here.
*
* @since 0.6.0
* @var array
* @access private
*/
private $field_labels;
/**
* Only load settings once. Cache results for easy and efficient usage.
*
* @var Settings
* @access private
*/
private $loaded_settings;
/**
* Available settings sections.
*
* @var array
* @access private
*/
private $sections;
/**
* Settings page name.
*
* @var string
* @access private
*/
private $page_name;
/**
* Constructor.
*/
public function __construct() {
$this->loaded_settings = null;
$this->sections = [];
$this->page_name = $this->plugin_domain . '-options';
if ( ! self::$initialized ) {
add_action( 'admin_init', [ $this, 'register_sections' ], 5 );
add_action( 'admin_menu', [ $this, 'setup_options_page' ], 99 );
add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] );
self::$initialized = true;
}
}
/**
* Add settings sections.
*
* @access private
*/
private function add_sections() {
$this->add_section( new Admin_Apple_Settings_Section_API( $this->page_name ) );
$this->add_section( new Admin_Apple_Settings_Section_Post_Types( $this->page_name ) );
$this->add_section( new Admin_Apple_Settings_Section_Advanced( $this->page_name ) );
$this->add_section( new Admin_Apple_Settings_Section_Developer_Tools( $this->page_name ) );
}
/**
* Add a settings section.
*
* @param Admin_Apple_Settings_Section $section The section to add.
* @access private
*/
private function add_section( $section ) {
$this->sections[] = $section;
}
/**
* Load exporter settings and register them.
*
* @since 0.4.0
* @access public
*/
public function register_sections() {
$this->add_sections();
/**
* Modifies the available sections on the settings page.
*
* @param array $sections The list of available settings sections.
*/
$this->sections = apply_filters( 'apple_news_settings_sections', $this->sections );
}
/**
* Options page setup.
*
* @access public
*/
public function setup_options_page() {
add_submenu_page(
'apple_news_index',
__( 'Apple News Options', 'apple-news' ),
__( 'Settings', 'apple-news' ),
/** This filter is documented in admin/class-admin-apple-settings.php */
apply_filters( 'apple_news_settings_capability', 'manage_options' ),
$this->page_name,
[ $this, 'page_options_render' ]
);
}
/**
* Options page render.
*
* @access public
*/
public function page_options_render() {
/**
* Modifies the capability required to edit Apple News settings. Defaults
* to `manage_options`.
*
* @param string $capability The capability required to edit Apple News settings. Defaults to `manage_options`.
*/
if ( ! current_user_can( apply_filters( 'apple_news_settings_capability', 'manage_options' ) ) ) {
wp_die( esc_html__( 'You do not have permissions to access this page.', 'apple-news' ) );
}
/* phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable */
$sections = $this->sections;
/* phpcs:enable */
include __DIR__ . '/partials/page-options.php';
}
/**
* Register assets for the options page.
*
* @param string $hook The context under which this function is called.
* @access public
*/
public function register_assets( $hook ) {
if ( 'apple-news_page_apple-news-options' !== $hook ) {
return;
}
wp_enqueue_style(
'apple-news-select2-css',
plugin_dir_url( __FILE__ ) . '../assets/css/select2.min.css',
[],
self::$version
);
wp_enqueue_script(
'apple-news-select2-js',
plugin_dir_url( __FILE__ ) . '../assets/js/select2.full.min.js',
[ 'jquery' ],
self::$version,
false
);
wp_enqueue_script(
'apple-news-settings',
plugin_dir_url( __FILE__ ) . '../assets/js/settings.js',
[ 'jquery' ],
self::$version,
true
);
}
/**
* Creates a new Settings object and loads it with WordPress' saved settings.
*
* Merges saved settings from WordPress with default settings in the object.
*
* @access public
* @return Settings A Settings object containing merged settings.
*/
public function fetch_settings() {
// If settings are not already loaded, load them.
if ( is_null( $this->loaded_settings ) ) {
// Initialize.
$settings = new Settings();
$wp_settings = get_option( self::$option_name );
// Merge settings in the option with defaults.
foreach ( $settings->all() as $key => $value ) {
$wp_value = ( ! isset( $wp_settings[ $key ] ) )
? $value
: $wp_settings[ $key ];
$settings->$key = $wp_value;
}
// Store in local object storage.
$this->loaded_settings = $settings;
}
/**
* Allows for filtering of the merged settings before returning.
*
* @since 0.4.0
*
* @param Settings $settings The settings to be filtered.
*/
return apply_filters( 'apple_news_loaded_settings', $this->loaded_settings );
}
/**
* Replaces the current settings.
*
* @param array Settings $settings New settings to replace the old.
* @access public
*/
public function save_settings( $settings ) {
update_option( self::$option_name, $settings );
$this->loaded_settings = $settings;
}
}