add_brightcove_video_field()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Brightcove Video CMB2 Field Register.
4
 *
5
 * @package cmb2-brightcove-video-field
6
 */
7
8
namespace CMB2\BrightcoveVideoField;
9
10
use BC_Video_Shortcode;
11
12
/**
13
 * Hook up all the filters and actions.
14
 */
15
function bootstrap() {
16
17 1
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_textdomain' );
18 1
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_plugin' );
19 1
}
20
21
/**
22
 * Load plugin text domain for text translation.
23
 */
24
function load_textdomain() {
25
26
	load_plugin_textdomain(
27
		'cmb2-brightcove-video-field',
28
		false,
29
		basename( plugin_dir_url( __DIR__ ) ) . '/languages'
30
	);
31
}
32
33
/**
34
 * Dependency check before loading the plugin.
35
 */
36
function is_dependency_loaded() {
37
38
	return (
39 1
		defined( 'CMB2_LOADED' )
40 1
		&& ! empty( constant( 'CMB2_LOADED' ) )
41 1
		&& defined( 'BRIGHTCOVE_URL' )
42 1
		&& ! empty( constant( 'BRIGHTCOVE_URL' ) )
43
	);
44
}
45
46
/**
47
 * Load plugin functionality if dependency are loaded correctly.
48
 */
49
function load_plugin() {
50
51 1
	if ( ! is_dependency_loaded() ) {
52 1
		add_action( 'admin_notices', __NAMESPACE__ . '\\dependency_admin_notice' );
53 1
		add_action( 'network_admin_notices', __NAMESPACE__ . '\\dependency_admin_notice' );
54 1
		return;
55
	}
56
57 1
	add_brightcove_video_field();
58 1
}
59
60
/**
61
 * Plugin dependency error message for admin notice.
62
 */
63
function dependency_admin_notice() {
64
65 1
	echo '<div class="error"><p>';
66 1
	esc_html_e( '"CMB2 BrightCove Video Field" plugin can\'t be loaded, It requires following plugins to be installed and activated.', 'cmb2-brightcove-video-field' );
67 1
		echo '<ol>';
68 1
			printf(
69 1
				'<li><a href="https://wordpress.org/plugins/cmb2/" target="_blank">%s</a></li>',
70 1
				esc_html__( 'CMB2', 'cmb2-brightcove-video-field' )
71
			);
72 1
			printf(
73 1
				' <li><a href="https://wordpress.org/plugins/brightcove-video-connect" target="_blank">%s</a></li>',
74 1
				esc_html__( 'Brightcove Video Connect', 'cmb2-brightcove-video-field' )
75
			);
76 1
		echo '</ol>';
77 1
	esc_html_e( 'Please verify the dependency to enable this field type.', 'cmb2-brightcove-video-field' );
78 1
	echo '</p></div>';
79 1
}
80
81
/**
82
 * Register brightcove video field.
83
 */
84
function add_brightcove_video_field() {
85
86 2
	add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts', 11 );
87 2
	add_action( 'cmb2_render_brightcove_video', __NAMESPACE__ . '\\cmb2_render_callback_for_brightcove_video', 10, 5 );
88 2
	add_action( 'admin_footer', __NAMESPACE__ . '\\js_wp_templates' );
89 2
}
90
91
/**
92
 * Enqueue helper JS script in the admin.
93
 *
94
 * @param string $hook Hook for the current page in the admin.
95
 */
96
function enqueue_scripts( $hook ) {
97
98 1
	if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) {
99 1
		return;
100
	}
101
102 1
	wp_enqueue_style(
103 1
		'cmb2-brightcove-video-field-css',
104 1
		plugin_dir_url( __FILE__ ) . 'assets/css/brightcove-video-field.css',
105 1
		[],
106 1
		VERSION
107
	);
108
109 1
	wp_enqueue_script(
110 1
		'cmb2-brightcove-video-field-js',
111 1
		plugin_dir_url( __FILE__ ) . 'assets/js/brightcove-video-field.js',
112
		[
113 1
			'wp-util',
114
			'brightcove-admin',
115
		],
116 1
		VERSION,
117 1
		true
118
	);
119 1
}
120
121
122
/**
123
 * Brightcove Video Field render callback function.
124
 *
125
 * @param \CMB2_Field $field `CMB2_Field` object.
126
 * @param array       $escaped_value Field escaped values.
127
 * @param int         $object_id Field object id, ex: post id, term id etc.
128
 * @param string      $object_type Field object type, ex: post type, user, option-page etc.
129
 * @param \CMB2_Types $field_type_object `CMB2_Types` object.
130
 */
