Completed
Push — renovate/dealerdirect-phpcodes... ( e7e821...e6cc4c )
by
unknown
95:47 queued 87:17
created

Jetpack_Tweetstorm_Helper::gather()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 7
nop 1
dl 0
loc 40
rs 8.0355
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tweetstorm block and API helper.
4
 *
5
 * @package jetpack
6
 * @since 8.7.0
7
 */
8
9
use Automattic\Jetpack\Connection\Client;
10
use Automattic\Jetpack\Status;
11
/**
12
 * Class Jetpack_Tweetstorm_Helper
13
 *
14
 * @since 8.7.0
15
 */
16
class Jetpack_Tweetstorm_Helper {
17
	/**
18
	 * Gather the Tweetstorm.
19
	 *
20
	 * @param  string $url The tweet URL to gather from.
21
	 * @return mixed
22
	 */
23
	public static function gather( $url ) {
24
		if ( ( new Status() )->is_development_mode() ) {
25
			return new WP_Error(
26
				'dev_mode',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'dev_mode'.

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...
27
				__( 'Tweet unrolling is not available in development mode.', 'jetpack' )
28
			);
29
		}
30
31
		$site_id = self::get_site_id();
32
		if ( is_wp_error( $site_id ) ) {
33
			return $site_id;
34
		}
35
36
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
37
			if ( ! class_exists( 'WPCOM_Gather_Tweetstorm' ) ) {
38
				\jetpack_require_lib( 'gather-tweetstorm' );
39
			}
40
41
			return WPCOM_Gather_Tweetstorm::gather( $url );
42
		}
43
44
		$response = Client::wpcom_json_api_request_as_blog(
45
			sprintf( '/sites/%d/tweetstorm/gather?url=%s', $site_id, rawurlencode( $url ) ),
46
			2,
47
			array( 'headers' => array( 'content-type' => 'application/json' ) ),
48
			null,
49
			'wpcom'
50
		);
51
		if ( is_wp_error( $response ) ) {
52
			return $response;
53
		}
54
55
		$data = json_decode( wp_remote_retrieve_body( $response ) );
56
57
		if ( wp_remote_retrieve_response_code( $response ) >= 400 ) {
58
			return new WP_Error( $data->code, $data->message, $data->data );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $data->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...
59
		}
60
61
		return $data;
62
	}
63
64
	/**
65
	 * Get the WPCOM or self-hosted site ID.
66
	 *
67
	 * @return mixed
68
	 */
69 View Code Duplication
	public static function get_site_id() {
70
		$is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM );
71
		$site_id  = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
72
		if ( ! $site_id ) {
73
			return new WP_Error(
74
				'unavailable_site_id',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unavailable_site_id'.

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...
75
				__( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ),
76
				403
77
			);
78
		}
79
		return (int) $site_id;
80
	}
81
}
82