forked from siteorigin/focus-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus-videos.php
149 lines (134 loc) · 5.57 KB
/
focus-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
<?php
/*
Plugin Name: Focus Video Plugin
Description: A plugin for setting the video in Focus theme.
Version: 1.0
Author: SiteOrigin
Author URI: http://siteorigin.com
License: GPL 3.0
License URI: license.txt
*/
/**
* Enqueue the video scripts.
*
* @param $prefix
*/
function siteorigin_focus_admin_scripts($prefix){
$screen = get_current_screen();
if($prefix == 'post.php' || $prefix == 'post-new.php' && $screen->id == 'post'){
wp_enqueue_script('focus-admin-video', plugin_dir_url(__FILE__).'admin.video.js', array('jquery'), '1.0');
wp_localize_script('focus-admin-video', 'focusVideoSettings', array(
'button' => __('Set Video', 'focus')
));
}
}
add_action('admin_enqueue_scripts', 'siteorigin_focus_admin_scripts');
/**
* Set which post types we'll display the premium teaser on.
*/
function siteorigin_focus_admin_init(){
if(function_exists('siteorigin_premium_teaser_post_types')){
siteorigin_premium_teaser_post_types('post');
}
}
add_action('admin_init', 'siteorigin_focus_admin_init');
/**
* Add the video metabox.
*/
function siteorigin_focus_add_metabox(){
add_meta_box('focus-video', __('Video', 'focus'), 'siteorigin_focus_video_metabox_render', 'post');
}
add_action('add_meta_boxes', 'siteorigin_focus_add_metabox');
/**
* Render the video metabox.
*/
function siteorigin_focus_video_metabox_render(){
siteorigin_focus_video_field('public', __('Public', 'focus'));
if(function_exists('siteorigin_premium_call_function')) {
siteorigin_premium_call_function(
'siteorigin_focus_video_field',
array('premium', __('Premium', 'focus')),
array(
'before' => '<h4>'.__('Premium Video', 'focus').'</h4>',
'description' => __('Allows you to set a premium version of a video, and only show that video to paying members of your site using membership plugins like S2Member', 'focus')
)
);
}
do_action('focus_admin_after_video_metabox');
wp_nonce_field('save', '_focusnonce');
}
/**
* Render a single video field.
*
* @param $type
* @param $title
*/
function siteorigin_focus_video_field($type, $title){
global $post;
$video = get_post_meta($post->ID, 'focus_video', true);
$video = !empty($video[$type]) ? $video[$type] : array();
$video = wp_parse_args($video, array(
'type' => '',
'external' => '',
'self' => '',
'remote' => '',
'custom' => '',
));
$self = !empty($video['self']) ? get_post($video['self']) : null;
?>
<h4><?php printf(__('%s Video', 'focus'), $title) ?></h4>
<table class="form-table focus-video-table">
<tbody>
<tr>
<th scope="row" valign="top"><?php _e('Video Type', 'focus') ?></th>
<td>
<select name="focus_video[<?php echo esc_attr($type) ?>][type]" class="focus-video-type-select">
<option value="external" <?php selected('external', $video['type']) ?>><?php esc_html_e('External (YouTube, Vimeo, etc)', 'focus') ?></option>
<option value="self" <?php selected('self', $video['type']) ?>><?php esc_html_e('Self Hosted', 'focus') ?></option>
<option value="remote" <?php selected('remote', $video['type']) ?>><?php esc_html_e('Remote Video File', 'focus') ?></option>
<option value="custom" <?php selected('custom', $video['type']) ?>><?php esc_html_e('Custom Embed Code', 'focus') ?></option>
</select>
</td>
</tr>
<tr class="field-<?php echo esc_attr($type) ?>-external field-external field">
<th scope="row" valign="top"><?php _e('External Video URL', 'focus') ?></th>
<td><input type="text" name="focus_video[<?php echo esc_attr($type) ?>][external]" class="regular-text" value="<?php echo esc_attr($video['external']) ?>" /></td>
</tr>
<tr class="field-<?php echo esc_attr($type) ?>-self field-self field">
<th scope="row" valign="top"><?php _e('Self Hosted Video', 'focus') ?></th>
<td class="wp-media-buttons">
<strong style="margin-right: 10px" class="video-name"><?php if(!empty($self)) echo $self->post_title ?></strong>
<a href="#" class="button add_media focus-add-video" data-video-type="<?php echo esc_attr($type) ?>" title="Add Media" data-choose="<?php esc_attr_e('Select Video File', 'focus') ?>" data-update="<?php esc_attr_e('Set Video', 'focus') ?>">
<span class="wp-media-buttons-icon"></span> <?php printf(__('Add %s Video', 'focus'), $title) ?>
</a>
<a href="#" class="focus-remove-video"><?php printf(__('Remove %s Video', 'focus'), $title) ?></a>
<input type="hidden" name="focus_video[<?php echo esc_attr($type) ?>][self]" class="regular-text field-video-self" value="<?php if(!empty($self)) echo $self->ID ?>" />
</td>
</tr>
<tr class="field-<?php echo esc_attr($type) ?>-remote field-remote field">
<th scope="row" valign="top"><?php _e('MP4 File URL', 'focus') ?></th>
<td><input type="text" name="focus_video[<?php echo esc_attr($type) ?>][remote]" class="regular-text" value="<?php echo esc_attr($video['remote']) ?>" /></td>
</tr>
<tr class="field-<?php echo esc_attr($type) ?>-custom field-custom field">
<th scope="row" valign="top"><?php _e('Custom Embed Code', 'focus') ?></th>
<td>
<textarea name="focus_video[<?php echo esc_attr($type) ?>][custom]" class="widefat"><?php echo esc_textarea($video['custom']) ?></textarea>
</td>
</tr>
</tbody>
</table>
<?php
}
/**
* Save the post videos.
*
* @param $post_id
*/
function siteorigin_focus_video_save($post_id){
if(empty($_POST['_focusnonce']) || !wp_verify_nonce($_POST['_focusnonce'], 'save')) return;
if(!current_user_can('edit_post', $post_id)) return;
if(defined('DOING_AUTOSAVE')) return;
$video = array_map('stripslashes_deep', $_POST['focus_video']);
update_post_meta($post_id, 'focus_video', $video);
}
add_action('save_post', 'siteorigin_focus_video_save');