-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivs-meeloop-portaal.php
418 lines (301 loc) · 12.6 KB
/
ivs-meeloop-portaal.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
// Check if WordPress core has loaded
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: Open day portal
* Plugin URI: #
* Description: This WordPress plugin adds a 'meeloop portaal' as functionality, where students can see their date, planning, tasks and informational resources concerning the open day
* Version: 0.1
* Requires at least: 4.0
* Author: Yari Morcus
*/
// Define the plugin name
define( 'IVS_MEELOOP_PORTAAL', __FILE__ );
// Include the general definition file
require_once( plugin_dir_path( __FILE__ ) . 'includes/defs.php' );
/**
* Include class related to pages
* @author Yari Morcus
* @version 0.1
*/
require_once( IVS_MEELOOP_PORTAAL_PLUGIN_INCLUDES_VIEWS_DIR . '/PageView.php' );
// Tell WordPress what to do when plugin has been activated
register_activation_hook( __FILE__, array( 'IVSMeeloopPortaal', 'on_activation' ) );
// Tell WordPress what to do when plugin has been deactivated
register_deactivation_hook( __FILE__, array( 'IVSMeeloopPortaal', 'on_deactivation' ) );
/**
* Class to setup the plugin
* @author Yari Morcus
* @version 0.1
*
*/
class IVSMeeloopPortaal {
/**
* __construct
*
* Execute function when new instance has been made of class IVSMeeloopPortaal
*/
public function __construct() {
// Fire a hook before the class is setup
do_action( 'ivs_meeloop_portaal_pre_init' );
// Load the plugin
add_action( 'init', array( $this, 'init' ), 1 );
}
// Show custom post label in pages list for pages that have been created by the plugin
public function showPostState( $states, $post ) {
// Register all the templates
$template_list = array(
IVS_MEELOOP_PORTAAL_PLUGIN_TEMPLATES_FOLDER_FRONTEND . '/template-mijn-rooster.php',
IVS_MEELOOP_PORTAAL_PLUGIN_TEMPLATES_FOLDER_FRONTEND . '/template-dashboard.php',
IVS_MEELOOP_PORTAAL_PLUGIN_TEMPLATES_FOLDER_FRONTEND . '/template-indienen-opdrachten.php',
IVS_MEELOOP_PORTAAL_PLUGIN_TEMPLATES_FOLDER_FRONTEND . '/template-inzien-opdrachten.php'
);
foreach( $template_list as $template ) {
// If page in list (wp-admin) contains one of these templates, assign the label to it
if ( $template == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
$states[] = __( 'IVS MP' );
// Markup for better notice
echo "
<script type=\"text/javascript\">
setTimeout(function() {
document.querySelector(\"#post-$post->ID .post-state\").style=\"margin-right: 2rem; padding: 0 2rem; color: #000; font-weight: bold; background: #fdd02a\";
var pageName = document.querySelector('#post-$post->ID .row-title');
var postState = document.querySelector('#post-$post->ID .post-state');
var parentNode = pageName.parentNode;
postState.parentNode.insertBefore(pageName, postState)
pageName.parentNode.insertBefore(postState, pageName)
postState.parentNode.removeChild(postState.previousSibling)
}, 200)
</script>'
";
}
}
return $states;
}
// Show info message for more information about the labels (see above function)
public function infoMessage() {
$current_screen = get_current_screen();
if ( $current_screen->id != 'edit-page' ) return;
?>
<div class="notice notice-info">
<p><strong>Ter informatie:</strong> pagina's waarvoor het label <span style="padding: 0 2rem; color: #000; font-weight: bold; background: #fdd02a;">IVS MP</span> staat, zijn een onderdeel van het meeloop portaal. Deze <strong>niet</strong> wijzigen.</p>
</div>
<?php
}
function my_admin_queue( $hook_suffix ) {
if ( $hook_suffix == 'meeloop-portaal_page_roosters-meeloopdag' ) {
wp_enqueue_script( 'jquery-accordion-functionality', 'https://code.jquery.com/jquery-3.6.0.js' );
wp_enqueue_script( 'jquery-ui', 'https://code.jquery.com/ui/1.13.0/jquery-ui.js', array( 'jquery-accordion-functionality' ) );
wp_enqueue_style( 'jquery-ui', 'http://code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css' );
?>
<script type="text/javascript">
// Only execute jQuery when HTML body is fully loaded.
// Will throw error otherwise
document.addEventListener('DOMContentLoaded', function() {
( function( $ ) {
$( '.collapse-header' ).click(function() {
$header = $(this);
// Get the next element
$content = $header.next();
// If collapse is already open, close it and add the bottom border radius
// Else, open the collapse, and remove bottom border radius
if ( $header.hasClass( 'no-bottom-border-radius' ) ) {
// Add bottom border radius back to how it was
$header.removeClass( 'no-bottom-border-radius' );
// Hide the content
$content.slideToggle( '500' );
} else {
// Show the content when user clicks on header, or close it if user has already opened it
$content.slideToggle( '500', function() {
$header.addClass('no-bottom-border-radius');
} );
}
});
/**
1. Get current .collapse-header
2. check if next element contains style display block
3. If yes, remove .no-border-bottom-radius from .collapse-header
*/
})( jQuery );
});
</script>
<?php
}
}
/**
* init
*
* Loads the plugin into WordPress
*/
public function init() {
// Load Admin only components
if ( is_admin() ) {
// Load all Admin specific includes
$this->requireAdmin();
// Setup admin page
$this->createAdmin();
}
// Instantiate the class
$page_view = new PageView();
add_filter( 'display_post_states', array( $this, 'showPostState'), 10, 2 );
add_action( 'admin_notices', array( $this, 'infoMessage' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'my_admin_queue' ) );
// Load the frontend css
$page_view->loadFrontendCSS();
}
/**
* requireAdmin
*
* Loads all Admin related files into scope
*/
public function requireAdmin() {
// Admin controller file
require_once IVS_MEELOOP_PORTAAL_PLUGIN_ADMIN_DIR . '/IVSMeeloopPortaal_AdminController.php';
}
/**
* createAdmin
*
* Admin controller functionality
*/
public function createAdmin() {
// Execute the prepare function
IVSMeeloopPortaal_AdminController::prepare();
}
/**
* on_activation
*
* if { ! current_user_can( 'activate_plugins' ) }, if user cannot activate plugin's, abort function
* (prevent user from activating plugin)
*/
public static function on_activation() {
add_action( 'admin_notices', 'tester' );
if ( ! current_user_can( 'activate_plugins' ) ) return;
// Include the CreateDatabaseTables class
require_once( IVS_MEELOOP_PORTAAL_PLUGIN_INCLUDES_MODEL_DIR . '/IVS_DatabaseSetup.php');
// Insert the tables
IVS_DatabaseSetup::createDBTables();
// Insert the data
IVS_DatabaseSetup::insertDBData();
// Add the plugin capabilities
IVSMeeloopPortaal::add_plugin_caps();
$page_view = new PageView();
// Create page templates
$page_view->createTemplates();
// Create the corresponding pages for the dashboard
$page_view->createPages();
}
/**
* on_deactivation
*
* if { ! current_user_can( 'activate_plugins' ) }, if user cannot activate plugin's, abort function
* (prevent user from deactivating plugin)
*/
public static function on_deactivation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
// Include the CreateDatabaseTables class
require_once( IVS_MEELOOP_PORTAAL_PLUGIN_INCLUDES_MODEL_DIR . '/IVS_DatabaseSetup.php');
// Remove the database tables
IVS_DatabaseSetup::removeDBTables();
// Remove the plugin specific capabilities
IVSMeeloopPortaal::remove_plugin_caps();
$page_view = new PageView();
// Remove templates directory
$page_view->removeDirectory();
// Remove WordPress pages
$page_view->removePages();
}
/**
* get_plugin_roles_and_caps
*
* Define the array with plugin specific capabilities per role
*/
public static function get_plugin_roles_and_caps() {
// Define the desired roles for this plugin
return array(
/* Is always available - Should be on the first line */
array(
'administrator',
'Admin',
array(
'ivs_mp_read',
'ivs_mp_create',
'ivs_mp_update',
'ivs_mp_delete'
) ),
array(
'docent',
'Docent',
array(
'ivs_mp_read',
'ivs_mp_create',
'ivs_mp_update',
'ivs_mp_delete'
) ),
array(
'meeloper',
'Meelooper',
array(
'ivs_mp_read'
) )
);
}
/**
* add_plugin_caps
*
* Add plugin specific capabilities
* Check first for the specific roles
* If they not exist, add specific roles
* Add plugin specific caps per role
*/
public static function add_plugin_caps() {
// Include the roles and capabilities definition file
require_once plugin_dir_path( __FILE__ ) . 'includes/roles_and_caps_defs.php';
$role_array = IVSMeeloopPortaal::get_plugin_roles_and_caps();
// Check for the roles
foreach( $role_array as $key => $role_name ) {
// Check specific role
if ( !( $GLOBALS['wp_roles']->is_role( $role_name[IVS_MP_ROLE_NAME] ) ) ) {
$role = add_role(
$role_name[IVS_MP_ROLE_NAME],
$role_name[IVS_MP_ROLE_ALIAS],
array( 'read' => true, 'level_0' => true )
);
}
}
// Add the capabilities per role
foreach( $role_array as $key => $role_name ) {
// Create the capabilities for this role
foreach( $role_name[IVS_MP_ROLE_CAP_ARRAY] as $cap_key => $cap_name ) {
// Gets the author role
$role = get_role( $role_name[IVS_MP_ROLE_NAME] );
// This only works, because it accesses the class instance.
// Would allow the author to edit others' posts for current theme only
$role->add_cap( $cap_name );
}
}
}
/**
* remove_plugin_caps
*
* Remove all the specific capabilities for this plugin
*/
public static function remove_plugin_caps() {
// Include the roles and capabilities definition file
require_once plugin_dir_path( __FILE__ ) . 'includes/roles_and_caps_defs.php';
// Get the plugin specific capabilities per role
$role_array = IVSMeeloopPortaal::get_plugin_roles_and_caps();
// Add the capabilities per role
foreach( $role_array as $key => $role_name ) {
// Create the capabilities for this role
foreach( $role_name[IVS_MP_ROLE_CAP_ARRAY] as $cap_key => $cap_name ) {
// Get the specific role
$role = get_role( $role_name[IVS_MP_ROLE_NAME] );
// This only works, because it accesses the class instance.
$role->remove_cap( $cap_name );
}
}
}
}
// Instantiate the class
$ivs_meeloop_portaal = new IVSMeeloopPortaal();
?>