Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
created

editor-media-view.php ➔ videopress_media_send_to_editor()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 14
rs 8.8571
1
<?php
2
3
/**
4
 * WordPress Shortcode Editor View JS Code
5
 */
6
function videopress_handle_editor_view_js() {
7
	global $content_width;
8
	$current_screen = get_current_screen();
9
	if ( ! isset( $current_screen->id ) || $current_screen->base !== 'post' ) {
10
		return;
11
	}
12
13
	add_action( 'admin_print_footer_scripts', 'videopress_editor_view_js_templates' );
14
15
	wp_enqueue_style( 'videopress-editor-ui', plugins_url( 'css/editor.css', __FILE__ ) );
16
	wp_enqueue_script( 'videopress-editor-view', plugins_url( 'js/editor-view.js', __FILE__ ), array( 'wp-util', 'jquery' ), false, true );
17
	wp_localize_script( 'videopress-editor-view', 'vpEditorView', array(
18
		'home_url_host'     => parse_url( home_url(), PHP_URL_HOST ),
19
		'min_content_width' => VIDEOPRESS_MIN_WIDTH,
20
		'content_width'     => $content_width,
21
		'modal_labels'      => array(
22
			'title'     => esc_html__( 'VideoPress Shortcode', 'jetpack' ),
23
			'guid'      => esc_html__( 'Video ID', 'jetpack' ),
24
			'w'         => esc_html__( 'Video Width', 'jetpack' ),
25
			'w_unit'    => esc_html__( 'pixels', 'jetpack' ),
26
			/* Translators: example of usage of this is "Start Video After 10 seconds" */
27
			'at'        => esc_html__( 'Start Video After', 'jetpack' ),
28
			'at_unit'   => esc_html__( 'seconds', 'jetpack' ),
29
			'hd'        => esc_html__( 'High definition on by default', 'jetpack' ),
30
			'permalink' => esc_html__( 'Link the video title to its URL on VideoPress.com', 'jetpack' ),
31
			'autoplay'  => esc_html__( 'Autoplay video on page load', 'jetpack' ),
32
			'loop'      => esc_html__( 'Loop video playback', 'jetpack' ),
33
			'freedom'   => esc_html__( 'Use only Open Source codecs (may degrade performance)', 'jetpack' ),
34
			'flashonly' => esc_html__( 'Use legacy Flash Player (not recommended)', 'jetpack' ),
35
		)
36
	) );
37
38
	add_editor_style( plugins_url( 'css/videopress-editor-style.css', __FILE__ ) );
39
}
40
add_action( 'admin_notices', 'videopress_handle_editor_view_js' );
41
42
/**
43
 * WordPress Editor Views
44
 */
45
function videopress_editor_view_js_templates() {
46
	/**
47
	 * This template uses the following parameters, and displays the video as an iframe:
48
	 *  - data.guid     // The guid of the video.
49
	 *  - data.width    // The width of the iframe.
50
	 *  - data.height   // The height of the iframe.
51
	 *  - data.urlargs  // Arguments serialized into a get string.
52
	 *
53
	 * In addition, the calling script will need to ensure that the following
54
	 * JS file is added to the header of the editor iframe:
55
	 *  - https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js
56
	 */
57
	?>
58
	<script type="text/html" id="tmpl-videopress_iframe_vnext">
59
		<div class="tmpl-videopress_iframe_next" style="max-height:{{ data.height }}px;">
60
			<div class="videopress-editor-wrapper" style="padding-top:{{ data.ratio }}%;">
61
				<iframe style="display: block;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
62
			</div>
63
		</div>
64
	</script>
65
	<?php
66
}
67
68
/*************************************************\
69
| This is the chunk that handles overriding core  |
70
| media stuff so VideoPress can display natively. |
71
\*************************************************/
72
73
/**
74
 * Media Grid:
75
 * Filter out any videopress video posters that we've downloaded,
76
 * so that they don't seem to display twice.
77
 */
78
add_filter( 'ajax_query_attachments_args', 'videopress_ajax_query_attachments_args' );
79
function videopress_ajax_query_attachments_args( $args ) {
80
	$meta_query = array(
81
		array(
82
			'key'     => 'videopress_poster_image',
83
			'compare' => 'NOT EXISTS',
84
		),
85
	);
86
87
	// If there was already a meta query, let's AND it via
88
	// nesting it with our new one. No need to specify the
89
	// relation, as it defaults to AND.
90
	if ( ! empty( $args['meta_query'] ) ) {
91
		$meta_query[] = $args['meta_query'];
92
	}
93
	$args['meta_query'] = $meta_query;
94
95
	return $args;
96
}
97
98
/**
99
 * Media List:
100
 * Do the same as ^^ but for the list view.
101
 */
