Completed
Branch debug-scrutinizer (a948cf)
by Rahul
04:00
created

namespace.php ➔ enqueue_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 24
rs 9.536
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
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_textdomain' );
18
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_plugin' );
19
}
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
		defined( 'CMB2_LOADED' )
40
		&& ! empty( constant( 'CMB2_LOADED' ) )
41
		&& defined( 'BRIGHTCOVE_URL' )
42
		&& ! empty( constant( 'BRIGHTCOVE_URL' ) )
43
	);
44
}
45
46
/**
47
 * Load plugin functionality if dependency are loaded correctly.
48
 */
49
function load_plugin() {
50
51
	if ( ! is_dependency_loaded() ) {
52
		add_action( 'admin_notices', __NAMESPACE__ . '\\dependency_admin_notice' );
53
		add_action( 'network_admin_notices', __NAMESPACE__ . '\\dependency_admin_notice' );
54
		return;
55
	}
56
57
	add_brightcove_video_field();
58
}
59
60
/**
61
 * Plugin dependency error message for admin notice.
62
 */
63
function dependency_admin_notice() {
64
65
	echo '<div class="error"><p>';
66
	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
		echo '<ol>';
68
			printf(
69
				'<li><a href="https://wordpress.org/plugins/cmb2/" target="_blank">%s</a></li>',
70
				esc_html__( 'CMB2', 'cmb2-brightcove-video-field' )
71
			);
72
			printf(
73
				' <li><a href="https://wordpress.org/plugins/brightcove-video-connect" target="_blank">%s</a></li>',
74
				esc_html__( 'Brightcove Video Connect', 'cmb2-brightcove-video-field' )
75
			);
76
		echo '</ol>';
77
	esc_html_e( 'Please verify the dependency to enable this field type.', 'cmb2-brightcove-video-field' );
78
	echo '</p></div>';
79
}
80
81
/**
82
 * Register brightcove video field.
83
 */
84
function add_brightcove_video_field() {
85
86
	add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts', 11 );
87
	add_action( 'cmb2_render_brightcove_video', __NAMESPACE__ . '\\cmb2_render_callback_for_brightcove_video', 10, 5 );
88
	add_action( 'admin_footer', __NAMESPACE__ . '\\js_wp_templates' );
89
}
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
	if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) {
99
		return;
100
	}
101
102
	wp_enqueue_style(
103
		'cmb2-brightcove-video-field-css',
104
		plugin_dir_url( __FILE__ ) . 'assets/css/brightcove-video-field.css',
105
		[],
106
		VERSION
107
	);
108
109
	wp_enqueue_script(
110
		'cmb2-brightcove-video-field-js',
111
		plugin_dir_url( __FILE__ ) . 'assets/js/brightcove-video-field.js',
112
		[
113
			'wp-util',
114
			'brightcove-admin',
115
		],
116
		VERSION,
117
		true
118
	);
119
}
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_id 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...
Unused Code introduced by
The parameter $object_type 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...
132
133
	$field_id = $field->id();
134
135
	$bc_video_values = [
136
		'video_id'       => $escaped_value['bc_video_id'] ?? '',
137
		'player_id'      => $escaped_value['bc_player_id'] ?? '',
138
		'account_id'     => $escaped_value['bc_account_id'] ?? '',
139
		'video_duration' => $escaped_value['bc_video_duration'] ?? '',
140
	];
141
142
	$bc_video_args = [
143
		'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
	$bc_video_args = wp_parse_args( $bc_video_values, $bc_video_args );
150
151
	$allowed_html           = wp_kses_allowed_html( 'post' );
152
	$allowed_html['iframe'] = [
153
		'src'                   => true,
154
		'webkitallowfullscreen' => true,
155
		'allowfullscreen'       => true,
156
		'mozallowfullscreen'    => true,
157
		'style'                 => true,
158
	];
159
	$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 View Code Duplication
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	<div class="cmb2-brightcove-video-metabox">
177
		<button type="button" class="button brightcove-add-media-btn">
178
			<?php
179
			$button_title = __( 'Select Brightcove Video', 'cmb2-brightcove-video-field' );
180
			printf(
181
				'<img class="bc-button-icon" src="%s" alt="%s" />%s',
182
				esc_url( BRIGHTCOVE_URL . 'images/menu-icon.svg' ),
183
				esc_attr( $button_title ),
184
				esc_html( $button_title )
185
			);
186
			?>
187
		</button>
188
		<div class="brightcove-video-preview">
189
			<?php
190
				if( ! empty( $bc_video_args['video_id'] ) ) {
191
					echo wp_kses( BC_Video_Shortcode::bc_video( $bc_video_args ), $allowed_html );
192
					echo '<a href="#" class="bc-remove-video">Remove brightcove video</a>';
193
				}
194
			?>
195
		</div>
196
		<?php
197
198
		echo wp_kses(
199
			$field_type_object->input(
200
				[
201
					'type'  => 'hidden',
202
					'id'    => esc_attr( $field_id . '_bc_video_id' ),
203
					'class' => 'bc_video_id',
204
					'name'  => esc_attr( $field_id . '[bc_video_id]' ),
205
					'value' => esc_attr( $bc_video_values['video_id'] ) ?? '',
206
				]
207
			),
208
			$allowed_html
209
		);
210
		echo wp_kses(
211
			$field_type_object->input(
212
				[
213
					'type'  => 'hidden',
214
					'id'    => esc_attr( $field_id . '_bc_video_duration' ),
215
					'class' => 'bc_video_duration',
216
					'name'  => esc_attr( $field_id . '[bc_video_duration]' ),
217
					'value' => esc_attr( $bc_video_values['video_duration'] ) ?? '',
218
				]
219
			),
220
			$allowed_html
221
		);
222
		echo wp_kses(
223
			$field_type_object->input(
224
				[
225
					'type'  => 'hidden',
226
					'id'    => esc_attr( $field_id . '_bc_player_id' ),
227
					'class' => 'bc_player_id',
228
					'name'  => esc_attr( $field_id . '[bc_player_id]' ),
229
					'value' => esc_attr( $bc_video_values['player_id'] ) ?? '',
230
				]
231
			),
232
			$allowed_html
233
		);
234
		echo wp_kses(
235
			$field_type_object->input(
236
				[
237
					'type'  => 'hidden',
238
					'id'    => esc_attr( $field_id . '_bc_account_id' ),
239
					'class' => 'bc_account_id',
240
					'name'  => esc_attr( $field_id . '[bc_account_id]' ),
241
					'value' => esc_attr( $bc_video_values['account_id'] ) ?? '',
242
				]
243
			),
244
			$allowed_html
245
		);
246
		?>
247
	</div>
248
	<?php
249
250
	// Enable jetpack embed filter.
251 View Code Duplication
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
}
256
257
/**
258
 * JS WP Template to add video preview from Brightcove modal selection.
259
 */
260
function js_wp_templates() {
261
	?>
262
	<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