Completed
Push — master ( 4679e7...74d050 )
by
unknown
10:55
created

is_display_embed_valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handle the VideoPress metadata properties.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
10
/**
11
 * Class Videopress_Attachment_Metadata
12
 */
13
class Videopress_Attachment_Metadata {
14
15
	/**
16
	 * Persist the VideoPress metadata information, including rating and display_embed.
17
	 *
18
	 * @param string|int $post_id       The post id.
19
	 * @param string     $guid          VideoPress Guid.
20
	 * @param string     $post_title    The post title.
21
	 * @param string     $caption       Video caption.
22
	 * @param string     $post_excerpt  The post excerpt.
23
	 * @param string     $rating        The rating.
24
	 * @param int        $display_embed The display_embed.
25
	 *
26
	 * @return bool|\WP_Error
27
	 */
28
	public static function persist_metadata( $post_id, $guid, $post_title, $caption, $post_excerpt, $rating, $display_embed ) {
29
		$post_id = absint( $post_id );
30
31
		$args = array(
32
			'method'  => 'POST',
33
			'headers' => array( 'content-type' => 'application/json' ),
34
		);
35
36
		$display_embed = (int) $display_embed;
37
38
		$values         = self::build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed );
39
		$endpoint       = 'videos';
40
		$values['guid'] = $guid;
41
42
		$result = Client::wpcom_json_api_request_as_blog( $endpoint, '2', $args, wp_json_encode( $values ), 'wpcom' );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($values) targeting wp_json_encode() can also be of type false; however, Automattic\Jetpack\Conne...n_api_request_as_blog() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
43
44
		$validated_result = self::validate_result( $result );
45
		if ( true !== $validated_result ) {
46
			return $validated_result;
47
		}
48
49
		// If we are in WPCOM, then we don't need to make anything else since we've already updated the video information.
50
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
51
			return true;
52
		}
53
54
		$meta = wp_get_attachment_metadata( $post_id );
55
56
		if ( self::is_display_embed_valid( $display_embed ) ) {
57
			$meta['videopress']['display_embed'] = (bool) $values['display_embed']; // convert it to bool since that's how we store it on wp-admin side.
58
		}
59
60
		if ( isset( $values['rating'] ) ) {
61
			$meta['videopress']['rating'] = $values['rating'];
62
		}
63
64
		wp_update_attachment_metadata( $post_id, $meta );
65
66
		return true;
67
	}
68
69
	/**
70
	 * Check if the given media item is a VideoPress file.
71
	 *
72
	 * @param stdClass $item The media item.
73
	 *
74
	 * @return bool
75
	 */
76
	public static function is_videopress_media( $item ) {
77
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
78
			return 0 === strpos( $item->mime_type, 'video/' );
79
		}
80
81
		// Else, we are in Jetpack and we need to check if the video is video/videopress.
82
		return 'video/videopress' === $item->mime_type;
83
	}
84
85
	/**
86
	 * Check if display_embed has valid values.
87
	 *
88
	 * @param mixed $display_embed The input display embed.
89
	 *
90
	 * @return bool
91
	 */
92
	private static function is_display_embed_valid( $display_embed ) {
93
		return ( 0 === $display_embed || 1 === $display_embed );
94
	}
95
96
	/**
97
	 * Validate the response received from WPCOM.
98
	 *
99
	 * @param array|\WP_Error $result The result returned by the client.
100
	 */
101
	private static function validate_result( $result ) {
102
		$response_code = isset( $result['response']['code'] ) ? $result['response']['code'] : 500;
103
104
		// When Client::wpcom_json_api_request_as_blog is called in WPCOM, bad response codes are not converted to WP_Error.
105
		// Because of this, we need to manually check the response code to check if the direct API call is 200 (OK).
106
		if ( 200 === $response_code && ! is_wp_error( $result ) ) {
107
			return true;
108
		}
109
110
		$error_message = __(
111
			'There was an issue saving your updates to the VideoPress service. Please try again later.',
112
			'jetpack'
113
		);
114
115
		$error_code = $response_code;
116
117
		if ( is_wp_error( $result ) ) {
118
			$error_code = $result->get_error_code();
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
		}
120
121
		return new \WP_Error( $error_code, $error_message );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $error_code.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
122
	}
123
124
	/**
125
	 * Build the request values that will be passed to the WPCOM API.
126
	 *
127
	 * @param string $post_title The video title.
128
	 * @param string $caption The video caption.
129
	 * @param string $post_excerpt The except.
130
	 * @param string $rating The video rating.
131
	 * @param string $display_embed The video display_embed.
132
	 *
133
	 * @return array
134
	 */
135
	private static function build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed ) {
136
		$values = array();
137
138
		// Add the video title & description in, so that we save it properly.
139
		if ( isset( $post_title ) ) {
140
			$values['title'] = trim( wp_strip_all_tags( $post_title ) );
141
		}
142
143
		if ( isset( $caption ) ) {
144
			$values['caption'] = trim( wp_strip_all_tags( $caption ) );
145
		}
146
147
		if ( isset( $post_excerpt ) ) {
148
			$values['description'] = trim( wp_strip_all_tags( $post_excerpt ) );
149
		}
150
151
		if ( isset( $rating ) ) {
152
			$values['rating'] = $rating;
153
		}
154
155
		if ( self::is_display_embed_valid( $display_embed ) ) {
156
			$values['display_embed'] = $display_embed;
157
		}
158
159
		return $values;
160
	}
161
}
162