-
Notifications
You must be signed in to change notification settings - Fork 2
/
external-videos.php
740 lines (617 loc) · 21.4 KB
/
external-videos.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
<?php
/*
* Plugin Name: External Videos
* Plugin URI: http://wordpress.org/extend/plugins/external-videos/
* Description: Automatically syncs your videos from YouTube, Vimeo, Wistia or Dailymotion to your WordPress site as new posts.
* Author: Silvia Pfeiffer and Andrew Nimmo
* Version: 2.0.1
* Author URI: http://www.gingertech.net/
* License: GPL2
* Text Domain: external-videos
* Domain Path: /localization
*/
/*
Copyright 2010+ Silvia Pfeiffer (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@package external-videos
@author Silvia Pfeiffer <[email protected]>, Andrew Nimmo <[email protected]>
@copyright Copyright 2010+ Silvia Pfeiffer
@license http://www.gnu.org/licenses/gpl.txt GPL 2.0
@version 2.0.0
@link http://wordpress.org/extend/plugins/external-videos/
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if( ! class_exists( 'SP_External_Videos' ) ) :
class SP_External_Videos {
public function __construct() {
$this->constants();
require_once( ABSPATH . 'wp-admin/includes/taxonomy.php' );
require( plugin_dir_path( __FILE__ ) . 'core/ev-admin.php' );
require( plugin_dir_path( __FILE__ ) . 'core/ev-sync-posts.php' );
require( plugin_dir_path( __FILE__ ) . 'core/ev-helpers.php' );
require( plugin_dir_path( __FILE__ ) . 'core/ev-widget.php' );
require( plugin_dir_path( __FILE__ ) . 'core/ev-media-gallery.php' );
require( plugin_dir_path( __FILE__ ) . 'core/simple_html_dom.php' );
foreach( glob( plugin_dir_path( __FILE__ ) . '/hosts/*/ev-*.php' ) as $host ) {
require $host;
}
require_once( plugin_dir_path( __FILE__ ) . 'mexp/media-explorer.php' );
// includes do not bring methods into the class! they're standalone functions
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
register_activation_hook( __FILE__, array( $this, 'rewrite_flush' ) );
add_action( 'init', array( $this, 'initialize' ) );
add_action( 'admin_menu', array( $this, 'admin_settings' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
/// *** Setup of Videos Gallery: implemented in ev-media-gallery.php *** ///
add_shortcode( 'external-videos', array( $this, 'gallery' ) );
/// *** Setup of Widget: implemented in ev-widget.php file *** ///
add_action( 'widgets_init', array( $this, 'load_widget' ) );
add_filter( 'pre_get_posts', array( $this, 'filter_query_post_type' ) );
add_filter( 'pre_get_posts', array( $this, 'add_to_main_query' ) );
add_filter( 'request', array( $this, 'feed_request' ) );
}
// https://wordpress.stackexchange.com/questions/18268/i-want-to-get-a-plugin-version-number-dynamically
function constants() {
if (!defined('EXTERNAL_VIDEOS_VERSION')) {
$plugin_data = get_file_data( __FILE__,
array('Version' => 'Version'),
false );
define('EXTERNAL_VIDEOS_VERSION', $plugin_data['Version']);
}
}
/*
* initialize
*
* actions that need to go on the init hook
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param
* @return
*/
function initialize() {
$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( 'external-videos', false, $plugin_dir . '/localization/' );
// create a "video" category to store posts against
wp_create_category(__( 'External Videos', 'external-videos' ) );
// load the saved options or initialize with default values
$options = $this->get_options();
// echo '<pre style="margin-left:150px;">$options: '; print_r($options); echo '</pre>';
// create "external videos" post type
register_post_type( 'external-videos', array(
'label' => __( 'External Videos', 'external-videos' ),
'singular_label' => __( 'External Video', 'external-videos' ),
'description' => __( 'Pulls in videos from external hosting sites', 'external-videos' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 42,
'menu_icon' => 'dashicons-embed-video',
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
'taxonomies' => array( 'post_tag', 'category' ),
'has_archive' => true,
'rewrite' => array( 'slug' => $options['slug'] ),
'yarpp_support' => true
));
// enable thickbox use for gallery
wp_enqueue_style( 'thickbox' );
wp_enqueue_script( 'thickbox' );
// error_log( print_r( get_post_types(), true ));
}
/*
* admin_settings
*
* Settings page
* Add the options page for External Videos Settings
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param
* @return
*/
function admin_settings() {
add_options_page(
__( 'External Videos Settings', 'external-videos' ),
__( 'External Videos', 'external-videos' ),
'edit_posts',
__FILE__,
array( $this, 'settings_page' )
);
}
/*
* settings_page
*
* Used by admin_settings()
* This separate callback function creates the settings page html
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param
* @return
*/
function settings_page() {
// activate cron hook if not active
$this->activation();
// The form HTML
include( plugin_dir_path( __FILE__ ) . 'core/ev-settings-forms.php' );
}
/*
* admin_scripts
*
* Settings page
* Script necessary for presenting proper form options per host
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param $hook
* @return
*/
function admin_scripts( $hook ) {
if( "settings_page_external-videos/external-videos" != $hook ) return;
wp_register_style( 'ev-admin', plugin_dir_url( __FILE__ ) . '/css/ev-admin.css', array(), null, 'all' );
wp_enqueue_style( 'ev-admin' );
wp_register_script( 'ev-admin', plugin_dir_url( __FILE__ ) . '/js/ev-admin.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'ev-admin' );
// for the nonce
$settings_nonce = wp_create_nonce( 'ev_settings' );
// Make these variables an object array for the jquery later
wp_localize_script( 'ev-admin', 'evSettings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => $settings_nonce
) );
}
/*
* get_options
*
* Gets sp_external_videos_options, returns usable array $options
* which is stored into the SP_External_Videos class
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param
* @return
*/
static function get_options(){
// Load existing plugin options
$options = get_option( 'sp_external_videos_options' );
// echo '<pre>$raw_options: '; print_r($raw_options); echo '</pre>';
if( !$options ) {
$options = array();
}
// Upgrade options to v1.0: move author options to hosts
$options = SP_External_Videos::upgrade_options( $options );
// Set defaults for the basic options
// There are new options that may not be set yet
if( !array_key_exists( 'version', $options ) ) $options['version'] = 1;
if( !array_key_exists( 'rss', $options ) ) $options['rss'] = false;
if( !array_key_exists( 'delete', $options ) ) $options['delete'] = false;
if( !array_key_exists( 'hosts', $options ) ) $options['hosts'] = array();
if( !array_key_exists( 'embed', $options ) ) $options['embed'] = true;
if( !array_key_exists( 'title_sync', $options ) )
$options['title_sync'] = false;
if( !array_key_exists( 'content_sync', $options ) )
$options['content_sync'] = false;
if( !array_key_exists( 'attrib', $options ) ) $options['attrib'] = false;
if( !array_key_exists( 'loop', $options ) ) $options['loop'] = false;
if( !array_key_exists( 'slug', $options ) )
$options['slug'] = 'external-videos';
// echo '<pre style="margin-left:150px;">$options: '; print_r($options); echo '</pre>';
return $options;
}
/*
* upgrade_options
*
* new function to update options
*
* @type function
* @date 25/07/23
* @since 2.0.1
*
* remove dotsub
*
* convert_author_options was for an update of the plugin to version 1.0.
* Move authors array under respective hosts for much easier indexing.
* This is a database conversion function that is light and runs automatically
* but is really needed once only. Could be moved to settings-page AJAX
* to be explicitly run by user only if author options are detected.
*
* @type function
* @date 31/10/16
* @since 1.0
*
* @param
* @return
*/
static function upgrade_options( $options ){
// return if there are no hosts yet
if( !array_key_exists( 'hosts', $options ) ) return $options;
// error_log(print_r( $options['hosts'], true ));
// return if we've already converted the data
if( !array_key_exists( 'authors', $options ) ) return $options;
// nest each author under the respective host in the $options array
$AUTHORS = $options['authors'];
foreach( $AUTHORS as $author ){
$host_id = $author['host_id'];
$author_id = $author['author_id'];
$options['hosts'][$host_id]['authors'][$author_id] = $author;
}
// remove the old "authors" options, now nested under hosts
unset( $options['authors'] );
// remove host dotsub, now defunct
unset( $options['hosts']['dotsub'] );
update_option( 'sp_external_videos_options', $options );
return $options;
}
/*
* filter_query_post_type
*
* add external-video posts to query on Category and Tag archive pages
* FIX by Chris Jean, [email protected]
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param $query
* @return
*/
function filter_query_post_type( $query ) {
if ( ( isset( $query->query_vars['suppress_filters'] ) && $query->query_vars['suppress_filters'] ) || ( ! is_category() && ! is_tag() && ! is_author() ) )
return $query;
$post_type = get_query_var( 'post_type' );
if ( 'any' == $post_type )
return $query;
if ( empty( $post_type ) ) {
$post_type = 'any';
}
else {
if ( ! is_array( $post_type ) )
$post_type = array( $post_type );
$post_type[] = 'external-videos';
}
$query->set( 'post_type', $post_type );
return $query;
}
/*
* add_to_main_query
*
* add external-video posts to Home page (Latest posts) query
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param $query
* @return
*/
function add_to_main_query( $query ) {
$options = $this->get_options();
if( $options['loop'] == false ) return;
if( is_home() ) {
$post_type = get_query_var( 'post_type' );
// Default index page has no post_type set
if( !isset( $post_type ) || !is_array( $post_type ) ) {
$post_type = array( 'post' );
}
if( !in_array( 'external-videos', $post_type ) ) $post_type[] = 'external-videos';
set_query_var ( 'post_type', $post_type );
}
}
/*
* gallery
*
* Sets up a shortcode for the videos.
* [external-videos link="page/overlay"]
* - provides a gallery with links to video pages or overlays
* [external-videos feature="embed" width="600" height="360"]
* - provides the embed code
* for new newest video to feature as an embed *
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param $atts, $content
* @return shortcode HTML
*/
function gallery( $atts, $content = null ) {
global $wp_query, $post;
// handles [external-videos ...] shortcode
// extract shortcode parameters
// the "feature" parameter should allow for {thumbnail, embed}
// as a choice between an embedded video and a thumbnail with overlay
// - does embed only right now
extract( shortcode_atts( array(
'link' => 'overlay',
'feature' => '',
'width' => '600',
'height' => '360',
), $atts));
// start output buffer collection
ob_start();
// depending on shortcode parameters, do different things
if ( $feature == 'embed' ) {
$params = array(
'posts_per_page' => 1,
'post_type' => 'external-videos',
'post_status' => 'publish',
);
query_posts( $params );
if ( have_posts() ) {
// extract the video for the feature
the_post();
$embed_url = get_post_meta( get_the_ID(), 'embed_url' );
$video = trim( $embed_url[0] );
// get oEmbed code
$oembed = new WP_Embed();
$html = $oembed->shortcode( null, $video );
// replace width with 600, height with 360
$html = preg_replace ( '/width="\d+"/', 'width="'.$width.'"', $html );
$html = preg_replace ( '/height="\d+"/', 'height="'.$height.'"', $html );
// just print the embed code for the newest video
echo $html;
}
} else {
// extract the videos for the gallery
$params = array(
'posts_per_page' => 20,
'post_type' => 'external-videos',
'post_status' => 'publish',
);
$params['paged'] = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
query_posts( $params );
// display the gallery
$this->display_gallery( $width, $height, $link );
}
//Reset Query
wp_reset_query();
$result = ob_get_clean();
return $result;
}
/*
* display_gallery
*
* HTML for the shortcode.
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param $width, $height, $link
* @return HTML
*/
function display_gallery ( $width, $height, $link ) {
global $wp_query, $post, $features_3_0;
// if ( $wp_query->max_num_pages > 1 ) : ?>
<!-- see http://core.trac.wordpress.org/ticket/6453 -->
<script type="text/javascript">
//<![CDATA[
var tb_pathToImage = "wp-includes/js/thickbox/loadingAnimation.gif";
var tb_closeImage = "wp-includes/js/thickbox/tb-close.png";
//]]>
</script>
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older videos', 'external-videos' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer videos <span class="meta-nav">→</span>', 'external-videos' ) ); ?></div>
</div><!-- #nav-above -->
<?php // endif; ?>
<div class="gallerycontainer" style="clear:all;">
<?php
while ( have_posts() ) {
the_post();
$thumbnail = esc_url( get_post_meta( get_the_ID(), 'thumbnail_url' ) );
$thumb = $thumbnail[0];
$embed_url = esc_url( get_post_meta( get_the_ID(), 'embed_url' ) );
$video = trim( $embed_url[0] );
$description = esc_attr( get_post_meta( get_the_ID(), 'description' ) );
$desc = $description[0];
$short_title = SP_EV_Helpers::shorten_text( get_the_title(), 33 );
$thickbox_title = SP_EV_Helpers::shorten_text( get_the_title(), 90 );
// get oEmbed code
$oembed = new WP_Embed();
$html = $oembed->shortcode( null, $video );
// replace width with 600, height with 360
$html = preg_replace ( '/width="\d+"/', 'width="' . $width . '"', $html );
$html = preg_replace ( '/height="\d+"/', 'height="' . $height . '"', $html );
?>
<div style="margin:2px; height:auto; width:auto; float:left;">
<?php
// display overlay if requested
if ($link == "page") {
?>
<a href="<?php the_permalink() ?>"
title="<?php echo $thickbox_title ?>">
<div style="display:box; width:120px; height:90px;">
<img title="<?php the_title() ?>" src="<?php echo $thumb ?>"
style="display:inline; margin:0; border:1px solid black; width:120px; height:90px"/>
</div>
</a>
<div style="width:120px; height: 12px; margin-bottom:7px; line-height: 90%">
<small><i><?php echo get_the_time('F j, Y') ?></i></small>
</div>
<div style="width:120px; height: 30px; margin-bottom:20px; line-height: 80%">
<small><?php echo $short_title ?></small>
</div>
<?php
// display overlay if requested
} else {
?>
<a href="#TB_inline?height=500&width=700&inlineId=hiddenModalContent_<?php the_ID() ?>"
title="<?php echo $thickbox_title ?>" class="thickbox">
<div style="display:box; width:120px; height:90px;">
<img title="<?php the_title() ?>" src="<?php echo $thumb ?>"
style="display:inline; margin:0; border:1px solid black; width:120px; height:90px"/>
</div>
</a>
<div style="width:120px; height: 12px; margin-bottom:7px; line-height: 90%">
<small><i><?php echo get_the_time('F j, Y') ?></i></small>
</div>
<div style="width:120px; height: 30px; margin-bottom:20px; line-height: 80%">
<small><?php echo $short_title ?></small>
</div>
<!-- Hidden content for the thickbox -->
<div id="hiddenModalContent_<?php echo $post->ID ?>" style="display:none;">
<p align="center" style="margin-bottom:10px;">
<?php echo $html ?>
</p>
<div style="margin-bottom:10px;">
<?php
if ( $post->post_parent > 0 ) {
?>
<a href="<?php echo get_permalink( $post->post_parent ) ?>"><?php _e( 'Blog post related to this video', 'external-videos' ); ?></a>
<?php
}
?>
<br/>
<?php
if ( $features_3_0 ) {
?>
<a href="<?php the_permalink(); ?>"><?php _e( 'Video page', 'external-videos' ); ?></a>
<?php
}
?>
</div>
<div style="margin-bottom:10px;">
<?php echo $desc ?>
</div>
<div style="text-align: center;">
<input type="submit" id="Login" value="OK" onclick="tb_remove()"/>
</div>
</div>
<?php
}
?>
</div>
<?php
}
?>
<div style="clear: both;"></div>
</div>
<?php // if ( $wp_the_query->max_num_pages > 1 ) : ?>
<br/>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older videos', 'external-videos' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer videos <span class="meta-nav">→</span>', 'external-videos' ) ); ?></div>
</div><!-- #nav-below -->
<?php // endif; ?>
<?php
}
/*
* feed_request
*
* add external-video posts to RSS feed
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param $qv
* @return $qv
*/
function feed_request( $qv ) {
$options = $this->get_options();
if ( $options['rss'] == true ) {
if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) )
$qv['post_type'] = array( 'external-videos', 'post' );
}
return $qv;
}
/*
* load_widget
*
* load the widget defined in ev-widget.php
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param
* @return
*/
function load_widget() {
return register_widget( 'WP_Widget_SP_External_Videos' );
}
/*
* activation
*
* register daily cron on plugin activation
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param
* @return
*/
static function activation() {
if ( !wp_next_scheduled( 'ev_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'ev_daily_event' );
}
}
/*
* activation
*
* unregister daily cron on plugin deactivation
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param
* @return
*/
function deactivation() {
wp_clear_scheduled_hook( 'ev_daily_event' );
}
/*
* rewrite_flush
*
* Flush rewrite on plugin activation
*
* @type function
* @date 31/10/16
* @since 0.26
*
* @param
* @return
*/
function rewrite_flush() {
// First, we "add" the custom post type via the initialize function.
$this->initialize();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
} // end class
endif;
/*
* Launch the whole plugin
*/
global $SP_External_Videos;
$SP_External_Videos = new SP_External_Videos();
?>