Passed
Push — debug-scrutinizer ( ae1241...11c09a )
by Rahul
14:31
created

is_dependency_loaded()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 0
dl 0
loc 7
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;
0 ignored issues
show
Bug introduced by
The type BC_Video_Shortcode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Hook up all the filters and actions.
14
 */
15
function bootstrap() {
16
17
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_textdomain' );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

17
	/** @scrutinizer ignore-call */ 
18
 add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_textdomain' );
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function load_plugin_textdomain was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

26
	/** @scrutinizer ignore-call */ 
27
 load_plugin_textdomain(
Loading history...
27
		'cmb2-brightcove-video-field',
28
		false,
29
		basename( plugin_dir_url( __DIR__ ) ) . '/languages'
0 ignored issues
show
Bug introduced by
The function plugin_dir_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

29
		basename( /** @scrutinizer ignore-call */ plugin_dir_url( __DIR__ ) ) . '/languages'
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
		/** @scrutinizer ignore-call */ 
53
  add_action( 'admin_notices', __NAMESPACE__ . '\\dependency_admin_notice' );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function esc_html_e was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

66
	/** @scrutinizer ignore-call */ 
67
 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' );
Loading history...
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' )
0 ignored issues
show
Bug introduced by
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

70
				/** @scrutinizer ignore-call */ 
71
    esc_html__( 'CMB2', 'cmb2-brightcove-video-field' )
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
	/** @scrutinizer ignore-call */ 
87
 add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts', 11 );
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

102
	/** @scrutinizer ignore-call */ 
103
 wp_enqueue_style(
Loading history...
103
		'cmb2-brightcove-video-field-css',
104
		plugin_dir_url( __FILE__ ) . 'assets/css/brightcove-video-field.css',
0 ignored issues
show
Bug introduced by
The function plugin_dir_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

104
		/** @scrutinizer ignore-call */ 
105
  plugin_dir_url( __FILE__ ) . 'assets/css/brightcove-video-field.css',
Loading history...
105
		[],
106
		VERSION
107
	);
108
109
	wp_enqueue_script(
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

109
	/** @scrutinizer ignore-call */ 
110
 wp_enqueue_script(
Loading history...
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.
0 ignored issues
show
Bug introduced by
The type CMB2_Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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.
0 ignored issues
show
Bug introduced by
The type CMB2_Types was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	$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 );
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

149
	$bc_video_args = /** @scrutinizer ignore-call */ wp_parse_args( $bc_video_values, $bc_video_args );
Loading history...
150
151
	$allowed_html           = wp_kses_allowed_html( 'post' );
0 ignored issues
show
Bug introduced by
The function wp_kses_allowed_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

151
	$allowed_html           = /** @scrutinizer ignore-call */ wp_kses_allowed_html( 'post' );
Loading history...
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
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
171
		remove_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
0 ignored issues
show
Bug introduced by
The function remove_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

171
		/** @scrutinizer ignore-call */ 
172
  remove_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

179
			$button_title = /** @scrutinizer ignore-call */ __( 'Select Brightcove Video', 'cmb2-brightcove-video-field' );
Loading history...
180
			printf(
181
				'<img class="bc-button-icon" src="%s" alt="%s" />%s',
182
				esc_url( BRIGHTCOVE_URL . 'images/menu-icon.svg' ),
0 ignored issues
show
Bug introduced by
The constant CMB2\BrightcoveVideoField\BRIGHTCOVE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

182
				/** @scrutinizer ignore-call */ 
183
    esc_url( BRIGHTCOVE_URL . 'images/menu-icon.svg' ),
Loading history...
183
				esc_attr( $button_title ),
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

183
				/** @scrutinizer ignore-call */ 
184
    esc_attr( $button_title ),
Loading history...
184
				esc_html( $button_title )
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

184
				/** @scrutinizer ignore-call */ 
185
    esc_html( $button_title )
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function wp_kses was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

191
					echo /** @scrutinizer ignore-call */ wp_kses( BC_Video_Shortcode::bc_video( $bc_video_args ), $allowed_html );
Loading history...
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
	if ( class_exists( 'Filter_Embedded_HTML_Objects' ) ) {
252
		add_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

252
		/** @scrutinizer ignore-call */ 
253
  add_filter( 'pre_kses', [ 'Filter_Embedded_HTML_Objects', 'filter' ], 11 );
Loading history...
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