-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathclass-h5p-wordpress.php
1308 lines (1163 loc) · 36.2 KB
/
class-h5p-wordpress.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
class H5PWordPress implements H5PFrameworkInterface {
/**
* Kesps track of messages for the user.
*
* @since 1.0.0
* @var array
*/
private $messages = array('error' => array(), 'info' => array());
/**
* Keys that should be stored as a network setting
*
* @since 1.15.7
* @var array
*/
private $networkSettings = array( 'content_type_cache_updated_at' );
/**
* Implements setErrorMessage
*/
public function setErrorMessage($message, $code = NULL) {
if (current_user_can('edit_h5p_contents')) {
$this->messages['error'][] = (object)array(
'code' => $code,
'message' => $message
);
}
}
/**
* Implements setInfoMessage
*/
public function setInfoMessage($message) {
if (current_user_can('edit_h5p_contents')) {
$this->messages['info'][] = $message;
}
}
/**
* Return the selected messages.
*
* @since 1.0.0
* @param string $type
* @return array
*/
public function getMessages($type) {
if (empty($this->messages[$type])) {
return NULL;
}
$messages = $this->messages[$type];
$this->messages[$type] = array();
return $messages;
}
/**
* Implements t
*/
public function t($message, $replacements = array()) {
// Insert !var as is, escape @var and emphasis %var.
foreach ($replacements as $key => $replacement) {
if ($key[0] === '@') {
$replacements[$key] = esc_html($replacement);
}
elseif ($key[0] === '%') {
$replacements[$key] = '<em>' . esc_html($replacement) . '</em>';
}
}
$message = preg_replace('/(!|@|%)[a-z0-9-]+/i', '%s', $message);
$plugin = H5P_Plugin::get_instance();
$this->plugin_slug = $plugin->get_plugin_slug();
// Assumes that replacement vars are in the correct order.
return vsprintf(__($message, $this->plugin_slug), $replacements);
}
/**
* Helper
*/
private function getH5pPath() {
$plugin = H5P_Plugin::get_instance();
return $plugin->get_h5p_path();
}
/**
* Get the URL to a library file
*/
public function getLibraryFileUrl($libraryFolderName, $fileName) {
$upload_dir = wp_upload_dir();
return $upload_dir['baseurl'] . '/h5p/libraries/' . $libraryFolderName . '/' . $fileName;
}
/**
* Implements getUploadedH5PFolderPath
*/
public function getUploadedH5pFolderPath() {
static $dir;
if (is_null($dir)) {
$plugin = H5P_Plugin::get_instance();
$core = $plugin->get_h5p_instance('core');
$dir = $core->fs->getTmpPath();
}
return $dir;
}
/**
* Implements getUploadedH5PPath
*/
public function getUploadedH5pPath() {
static $path;
if (is_null($path)) {
$plugin = H5P_Plugin::get_instance();
$core = $plugin->get_h5p_instance('core');
$path = $core->fs->getTmpPath() . '.h5p';
}
return $path;
}
/**
* Implements getLibraryId
*/
public function getLibraryId($name, $majorVersion = NULL, $minorVersion = NULL) {
global $wpdb;
// Look for specific library
$sql_where = 'WHERE name = %s';
$sql_args = array($name);
if ($majorVersion !== NULL) {
// Look for major version
$sql_where .= ' AND major_version = %d';
$sql_args[] = $majorVersion;
if ($minorVersion !== NULL) {
// Look for minor version
$sql_where .= ' AND minor_version = %d';
$sql_args[] = $minorVersion;
}
}
// Get the lastest version which matches the input parameters
$id = $wpdb->get_var($wpdb->prepare(
"SELECT id
FROM {$wpdb->prefix}h5p_libraries
{$sql_where}
ORDER BY major_version DESC,
minor_version DESC,
patch_version DESC
LIMIT 1",
$sql_args)
);
return $id === NULL ? FALSE : $id;
}
/**
* Implements isPatchedLibrary
*/
public function isPatchedLibrary($library) {
global $wpdb;
if (defined('H5P_DEV') && H5P_DEV) {
// Makes sure libraries are updated, patch version does not matter.
return TRUE;
}
$operator = $this->isInDevMode() ? '<=' : '<';
return $wpdb->get_var($wpdb->prepare(
"SELECT id
FROM {$wpdb->prefix}h5p_libraries
WHERE name = '%s'
AND major_version = %d
AND minor_version = %d
AND patch_version {$operator} %d",
$library['machineName'],
$library['majorVersion'],
$library['minorVersion'],
$library['patchVersion']
)) !== NULL;
}
/**
* Implements isInDevMode
*/
public function isInDevMode() {
return false;
}
/**
* Implements mayUpdateLibraries
*/
public function mayUpdateLibraries() {
return current_user_can('manage_h5p_libraries');
}
/**
* Implements getLibraryUsage
*/
public function getLibraryUsage($id, $skipContent = FALSE) {
global $wpdb;
return array(
'content' => $skipContent ? -1 : intval($wpdb->get_var($wpdb->prepare(
"SELECT COUNT(distinct c.id)
FROM {$wpdb->prefix}h5p_libraries l
JOIN {$wpdb->prefix}h5p_contents_libraries cl ON l.id = cl.library_id
JOIN {$wpdb->prefix}h5p_contents c ON cl.content_id = c.id
WHERE l.id = %d",
$id)
)),
'libraries' => intval($wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*)
FROM {$wpdb->prefix}h5p_libraries_libraries
WHERE required_library_id = %d",
$id)
))
);
}
/**
* Implements saveLibraryData
*/
public function saveLibraryData(&$library, $new = TRUE) {
global $wpdb;
$preloadedJs = $this->pathsToCsv($library, 'preloadedJs');
$preloadedCss = $this->pathsToCsv($library, 'preloadedCss');
$dropLibraryCss = '';
if (isset($library['dropLibraryCss'])) {
$libs = array();
foreach ($library['dropLibraryCss'] as $lib) {
$libs[] = $lib['machineName'];
}
$dropLibraryCss = implode(', ', $libs);
}
$embedTypes = '';
if (isset($library['embedTypes'])) {
$embedTypes = implode(', ', $library['embedTypes']);
}
if (!isset($library['semantics'])) {
$library['semantics'] = '';
}
if (!isset($library['fullscreen'])) {
$library['fullscreen'] = 0;
}
if (!isset($library['hasIcon'])) {
$library['hasIcon'] = 0;
}
if ($new) {
$wpdb->insert(
$wpdb->prefix . 'h5p_libraries',
array(
'name' => $library['machineName'],
'title' => $library['title'],
'major_version' => $library['majorVersion'],
'minor_version' => $library['minorVersion'],
'patch_version' => $library['patchVersion'],
'runnable' => $library['runnable'],
'fullscreen' => $library['fullscreen'],
'embed_types' => $embedTypes,
'preloaded_js' => $preloadedJs,
'preloaded_css' => $preloadedCss,
'drop_library_css' => $dropLibraryCss,
'tutorial_url' => '', // NOT NULL, has to be there
'semantics' => $library['semantics'],
'has_icon' => $library['hasIcon'] ? 1 : 0,
'metadata_settings'=> $library['metadataSettings'],
'add_to' => isset($library['addTo']) ? json_encode($library['addTo']) : NULL
// Missing? created_at, updated_at
),
array(
'%s',
'%s',
'%d',
'%d',
'%d',
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%s',
'%s',
)
);
$library['libraryId'] = $wpdb->insert_id;
}
else {
$wpdb->update(
$wpdb->prefix . 'h5p_libraries',
array(
'title' => $library['title'],
'patch_version' => $library['patchVersion'],
'runnable' => $library['runnable'],
'fullscreen' => $library['fullscreen'],
'embed_types' => $embedTypes,
'preloaded_js' => $preloadedJs,
'preloaded_css' => $preloadedCss,
'drop_library_css' => $dropLibraryCss,
'semantics' => $library['semantics'],
'has_icon' => $library['hasIcon'] ? 1 : 0,
'metadata_settings'=> $library['metadataSettings'],
'add_to' => isset($library['addTo']) ? json_encode($library['addTo']) : NULL
),
array('id' => $library['libraryId']),
array(
'%s',
'%d',
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%s',
'%s'
),
array('%d')
);
$this->deleteLibraryDependencies($library['libraryId']);
}
// Log library successfully installed/upgraded
new H5P_Event('library', ($new ? 'create' : 'update'),
NULL, NULL,
$library['machineName'], $library['majorVersion'] . '.' . $library['minorVersion']);
// Update languages
$wpdb->delete(
$wpdb->prefix . 'h5p_libraries_languages',
array('library_id' => $library['libraryId']),
array('%d')
);
if (isset($library['language'])) {
foreach ($library['language'] as $languageCode => $translation) {
$wpdb->insert(
$wpdb->prefix . 'h5p_libraries_languages',
array(
'library_id' => $library['libraryId'],
'language_code' => $languageCode,
'translation' => $translation
),
array(
'%d',
'%s',
'%s'
)
);
}
}
}
/**
* Convert list of file paths to csv
*
* @param array $library
* Library data as found in library.json files
* @param string $key
* Key that should be found in $libraryData
* @return string
* file paths separated by ', '
*/
private function pathsToCsv($library, $key) {
if (isset($library[$key])) {
$paths = array();
foreach ($library[$key] as $file) {
$paths[] = $file['path'];
}
return implode(', ', $paths);
}
return '';
}
/**
* Implements deleteLibraryDependencies
*/
public function deleteLibraryDependencies($id) {
global $wpdb;
$wpdb->delete(
$wpdb->prefix . 'h5p_libraries_libraries',
array('library_id' => $id),
array('%d')
);
}
/**
* Implements deleteLibrary
*/
public function deleteLibrary($library) {
global $wpdb;
// Delete library files
H5PCore::deleteFileTree($this->getH5pPath() . '/libraries/' . $library->name . '-' . $library->major_version . '.' . $library->minor_version);
// Remove library data from database
$wpdb->delete($wpdb->prefix . 'h5p_libraries_libraries', array('library_id' => $library->id), array('%d'));
$wpdb->delete($wpdb->prefix . 'h5p_libraries_languages', array('library_id' => $library->id), array('%d'));
$wpdb->delete($wpdb->prefix . 'h5p_libraries', array('id' => $library->id), array('%d'));
}
/**
* Implements saveLibraryDependencies
*/
public function saveLibraryDependencies($id, $dependencies, $dependencyType) {
global $wpdb;
foreach ($dependencies as $dependency) {
$wpdb->query($wpdb->prepare(
"INSERT INTO {$wpdb->prefix}h5p_libraries_libraries (library_id, required_library_id, dependency_type)
SELECT %d, hl.id, %s
FROM {$wpdb->prefix}h5p_libraries hl
WHERE name = %s
AND major_version = %d
AND minor_version = %d
ON DUPLICATE KEY UPDATE dependency_type = %s",
$id, $dependencyType, $dependency['machineName'], $dependency['majorVersion'], $dependency['minorVersion'], $dependencyType)
);
}
}
/**
* Implements updateContent
*/
public function updateContent($content, $contentMainId = NULL) {
global $wpdb;
$metadata = (array)$content['metadata'];
$table = $wpdb->prefix . 'h5p_contents';
$format = array();
$data = array_merge(\H5PMetadata::toDBArray($metadata, true, true, $format), array(
'updated_at' => current_time('mysql', 1),
'parameters' => $content['params'],
'embed_type' => 'div', // TODO: Determine from library?
'library_id' => $content['library']['libraryId'],
'filtered' => '',
'disable' => $content['disable']
));
$format[] = '%s'; // updated_at
$format[] = '%s'; // parameters
$format[] = '%s'; // embed_type
$format[] = '%d'; // library_id
$format[] = '%s'; // filtered
$format[] = '%d'; // disable
if (!isset($content['id'])) {
// Insert new content
$data['created_at'] = $data['updated_at'];
$format[] = '%s';
$data['user_id'] = get_current_user_id();
$format[] = '%d';
$wpdb->insert($table, $data, $format);
$content['id'] = $wpdb->insert_id;
$event_type = 'create';
}
else {
// Update existing content
$wpdb->update($table, $data, array('id' => $content['id']), $format, array('%d'));
$event_type = 'update';
}
// Log content create/update/upload
if (!empty($content['uploaded'])) {
$event_type .= ' upload';
}
new H5P_Event('content', $event_type,
$content['id'],
$metadata['title'],
$content['library']['machineName'],
$content['library']['majorVersion'] . '.' . $content['library']['minorVersion']);
do_action('h5p_save_content', $content['id'], $metadata);
return $content['id'];
}
/**
* Implements insertContent
*/
public function insertContent($content, $contentMainId = NULL) {
return $this->updateContent($content);
}
/**
* Implement getWhitelist
*/
public function getWhitelist($isLibrary, $defaultContentWhitelist, $defaultLibraryWhitelist) {
// TODO: Get this value from a settings page.
$whitelist = $defaultContentWhitelist;
if ($isLibrary) {
$whitelist .= ' ' . $defaultLibraryWhitelist;
}
return $whitelist;
}
/**
* Implements copyLibraryUsage
*/
public function copyLibraryUsage($contentId, $copyFromId, $contentMainId = NULL) {
global $wpdb;
$wpdb->query($wpdb->prepare(
"INSERT INTO {$wpdb->prefix}h5p_contents_libraries (content_id, library_id, dependency_type, weight, drop_css)
SELECT %d, hcl.library_id, hcl.dependency_type, hcl.weight, hcl.drop_css
FROM {$wpdb->prefix}h5p_contents_libraries hcl
WHERE hcl.content_id = %d",
$contentId, $copyFromId)
);
}
/**
* Implements deleteContentData
*/
public function deleteContentData($id) {
global $wpdb;
// Remove content data and library usage
$wpdb->delete($wpdb->prefix . 'h5p_contents', array('id' => $id), array('%d'));
$this->deleteLibraryUsage($id);
// Remove user scores/results
$wpdb->delete($wpdb->prefix . 'h5p_results', array('content_id' => $id), array('%d'));
// Remove contents user/usage data
$wpdb->delete($wpdb->prefix . 'h5p_contents_user_data', array('content_id' => $id), array('%d'));
}
/**
* Implements deleteLibraryUsage
*/
public function deleteLibraryUsage($contentId) {
global $wpdb;
$wpdb->delete($wpdb->prefix . 'h5p_contents_libraries', array('content_id' => $contentId), array('%d'));
}
/**
* Implements saveLibraryUsage
*/
public function saveLibraryUsage($contentId, $librariesInUse) {
global $wpdb;
$dropLibraryCssList = array();
foreach ($librariesInUse as $dependency) {
if (!empty($dependency['library']['dropLibraryCss'])) {
$dropLibraryCssList = array_merge($dropLibraryCssList, explode(', ', $dependency['library']['dropLibraryCss']));
}
}
foreach ($librariesInUse as $dependency) {
$dropCss = in_array($dependency['library']['machineName'], $dropLibraryCssList) ? 1 : 0;
$wpdb->insert(
$wpdb->prefix . 'h5p_contents_libraries',
array(
'content_id' => $contentId,
'library_id' => $dependency['library']['libraryId'],
'dependency_type' => $dependency['type'],
'drop_css' => $dropCss,
'weight' => $dependency['weight']
),
array(
'%d',
'%d',
'%s',
'%d',
'%d'
)
);
}
}
/**
* Implements loadLibrary
*/
public function loadLibrary($name, $majorVersion, $minorVersion) {
global $wpdb;
$library = $wpdb->get_row($wpdb->prepare(
"SELECT id as libraryId, name as machineName, title, major_version as majorVersion, minor_version as minorVersion, patch_version as patchVersion,
embed_types as embedTypes, preloaded_js as preloadedJs, preloaded_css as preloadedCss, drop_library_css as dropLibraryCss, fullscreen, runnable,
semantics, has_icon as hasIcon
FROM {$wpdb->prefix}h5p_libraries
WHERE name = %s
AND major_version = %d
AND minor_version = %d",
$name,
$majorVersion,
$minorVersion),
ARRAY_A
);
$dependencies = $wpdb->get_results($wpdb->prepare(
"SELECT hl.name as machineName, hl.major_version as majorVersion, hl.minor_version as minorVersion, hll.dependency_type as dependencyType
FROM {$wpdb->prefix}h5p_libraries_libraries hll
JOIN {$wpdb->prefix}h5p_libraries hl ON hll.required_library_id = hl.id
WHERE hll.library_id = %d",
$library['libraryId'])
);
foreach ($dependencies as $dependency) {
$library[$dependency->dependencyType . 'Dependencies'][] = array(
'machineName' => $dependency->machineName,
'majorVersion' => $dependency->majorVersion,
'minorVersion' => $dependency->minorVersion,
);
}
if ($this->isInDevMode()) {
$semantics = $this->getSemanticsFromFile($library['machineName'], $library['majorVersion'], $library['minorVersion']);
if ($semantics) {
$library['semantics'] = $semantics;
}
}
return $library;
}
private function getSemanticsFromFile($name, $majorVersion, $minorVersion) {
$semanticsPath = $this->getH5pPath() . '/libraries/' . $name . '-' . $majorVersion . '.' . $minorVersion . '/semantics.json';
if (file_exists($semanticsPath)) {
$semantics = file_get_contents($semanticsPath);
if (!json_decode($semantics, TRUE)) {
$this->setErrorMessage($this->t('Invalid json in semantics for %library', array('%library' => $name)));
}
return $semantics;
}
return FALSE;
}
/**
* Implements loadLibrarySemantics
*/
public function loadLibrarySemantics($name, $majorVersion, $minorVersion) {
global $wpdb;
if ($this->isInDevMode()) {
$semantics = $this->getSemanticsFromFile($name, $majorVersion, $minorVersion);
}
else {
$semantics = $wpdb->get_var($wpdb->prepare(
"SELECT semantics
FROM {$wpdb->prefix}h5p_libraries
WHERE name = %s
AND major_version = %d
AND minor_version = %d",
$name, $majorVersion, $minorVersion)
);
}
return ($semantics === FALSE ? NULL : $semantics);
}
/**
* Implements alterLibrarySemantics
*/
public function alterLibrarySemantics(&$semantics, $name, $majorVersion, $minorVersion) {
/**
* Allows you to alter the H5P library semantics, i.e. changing how the
* editor looks and how content parameters are filtered.
*
* @since 1.5.3
*
* @param object &$semantics
* @param string $libraryName
* @param int $libraryMajorVersion
* @param int $libraryMinorVersion
*/
do_action_ref_array('h5p_alter_library_semantics', array(&$semantics, $name, $majorVersion, $minorVersion));
}
/**
* Implements loadContent
*/
public function loadContent($id) {
global $wpdb;
$content = $wpdb->get_row($wpdb->prepare(
"SELECT hc.id
, hc.title
, hc.parameters AS params
, hc.filtered
, hc.slug AS slug
, hc.user_id
, hc.embed_type AS embedType
, hc.disable
, hl.id AS libraryId
, hl.name AS libraryName
, hl.major_version AS libraryMajorVersion
, hl.minor_version AS libraryMinorVersion
, hl.embed_types AS libraryEmbedTypes
, hl.fullscreen AS libraryFullscreen
, hc.authors AS authors
, hc.source AS source
, hc.year_from AS yearFrom
, hc.year_to AS yearTo
, hc.license AS license
, hc.license_version AS licenseVersion
, hc.license_extras AS licenseExtras
, hc.author_comments AS authorComments
, hc.changes AS changes
, hc.default_language AS defaultLanguage
, hc.a11y_title AS a11yTitle
FROM {$wpdb->prefix}h5p_contents hc
JOIN {$wpdb->prefix}h5p_libraries hl ON hl.id = hc.library_id
WHERE hc.id = %d",
$id),
ARRAY_A
);
if ($content !== NULL) {
$content['metadata'] = array();
$metadata_structure = array('title', 'authors', 'source', 'yearFrom', 'yearTo', 'license', 'licenseVersion', 'licenseExtras', 'authorComments', 'changes', 'defaultLanguage', 'a11yTitle');
foreach ($metadata_structure as $property) {
if (!empty($content[$property])) {
if ($property === 'authors' || $property === 'changes') {
$content['metadata'][$property] = json_decode($content[$property]);
}
else {
$content['metadata'][$property] = $content[$property];
}
if ($property !== 'title') {
unset($content[$property]); // Unset all except title
}
}
}
}
return $content;
}
/**
* Implements loadContentDependencies
*/
public function loadContentDependencies($id, $type = NULL) {
global $wpdb;
$query =
"SELECT hl.id
, hl.name AS machineName
, hl.major_version AS majorVersion
, hl.minor_version AS minorVersion
, hl.patch_version AS patchVersion
, hl.preloaded_css AS preloadedCss
, hl.preloaded_js AS preloadedJs
, hcl.drop_css AS dropCss
, hcl.dependency_type AS dependencyType
FROM {$wpdb->prefix}h5p_contents_libraries hcl
JOIN {$wpdb->prefix}h5p_libraries hl ON hcl.library_id = hl.id
WHERE hcl.content_id = %d";
$queryArgs = array($id);
if ($type !== NULL) {
$query .= " AND hcl.dependency_type = %s";
$queryArgs[] = $type;
}
$query .= " ORDER BY hcl.weight";
return $wpdb->get_results($wpdb->prepare($query, $queryArgs), ARRAY_A);
}
/**
* Implements getOption().
*/
public function getOption($name, $default = FALSE) {
if(in_array($name, $this->networkSettings)) {
return get_site_option('h5p_' . $name, $default);
}
if ($name === 'site_uuid') {
$name = 'h5p_site_uuid'; // Make up for old core bug
}
return get_option('h5p_' . $name, $default);
}
/**
* Implements setOption().
*/
public function setOption($name, $value) {
if ($name === 'site_uuid') {
$name = 'h5p_site_uuid'; // Make up for old core bug
}
$var = $this->getOption($name);
if(in_array($name, $this->networkSettings)) {
$name = 'h5p_' . $name; // Always prefix to avoid conflicts
if ($var === FALSE) {
add_site_option($name, $value);
}
else {
update_site_option($name, $value);
}
} else {
$name = 'h5p_' . $name; // Always prefix to avoid conflicts
if ($var === FALSE) {
add_option($name, $value);
}
else {
update_option($name, $value);
}
}
}
/**
* Convert variables to fit our DB.
*/
private static function camelToString($input) {
$input = preg_replace('/[a-z0-9]([A-Z])[a-z0-9]/', '_$1', $input);
return strtolower($input);
}
/**
* Implements setFilteredParameters().
*/
public function updateContentFields($id, $fields) {
global $wpdb;
$processedFields = array();
$format = array();
foreach ($fields as $name => $value) {
if (is_int($value)) {
$format[] = '%d'; // Int
}
else if (is_float($value)) {
$format[] = '%f'; // Float
}
else {
$format[] = '%s'; // String
}
$processedFields[self::camelToString($name)] = $value;
}
$wpdb->update(
$wpdb->prefix . 'h5p_contents',
$processedFields,
array('id' => $id),
$format,
array('%d'));
}
/**
* Implements clearFilteredParameters().
*/
public function clearFilteredParameters($library_ids) {
global $wpdb;
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->prefix}h5p_contents
SET filtered = NULL
WHERE library_id IN (%s)",
implode(',', $library_ids))
);
}
/**
* Implements getNumNotFiltered().
*/
public function getNumNotFiltered() {
global $wpdb;
return (int) $wpdb->get_var(
"SELECT COUNT(id)
FROM {$wpdb->prefix}h5p_contents
WHERE filtered = ''"
);
}
/**
* Implements getNumContent().
*/
public function getNumContent($library_id, $skip = NULL) {
global $wpdb;
$skip_query = empty($skip) ? '' : " AND id NOT IN ($skip)";
return (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(id)
FROM {$wpdb->prefix}h5p_contents
WHERE library_id = %d
{$skip_query}",
$library_id
));
}
/**
* Implements loadLibraries.
*/
public function loadLibraries() {
global $wpdb;
$results = $wpdb->get_results(
"SELECT id, name, title, major_version, minor_version, patch_version, runnable, restricted
FROM {$wpdb->prefix}h5p_libraries
ORDER BY title ASC, major_version ASC, minor_version ASC"
);
$libraries = array();
foreach ($results as $library) {
$libraries[$library->name][] = $library;
}
return $libraries;
}
/**
* Implements getAdminUrl.
*/
public function getAdminUrl() {
}
/**
* Implements getPlatformInfo
*/
public function getPlatformInfo() {
global $wp_version;
return array(
'name' => 'WordPress',
'version' => $wp_version,
'h5pVersion' => H5P_Plugin::VERSION
);
}
/**
* Implements fetchExternalData
*/
public function fetchExternalData($url, $data = NULL, $blocking = TRUE, $stream = NULL) {
@set_time_limit(0);
$options = array(
'timeout' => !empty($blocking) ? 30 : 0.01,
'stream' => !empty($stream),
'filename' => !empty($stream) ? $stream : FALSE
);
if ($data !== NULL) {
// Post
$options['body'] = $data;
$response = wp_remote_post($url, $options);
}
else {
// Get
if (empty($options['filename'])) {
// Support redirects
$response = wp_remote_get($url);
}
else {
// Use safe when downloading files
$response = wp_safe_remote_get($url, $options);
}
}
if (is_wp_error($response)) {
$this->setErrorMessage($response->get_error_message(), 'failed-fetching-external-data');
return FALSE;
}
elseif ($response['response']['code'] === 200) {
return empty($response['body']) ? TRUE : $response['body'];
}
return NULL;
}
/**
* Implements setLibraryTutorialUrl
*/
public function setLibraryTutorialUrl($library_name, $url) {
global $wpdb;
$wpdb->update(
$wpdb->prefix . 'h5p_libraries',
array('tutorial_url' => $url),
array('name' => $library_name),
array('%s'),
array('%s')
);
}
/**
* Implements resetContentUserData
*/
public function resetContentUserData($contentId) {
global $wpdb;
// Reset user datas for this content