summaryrefslogtreecommitdiff
blob: aced7c897736f69864eea32e74eeaf91107dc9f3 (plain)
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
<?php
/**
 * VideoPress admin media-view functions.
 *
 * @package automattic/jetpack
 */

use Automattic\Jetpack\Assets;

/**
 * WordPress Shortcode Editor View JS Code
 */
function videopress_handle_editor_view_js() {
	global $content_width;
	$current_screen = get_current_screen();
	if ( ! isset( $current_screen->id ) || $current_screen->base !== 'post' ) {
		return;
	}

	add_action( 'admin_print_footer_scripts', 'videopress_editor_view_js_templates' );

	wp_enqueue_style(
		'videopress-editor-ui',
		plugins_url( 'css/editor.css', __FILE__ ),
		array(),
		Jetpack_VideoPress::VERSION
	);
	wp_enqueue_script(
		'videopress-editor-view',
		Assets::get_file_url_for_environment(
			'_inc/build/videopress/js/editor-view.min.js',
			'modules/videopress/js/editor-view.js'
		),
		array( 'wp-util', 'jquery' ),
		Jetpack_VideoPress::VERSION,
		true
	);
	wp_localize_script(
		'videopress-editor-view',
		'vpEditorView',
		array(
			'home_url_host'     => wp_parse_url( home_url(), PHP_URL_HOST ),
			'min_content_width' => VIDEOPRESS_MIN_WIDTH,
			'content_width'     => $content_width,
			'modal_labels'      => array(
				'title'     => esc_html__( 'VideoPress Shortcode', 'jetpack' ),
				'guid'      => esc_html__( 'Video ID', 'jetpack' ),
				'w'         => esc_html__( 'Video Width', 'jetpack' ),
				'w_unit'    => esc_html__( 'pixels', 'jetpack' ),
				/* Translators: example of usage of this is "Start Video After 10 seconds" */
				'at'        => esc_html__( 'Start Video After', 'jetpack' ),
				'at_unit'   => esc_html__( 'seconds', 'jetpack' ),
				'hd'        => esc_html__( 'High definition on by default', 'jetpack' ),
				'permalink' => esc_html__( 'Link the video title to its URL on VideoPress.com', 'jetpack' ),
				'autoplay'  => esc_html__( 'Autoplay video on page load', 'jetpack' ),
				'loop'      => esc_html__( 'Loop video playback', 'jetpack' ),
				'freedom'   => esc_html__( 'Use only Open Source codecs (may degrade performance)', 'jetpack' ),
				'flashonly' => esc_html__( 'Use legacy Flash Player (not recommended)', 'jetpack' ),
			),
		)
	);

	add_editor_style( plugins_url( 'css/videopress-editor-style.css', __FILE__ ) );
}
add_action( 'admin_notices', 'videopress_handle_editor_view_js' );

/**
 * WordPress Editor Views
 */
function videopress_editor_view_js_templates() {
	/**
	 * This template uses the following parameters, and displays the video as an iframe:
	 *  - data.guid     // The guid of the video.
	 *  - data.width    // The width of the iframe.
	 *  - data.height   // The height of the iframe.
	 *  - data.urlargs  // Arguments serialized into a get string.
	 *
	 * In addition, the calling script will need to ensure that the following
	 * JS file is added to the header of the editor iframe:
	 *  - https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js
	 */
	?>
	<script type="text/html" id="tmpl-videopress_iframe_vnext">
		<div class="tmpl-videopress_iframe_next" style="max-height:{{ data.height }}px;">
			<div class="videopress-editor-wrapper" style="padding-top:{{ data.ratio }}%;">
				<iframe style="display: block; max-width: 100%; max-height: 100%;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
			</div>
		</div>
	</script>
	<?php
}

/*************************************************\
| This is the chunk that handles overriding core  |
| media stuff so VideoPress can display natively. |
\*/

/**
 * Media Grid:
 * Filter out any videopress video posters that we've downloaded,
 * so that they don't seem to display twice.
 *
 * @param array $args Query variables.
 */
function videopress_ajax_query_attachments_args( $args ) {
	$meta_query = array(
		array(
			'key'     => 'videopress_poster_image',
			'compare' => 'NOT EXISTS',
		),
	);

	// If there was already a meta query, let's AND it via
	// nesting it with our new one. No need to specify the
	// relation, as it defaults to AND.
	if ( ! empty( $args['meta_query'] ) ) {
		$meta_query[] = $args['meta_query'];
	}
	$args['meta_query'] = $meta_query;

	return $args;
}
add_filter( 'ajax_query_attachments_args', 'videopress_ajax_query_attachments_args' );

/**
 * Media List:
 * Do the same as `videopress_ajax_query_attachments_args()` but for the list view.
 *
 * @param array $query WP_Query instance.
 */
