-
Notifications
You must be signed in to change notification settings - Fork 23
/
TextformatterVideoEmbed.module
1009 lines (904 loc) · 28.6 KB
/
TextformatterVideoEmbed.module
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 namespace ProcessWire;
/**
* ProcessWire Video Embedding Textformatter
*
* Looks for Youtube or Vimeo URLs and automatically converts them to embeds
*
* Copyright (C) 2021 by Ryan Cramer
* Licensed under MPL 2.0
* https://processwire.com
*
* @property int $maxWidth
* @property int $maxHeight
* @property string $maxSize
* @property int $responsive
* @property string $wrapStyles
* @property string $frameStyles
* @property int $refreshDays
* @property int $lastMaint Timestamp of last maintenance
* @property string $aspectRatio
* @property int $failAction
* @property int|bool $noCookies
*
* @method array getAspectRatios()
* @method array getVideoSizes()
* @method string wrapEmbedCode($embedCode, array $data)
* @method string embedError($line, $videoURL, $data)
*
*/
class TextformatterVideoEmbed extends Textformatter implements ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Video embed for YouTube (and Vimeo)',
'version' => 202,
'summary' => 'Enter a full YouTube (or Vimeo) URL by itself in any paragraph (example: https://youtu.be/Wl4XiYadV_k) and this will automatically convert it to an embedded video. This formatter is intended to be run on trusted input. Recommended for use with CKEditor textarea fields.',
'author' => 'Ryan Cramer',
'href' => 'https://processwire.com/modules/textformatter-video-embed/',
'requires' => 'ProcessWire>=3.0.148',
);
}
const dbTableName = 'textformatter_video_embed';
const logName = 'textformatter-video-embed';
/**
* Default configuration values
*
* @var array
*
*/
protected $configDefaults = array(
'maxSize' => '720p',
'wrapStyles' => 'position:relative;margin:1em 0;padding-bottom:{pct}%;height:0;overflow:hidden;',
'frameStyles' => 'position:absolute;top:0;left:0;width:100%;height:100%;',
'refreshDays' => 0,
'lastMaint' => 0,
'failAction' => 0,
'aspectRatio' => '0',
'noCookies' => 0,
);
/**
* Verbose embed data defaults
*
* @var array
*
*/
protected $verboseDataDefaults = array(
'valid' => false,
'title' => '',
'author_name' => '',
'author_url' => '',
'type' => '',
'height' => '',
'width' => '',
'version' => '',
'provider_name' => '',
'provider_url' => '',
'thumbnail_height' => '',
'thumbnail_width' => '',
'thumbnail_url' => '',
'embed_code' => '',
'video_url' => '',
'page_id' => 0,
'field' => '',
);
/**
* Video sizes
*
* @var array
*
*/
protected $videoSizes = array(
'240p' => array(
'width' => 426,
'height' => 240,
'label' => 'Minimum YouTube size'
),
'360p' => array(
'width' => 640,
'height' => 360,
'label' => 'Typical website resolution'
),
'480p' => array(
'width' => 854,
'height' => 480,
'label' => 'Standard definition (SD)'
),
'720p' => array(
'width' => 1280,
'height' => 720,
'label' => 'High definition (HD) min'
),
'1080p' => array(
'width' => 1920,
'height' => 1080,
'label' => 'High definition (HD) max'
),
'1440p' => array(
'width' => 2560,
'height' => 1440,
'label' => '2K resolution'
),
'2160p' => array(
'width' => 3840,
'height' => 2160,
'label' => '4K resolution'
),
);
/**
* Data as used by the get/set functions
*
*/
protected $data = array();
/**
* Last used HTTP GET URL
*
* @var string
*
*/
protected $lastHttpGetVideoID = '';
/**
* Last Page passed to format()
*
* @var int
*
*/
protected $lastPageId = 0;
/**
* Name of last Field passed to format()
*
* @var string
*
*/
protected $lastFieldName = '';
/**
* Set our configuration defaults
*
*/
public function __construct() {
parent::__construct();
foreach($this->configDefaults as $key => $value) {
$this->set($key, $value);
}
}
/**
* Run daily maintenance
*
* @return bool|int Return false if not yet time to run, int with quantity of deleted items when run
*
*/
protected function maintenance() {
if($this->refreshDays < 1) return false;
if($this->lastMaint >= (time() - 86400)) return false;
$table = self::dbTableName;
$query = $this->wire()->database->prepare("DELETE FROM $table WHERE created<:maxAge");
$query->bindValue(':maxAge', date('Y-m-d H:i:s', strtotime("-$this->refreshDays DAYS")));
$query->execute();
$rowCount = $query->rowCount();
$this->lastMaint = time();
$this->wire()->modules->saveConfig($this, 'lastMaint', $this->lastMaint);
if($rowCount) $this->log("$rowCount videos older than $this->refreshDays days cleared for maintenance");
return $rowCount;
}
/**
* Given a service oembed URL and video ID, return the corresponding embed code.
*
* A cached version of the embed code will be used if possible. When not possible,
* it will be retrieved from the service's oembed URL, and then cached.
*
* @param string $oembedURL
* @param string $videoID
* @param string $videoURL
* @param bool $verbose Get verbose array of data rather than just embed code?
* @return string|int|array Returns embed code (string) or HTTP error code (int)
*
*/
protected function getEmbedCode($oembedURL, $videoID, $videoURL, $verbose = true) {
$this->maintenance();
$database = $this->wire()->database;
$table = self::dbTableName;
$data = array();
$query = $database->prepare("SELECT * FROM $table WHERE video_id=:video_id");
$query->bindValue(":video_id", $videoID);
$query->execute();
if($query->rowCount()) {
$row = $query->fetch(\PDO::FETCH_ASSOC);
$data = empty($row['data']) ? array() : json_decode($row['data'], true);
$data = is_array($data) ? array_merge($this->verboseDataDefaults, $data) : $this->verboseDataDefaults;
$data['created'] = $row['created'];
$data['embed_code'] = ctype_digit($row['embed_code']) ? (int) $row['embed_code'] : $row['embed_code'];
$data['valid'] = is_string($data['embed_code']);
}
$query->closeCursor();
if(empty($data)) {
$data = $this->getNewEmbedCode($oembedURL, $videoID, $videoURL, $verbose);
}
return $data;
}
/**
* Get new embed code now from HTTP
*
* @param string $oembedURL
* @param string $videoID
* @param string $videoURL
* @param bool $verbose
* @return array|int|string
* @throws WireException
*
*/
protected function getNewEmbedCode($oembedURL, $videoID, $videoURL, $verbose = true) {
$database = $this->wire()->database;
$table = self::dbTableName;
$httpErrorCode = 0;
$maxTries = 3;
$retry = 0;
$oembedURL = $this->oembedURL($oembedURL, $videoURL, $videoID);
$http = new WireHttp();
$this->wire($http);
$this->lastHttpGetVideoID = $videoID;
$data = $http->get($oembedURL);
if($data) $data = json_decode($data, true);
if(is_array($data) && isset($data['html'])) {
// success
$data = array_merge($this->verboseDataDefaults, $data);
$data['valid'] = true;
$data['video_url'] = $videoURL;
$data['page_id'] = $this->lastPageId;
$data['field'] = $this->lastFieldName;
$embedCode = $data['html'];
if($this->noCookies) {
// youtube
$embedCode = str_replace(
"youtube.com/",
"youtube-nocookie.com/", // set nocookie domain
$embedCode
);
// vimeo
$embedCode = str_replace(
"?app_id=",
"?dnt=1&app_id=", // add do-not-track parameter
$embedCode
);
}
unset($data['html']);
} else {
// fail: store http code for error reporting and to avoid repeating request
$data = $this->verboseDataDefaults;
$httpErrorCode = (int) $http->getHttpCode();
$data['valid'] = false;
$embedCode = $httpErrorCode;
}
do {
try {
$sql = "INSERT INTO $table SET video_id=:videoID, embed_code=:embedCode, created=NOW(), data=:data";
$query = $database->prepare($sql);
$query->bindValue(":videoID", $videoID);
$query->bindValue(":embedCode", "$embedCode");
$query->bindValue(":data", json_encode($data));
$query->execute();
$retry = 0;
} catch(\Exception $e) {
$retry++;
if($retry >= $maxTries) throw $e;
$this->___upgrade(0, 0);
}
} while($retry > 0 && $retry < $maxTries);
if($httpErrorCode) {
$this->log("HTTP $httpErrorCode fail for: $videoURL");
} else {
$this->log("Retrieved embed for: $videoURL");
}
if($verbose) {
$data['embed_code'] = $embedCode;
}
return $verbose ? $data : $embedCode;
}
/**
* Apply replacements and additions to oembed URL
*
* @param $oembedURL
* @param $videoURL
* @param $videoID
* @return string
*
*/
protected function oembedURL($oembedURL, $videoURL, $videoID) {
$oembedURL = str_replace(
array('{url}', '{id}'),
array(urlencode($videoURL), urlencode($videoID)),
$oembedURL
);
if($this->maxSize && strpos($oembedURL, 'maxwidth=') === false) {
$videoSizes = $this->getVideoSizes();
$size = $videoSizes[$this->maxSize];
$oembedURL .= strpos($oembedURL, '?') ? '&' : '?';
$oembedURL .= "maxwidth=$size[width]&maxheight=$size[height]";
}
return $oembedURL;
}
/**
* Wrap video embed code with responsive div
*
* @param string $embedCode
* @param array $data
* @return string
*
*/
protected function ___wrapEmbedCode($embedCode, array $data) {
$sanitizer = $this->wire()->sanitizer;
$frameStyles = $sanitizer->entities($this->frameStyles);
$wrapStyles = $sanitizer->entities($this->wrapStyles);
$pct = $this->aspectRatio === '0' ? 0 : (float) $this->aspectRatio;
if($pct === 0) {
// auto aspect ratio
if(!empty($data['height']) && !empty($data['width'])) {
$pct = 100 * ((int) $data['height']) / ((int) $data['width']);
$pct = str_replace(',', '.', $pct);
} else {
$pct = '56.25'; // 16:9
}
}
if($frameStyles) {
$frameStyles = str_replace(array('{pct}%', '{pct}', '{percent}%', '{percent}'), "$pct%", $frameStyles);
$embedCode = str_ireplace('<iframe ', "<iframe style='$frameStyles' ", $embedCode);
}
if($wrapStyles) {
$wrapStyles = str_replace(array('{pct}%', '{pct}', '{percent}%', '{percent}'), "$pct%", $wrapStyles);
$out = "<div class='TextformatterVideoEmbed' style='$wrapStyles'>$embedCode</div>";
} else {
$out = "<div class='TextformatterVideoEmbed'>$embedCode</div>";
}
return $out;
}
/**
* Format the given text string with Page and Field provided.
*
* @param Page $page
* @param Field $field
* @param string|mixed $value Value is provided as a reference, so is modified directly (not returned).
*
*/
public function formatValue(Page $page, Field $field, &$value) {
// apply video embeds
$this->lastPageId = $page->id;
$this->lastFieldName = $field->name;
$this->format($value);
}
/**
* Text formatting function as used by the Textformatter interface
*
* Here we look for video codes on first pass using a fast strpos() function.
* When found, we do our second pass with preg_match_all and replace the video URLs
* with the proper embed codes obtained from each service's oembed web service.
*
* @var string $str
*
*/
public function format(&$str) {
$youtube = strpos($str, 'youtu') !== false;
$vimeo = strpos($str, 'vimeo.com') !== false;
if(!$youtube && !$vimeo) return;
if((strpos($str, 'https://') === 0 || strpos($str, 'http://') === 0)
&& strpos($str, '<') === false && strpos(trim($str), ' ') === false) {
$originalStr = $str;
$paraStr = "<p>" . trim($str) . "</p>";
$str = $paraStr;
if($youtube) $this->embedYoutube($str);
if($vimeo) $this->embedVimeo($str);
if($str === $paraStr) $str = $originalStr; // no change? restore
} else {
if($youtube) $this->embedYoutube($str);
if($vimeo) $this->embedVimeo($str);
}
}
/**
* Check for Youtube URLS and embed when found
*
* @var string $str
*
*/
protected function embedYoutube(&$str) {
// perform fast check before performing regex check
if(strpos($str, '://www.youtube.com/watch') === false
&& strpos($str, '://www.youtube.com/v/') === false
&& strpos($str, '://youtu.be/') === false) return;
// 1: full URL, 2:video id, 3: query string (optional)
$regex =
'#' .
'<(?:p|h[1-6])' . // open tag <p or <h1, <h2, etc.
'(?:>|\s+[^>]+>)\s*' . // rest of open tag and close bracket
'(' . // capture #1: full URL
'https?://(?:www\.)?youtu(?:\.be|be\.com)+/' . // scheme + host "https://youtu.be/"
'(?:watch/?\?v=|v/)?' . // optional "watch?v=" or "v/"
'([^\s&<\'"]+)' . // capture #2: video ID (U&LC letters, numbers)
')' . // end of capture #1: full URL
'((?:&|&|\?)[-_,.=&;a-zA-Z0-9]*)?.*?' . // capture #3: optional query string
'</[ph123456]+>' . // close tag
'#';
if(!preg_match_all($regex, $str, $matches)) return;
$oembedUrl = "https://www.youtube.com/oembed?url={url}&format=json";
foreach($matches[0] as $key => $line) {
$youtubeUrl = $matches[1][$key];
$videoID = $matches[2][$key];
$queryString = isset($matches[3][$key]) ? $matches[3][$key] : '';
$data = $this->getEmbedCode($oembedUrl, $videoID, $youtubeUrl);
if(is_int($data['embed_code'])) {
// http error code
if($this->lastHttpGetVideoID === $videoID) {
// http error code just now, try again using generated URL
$youtubeUrl2 = "https://www.youtube.com/watch?v=$videoID";
if($youtubeUrl != $youtubeUrl2) {
$this->clearVideo($videoID);
$data = $this->getNewEmbedCode($oembedUrl, $videoID, $youtubeUrl2);
}
}
}
$embedCode = $data['embed_code'];
if($data['valid']) {
if(strlen($queryString)) {
$queryString = str_replace('&', '&', $queryString);
$queryString = trim($queryString, '&');
$embedCode = str_replace("?", "?$queryString&", $embedCode);
}
$embedCode = $this->wrapEmbedCode($embedCode, $data);
} else {
$embedCode = $this->embedError($line, $youtubeUrl, $data);
}
$str = str_replace($line, $embedCode, $str);
}
}
/**
* Check for Vimeo URLS and embed when found
*
* @var string $str
*
*/
protected function embedVimeo(&$str) {
if(strpos($str, '://vimeo.com/') === false) return;
if(!preg_match_all('#<(?:p|h[1-6])(?:>|\s+[^>]+>)\s*(https?://vimeo.com/(?:[^<]*?/|)(\d+)).*?</(?:p|h[1-6])>#', $str, $matches)) return;
foreach($matches[0] as $key => $line) {
$videoID = $matches[2][$key];
$videoURL = "https://vimeo.com/$videoID";
$oembedURL = "https://vimeo.com/api/oembed.json?url={url}";
$data = $this->getEmbedCode($oembedURL, $videoID, $videoURL);
if($data['valid']) {
$embedCode = $data['embed_code'];
$embedCode = $this->wrapEmbedCode($embedCode, $data);
} else {
$embedCode = $this->embedError($line, $videoURL, $data);
}
if($embedCode) $str = str_replace($line, $embedCode, $str);
}
}
/**
* Render embed error
*
* @param string $line
* @param string $videoURL
* @param array $data
* @return string
*
*/
protected function ___embedError($line, $videoURL, $data) {
$openTag = substr($line, 0, strpos($line, '>')+1);
$closeTag = substr($line, strrpos($line, '</'));
if($this->failAction > 0) {
$out = "<!--{$videoURL} ($data[embed_code])-->";
} else {
$out = "$openTag<span class='TextformatterVideoEmbedError'>$videoURL ($data[embed_code])</span>$closeTag";
}
return $out;
}
/**
* Clear one video from cache
*
* @param string $videoID
* @return bool|int
* @throws WireException
*
*/
public function clearVideo($videoID) {
$table = self::dbTableName;
$query = $this->wire()->database->prepare("DELETE FROM $table WHERE video_id=:video_id");
$query->bindValue(":video_id", $videoID);
$result = $query->execute();
return $result ? $query->rowCount() : false;
}
/**
* Clear all cached video embed codes, forcing it to re-pull
*
*/
public function clearAllVideos() {
$this->wire()->database->query("DELETE FROM " . self::dbTableName);
$this->log("Cleared all video embeds");
}
/**
* Get verbose data of videos (up to 100 or $options specified limit)
*
* @param array $options
* - `limit` (int): Max items to return (default=100)
* - `start` (int): Item to start with, -1 for auto according to current pageNum, 0 for first. (default=-1)
* - `sort` (string): How to sort items, one of 'created' (ascending), or `-created` (descending). (default=-created)
* @return array
*
*/
public function getVideos(array $options = array()) {
$defaults = array(
'start' => -1,
'limit' => 100,
'sort' => '-created',
);
$sorts = array(
'created' => 'created',
'-created' => 'created DESC',
);
$database = $this->wire()->database;
$table = self::dbTableName;
$videos = array();
$options = array_merge($defaults, $options);
$sort = $options['sort'];
if(!isset($sorts[$sort])) $sort = $defaults['sort'];
$orderBy = $sorts[$sort];
$start = (int) $options['start'];
$limit = (int) $options['limit'];
if($limit > 0 && $start < 0) {
$start = ($this->wire()->input->pageNum() - 1) * $limit;
}
$sql =
"SELECT * FROM $table " .
"ORDER BY $orderBy " .
($limit ? "LIMIT $start,$limit" : "");
$query = $database->prepare($sql);
$query->execute();
while($row = $query->fetch(\PDO::FETCH_ASSOC)) {
$data = empty($row['data']) ? array() : json_decode($row['data'], true);
$row = array_merge($this->verboseDataDefaults, $row, $data);
$videos[] = $row;
}
$query->closeCursor();
return $videos;
}
/**
* Get verbose data of all videos
*
* @param array $options
* @return array
*
*/
public function getAllVideos(array $options = array()) {
if(empty($options['limit'])) $options['limit'] = 0;
if(empty($options['start'])) $options['start'] = 0;
return $this->getVideos($options);
}
/**
* Get count of all videos currently in cache
*
* @return int
*
*/
public function getNumVideos() {
$query = $this->wire()->database->prepare("SELECT COUNT(*) FROM " . self::dbTableName);
$query->execute();
$count = (int) $query->fetchColumn();
$query->closeCursor();
return $count;
}
/**
* Get aspect ratios
*
* @return array
*
*/
public function ___getAspectRatios() {
return array(
'0' => $this->_('Auto (use video aspect ratio)'),
'56.25' => $this->_('Fixed 16:9 (YouTube default)'),
'75.0' => $this->_('Fixed 4:3'),
'100.0' => $this->_('Fixed 1:1 (square)'),
);
}
/**
* Get video sizes
*
* @return array
*
*/
public function ___getVideoSizes() {
return array(
'240p' => array(
'width' => 426,
'height' => 240,
'label' => $this->_('Minimum YouTube size')
),
'360p' => array(
'width' => 640,
'height' => 360,
'label' => $this->_('Typical website resolution')
),
'480p' => array(
'width' => 854,
'height' => 480,
'label' => $this->_('Standard definition (SD)')
),
'720p' => array(
'width' => 1280,
'height' => 720,
'label' => $this->_('High definition (HD) min')
),
'1080p' => array(
'width' => 1920,
'height' => 1080,
'label' => $this->_('High definition (HD) max')
),
'1440p' => array(
'width' => 2560,
'height' => 1440,
'label' => $this->_('2K resolution')
),
'2160p' => array(
'width' => 3840,
'height' => 2160,
'label' => $this->_('4K resolution')
),
);
}
/**
* Log a message for this class
*
* @param string $str Text to log, or omit to return the `$log` API variable.
* @param array $options Optional extras to include:
* - `url` (string): URL to record the with the log entry (default=auto-detect)
* - `name` (string): Name of log to use (default=auto-detect)
* - `user` (User|string|null): User instance, user name, or null to log for current User. (default=null)
* @return WireLog
*
*/
public function ___log($str = '', array $options = array()) {
if(empty($options['name'])) $options['name'] = self::logName;
if($str && $this->lastPageId) $str .= " [page:$this->lastPageId, field:$this->lastFieldName]";
return parent::___log($str, $options);
}
/**
* Module configuration screen
*
* @param array $data
* @return InputfieldWrapper
*
*/
public function getModuleConfigInputfields(array $data) {
$modules = $this->wire()->modules;
$input = $this->wire()->input;
foreach($this->configDefaults as $key => $value) {
if(!isset($data[$key])) $data[$key] = $value;
}
unset($data['_clearCache']);
$inputfields = new InputfieldWrapper();
/** @var InputfieldFieldset $fs */
$fs = $modules->get('InputfieldFieldset');
$fs->attr('name', '_fs_sizes');
$fs->label = $this->_('Sizes');
$fs->description = $this->_('These settings affect the embed code that is used. As a result, if you change these settings you should clear your video cache afterwards (when videos already present).');
$fs->themeOffset = 1;
$inputfields->add($fs);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'maxSize');
$f->label = $this->_('Max video size to request');
$f->columnWidth = 50;
$f->addOption('', $this->_('None'));
foreach($this->getVideoSizes() as $name => $info) {
$f->addOption($name, "$name: $info[label] ($info[width]x$info[height])");
}
$f->val($data['maxSize']);
$fs->add($f);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'aspectRatio');
$f->label = $this->_('Aspect ratio');
foreach($this->getAspectRatios() as $value => $label) {
$f->addOption($value, $label);
}
$f->val($data['aspectRatio']);
$f->columnWidth = 50;
$fs->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'noCookies');
$f->label = $this->_('GDPR: Use the no-cookie / do-not-track version of video URLs');
$f->attr('checked', $this->noCookies ? 'checked' : '');
$f->themeOffset = 1;
$f->notes = $this->_('This setting affects the embed code that is used. As a result, if you change this setting you should clear your video cache afterwards (when videos already present).');
$inputfields->add($f);
/** @var InputfieldRadios $f */
$f = $modules->get('InputfieldRadios');
$f->attr('name', 'failAction');
$f->label = $this->_('Fail action');
$f->description = $this->_('What to do with a video URL that the service returns an error for.');
$f->notes = $this->_('Note that errors and other activity is logged in Setup > Logs > textformatter-video-embed.');
$f->addOption(0, sprintf($this->_('Leave the URL, span-wrap it and append error code i.e. `%s`'), "<span class='TextformatterVideoEmbedError'>https://youtu.be/abc123 (404)</span>"));
$f->addOption(1, sprintf($this->_('Add HTML comment around the video URL to hide it, i.e. `%s`'), "<!--https://youtu.be/abc123 (404)-->"));
if(!$this->failAction) $f->collapsed = Inputfield::collapsedYes;
$f->val((int) $this->failAction);
$f->themeOffset = 1;
$inputfields->add($f);
/** @var InputfieldFieldset $fs */
$fs = $modules->get('InputfieldFieldset');
$fs->attr('name', '_fs_styles');
$fs->label = $this->_('Embed styles');
$fs->description = $this->_('The default styles enable a responsive video presentation and support of different aspect ratios.');
$fs->collapsed = Inputfield::collapsedYes;
$fs->themeOffset = 1;
$inputfields->add($fs);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'wrapStyles');
$f->label = $this->_('Wrap styles');
$f->description = $this->_('Inline styles applied to `div.TextformatterVideoEmbed` element that wraps the video embed `iframe`.');
$f->notes = $this->_('Default:') . ' `' . $this->configDefaults['wrapStyles'] . '`';
$f->val($data['wrapStyles']);
$fs->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'frameStyles');
$f->label = $this->_('Frame styles');
$f->description = $this->_('Inline styles applied to the `iframe` element containing the embedded video.');
$f->notes = $this->_('Default:') . ' `' . $this->configDefaults['frameStyles'] . '`';
$f->val($data['frameStyles']);
$fs->add($f);
/** @var InputfieldFieldset $fs */
$fs = $modules->get('InputfieldFieldset');
$fs->attr('name', '_fs_cache');
$fs->label = $this->_('Cache');
$fs->themeOffset = 1;
$inputfields->add($fs);
/** @var InputfieldInteger $f */
$f = $modules->get('InputfieldInteger');
$f->attr('name', 'refreshDays');
$f->attr('value', isset($data['refreshDays']) ? (int) $data['refreshDays'] : 0);
$f->label = $this->_('Refresh time');
$f->description = $this->_('Refresh video embed codes after this many days, or 0 to cache forever.');
$fs->add($f);
if($input->post('_clearCache')) {
$this->clearAllVideos();
$modules->message(__('Cleared video embed cache'));
$numVideos = 0;
} else {
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', '_clearCache');
$f->attr('value', 1);
$f->label = __('Clear videos?');
$f->description = __('This will clear out cached embed codes. There is no harm in doing this, other than that it will force them to be re-pulled when next accessed.');
$numVideos = $this->getNumVideos();
$f->notes = sprintf(__('There are currently %d video(s) cached'), $numVideos);
if(!$numVideos) $f->collapsed = Inputfield::collapsedYes;
$fs->add($f);
}
/** @var InputfieldMarkup $f */
$f = $modules->get('InputfieldMarkup');
$f->attr('name', '_video_list');
$f->label = $this->_('Recently embedded videos in cache (up to 100)');
$fs->add($f);
if($numVideos > 0) {
/** @var MarkupAdminDataTable $table */
$table = $modules->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);
$videos = $this->getVideos();
$sanitizer = $this->wire()->sanitizer;
foreach($videos as $video) {
if(empty($video['valid'])) continue;
foreach($video as $key => $value) {
$video[$key] = $sanitizer->entities($value);
}
$thumbUrl = $video['thumbnail_url'];
$thumb = $thumbUrl ? "<img width='200' src='$thumbUrl' alt='' />" : " ";
$page = $this->wire()->pages->get((int) $video['page_id']);
$table->headerRow(array(
'Thumb',
'Title',
'Author',
'Source',
'Size',
'Added',
'Page',
'Field',
));
$table->row(array(
"<a target='_blank' href='$video[video_url]'>$thumb</a>",
"<a target='_blank' href='$video[video_url]'>$video[title]</a>",
"<a target='_blank' href='$video[author_url]'>$video[author_name]</a>",
"$video[provider_name]",
"$video[width]x$video[height]",
wireRelativeTimeStr($video['created'], true),
"<a href='$page->url'>" . $page->get('title|name') . "</a>",
"$video[field]",
));
}
$f->value = $table->render();
} else {
$f->value = "<p>" . $this->_('There are currently no videos in the cache.') . "</p>";
}
return $inputfields;
}
/**
* Installation routine
*
*/
public function ___install() {
/** @var WireDatabasePDO $database */
$database = $this->wire('database');
$sql =
"CREATE TABLE " . self::dbTableName . " (" .
"video_id VARCHAR(128) NOT NULL PRIMARY KEY, " .
"embed_code VARCHAR(1024) NOT NULL DEFAULT '', " .
"created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " .
"`data` TEXT, " .
"INDEX created (created) " .
")";
$database->exec($sql);
$this->log('Module installed');
}
/**
* Upgrade
*
* @param $fromVersion
* @param $toVersion
*
*/
public function ___upgrade($fromVersion, $toVersion) {
if($fromVersion || $toVersion) {}
$database = $this->wire()->database;
$table = self::dbTableName;
$query = $database->prepare("SHOW COLUMNS FROM `$table` WHERE Field='data'");
$query->execute();
$numRows = (int) $query->rowCount();
if(!$numRows) {
$this->clearAllVideos();
$query = $database->prepare("ALTER TABLE `$table` ADD `data` TEXT");
$query->execute();
$this->log("Added 'data' column to table: $table");
}
}
/**
* Uninstallation routine
*
*/
public function ___uninstall() {
/** @var WireDatabasePDO $database */
$database = $this->wire('database');
try { $database->exec("DROP TABLE " . self::dbTableName); } catch(\Exception $e) { }
$log = $this->wire()->log;
$logFile = $log->getFilename(self::logName);
if(is_file($logFile)) $log->delete(self::logName);
}
/**
* The following functions are to support the ConfigurableModule interface
* since Textformatter does not originate from WireData
*
* @param string $key
* @param mixed $value
* @return $this
*
*/
public function set($key, $value) {
if($key === 'nocookies') $key = 'noCookies';
$this->data[$key] = $value;
return $this;
}
/**
* Get configuration item
*
* @param string $key
* @return mixed
*
*/
public function get($key) {
$value = $this->wire($key);
if($value) return $value;
return isset($this->data[$key]) ? $this->data[$key] : null;
}