-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathndla-h5p-caretaker.php
executable file
·162 lines (137 loc) · 4.16 KB
/
ndla-h5p-caretaker.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
<?php
/**
* Plugin Name: NDLA's H5P Caretaker
* Description: A plugin to allow checking H5P content for issues.
* Text Domain: ndla-h5p-caretaker
* Domain Path: /languages
* Version: 1.0.10
* Author: NDLA, Oliver Tacke
* License: MIT
*
* @package ndla-h5p-caretaker
*/
namespace NDLAH5PCARETAKER;
// as suggested by the WordPress community.
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'class-localeutils.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'class-options.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'class-main.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'render-index.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fetch-analysis.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'clean-up.php';
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
if ( ! defined( 'NDLAH5PCARETAKER_VERSION' ) ) {
define( 'NDLAH5PCARETAKER_VERSION', '1.0.10' );
}
/**
* Main plugin class.
*
* @return object NDLAH5PCARETAKER
*/
function ndla_h5p_caretaker() {
register_activation_hook( __FILE__, 'NDLAH5PCARETAKER\on_activation' );
register_deactivation_hook( __FILE__, 'NDLAH5PCARETAKER\on_deactivation' );
register_uninstall_hook( __FILE__, 'NDLAH5PCARETAKER\on_uninstall' );
return new Main();
}
ndla_h5p_caretaker();
/**
* Handle plugin activation.
*/
function on_activation() {
Options::set_defaults();
add_capabilities();
}
/**
* Handle plugin deactivation.
*/
function on_deactivation() {
flush_rewrite_rules();
}
/**
* Handle plugin uninstallation.
*/
function on_uninstall() {
Options::delete_options();
flush_rewrite_rules();
remove_capabilities();
remove_directories();
}
/**
* Add default capabilities.
*/
function add_capabilities() {
// Add capabilities.
global $wp_roles;
$all_roles = $wp_roles->roles;
foreach ( $all_roles as $role_name => $role_info ) {
$role = get_role( $role_name );
// Use the capability to edit H5P contents as a base.
map_capability( $role, $role_info, 'edit_h5p_contents', 'use-h5p-caretaker' );
}
}
/**
* Remove default capabilities.
*/
function remove_capabilities() {
// Remove capabilities.
global $wp_roles;
$all_roles = $wp_roles->roles;
foreach ( $all_roles as $role_name => $role_info ) {
$role = get_role( $role_name );
if ( isset( $role_info['capabilities']['use-h5p-caretaker'] ) ) {
$role->remove_cap( 'use-h5p-caretaker' );
}
}
}
/**
* Make sure that a role has or hasn't the provided capability depending on existing roles.
*
* @param stdClass $role Role object.
* @param array $role_info Role information.
* @param string|array $existing_cap Existing capability.
* @param string $new_cap New capability.
*/
function map_capability( $role, $role_info, $existing_cap, $new_cap ) {
if ( isset( $role_info['capabilities'][ $new_cap ] ) ) {
// Already has new cap.
if ( ! has_capability( $role_info['capabilities'], $existing_cap ) ) {
// But shouldn't have it!
$role->remove_cap( $new_cap );
}
} elseif ( has_capability( $role_info['capabilities'], $existing_cap ) ) {
// Should have new cap.
$role->add_cap( $new_cap );
}
}
/**
* Check that role has the needed capabilities.
*
* @param array $role_capabilities Role capabilities.
* @param string|array $capability Capabilities to check for.
*
* @return bool True, if role has capability, else false.
*/
function has_capability( $role_capabilities, $capability ) {
$capabilities = (array) $capability;
foreach ( $capabilities as $cap ) {
if ( ! isset( $role_capabilities[ $cap ] ) ) {
return false;
}
}
return true;
}
/**
* Remove tmp and cache directories.
*/
function remove_directories() {
global $wp_filesystem;
$upload_dir = wp_upload_dir( null, true )['basedir'] . DIRECTORY_SEPARATOR . 'h5p-caretaker';
$wp_filesystem->delete( $upload_dir, true );
}