131
function cmb2_render_callback_for_brightcove_video( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $object_type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
function cmb2_render_callback_for_brightcove_video( $field, $escaped_value, $object_id, /** @scrutinizer ignore-unused */ $object_type, $field_type_object ) {

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

Loading history...
Unused Code introduced by
The parameter $object_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
function cmb2_render_callback_for_brightcove_video( $field, $escaped_value, /** @scrutinizer ignore-unused */ $object_id, $object_type, $field_type_object ) {

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

Loading history...
132
133 1
	$field_id = $field->id();
134
135
	$bc_video_values = [
136 1
		'video_id'       => $escaped_value['bc_video_id'] ?? '',
137 1
		'player_id'      => $escaped_value['bc_player_id'] ?? '',
138 1
		'account_id'     => $escaped_value['bc_account_id'] ?? '',
139 1
		'video_duration' => $escaped_value['bc_video_duration'] ?? '',
140
	];
141
142
	$bc_video_args = [
143 1
		'embed'       => 'iframe',
144
		'width'       => '100%',
145
		'height'      => 'auto',
146
		'padding_top' => '10', // Adding min 10 padding since by-default it's adding 56.25% padding even with 0 padding.
147
	];
148
149 1
	$bc_video_args = wp_parse_args( $bc_video_values, $bc_video_args );
150
151 1
	$allowed_html           = wp_kses_allowed_html( 'post' );
152 1
	$allowed_html['iframe'] = [
153
		'src'                   => true,
154
		'webkitallowfullscreen' => true,
155
		'allowfullscreen'       => true,
156
		'mozallowfullscreen'    => true,
157
		'style'                 => true,
158
	];
159 1
	$allowed_html['input']  = [
160
		'type'      => true,
161
		'class'     => true,
162
		'name'      => true,
163
		'id'        => true,
164
		'value'     => true,
165
		'data-hash' => true,
166
	];
167
168
	// Remove jetpack shortcode module filters which is converting this shortcode to anchor tag when jetpack is enabled.
169
	// ref: https://github.com/Automattic/jetpack/blob/cb04cfc4479515f12945256555bbab1192711c57/modules/shortcodes/class.filter-embedded-html-objects.php#L11-L12.
170 1
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
171
		remove_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
172
		remove_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ], 100 );
173
	}
174
175
	?>
176 1
	<div class="cmb2-brightcove-video-metabox">
177
		<button type="button" class="button brightcove-add-media-btn">
178
			<?php
179 1
			$button_title = __( 'Select Brightcove Video', 'cmb2-brightcove-video-field' );
180 1
			printf(
181 1
				'<img class="bc-button-icon" src="%s" alt="%s" />%s',
182 1
				esc_url( BRIGHTCOVE_URL . 'images/menu-icon.svg' ),
183 1
				esc_attr( $button_title ),
184 1
				esc_html( $button_title )
185
			);
186
			?>
187 1
		</button>
188
		<div class="brightcove-video-preview">
189
			<?php
190 1
				if( ! empty( $bc_video_args['video_id'] ) ) {
191 1
					echo wp_kses( BC_Video_Shortcode::bc_video( $bc_video_args ), $allowed_html );
192 1
					echo '<a href="#" class="bc-remove-video">Remove brightcove video</a>';
193
				}
194
			?>
195 1
		</div>
196
		<?php
197
198 1
		echo wp_kses(
199 1
			$field_type_object->input(
200
				[
201 1
					'type'  => 'hidden',
202 1
					'id'    => esc_attr( $field_id . '_bc_video_id' ),
203 1
					'class' => 'bc_video_id',
204 1
					'name'  => esc_attr( $field_id . '[bc_video_id]' ),
205 1
					'value' => esc_attr( $bc_video_values['video_id'] ) ?? '',
206
				]
207
			),
208 1
			$allowed_html
209
		);
210 1
		echo wp_kses(
211 1
			$field_type_object->input(
212
				[
213 1
					'type'  => 'hidden',
214 1
					'id'    => esc_attr( $field_id . '_bc_video_duration' ),
215 1
					'class' => 'bc_video_duration',
216 1
					'name'  => esc_attr( $field_id . '[bc_video_duration]' ),
217 1
					'value' => esc_attr( $bc_video_values['video_duration'] ) ?? '',
218
				]
219
			),
220 1
			$allowed_html
221
		);
222 1
		echo wp_kses(
223 1
			$field_type_object->input(
224
				[
225 1
					'type'  => 'hidden',
226 1
					'id'    => esc_attr( $field_id . '_bc_player_id' ),
227 1
					'class' => 'bc_player_id',
228 1
					'name'  => esc_attr( $field_id . '[bc_player_id]' ),
229 1
					'value' => esc_attr( $bc_video_values['player_id'] ) ?? '',
230
				]
231
			),
232 1
			$allowed_html
233
		);
234 1
		echo wp_kses(
235 1
			$field_type_object->input(
236
				[
237 1
					'type'  => 'hidden',
238 1
					'id'    => esc_attr( $field_id . '_bc_account_id' ),
239 1
					'class' => 'bc_account_id',
240 1
					'name'  => esc_attr( $field_id . '[bc_account_id]' ),
241 1
					'value' => esc_attr( $bc_video_values['account_id'] ) ?? '',
242
				]
243
			),
244 1
			$allowed_html
245
		);
246
		?>
247 1
	</div>
248
	<?php
249
250
	// Enable jetpack embed filter.
251 1
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
252
		add_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
253
		add_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ], 100 );
254
	}
255 1
}
256
257
/**
258
 * JS WP Template to add video preview from Brightcove modal selection.
259
 */
260
function js_wp_templates() {
261
	?>
262 1
	<script type="text/html" id="tmpl-cmb2-brightcove-video-preview">
263
		<iframe
264
			width="100%"
265
			src="https://players.brightcove.net/{{data.account_id}}/{{data.player_id}}_default/index.html?videoId={{data.id}}"
266
			allowfullscreen
267
			webkitallowfullscreen
268
			mozallowfullscreen
269
		></iframe>
270
		<a href="#" class="bc-remove-video">Remove brightcove video</a>
271
	</script>
272
	<?php
273
}
274