-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathclass-h5p-plugin.php
1721 lines (1517 loc) · 52.2 KB
/
class-h5p-plugin.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* H5P Plugin.
*
* @package H5P
* @author Joubel <[email protected]>
* @license MIT
* @link http://joubel.com
* @copyright 2014 Joubel
*/
/**
* Plugin class.
*
* @package H5P_Plugin
* @author Joubel <[email protected]>
*/
class H5P_Plugin {
/**
* Plugin version, used for cache-busting of style and script file references.
* Keeping track of the DB version.
*
* @since 1.0.0
* @var string
*/
const VERSION = '1.16.0';
/**
* The Unique identifier for this plugin.
*
* @since 1.0.0
* @var string
*/
protected $plugin_slug = 'h5p';
/**
* Instance of this class.
*
* @since 1.0.0
* @var \H5P_Plugin
*/
protected static $instance = null;
/**
* Instance of H5P WordPress Framework Interface.
*
* @since 1.0.0
* @var \H5PWordPress[]
*/
protected static $interface = array();
/**
* Instance of H5P Core.
*
* @since 1.0.0
* @var \H5PCore[]
*/
protected static $core = array();
/**
* JavaScript settings to add for H5Ps.
*
* @since 1.0.0
* @var array
*/
protected static $settings = null;
/**
* Default settings for HTTP Feature Policy.
*
* @var string
*/
protected static $h5p_http_feature_policy = array(
'accelerometer' => '*',
'autoplay' => '*',
'camera' => '*',
'clipboard-write' => '*',
'fullscreen' => '*',
'geolocation' => '*',
'gyroscope' => '*',
'magnetometer' => '*',
'microphone' => '*'
);
/**
* Initialize the plugin by setting localization and loading public scripts
* and styles.
*
* @since 1.0.0
*/
private function __construct() {
global $wp_version;
// Load plugin text domain
add_action('init', array($this, 'load_plugin_textdomain'));
// Load public-facing style sheet and JavaScript.
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles_and_scripts'));
// Add support for h5p shortcodes.
add_shortcode('h5p', array($this, 'shortcode'));
// Adds JavaScript settings to the bottom of the page.
add_action('wp_footer', array($this, 'add_settings'));
// Clean up tmp editor files
add_action('h5p_daily_cleanup', array($this, 'remove_old_tmp_files'));
// Check for library updates
add_action('h5p_daily_cleanup', array($this, 'get_library_updates'));
// Remove old log messages
add_action('h5p_daily_cleanup', array($this, 'remove_old_log_events'));
// Always check if the plugin has been updated to a newer version
add_action('init', array('H5P_Plugin', 'check_for_updates'), 1);
// Add menu options to admin bar.
add_action('admin_bar_menu', array($this, 'admin_bar'), 999);
// REST API
add_action('rest_api_init', array($this, 'rest_api_init'));
// Removes all H5P data for this blog
if (version_compare($wp_version, '5.1', '>=')) {
add_action('wp_delete_site', array($this, 'delete_site'));
}
else {
// Deprecated since 5.1
add_action('delete_blog', array($this, 'delete_blog'));
}
}
/**
* Return the plugin slug.
*
* @since 1.0.0
* @return string Plugin slug variable.
*/
public function get_plugin_slug() {
return $this->plugin_slug;
}
/**
* Return an instance of this class.
*
* @since 1.0.0
* @return \H5P_Plugin A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
* @global \wpdb $wpdb
* @param boolean $network_wide
*/
public static function activate($network_wide) {
// Check to see if the plugin has been updated to a newer version
self::check_for_updates();
// Check for library updates
$plugin = self::get_instance();
$plugin->get_library_updates();
// Always check setup requirements when activating
update_option('h5p_check_h5p_requirements', TRUE);
// Cleaning rutine
wp_schedule_event(time() + (3600 * 24), 'daily', 'h5p_daily_cleanup');
}
/**
* Drop the given column from the given table.
*
* @since 1.11.0
* @global \wpdb $wpdb
* @param string $table
* @param string $column
*/
public static function drop_column($table, $column) {
global $wpdb;
$wpdb->get_results("SHOW COLUMNS FROM {$table} LIKE '{$column}'");
if (!empty($wpdb->num_rows)) {
$wpdb->query("ALTER TABLE {$table} DROP COLUMN {$column}");
}
}
/**
* Makes sure the database is up to date.
*
* @since 1.1.0
* @global \wpdb $wpdb
*/
public static function update_database() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Get charset to use
$charset = self::determine_charset();
// Keep track of h5p content entities
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_contents (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT 0,
updated_at TIMESTAMP NOT NULL DEFAULT 0,
user_id INT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
library_id INT UNSIGNED NOT NULL,
parameters LONGTEXT NOT NULL,
filtered LONGTEXT NOT NULL,
slug VARCHAR(127) NOT NULL,
embed_type VARCHAR(127) NOT NULL,
disable INT UNSIGNED NOT NULL DEFAULT 0,
content_type VARCHAR(127) NULL,
authors LONGTEXT NULL,
source VARCHAR(2083) NULL,
year_from INT UNSIGNED NULL,
year_to INT UNSIGNED NULL,
license VARCHAR(32) NULL,
license_version VARCHAR(10) NULL,
license_extras LONGTEXT NULL,
author_comments LONGTEXT NULL,
changes LONGTEXT NULL,
default_language VARCHAR(32) NULL,
a11y_title VARCHAR(255) NULL,
PRIMARY KEY (id)
) {$charset};");
// Keep track of content dependencies
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_contents_libraries (
content_id INT UNSIGNED NOT NULL,
library_id INT UNSIGNED NOT NULL,
dependency_type VARCHAR(31) NOT NULL,
weight SMALLINT UNSIGNED NOT NULL DEFAULT 0,
drop_css TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (content_id,library_id,dependency_type)
) {$charset};");
// Keep track of data/state when users use content (contents >-< users)
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_contents_user_data (
content_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
sub_content_id INT UNSIGNED NOT NULL,
data_id VARCHAR(127) NOT NULL,
data LONGTEXT NOT NULL,
preload TINYINT UNSIGNED NOT NULL DEFAULT 0,
invalidate TINYINT UNSIGNED NOT NULL DEFAULT 0,
updated_at TIMESTAMP NOT NULL DEFAULT 0,
PRIMARY KEY (content_id,user_id,sub_content_id,data_id)
) {$charset};");
// Create a relation between tags and content
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_contents_tags (
content_id INT UNSIGNED NOT NULL,
tag_id INT UNSIGNED NOT NULL,
PRIMARY KEY (content_id,tag_id)
) {$charset};");
// Keep track of tags
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(31) NOT NULL,
PRIMARY KEY (id)
) {$charset};");
// Keep track of results (contents >-< users)
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_results (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
content_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
score INT UNSIGNED NOT NULL,
max_score INT UNSIGNED NOT NULL,
opened INT UNSIGNED NOT NULL,
finished INT UNSIGNED NOT NULL,
time INT UNSIGNED NOT NULL,
PRIMARY KEY (id),
KEY content_user (content_id,user_id)
) {$charset};");
// Keep track of h5p libraries
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_libraries (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
name VARCHAR(127) NOT NULL,
title VARCHAR(255) NOT NULL,
major_version INT UNSIGNED NOT NULL,
minor_version INT UNSIGNED NOT NULL,
patch_version INT UNSIGNED NOT NULL,
runnable INT UNSIGNED NOT NULL,
restricted INT UNSIGNED NOT NULL DEFAULT 0,
fullscreen INT UNSIGNED NOT NULL,
embed_types VARCHAR(255) NOT NULL,
preloaded_js TEXT NULL,
preloaded_css TEXT NULL,
drop_library_css TEXT NULL,
semantics TEXT NOT NULL,
tutorial_url VARCHAR(1023) NOT NULL,
has_icon INT UNSIGNED NOT NULL DEFAULT 0,
metadata_settings TEXT NULL,
add_to TEXT DEFAULT NULL,
PRIMARY KEY (id),
KEY name_version (name,major_version,minor_version,patch_version),
KEY runnable (runnable)
) {$charset};");
// Keep track of h5p libraries content type cache
dbDelta("CREATE TABLE {$wpdb->base_prefix}h5p_libraries_hub_cache (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
machine_name VARCHAR(127) NOT NULL,
major_version INT UNSIGNED NOT NULL,
minor_version INT UNSIGNED NOT NULL,
patch_version INT UNSIGNED NOT NULL,
h5p_major_version INT UNSIGNED,
h5p_minor_version INT UNSIGNED,
title VARCHAR(255) NOT NULL,
summary TEXT NOT NULL,
description TEXT NOT NULL,
icon VARCHAR(511) NOT NULL,
created_at INT UNSIGNED NOT NULL,
updated_at INT UNSIGNED NOT NULL,
is_recommended INT UNSIGNED NOT NULL,
popularity INT UNSIGNED NOT NULL,
screenshots TEXT,
license TEXT,
example VARCHAR(511) NOT NULL,
tutorial VARCHAR(511),
keywords TEXT,
categories TEXT,
owner VARCHAR(511),
PRIMARY KEY (id),
KEY name_version (machine_name,major_version,minor_version,patch_version)
) {$charset};");
// Keep track of h5p library dependencies
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_libraries_libraries (
library_id INT UNSIGNED NOT NULL,
required_library_id INT UNSIGNED NOT NULL,
dependency_type VARCHAR(31) NOT NULL,
PRIMARY KEY (library_id,required_library_id)
) {$charset};");
// Keep track of h5p library translations
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_libraries_languages (
library_id INT UNSIGNED NOT NULL,
language_code VARCHAR(31) NOT NULL,
translation TEXT NOT NULL,
PRIMARY KEY (library_id,language_code)
) {$charset};");
// Keep track of logged h5p events
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_events (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
created_at INT UNSIGNED NOT NULL,
type VARCHAR(63) NOT NULL,
sub_type VARCHAR(63) NOT NULL,
content_id INT UNSIGNED NOT NULL,
content_title VARCHAR(255) NOT NULL,
library_name VARCHAR(127) NOT NULL,
library_version VARCHAR(31) NOT NULL,
PRIMARY KEY (id)
) {$charset};");
// A set of global counters to keep track of H5P usage
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_counters (
type VARCHAR(63) NOT NULL,
library_name VARCHAR(127) NOT NULL,
library_version VARCHAR(31) NOT NULL,
num INT UNSIGNED NOT NULL,
PRIMARY KEY (type,library_name,library_version)
) {$charset};");
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_libraries_cachedassets (
library_id INT UNSIGNED NOT NULL,
hash VARCHAR(64) NOT NULL,
PRIMARY KEY (library_id,hash)
) {$charset};");
dbDelta("CREATE TABLE {$wpdb->prefix}h5p_tmpfiles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
path VARCHAR(255) NOT NULL,
created_at INT UNSIGNED NOT NULL,
PRIMARY KEY (id),
KEY created_at (created_at)
) {$charset};");
// Add default setting options
add_option('h5p_frame', TRUE);
add_option('h5p_export', TRUE);
add_option('h5p_embed', TRUE);
add_option('h5p_copyright', TRUE);
add_option('h5p_icon', TRUE);
add_option('h5p_track_user', TRUE);
add_option('h5p_save_content_state', FALSE);
add_option('h5p_save_content_frequency', 30);
add_option('h5p_site_key', get_option('h5p_h5p_site_uuid', FALSE));
add_option('h5p_show_toggle_view_others_h5p_contents', 0);
add_option('h5p_content_type_cache_updated_at', 0);
add_option('h5p_check_h5p_requirements', FALSE);
add_option('h5p_hub_is_enabled', FALSE);
add_option('h5p_send_usage_statistics', FALSE);
add_option('h5p_has_request_user_consent', FALSE);
}
/**
* Determine charset to use for database tables
*
* @since 1.2.0
* @global \wpdb $wpdb
*/
public static function determine_charset() {
global $wpdb;
$charset = '';
if (!empty($wpdb->charset)) {
$charset = "DEFAULT CHARACTER SET {$wpdb->charset}";
if (!empty($wpdb->collate)) {
$charset .= " COLLATE {$wpdb->collate}";
}
}
return $charset;
}
/**
* @since 1.0.0
*/
public static function deactivate() {
// Remove cleaning rutine
wp_clear_scheduled_hook('h5p_daily_cleanup');
}
/**
* Check if the plugin has been updated and if we need to run some upgrade
* scripts, change the database or something else.
*
* @since 1.2.0
*/
public static function check_for_updates() {
global $wpdb;
$current_version = get_option('h5p_version');
if ($current_version === self::VERSION) {
return; // Same version as before
}
// We have a new version!
if (!$current_version) {
// Never installed before
$current_version = '0.0.0';
}
// Split version number
$v = self::split_version($current_version);
$between_1710_1713 = ($v->major === 1 && $v->minor === 7 && $v->patch >= 10 && $v->patch <= 13); // Target 1.7.10, 1.7.11, 1.7.12, 1.7.13
if ($between_1710_1713) {
// Fix tmpfiles table manually :-)
$wpdb->query("ALTER TABLE {$wpdb->prefix}h5p_tmpfiles ADD COLUMN id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST, DROP PRIMARY KEY, ADD PRIMARY KEY(id)");
}
// Check and update database
self::update_database();
$pre_120 = ($v->major < 1 || ($v->major === 1 && $v->minor < 2)); // < 1.2.0
$pre_180 = ($v->major < 1 || ($v->major === 1 && $v->minor < 8)); // < 1.8.0
$pre_1102 = ($v->major < 1 || ($v->major === 1 && $v->minor < 10) ||
($v->major === 1 && $v->minor === 10 && $v->patch < 2)); // < 1.10.2
$pre_1110 = ($v->major < 1 || ($v->major === 1 && $v->minor < 11)); // < 1.11.0
$pre_1113 = ($v->major < 1 || ($v->major === 1 && $v->minor < 11) ||
($v->major === 1 && $v->minor === 11 && $v->patch < 3)); // < 1.11.3
$pre_1150 = ($v->major < 1 || ($v->major === 1 && $v->minor < 15)); // < 1.15.0
// Run version specific updates
if ($pre_120) {
// Re-assign all permissions
self::upgrade_120();
}
else {
// Do not run if upgrade_120 runs (since that remaps all the permissions)
if ($pre_180) {
// Does only add new permissions
self::upgrade_180();
}
if ($pre_1150) {
// Does only add new permissions
self::upgrade_1150();
}
}
if ($pre_180) {
// Force requirements check when hub is introduced.
update_option('h5p_check_h5p_requirements', TRUE);
}
if ($pre_1102 && $current_version !== '0.0.0') {
update_option('h5p_has_request_user_consent', TRUE);
}
if ($pre_1110) {
// Remove unused columns
self::drop_column("{$wpdb->prefix}h5p_contents", 'author');
self::drop_column("{$wpdb->prefix}h5p_contents", 'keywords');
self::drop_column("{$wpdb->prefix}h5p_contents", 'description');
}
if ($pre_1113 && !$pre_1110) { // 1.11.0, 1.11.1 or 1.11.2
// There are no tmpfiles in content folders, cleanup
$wpdb->query($wpdb->prepare(
"DELETE FROM {$wpdb->prefix}h5p_tmpfiles
WHERE path LIKE '%s'",
"%/h5p/content/%"));
}
// Keep track of which version of the plugin we have.
if ($current_version === '0.0.0') {
add_option('h5p_version', self::VERSION);
}
else {
update_option('h5p_version', self::VERSION);
}
}
/**
* Parse version string into smaller components.
*
* @since 1.7.9
* @param string $version
* @return stdClass|boolean False on failure to parse
*/
public static function split_version($version) {
$version_parts = explode('.', $version);
if (count($version_parts) !== 3) {
return FALSE;
}
return (object) array(
'major' => (int) $version_parts[0],
'minor' => (int) $version_parts[1],
'patch' => (int) $version_parts[2]
);
}
/**
* Migration procedures when upgrading to >= 1.2.0.
*
* @since 1.2.0
* @global \wpdb $wpdb
*/
public static function upgrade_120() {
global $wpdb;
// Add caps again, has not worked for everyone in 1.1.0
self::assign_capabilities();
// Clean up duplicate indexes (due to bug in dbDelta)
self::remove_duplicate_indexes('h5p_contents', 'id');
self::remove_duplicate_indexes('h5p_contents_libraries', 'content_id');
self::remove_duplicate_indexes('h5p_results', 'id');
self::remove_duplicate_indexes('h5p_libraries', 'id');
self::remove_duplicate_indexes('h5p_libraries_libraries', 'library_id');
self::remove_duplicate_indexes('h5p_libraries_languages', 'library_id');
// Make sure we use the charset defined in wp-config, and not DB default.
$charset = self::determine_charset();
if (!empty($charset)) {
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_contents` {$charset}");
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_contents_libraries` {$charset}");
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_results` {$charset}");
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_libraries` {$charset}");
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_libraries_libraries` {$charset}");
$wpdb->query("ALTER TABLE `{$wpdb->prefix}h5p_libraries_languages` {$charset}");
}
}
/**
* Add new permissions introduced with hub in 1.8.0.
*
* @since 1.8.0
* @global \WP_Roles $wp_roles
*/
public static function upgrade_180() {
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$all_roles = $wp_roles->roles;
foreach ($all_roles as $role_name => $role_info) {
$role = get_role($role_name);
self::map_capability($role, $role_info, 'edit_others_pages', 'install_recommended_h5p_libraries');
}
}
/**
* Add new permission for viewing others content
*
* @since 1.15.0
* @global \WP_Roles $wp_roles
*/
public static function upgrade_1150() {
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$all_roles = $wp_roles->roles;
foreach ($all_roles as $role_name => $role_info) {
$role = get_role($role_name);
self::map_capability($role, $role_info, 'read', 'view_h5p_contents');
self::map_capability($role, $role_info, 'read', 'view_others_h5p_contents');
}
}
/**
* Remove duplicate keys that might have been created by a bug in dbDelta.
*
* @since 1.2.0
* @global \wpdb $wpdb
* @param string $table Table name without wp prefix
* @param string $index Key name
*/
public static function remove_duplicate_indexes($table, $index) {
global $wpdb;
$wpdb->hide_errors();
if ($wpdb->query("SHOW INDEX FROM `{$wpdb->prefix}{$table}` WHERE Key_name = '{$index}'")) {
$wpdb->query("ALTER TABLE `{$wpdb->prefix}{$table}` DROP INDEX `{$index}`");
}
for ($i = 0; $i < 5; $i++) {
if ($wpdb->query("SHOW INDEX FROM `{$wpdb->prefix}{$table}` WHERE Key_name = '{$index}_$i'")) {
$wpdb->query("ALTER TABLE `{$wpdb->prefix}{$table}` DROP INDEX `{$index}_$i`");
}
}
$wpdb->show_errors();
}
/**
* Assign H5P capabilities to roles. "Copy" default WP caps on roles.
*
* @since 1.2.0
*/
public static function assign_capabilities() {
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$all_roles = $wp_roles->roles;
foreach ($all_roles as $role_name => $role_info) {
$role = get_role($role_name);
if (is_multisite()) {
// Multisite, only super admin should be able to disable security checks
self::map_capability($role, $role_info, array('install_plugins', 'manage_network_plugins'), 'disable_h5p_security');
}
else {
// Not multisite, regular admin can disable security checks
self::map_capability($role, $role_info, 'install_plugins', 'disable_h5p_security');
}
self::map_capability($role, $role_info, 'manage_options', 'manage_h5p_libraries');
self::map_capability($role, $role_info, 'edit_others_pages', 'install_recommended_h5p_libraries');
self::map_capability($role, $role_info, 'edit_others_pages', 'edit_others_h5p_contents');
self::map_capability($role, $role_info, 'edit_posts', 'edit_h5p_contents');
self::map_capability($role, $role_info, 'read', 'view_others_h5p_contents');
self::map_capability($role, $role_info, 'read', 'view_h5p_contents');
self::map_capability($role, $role_info, 'read', 'view_h5p_results');
}
// Keep track on how the capabilities are assigned (multisite caps or not)
update_option('h5p_multisite_capabilities', is_multisite() ? 1 : 0);
}
/**
* Make sure that the givn role has or hasn't the provided capability
* depending on existing roles.
*
* @since 1.7.2
* @param stdClass $role
* @param array $role_info
* @param string|array $existing_cap
* @param string $new_cap
*/
private static function map_capability($role, $role_info, $existing_cap, $new_cap) {
if (isset($role_info['capabilities'][$new_cap])) {
// Already has new cap…
if (!self::has_capability($role_info, $existing_cap)) {
// But shouldn't have it!
$role->remove_cap($new_cap);
}
}
else {
// Doesn't have new cap…
if (self::has_capability($role_info, $existing_cap)) {
// But should have it!
$role->add_cap($new_cap);
}
}
}
/**
* Check that the given role has the needed capability/-ies.
*
* @since 1.7.2
* @param array $role_info
* @param string|array $capability
* @return bool
*/
private static function has_capability($role_info, $capability) {
if (is_array($capability)) {
foreach ($capability as $cap) {
if (!isset($role_info['capabilities'][$cap])) {
return FALSE;
}
}
}
else if (!isset($role_info['capabilities'][$capability])) {
return FALSE;
}
return TRUE;
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters('plugin_locale', get_locale(), $domain);
load_textdomain($domain, trailingslashit(WP_LANG_DIR) . $domain . '/' . $domain . '-' . $locale . '.mo');
load_plugin_textdomain($domain, FALSE, basename(plugin_dir_path(dirname( __FILE__ ))) . '/languages');
}
/**
* Register and enqueue public-facing style sheets and JavaScript files.
*
* @since 1.0.0
*/
public function enqueue_styles_and_scripts() {
wp_enqueue_style($this->plugin_slug . '-plugin-styles', plugins_url('h5p/h5p-php-library/styles/h5p.css'), array(), self::VERSION);
}
/**
* Add menu options to the WordPress admin bar
*
* @since 1.2.2
*/
public function admin_bar($wp_admin_bar) {
$wp_admin_bar->add_menu(array(
'parent' => 'new-content',
'id' => 'new-h5p-content',
'title' => __('H5P Content', $this->plugin_slug),
'href' => admin_url('admin.php?page=h5p_new')
));
}
/**
* Get the path to the H5P files folder.
*
* @since 1.0.0
* @return string
*/
public function get_h5p_path() {
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'] . '/h5p';
}
/**
* Get the URL for the H5P files folder.
*
* @since 1.0.0
* @param $absolute Optional.
* @return string
*/
public function get_h5p_url($absolute = FALSE) {
static $url;
if (!$url) {
$url = array();
}
$id = get_current_blog_id();
if (empty($url[$id])) {
$upload_dir = wp_upload_dir();
// Absolute urls are used to enqueue assets.
$url[$id] = array('abs' => $upload_dir['baseurl'] . '/h5p');
// Relative URLs are used to support both http and https in iframes.
$url[$id]['rel'] = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $url[$id]['abs']);
// Check for HTTPS
if (is_ssl() && substr($url[$id]['abs'], 0, 5) !== 'https') {
// Update protocol
$url[$id]['abs'] = 'https' . substr($url[$id]['abs'], 4);
}
}
return $absolute ? $url[$id]['abs'] : $url[$id]['rel'];
}
/**
* Get H5P language code from WordPress.
*
* @since 1.0.0
* @return string
*/
public function get_language() {
if (defined('WPLANG')) {
$language = WPLANG;
}
if (empty($language)) {
$language = get_option('WPLANG');
}
if (!empty($language)) {
$languageParts = explode('_', $language);
return $languageParts[0];
}
return 'en';
}
/**
* Get the different instances of the core.
*
* @since 1.0.0
* @param string $type
* @return \H5PWordPress|\H5PCore|\H5PContentValidator|\H5PExport|\H5PStorage|\H5PValidator
*/
public function get_h5p_instance($type) {
$id = get_current_blog_id();
if (empty(self::$interface[$id])) {
self::$interface[$id] = new H5PWordPress();
$language = $this->get_language();
self::$core[$id] = new H5PCore(self::$interface[$id], $this->get_h5p_path(), $this->get_h5p_url(), $language, get_option('h5p_export', TRUE));
self::$core[$id]->aggregateAssets = !(defined('H5P_DISABLE_AGGREGATION') && H5P_DISABLE_AGGREGATION === true);
}
switch ($type) {
case 'validator':
return new H5PValidator(self::$interface[$id], self::$core[$id]);
case 'storage':
return new H5PStorage(self::$interface[$id], self::$core[$id]);
case 'contentvalidator':
return new H5PContentValidator(self::$interface[$id], self::$core[$id]);
case 'export':
return new H5PExport(self::$interface[$id], self::$core[$id]);
case 'interface':
return self::$interface[$id];
case 'core':
return self::$core[$id];
}
}
/**
* Get content with given id.
*
* @since 1.0.0
* @param int $id
* @return array
* @throws Exception
*/
public function get_content($id) {
if ($id === FALSE || $id === NULL) {
return __('Missing H5P identifier.', $this->plugin_slug);
}
// Try to find content with $id.
$core = $this->get_h5p_instance('core');
$content = $core->loadContent($id);
if (!$content) {
return sprintf(__('Cannot find H5P content with id: %d.', $this->plugin_slug), $id);
}
$content['language'] = $this->get_language();
return $content;
}
/**
* Translate h5p shortcode to html.
*
* @since 1.0.0
* @param array $atts
* @return string
*/
public function shortcode($atts) {
global $wpdb;
if (isset($atts['slug'])) {
$q=$wpdb->prepare(
"SELECT id ".
"FROM {$wpdb->prefix}h5p_contents ".
"WHERE slug=%s",
$atts['slug']
);
$row=$wpdb->get_row($q,ARRAY_A);
if ($wpdb->last_error) {
return sprintf(__('Database error: %s.', $this->plugin_slug), $wpdb->last_error);
}
if (!isset($row['id'])) {
return sprintf(__('Cannot find H5P content with slug: %s.', $this->plugin_slug), $atts['slug']);
}
$atts['id']=$row['id'];
}
$id = isset($atts['id']) ? intval($atts['id']) : NULL;
$content = $this->get_content($id);
if (is_string($content)) {
// Return error message if the user has the correct cap
return current_user_can('edit_h5p_contents') ? $content : NULL;
}
// Log view
new H5P_Event('content', 'shortcode',
$content['id'],
$content['title'],
$content['library']['name'],
$content['library']['majorVersion'] . '.' . $content['library']['minorVersion']);
return $this->add_assets($content);
}
/**
* Get permission policy property.
*
* @return string Permission policy.
*/
public function get_http_feature_policy_property() {
// Set HTTP feature policy attribute
$h5p_http_feature_policy_list = apply_filters( 'h5p_h5p_http_feature_policy', defined( 'H5P_HTTP_FEATURE_POLICY' ) && H5P_HTTP_FEATURE_POLICY ? H5P_HTTP_FEATURE_POLICY : self::$h5p_http_feature_policy );
array_walk(
$h5p_http_feature_policy_list,
function( &$feature_policy_value, $feature_policy_name ) {
$feature_policy_value = $feature_policy_name . ' \'' . $feature_policy_value . '\'';
}
);
return is_array( $h5p_http_feature_policy_list ) && 0 === count( $h5p_http_feature_policy_list ) ? '' : 'allow="' . implode( ';', $h5p_http_feature_policy_list ) . '"';
}
/**
* Get settings for given content
*
* @since 1.5.0
* @param array $content
* @return array
*/
public function get_content_settings($content) {
global $wpdb;
$core = $this->get_h5p_instance('core');
$safe_parameters = $core->filterParameters($content);
if (has_action('h5p_alter_filtered_parameters')) {
// Parse the JSON parameters
$decoded_parameters = json_decode($safe_parameters);
/**
* Allows you to alter the H5P content parameters after they have been
* filtered. This hook only fires before view.
*
* @since 1.5.3
*
* @param object &$parameters
* @param string $libraryName
* @param int $libraryMajorVersion
* @param int $libraryMinorVersion
*/
do_action_ref_array('h5p_alter_filtered_parameters', array(&$decoded_parameters, $content['library']['name'], $content['library']['majorVersion'], $content['library']['minorVersion']));
// Stringify the JSON parameters
$safe_parameters = json_encode($decoded_parameters);
}
// Getting author's user id
$author_id = (int)(is_array($content) ? $content['user_id'] : $content->user_id);
$metadata = $content['metadata'];