function videopress_media_list_table_query( $query ) {

	if (
		! function_exists( 'get_current_screen' )
		|| get_current_screen() === null
	) {
		return;
	}

	if ( is_admin() && $query->is_main_query() && ( 'upload' === get_current_screen()->id ) ) {
		$meta_query = array(
			array(
				'key'     => 'videopress_poster_image',
				'compare' => 'NOT EXISTS',
			),
		);

		$old_meta_query = $query->get( 'meta_query' );
		if ( $old_meta_query ) {
			$meta_query[] = $old_meta_query;
		}

		$query->set( 'meta_query', $meta_query );
	}
}
add_action( 'pre_get_posts', 'videopress_media_list_table_query' );

/**
 * Make sure that any Video that has a VideoPress GUID passes that data back.
 *
 * @param WP_Post $post Attachment object.
 */
function videopress_prepare_attachment_for_js( $post ) {
	if ( 'video' === $post['type'] ) {
		$guid = get_post_meta( $post['id'], 'videopress_guid' );
		if ( $guid ) {
			$post['videopress_guid'] = $guid;
		}
	}
	return $post;
}
add_filter( 'wp_prepare_attachment_for_js', 'videopress_prepare_attachment_for_js' );

/**
 * Wherever the Media Modal is deployed, also deploy our overrides.
 */
function add_videopress_media_overrides() {
	add_action( 'admin_print_footer_scripts', 'videopress_override_media_templates', 11 );
}
add_action( 'wp_enqueue_media', 'add_videopress_media_overrides' );

/**
 * Our video overrides!
 *
 * We have a template for the iframe to get injected.
 */
function videopress_override_media_templates() {
	?>
	<script type="text/html" id="tmpl-videopress_iframe_vnext">
		<iframe class="videopress-iframe" style="display: block; max-width: 100%; max-height: 100%;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
	</script>
	<script>
		(function( media ){
			// This handles the media library modal attachment details display.
			if ( 'undefined' !== typeof media.view.Attachment.Details.TwoColumn ) {
				var TwoColumn   = media.view.Attachment.Details.TwoColumn,
					old_render  = TwoColumn.prototype.render,
					vp_template = wp.template('videopress_iframe_vnext');

				TwoColumn.prototype.render = function() {
					// Have the original renderer run first.
					old_render.apply( this, arguments );

					// Now our stuff!
					if ( 'video' === this.model.get('type') ) {
						if ( this.model.get('videopress_guid') ) {
							this.$('.attachment-media-view .thumbnail-video').html( vp_template( {
								guid   : this.model.get('videopress_guid'),
								width  : this.model.get('width') > 0 ? this.model.get('width') : '100%',
								height : this.model.get('height') > 0 ? this.model.get('height') : '100%'
							}));
						}
					}
				};
			} else { /* console.log( 'media.view.Attachment.Details.TwoColumn undefined' ); */ }

			// This handles the recreating of the core video shortcode when editing the mce embed.
			if ( 'undefined' !== typeof media.video ) {
				media.video.defaults.videopress_guid = '';

				// For some reason, even though we're not currently changing anything, the following proxy
				// function is necessary to include the above default `videopress_guid` param. ¯\_(ツ)_/¯
				var old_video_shortcode = media.video.shortcode;
				media.video.shortcode = function( model ) {
					// model.videopress_guid = 'FOOBAR';
					return old_video_shortcode( model );
				};
			} else { /* console.log( 'media.video undefined' ); */ }

			// override the media modal in order to extend the escape method to unload the player on hide
			var BaseMediaModal = wp.media.view.Modal;

			wp.media.view.Modal = BaseMediaModal.extend( {
				escape: function () {
					BaseMediaModal.prototype.escape.apply( this );
					var playerIframe = document.getElementsByClassName( "videopress-iframe" )[0];
					playerIframe.parentElement.removeChild( playerIframe );
				}
			} );
		})( wp.media );
	</script>
	<?php
}

/**
 * Properly inject VideoPress data into Core shortcodes, and
 * generate videopress shortcodes for purely remote videos.
 *
 * @param string $html HTML markup for a media item sent to the editor.
 * @param int    $id Attachment ID.
 * @param array  $attachment Attachment metadata.
 */
function videopress_media_send_to_editor( $html, $id, $attachment ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
	$videopress_guid = get_post_meta( $id, 'videopress_guid', true );
	if ( $videopress_guid && videopress_is_valid_guid( $videopress_guid ) ) {
		if ( '[video ' === substr( $html, 0, 7 ) ) {
			$html = sprintf( '[videopress %1$s]', esc_attr( $videopress_guid ) );

		} elseif ( '<a href=' === substr( $html, 0, 8 ) ) {
			// We got here because `wp_attachment_is()` returned false for
			// video, because there isn't a local copy of the file.
			$html = sprintf( '[videopress %1$s]', esc_attr( $videopress_guid ) );
		}
	} elseif ( videopress_is_attachment_without_guid( $id ) ) {
		$html = sprintf( '[videopress postid=%d]', $id );
	}
	return $html;
}
add_filter( 'media_send_to_editor', 'videopress_media_send_to_editor', 10, 3 );