102
add_action( 'pre_get_posts', 'videopress_media_list_table_query' );
103
function videopress_media_list_table_query( $query ) {
104
	if ( is_admin() && $query->is_main_query() && ( 'upload' === get_current_screen()->id ) ) {
105
		$meta_query = array(
106
			array(
107
				'key'     => 'videopress_poster_image',
108
				'compare' => 'NOT EXISTS',
109
			),
110
		);
111
112
		if ( $old_meta_query = $query->get( 'meta_query' ) ) {
113
			$meta_query[] = $old_meta_query;
114
		}
115
116
		$query->set( 'meta_query', $meta_query );
117
	}
118
}
119
120
/**
121
 * Make sure that any Video that has a VideoPress GUID passes that data back.
122
 */
123
add_filter( 'wp_prepare_attachment_for_js', 'videopress_prepare_attachment_for_js' );
124
function videopress_prepare_attachment_for_js( $post ) {
125
	if ( 'video' === $post['type'] ) {
126
		$guid = get_post_meta( $post['id'], 'videopress_guid' );
127
		if ( $guid ) {
128
			$post['videopress_guid'] = $guid;
129
		}
130
	}
131
	return $post;
132
}
133
134
/**
135
 * Wherever the Media Modal is deployed, also deploy our overrides.
136
 */
137
add_action( 'wp_enqueue_media', 'add_videopress_media_overrides' );
138
function add_videopress_media_overrides() {
139
	add_action( 'admin_print_footer_scripts', 'videopress_override_media_templates', 11 );
140
}
141
142
/**
143
 * Our video overrides!
144
 *
145
 * We have a template for the iframe to get injected.
146
 */
147
function videopress_override_media_templates(){
148
	?>
149
	<script type="text/html" id="tmpl-videopress_iframe_vnext">
150
		<iframe style="display: block; max-width: 100%;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
151
	</script>
152
	<script>
153
		(function( media ){
154
			// This handles the media library modal attachment details display.
155
			if ( 'undefined' !== typeof media.view.Attachment.Details.TwoColumn ) {
156
				var TwoColumn   = media.view.Attachment.Details.TwoColumn,
157
					old_render  = TwoColumn.prototype.render,
158
					vp_template = wp.template('videopress_iframe_vnext');
159
160
				TwoColumn.prototype.render = function() {
161
					// Have the original renderer run first.
162
					old_render.apply( this, arguments );
163
164
					// Now our stuff!
165
					if ( 'video' === this.model.get('type') ) {
166
						if ( this.model.get('videopress_guid') ) {
167
							this.$('.attachment-media-view .thumbnail-video').html( vp_template( {
168
								guid   : this.model.get('videopress_guid'),
169
								width  : this.model.get('width'),
170
								height : this.model.get('height')
171
							}));
172
						}
173
					}
174
				};
175
			} else { /* console.log( 'media.view.Attachment.Details.TwoColumn undefined' ); */ }
176
177
			// This handles the recreating of the core video shortcode when editing the mce embed.
178
			if ( 'undefined' !== typeof media.video ) {
179
				media.video.defaults.videopress_guid = '';
180
181
				// For some reason, even though we're not currently changing anything, the following proxy
182
				// function is necessary to include the above default `videopress_guid` param. ¯\_(ツ)_/¯
183
				var old_video_shortcode = media.video.shortcode;
184
				media.video.shortcode = function( model ) {
185
					// model.videopress_guid = 'FOOBAR';
186
					return old_video_shortcode( model );
187
				};
188
			} else { /* console.log( 'media.video undefined' ); */ }
189
190
		})( wp.media );
191
	</script>
192
	<?php
193
}
194
195
/**
196
 * Properly inject VideoPress data into Core shortcodes, and
197
 * generate videopress shortcodes for purely remote videos.
198
 */
199
add_filter( 'media_send_to_editor', 'videopress_media_send_to_editor', 10, 3 );
200
function videopress_media_send_to_editor( $html, $id, $attachment ) {
0 ignored issues
show
Unused Code introduced by
The parameter $attachment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
	$videopress_guid = get_post_meta( $id, 'videopress_guid', true );
202
	if ( $videopress_guid && videopress_is_valid_guid( $videopress_guid ) ) {
203
		if ( '[video ' === substr( $html, 0, 7 ) ) {
204
			$replace = sprintf( ' videopress_guid="%1$s"][/video]', esc_attr( $videopress_guid ) );
205
			$html = str_replace( '][/video]', $replace, $html );
206
		} elseif ( '<a href=' === substr( $html, 0, 8 ) ) {
207
			// We got here because `wp_attachment_is()` returned false for
208
			// video, because there isn't a local copy of the file.
209
			$html = sprintf( '[videopress %1$s]', esc_attr( $videopress_guid ) );
210
		}
211
	}
212
	return $html;
213
}