-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-settings.php
312 lines (285 loc) · 8.12 KB
/
class-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
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
<?php
/**
* Settings
*
* The plugin settings.
*
* @package WPLDP
* @version 1.0.0
* @author Benoit Alessandroni
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU/GPLv2
* @since 2.0.0
*/
namespace WpLdp;
if ( ! class_exists( '\WpLdp\Settings' ) ) {
/**
* Handles everything related to the admin settings.
*
* @category Class
* @package WPLDP
* @author Benoit Alessandroni
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU/GPLv2
*/
class Settings {
/**
* Class default constructor.
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'ldp_menu' ) );
add_action( 'admin_menu', array( $this, 'menu_setup' ) );
add_action( 'admin_init', array( $this, 'backend_hooking' ) );
}
/**
* Initializes the PAIR containers if the associated
* option is checked.
*
* @param {boolean} $force Should the refresh be forced.
*/
function initialize_container( $force = false ) {
if ( isset( $_GET['settings-updated'] ) || $force ) {
$ldp_container_init = get_option( 'ldp_container_init', false );
if ( $ldp_container_init || $force ) {
$pair_terms = array(
'initiative' => array(
'label' => __( 'Initiative', 'wpldp' ),
'rdftype' => 'pair:initiative',
),
'organization' => array(
'label' => __( 'Organization', 'wpldp' ),
'rdftype' => 'foaf:organization',
),
'group' => array(
'label' => __( 'Group', 'wpldp' ),
'rdftype' => 'foaf:group',
),
'document' => array(
'label' => __( 'Document', 'wpldp' ),
'rdftype' => 'foaf:document',
),
'goodorservice' => array(
'label' => __( 'Good or Service', 'wpldp' ),
'rdftype' => 'goodRelation:goodOrService',
),
'artwork' => array(
'label' => __( 'Artwork', 'wpldp' ),
'rdftype' => 'schema:artwork',
),
'event' => array(
'label' => __( 'Event', 'wpldp' ),
'rdftype' => 'schema:event',
),
'place' => array(
'label' => __( 'Place', 'wpldp' ),
'rdftype' => 'schema:place',
),
'theme' => array(
'label' => __( 'Theme', 'wpldp' ),
'rdftype' => 'pair:theme',
),
'thesis' => array(
'label' => __( 'Thesis', 'wpldp' ),
'rdftype' => 'pair:thesis',
),
'person' => array(
'label' => __( 'Person', 'wpldp' ),
'rdftype' => 'pair:person',
)
);
foreach ( $pair_terms as $term => $properties ) {
// Loop on the models files (or hardcoded array) and push them each as taxonomy term in the database.
$model = file_get_contents( __DIR__ . '/models/' . $term . '.json' );
$term_id = null;
if ( ! term_exists( $term, 'ldp_container' ) ) {
$new_term = wp_insert_term(
$properties['label'],
'ldp_container',
array(
'slug' => $term,
'description' => sprintf(
__(
'The %1$s object model',
'wpldp'
),
$term
),
)
);
$term_id = $new_term['term_id'];
} else {
$existing_term = get_term_by( 'slug', $term, 'ldp_container' );
$updated_term = wp_update_term(
$existing_term->term_id,
'ldp_container',
array(
'slug' => $term,
'description' => sprintf(
__(
'The %1$s object model',
'wpldp'
),
$term
),
)
);
$term_id = $existing_term->term_id;
}
if ( ! empty( $term_id ) ) {
$term_meta = get_option( "ldp_container_$term_id" );
if ( ! is_array( $term_meta ) ) {
$term_meta = array();
}
$term_meta['ldp_rdf_type'] = $properties['rdftype'];
$term_meta['ldp_model'] = stripslashes_deep( $model );
update_option( "ldp_container_$term_id", $term_meta );
}
}
}
}
}
/**
* Overrides the default update message or/and add a new one.
*
* @return void
*/
function wpldp_validation_notice() {
global $pagenow;
if ( 'options-general.php' === $pagenow && 'wpldp' === $_GET['page'] ) { // change my-plugin to your plugin page.
if ( ( isset( $_GET['settings-updated'] ) && 'true' === $_GET['settings-updated'] ) ) {
$ldp_container_init = get_option( 'ldp_container_init', false );
if ( $ldp_container_init ) {
$update_message = __( 'The PAIR containers have been initialized, enjoy ;-)', 'wpldp' );
add_settings_error( 'general', 'settings_updated', $update_message, 'updated' );
}
}
}
}
/**
* Generates the containers-filtered menu entries.
*
* @return void
*/
function menu_setup() {
global $submenu;
// Removing all resources menu.
remove_submenu_page( 'edit.php?post_type=ldp_resource', 'edit.php?post_type=ldp_resource' );
$terms = get_terms(
'ldp_container',
array(
'hide_empty' => 0,
'order' => 'DESC'
)
);
$i = 0;
foreach ( $terms as $term ) {
$this->term_slug = $term->slug;
add_submenu_page(
'edit.php?post_type=ldp_resource',
sprintf(
__( 'List of all resources of type %1$s', 'wpldp' ),
$term->name
),
$term->name,
'edit_posts',
'edit.php?post_type=ldp_resource&ldp_container=' . $term->slug,
false
);
// Reordering position of menu pages.
$key_to_remove = null;
foreach ( $submenu['edit.php?post_type=ldp_resource'] as $submenu_item_key => $submenu_item_value ) {
if ( $submenu_item_value[0] === $term->name ) {
$submenu['edit.php?post_type=ldp_resource'][ 10 - $i ] = $submenu_item_value;
$key_to_remove = $submenu_item_key;
}
}
if ( ! empty( $key_to_remove ) ) {
unset( $submenu['edit.php?post_type=ldp_resource'][ $key_to_remove ] );
}
$i++;
}
ksort( $submenu['edit.php?post_type=ldp_resource'] );
}
/**
* Generates the plugin settings menu and associated page.
*
* @return void
*/
function ldp_menu() {
$hook = add_options_page(
__( 'WP-LDP Settings', 'wpldp' ),
__( 'WP-LDP Settings', 'wpldp' ),
'edit_posts',
'wpldp',
array( $this, 'wpldp_options_page' )
);
add_action( 'load-' . $hook, array( $this, 'initialize_container' ) );
add_action( 'admin_notices', array( $this, 'wpldp_validation_notice' ) );
}
/**
* Populates the plugin option page.
*
* @return void
*/
function wpldp_options_page() {
echo '<div class="wrap">';
echo '<h2>' . __( 'WP-LDP Settings', 'wpldp' ) . '</h2>';
echo '<form method="post" action="options.php">';
settings_fields( 'ldp_settings' );
do_settings_sections( 'wpldp' );
submit_button();
echo '</form>';
echo '</div>';
}
/**
* Adds the custom JSON-LD context field.
*
* @return void
*/
function ldp_context_field() {
echo "<input type='text' size='150' name='ldp_context' value='" . get_option( 'ldp_context', 'http://lov.okfn.org/dataset/lov/context' ) . "' />";
}
/**
* Initis the checkbox for container init.
*
* @return void
*/
function ldp_container_init_field() {
$option_value = get_option( 'ldp_container_init', false );
$option_value = ! empty( $option_value ) ? 1 : 0;
echo '<input type=\'checkbox\' name=\'ldp_container_init\' value=\'1\' ' . checked( $option_value, 1, false ) . ' />';
}
/**
* Adds every needed fiels to the options page.
*
* @return void
*/
function backend_hooking() {
add_settings_section(
'ldp_settings',
__( 'WP-LDP Settings', 'wpldp' ),
function() {
echo __( 'The generals settings of the WP-LDP plugin.', 'wpldp' );
},
'wpldp'
);
add_settings_field(
'ldp_context',
__( 'WP-LDP Context', 'wpldp' ),
array( $this, 'ldp_context_field' ),
'wpldp',
'ldp_settings'
);
add_settings_field(
'ldp_container_init',
__( 'Do you want to initialize PAIR containers ?', 'wpldp' ),
array( $this, 'ldp_container_init_field' ),
'wpldp',
'ldp_settings'
);
register_setting( 'ldp_settings', 'ldp_context' );
register_setting( 'ldp_settings', 'ldp_container_init' );
}
}
$wpldp_settings = new Settings();
} else {
exit( 'Class Settings already exists' );
}