Completed
Push — sync/publicize-ui ( 4f02ff...f38f8e )
by Bernhard
435:27 queued 401:11
created

Jetpack_Keyring_Service_Helper::api_url()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Keyring_Service_Helper {
4
	/**
5
	 * @var Jetpack_Keyring_Service_Helper
6
	 **/
7
	private static $instance = null;
8
9
	static function init() {
10
		if ( is_null( self::$instance ) ) {
11
			self::$instance = new Jetpack_Keyring_Service_Helper;
12
		}
13
14
		return self::$instance;
15
	}
16
17
	public static $SERVICES = array(
18
		'facebook' => array(
19
			'for' => 'publicize'
20
		),
21
		'twitter' => array(
22
			'for' => 'publicize'
23
		),
24
		'linkedin' => array(
25
			'for' => 'publicize'
26
		),
27
		'tumblr' => array(
28
			'for' => 'publicize'
29
		),
30
		'path' => array(
31
			'for' => 'publicize'
32
		),
33
		'google_plus' => array(
34
			'for' => 'publicize'
35
		),
36
		'google_site_verification' => array(
37
			'for' => 'other'
38
		)
39
	);
40
41
	private function __construct() {
42
		add_action( 'load-settings_page_sharing', array( __CLASS__, 'admin_page_load' ), 9 );
43
	}
44
45
	function get_services( $filter = 'all' ) {
46
		$services = array(
47
48
		);
49
50 View Code Duplication
		if ( 'all' == $filter ) {
51
			return $services;
52
		} else {
53
			$connected_services = array();
54
			foreach ( $services as $service => $empty ) {
55
				$connections = $this->get_connections( $service );
0 ignored issues
show
Bug introduced by
The method get_connections() does not seem to exist on object<Jetpack_Keyring_Service_Helper>.

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...
56
				if ( $connections ) {
57
					$connected_services[ $service ] = $connections;
58
				}
59
			}
60
			return $connected_services;
61
		}
62
	}
63
64
	/**
65
	 * Gets a URL to the public-api actions. Works like WP's admin_url
66
	 *
67
	 * @param string $service Shortname of a specific service.
68
	 *
69
	 * @return URL to specific public-api process
70
	 */
71
	// on WordPress.com this is/calls Keyring::admin_url
72
	static function api_url( $service = false, $params = array() ) {
73
		/**
74
		 * Filters the API URL used to interact with WordPress.com.
75
		 *
76
		 * @since 2.0.0
77
		 *
78
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
79
		 */
80
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );
81
82
		if ( $service ) {
83
			$url = add_query_arg( array( 'service' => $service ), $url );
84
		}
85
86
		if ( count( $params ) ) {
87
			$url = add_query_arg( $params, $url );
88
		}
89
90
		return $url;
91
	}
92
93 View Code Duplication
	static function connect_url( $service_name, $for ) {
94
		return add_query_arg( array(
95
			'action'   => 'request',
96
			'service'  => $service_name,
97
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
98
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
99
			'for'      => $for,
100
		), menu_page_url( 'sharing', false ) );
101
	}
102
103 View Code Duplication
	static function refresh_url( $service_name, $for ) {
104
		return add_query_arg( array(
105
			'action'   => 'request',
106
			'service'  => $service_name,
107
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
108
			'refresh'  => 1,
109
			'for'      => $for,
110
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
111
		), admin_url( 'options-general.php?page=sharing' ) );
112
	}
113
114 View Code Duplication
	static function disconnect_url( $service_name, $id ) {
115
		return add_query_arg( array(
116
			'action'   => 'delete',
117
			'service'  => $service_name,
118
			'id'       => $id,
119
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
120
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
121
		), menu_page_url( 'sharing', false ) );
122
	}
123
124
	static function admin_page_load() {
125
		if ( isset( $_GET['action'] ) ) {
126
			if ( isset( $_GET['service'] ) ) {
127
				$service_name = $_GET['service'];
128
			}
129
130
			switch ( $_GET['action'] ) {
131
132
				case 'request':
133
					check_admin_referer( 'keyring-request', 'kr_nonce' );
134
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
0 ignored issues
show
Bug introduced by
The variable $service_name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
135
136
					$verification = Jetpack::generate_secrets( 'publicize' );
137
					if ( ! $verification ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $verification of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138
						$url = Jetpack::admin_url( 'jetpack#/settings' );
139
						wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) );
140
141
					}
142
					$stats_options = get_option( 'stats_options' );
143
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
144
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];
145
146
					$user     = wp_get_current_user();
147
					$redirect = Jetpack_Keyring_Service_Helper::api_url( $service_name, urlencode_deep( array(
148
						'action'       => 'request',
149
						'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
150
						'for'          => 'publicize',
151
						// required flag that says this connection is intended for publicize
152
						'siteurl'      => site_url(),
153
						'state'        => $user->ID,
154
						'blog_id'      => $wpcom_blog_id,
155
						'secret_1'     => $verification['secret_1'],
156
						'secret_2'     => $verification['secret_2'],
157
						'eol'          => $verification['exp'],
158
					) ) );
159
					wp_redirect( $redirect );
160
					exit;
161
					break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
162
163
				case 'completed':
164
					Jetpack::load_xml_rpc_client();
165
					$xml = new Jetpack_IXR_Client();
166
					$xml->query( 'jetpack.fetchPublicizeConnections' );
167
168
					if ( ! $xml->isError() ) {
169
						$response = $xml->getResponse();
170
						Jetpack_Options::update_option( 'publicize_connections', $response );
171
					}
172
173
					break;
174
175
				case 'delete':
176
					$id = $_GET['id'];
177
178
					check_admin_referer( 'keyring-request', 'kr_nonce' );
179
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
180
181
					Jetpack_Keyring_Service_Helper::disconnect( $service_name, $id );
182
183
					do_action( 'connection_disconnected', $service_name );
184
					break;
185
			}
186
		}
187
	}
188
189
	/**
190
	 * Remove a Publicize connection
191
	 */
192
	static function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) {
193
		Jetpack::load_xml_rpc_client();
194
		$xml = new Jetpack_IXR_Client();
195
		$xml->query( 'jetpack.deletePublicizeConnection', $connection_id );
196
197
		if ( ! $xml->isError() ) {
198
			Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() );
199
		} else {
200
			return false;
201
		}
202
	}
203
204
}
205