-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-links-embed-services.php
975 lines (811 loc) · 23.1 KB
/
bp-links-embed-services.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
<?php
/**
* BP_Links Embed Service classes
*
* @package BP_Links
* @author Marshall Sorenson
*/
/**
* Video sites by reach:
* YouTube*, Flickr*, MetaCafe*, Hulu (no api), Veoh, Vimeo, ustream.tv, BlinkX, Revver
*
* Photo sites by reach:
* Flickr*, PhotoBucket, PicasaWeb, TwitPic, BuzzNet, twitgoo, imageshack.us?
*
* Just keeping track of thumb sizes here...
* (so far the sweet spot is 100 to 120 pixels)
* ---------------------------------
* YouTube = 120x90 or 130x97 wtf guys??
* MetaCafe = 136x81
* Flickr = 75x75, 100xN, 240xN
* PicasaWeb = tons of options
* http://picasaweb.google.com/data/feed/api/user/joe.geiger/photoid/5410429216279597890?thumbsize=150u&imgmax=512u
*/
/**
* Generic web page embedding service
*
* @package BP_Links
* @author Marshall Sorenson
*/
final class BP_Links_Embed_Service_WebPage
extends BP_Links_Embed_Service
implements BP_Links_Embed_From_Url,
BP_Links_Embed_From_Html,
BP_Links_Embed_Has_Selectable_Image
{
// max number of images to grab from page
const WEBPAGE_MAX_IMAGES = 12;
const WEBPAGE_MAX_IMAGE_HEAD = 24;
const WEBPAGE_MIN_IMAGE_BYTES = 2048;
const WEBPAGE_MAX_IMAGE_BYTES = 51200;
/**
* @var BP_Links_Embed_Page_Parser
*/
private $parser;
//
// required concrete methods
//
final public function from_url( $url )
{
if ( bp_links_is_url_valid( $url ) ) {
$this->data()->url = $url;
$page_parser = BP_Links_Embed_Page_Parser::GetInstance();
if ( $page_parser->from_url( $url ) ) {
$this->parser = $page_parser;
return $this->find_elements();
}
}
return false;
}
final public function from_html( $html )
{
$page_parser = BP_Links_Embed_Page_Parser::GetInstance();
if ( $page_parser->from_html( $html ) ) {
$this->parser = $page_parser;
return $this->find_elements();
}
return false;
}
final public function url()
{
return $this->data()->url;
}
final public function title()
{
return $this->data()->title;
}
final public function description()
{
return $this->data()->description;
}
final public function image_url()
{
if ( isset( $this->data()->images_idx ) ) {
$idx = $this->data()->images_idx;
return $this->data()->images[$idx]['src'];
}
return null;
}
final public function image_thumb_url()
{
return $this->image_url();
}
final public function image_large_thumb_url()
{
return $this->image_url();
}
final public function service_name()
{
return __( 'Web Page', 'buddypress-links' );
}
final public function from_url_pattern()
{
return '/^https?:\/\/([a-z0-9-]+\.)+[a-z0-9-]{2,4}\/?.*/i';
}
final public function image_selection()
{
$image_array = array();
foreach ( $this->data()->images as $image ) {
if ( isset( $image['bytes'] ) && $image['bytes'] >= self::WEBPAGE_MIN_IMAGE_BYTES && $image['bytes'] <= self::WEBPAGE_MAX_IMAGE_BYTES ) {
$image_array[] = $image['src'];
}
}
return $image_array;
}
final public function image_set_selected( $index )
{
// do some sanity checking
if ( is_numeric( $index ) && $index <= self::WEBPAGE_MAX_IMAGES ) {
// cast to integer
$idx = (integer) $index;
// must exist in found images array
if ( array_key_exists( $idx, $this->data()->images ) ) {
// ok, set the index
$this->data()->images_idx = (integer) $idx;
return true;
}
} else {
$this->data()->images_idx = null;
}
return false;
}
final public function image_get_selected()
{
return ( isset( $this->data()->images_idx ) ) ? $this->data()->images_idx : null;
}
//
// private methods
//
private function find_elements()
{
//
// try to get the title
//
$page_title = $this->parser->title();
if ( !empty( $page_title ) ) {
$this->data()->title = $page_title;
} else {
return false;
}
//
// try to get the description
//
$page_desc = $this->parser->description();
if ( !empty( $page_desc ) ) {
$this->data()->description = $page_desc;
} else {
$this->data()->description = null;
}
//
// try to find some images
//
$page_images = $this->parser->images( 100, 800, 2, 50 );
$page_images_sorted = $this->filter_images( $page_images );
$page_images_bytes = $this->get_images_bytes( $page_images_sorted );
if ( is_array( $page_images_bytes ) && count( $page_images_bytes ) ) {
// use the array as is
$this->data()->images = $page_images_bytes;
// set the default index if image selection has at least one entry
if ( count( $this->image_selection() ) ) {
// set index to first key from array
$this->data()->images_idx = key( $page_images_bytes );
} else {
// no images returned by selection method
$this->data()->images_idx = null;
}
} else {
return false;
}
return true;
}
// TODO implement a timeout based on total seconds elapsed
private function get_images_bytes( $images )
{
global $bp;
if ( count( $images ) < 1 ) {
return $images;
}
$return_array = array();
$checked_count = 0;
$good_count = 0;
foreach ( $images as $image ) {
// don't loop forever
$checked_count++;
// bytes are null by default
$image['bytes'] = null;
// run checks only if we are under thresholds
if ( $checked_count < self::WEBPAGE_MAX_IMAGE_HEAD && $good_count < self::WEBPAGE_MAX_IMAGES ) {
// do a head request for the image
$response =
wp_remote_head(
$image['src'],
array( 'timeout' => 2, 'headers' => array( 'Referer' => $bp->root_domain ) )
);
// did we get a valid response?
if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
// yes, grab content length (remote file size)
$content_length = (integer) wp_remote_retrieve_header( $response, 'content-length' );
// did we get the length header?
if ( $content_length > 0 ) {
// set the bytes
$image['bytes'] = $content_length;
// check if size is within range
if ( $content_length >= self::WEBPAGE_MIN_IMAGE_BYTES && $content_length <= self::WEBPAGE_MAX_IMAGE_BYTES ) {
// increment found count
$good_count++;
}
}
}
}
// append modified image details to return array
$return_array[] = $image;
}
return $return_array;
}
private function filter_images( $images )
{
if ( count( $images ) < 1 ) {
return $images;
}
$array_high = array();
$array_med = array();
$array_low = array();
$array_lowest = array();
foreach ( $images as $image ) {
$url = $image['src'];
$width = $image['width'];
$height = $image['height'];
// try to parse url
$url_parsed = parse_url( $url );
if ( isset( $url_parsed['path'] ) ) {
// parsed url successfully
if ( preg_match( '/\.jpe?g/i', $url_parsed['path'] ) ) {
// its a JPEG
if ( $width > 0 && $height > 0 ) {
// width and height were set in DOM
$array_high[] = $image;
} else {
// width and/or height missing from DOM
$array_med[] = $image;
}
} elseif ( preg_match( '/\.png/i', $url_parsed['path'] ) ) {
// PNG, lower priority
$array_low[] = $image;
} elseif ( preg_match( '/\.gif/i', $url_parsed['path'] ) ) {
// GIF, lowest priority
$array_lowest[] = $image;
} else {
// some other image type, we don't want it
continue;
}
} else {
// unable to parse url
continue;
}
}
return array_merge( $array_high, $array_med, $array_low, $array_lowest );
}
}
/**
* YouTube video embedding service
*
* Known URL formats:
* 1) https://www.youtube.com/watch?v=qjzLuddjIUI
* 2) https://youtu.be/qjzLuddjIUI
*
* @package BP_Links
* @author Marshall Sorenson
*/
final class BP_Links_Embed_Service_YouTube
extends BP_Links_Embed_Service
implements BP_Links_Embed_From_Url, BP_Links_Embed_From_Json, BP_Links_Embed_Has_Html
{
// thumb constants
const YT_TH_DEFAULT = 0;
const YT_TH_SMALL_1 = 1;
const YT_TH_SMALL_2 = 2;
const YT_TH_SMALL_3 = 3;
//
// required concrete methods
//
final public function from_url( $url )
{
if ( $this->check_url( $url ) ) {
if ( $this->parse_url( $url ) === true ) {
return $this->from_json( $this->api_oembed_fetch() );
} else {
throw new BP_Links_Embed_User_Exception( $this->err_embed_url() );
}
} else {
return false;
}
}
final public function from_json( $json )
{
// decode the json string
$result = json_decode( $json );
if ( $result instanceof stdClass ) {
// set data items
$this->data()->oembed_title = $this->deep_clean_string( (string) $result->title );
$this->data()->oembed_thumbnail_url = $this->deep_clean_string( (string) $result->thumbnail_url );
$this->data()->oembed_html = $this->deep_clean_string( (string) $result->html );
return true;
} else {
// could not load feed data
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
}
final public function url()
{
// have oembed url?
if ( true === isset( $this->data()->oembed_url ) ) {
// yep use it
return $this->data()->oembed_url;
// back compat
} else {
// return old YT link
return $this->data()->api_link_alt;
}
}
final public function title()
{
// have oembed title?
if ( true === isset( $this->data()->oembed_title ) ) {
// yep, use it
return $this->data()->oembed_title;
// back compat
} else {
// return old YT title
return $this->data()->api_title;
}
}
final public function description()
{
// only old YT api provided description
if ( true === isset( $this->data()->api_content ) ) {
return $this->data()->api_content;
}
}
final public function image_url()
{
return $this->yt_thumb_url();
}
final public function image_thumb_url()
{
return $this->yt_thumb_url( self::YT_TH_SMALL_2 );
}
final public function image_large_thumb_url()
{
return $this->yt_thumb_url( self::YT_TH_DEFAULT );
}
final public function html()
{
// have oembed html?
if ( true === isset( $this->data()->oembed_html ) ) {
// yep, use it
return $this->data()->oembed_html;
} else {
// try to launch old player
return sprintf(
'<object width="640" height="385" style="height: 385px;">' .
'<param name="movie" value="%1$s"></param>' .
'<param name="allowFullScreen" value="true"></param>' .
'<param name="allowscriptaccess" value="always"></param>' .
'<embed src="%1$s" type="application/x-shockwave-flash" ' .
'allowscriptaccess="always" allowfullscreen="true" ' .
'width="640" height="385" style="height: 385px;"></embed>' .
'</object>',
esc_url( $this->yt_player_url() )
);
}
}
public function service_name()
{
return __( 'YouTube', 'buddypress-links' );
}
public function from_url_pattern()
{
return '#^https?:\/\/(www\.)?(youtube\.com\/watch|youtu\.be\/)#';
}
//
// optional concrete methods
//
final public function avatar_play_video()
{
return true;
}
//
// private methods
//
private function check_url( $url )
{
return preg_match( '#^https?:\/\/(www\.)?(youtube\.com\/watch|youtu\.be\/).+$#', $url );
}
private function parse_url( $url )
{
// store url
$this->data()->oembed_url = (string) $url;
// parse the url
$url_parsed = parse_url( $url );
// is it the vanity url?
if (
false === empty( $url_parsed['host'] ) &&
false === empty( $url_parsed['path'] ) &&
'youtu.be' === $url_parsed['host']
) {
// yep, split the path up
$path_elements = array_filter( explode( '/', $url_parsed['path'] ) );
// the first path element is the video hash
$this->data()->video_hash = current( $path_elements );
// success
return true;
} elseif ( false === empty( $url_parsed['query'] ) ) {
// parse the query string
parse_str( $url_parsed['query'], $qs_vars );
// get the video hash
if ( false === empty( $qs_vars['v'] ) ) {
// found it!
$this->data()->video_hash = $qs_vars['v'];
// success
return true;
}
}
// failed to parse url
return false;
}
private function api_oembed_url()
{
return 'http://www.youtube.com/oembed?format=json&url=' . urlencode( $this->data()->oembed_url );
}
private function api_oembed_fetch()
{
// get oembed JSON data for this video
return $this->api_fetch( $this->api_oembed_url() );
}
private function yt_player_url()
{
return sprintf( 'http://www.youtube.com/v/%s&hl=%s&fs=1&&autoplay=1', $this->data()->video_hash, get_locale() );
}
private function yt_thumb_url( $num = self::YT_TH_DEFAULT )
{
// oembed thumbnail set?
if ( true === isset( $this->data()->oembed_thumbnail_url ) ) {
// yes, use it
return $this->data()->oembed_thumbnail_url;
// back compat with old YT data?
} elseif ( is_numeric( $num ) && $num >= self::YT_TH_DEFAULT && $num <= self::YT_TH_SMALL_3 ) {
// return old style image url
return sprintf( 'http://img.youtube.com/vi/%s/%d.jpg', $this->data()->video_hash, $num );
} else {
// fatal
throw new BP_Links_Embed_Fatal_Exception( 'YouTube thumbnail number must 0, 1, 2, or 3.' );
}
}
}
/**
* Flickr photo and video embedding service
*
* @link http://www.flickr.com/services/api/
* @package BP_Links
* @author Marshall Sorenson
*/
final class BP_Links_Embed_Service_Flickr
extends BP_Links_Embed_Service
implements BP_Links_Embed_From_Url, BP_Links_Embed_From_Json, BP_Links_Embed_Has_Html
{
// Flickr API keys
const FLICKR_API_KEY = 'e5fe3652529c0f75332019c3605cd46e';
const FLICKR_API_SECRET = '7600876afd78c7a2';
// Flickr media types
const FLICKR_MEDIA_PHOTO = 'photo';
const FLICKR_MEDIA_VIDEO = 'video';
// Flickr image sizes
const FLICKR_IMAGE_SQUARE = 's'; // 75 x 75
const FLICKR_IMAGE_THUMB = 't'; // 100 x N
const FLICKR_IMAGE_SMALL = 'm'; // 240 x N
const FLICKR_IMAGE_MEDIUM = null; // 500 x N
const FLICKR_IMAGE_LARGE = 'b'; // 1024 x N
//
// required concrete methods
//
final public function from_url( $url )
{
if ( $this->check_url( $url ) ) {
if ( $this->parse_url( $url ) === true ) {
return $this->from_json( $this->api_json_fetch( 'flickr.photos.getInfo' ) );
} else {
throw new BP_Links_Embed_User_Exception( $this->err_embed_url() );
}
} else {
return false;
}
}
final public function from_json( $json )
{
// decode json data
$api_data = json_decode( $json, true );
// if decoding successful, add details to embed data
if ( !empty( $api_data[self::FLICKR_MEDIA_PHOTO] ) ) {
// photo array
$photo = $api_data[self::FLICKR_MEDIA_PHOTO];
// copy values
$this->data()->api_id = $photo['id'];
$this->data()->api_secret = $photo['secret'];
$this->data()->api_server = $photo['server'];
$this->data()->api_farm = $photo['farm'];
$this->data()->api_license = $photo['license'];
$this->data()->api_title = $this->deep_clean_string( $photo['title']['_content'] );
$this->data()->api_description = $this->deep_clean_string( $photo['description']['_content'] );
// try for media type
switch ( $photo['media'] ) {
case self::FLICKR_MEDIA_PHOTO:
$this->data()->api_media = self::FLICKR_MEDIA_PHOTO;
break;
case self::FLICKR_MEDIA_VIDEO:
$this->data()->api_media = self::FLICKR_MEDIA_VIDEO;
break;
default:
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
// try for the url
if ( !empty( $photo['urls']['url'][0]['type'] ) && $photo['urls']['url'][0]['type'] == 'photopage' ) {
$this->data()->api_url = $this->deep_clean_string( $photo['urls']['url'][0]['_content'] );
}
// make sure we REALLY have a good url
if ( $this->parse_url( $this->data()->api_url ) !== true ) {
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
// made it!
return true;
} else {
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
}
final public function url()
{
return $this->data()->api_url;
}
final public function title()
{
return $this->data()->api_title;
}
final public function description()
{
return $this->data()->api_description;
}
final public function image_url()
{
return $this->flickr_image( self::FLICKR_IMAGE_MEDIUM );
}
final public function image_thumb_url()
{
return $this->flickr_image( self::FLICKR_IMAGE_THUMB );
}
final public function image_large_thumb_url()
{
return $this->flickr_image( self::FLICKR_IMAGE_SMALL );
}
final public function html()
{
switch ( $this->data()->api_media ) {
case self::FLICKR_MEDIA_PHOTO:
return $this->html_photo();
case self::FLICKR_MEDIA_VIDEO:
return $this->html_video();
default:
// this should never happen!
return null;
}
}
final public function service_name()
{
return __( 'Flickr', 'buddypress-links' );
}
final public function from_url_pattern()
{
return '/^https?:\/\/(www\.)?flickr\.com\/photos\/[^\/]+\/\d+\//';
}
//
// optional concrete methods
//
final public function avatar_play_photo()
{
return ( self::FLICKR_MEDIA_PHOTO == $this->data()->api_media );
}
final public function avatar_play_video()
{
return ( self::FLICKR_MEDIA_VIDEO == $this->data()->api_media );
}
//
// private methods
//
private function check_url( $url )
{
return preg_match( $this->from_url_pattern(), $url );
}
private function parse_url( $url )
{
// parse the url
$url_parsed = parse_url( $url );
// make sure we got something
if ( !empty( $url_parsed['path'] ) ) {
// get the video id
if ( preg_match( '/^\/photos\/[^\/]+\/(\d+)\//', $url_parsed['path'], $matches ) ) {
// must save this as a string, as its too long to be an integer
$this->data()->photo_id = (string) $matches[1];
return true;
}
}
return false;
}
private function flickr_image( $size = self::FLICKR_IMAGE_MEDIUM )
{
$suffix = ( $size ) ? '_' . $size : null;
return sprintf(
'http://farm%1$s.static.flickr.com/%2$s/%3$s_%4$s%5$s.jpg',
$this->data()->api_farm, // arg 1
$this->data()->api_server, // arg 2
$this->data()->api_id, // arg 3
$this->data()->api_secret, // arg 4
$suffix // arg 5
);
}
private function html_photo()
{
return sprintf(
'<img src="%1$s" alt="%2$s">',
esc_url( $this->image_url() ),
esc_attr( $this->data()->api_title )
);
}
private function html_video()
{
return sprintf(
'<object type="application/x-shockwave-flash" width="500" height="281"
data="https://www.flickr.com/apps/video/stewart.swf?v=1535363810"
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
<param name="flashvars" value="intl_lang=en-us&photo_secret=%2$s&photo_id=%1$s&flickr_show_info_box=true"></param>
<param name="movie" value="https://www.flickr.com/apps/video/stewart.swf?v=1535363810"></param>
<param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param>
<embed type="application/x-shockwave-flash"
src="https://www.flickr.com/apps/video/stewart.swf?v=1535363810"
bgcolor="#000000" allowfullscreen="true"
flashvars="intl_lang=en-us&photo_secret=%2$s&photo_id=%1$s&flickr_show_info_box=true"
height="281" width="500">
</embed>
</object>',
esc_attr( $this->data()->api_id ), // arg 1
esc_attr( $this->data()->api_secret ) // arg 2
);
}
private function api_rest_url( $method, $format = 'rest' )
{
return sprintf( 'https://api.flickr.com/services/rest/?method=%1$s&photo_id=%2$s&format=%3$s&api_key=%4$s&nojsoncallback=1', $method, $this->data()->photo_id, $format, self::FLICKR_API_KEY );
}
private function api_json_fetch( $method )
{
// get RSS2 feed data for this video
return $this->api_fetch( $this->api_rest_url( $method, 'json' ) );
}
}
/**
* MetaCafe video embedding service
*
* Fetching new content has been disabled as of 0.9.4 since the API is more or less defunct now.
*
* @link http://help.metacafe.com/?page_id=181
* @package BP_Links
* @author Marshall Sorenson
*/
final class BP_Links_Embed_Service_MetaCafe
extends BP_Links_Embed_Service
implements BP_Links_Embed_From_Url, BP_Links_Embed_From_Xml, BP_Links_Embed_Has_Html
{
//
// required concrete methods
//
final public function from_url( $url )
{
// don't try to fetch content anymore
return false;
}
final public function from_xml( $xml )
{
// load xml string into a SimpleXML object
libxml_use_internal_errors(true);
$sxml = simplexml_load_string( $xml );
if ( $sxml instanceof SimpleXMLElement ) {
// get nodes in media: namespace for media information
$media = $sxml->channel->item->children('http://search.yahoo.com/mrss/');
// do we have media namespace to look at?
if ( $media instanceof SimpleXMLElement ) {
// set title and content
$this->data()->api_title = $this->deep_clean_string( (string) $media->title );
$this->data()->api_description = $this->deep_clean_string( (string) $media->description );
$cont_attrs = $media->content->attributes();
$this->data()->api_content_url = $this->deep_clean_string( (string) $cont_attrs['url'] );
} else {
return false;
}
// set alternate link
$this->data()->api_link_alt = (string) $sxml->channel->item->link;
// make sure we have an alternate link
if ( empty( $this->data()->api_link_alt ) === false ) {
// set video id if missing
if ( empty( $this->data()->video_id ) ) {
$this->parse_url( $this->data()->api_link_alt );
}
} else {
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
return true;
} else {
// could not load feed data
throw new BP_Links_Embed_User_Exception( $this->err_api_fetch() );
}
}
final public function url()
{
return $this->data()->api_link_alt;
}
final public function title()
{
return $this->data()->api_title;
}
final public function description()
{
return $this->data()->api_description;
}
final public function image_url()
{
return $this->image_thumb_url();
}
final public function image_thumb_url()
{
return sprintf( 'http://www.metacafe.com/thumb/%s.jpg', $this->data()->video_id );
}
final public function image_large_thumb_url()
{
return $this->image_thumb_url();
}
final public function html()
{
return sprintf(
'<embed src="%1$s"
width="498" height="423" wmode="transparent"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" allowFullScreen="true"
allowScriptAccess="always" name="Metacafe_%2$s">
</embed>',
esc_url( $this->data()->api_content_url ),
$this->data()->video_id
);
}
public function service_name()
{
return __( 'MetaCafe', 'buddypress-links' );
}
public function from_url_pattern()
{
return '/^https?:\/\/(www\.)?metacafe.com\/watch\//';
}
//
// optional concrete methods
//
final public function avatar_play_video()
{
return true;
}
//
// private methods
//
private function check_url( $url )
{
return preg_match( $this->from_url_pattern(), $url );
}
private function parse_url( $url )
{
// parse the url
$url_parsed = parse_url( $url );
// make sure we got something
if ( !empty( $url_parsed['path'] ) ) {
// get the video id
if ( preg_match( '/^\/watch\/(\d+)\//', $url_parsed['path'], $matches ) ) {
// save this as a string in case its a huge integer!
$this->data()->video_id = (string) $matches[1];
return true;
}
}
return false;
}
private function api_xml_url()
{
return sprintf( 'http://www.metacafe.com/api/item/%s', $this->data()->video_id );
}
private function api_xml_fetch()
{
// get RSS2 feed data for this video
return $this->api_fetch( $this->api_xml_url() );
}
}
?>