Completed
Push — try/seperate-publicize-handlin... ( 05ee48...67cfeb )
by
unknown
07:54
created

Jetpack_Service_Helper::disconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 5
dl 0
loc 11
rs 9.9
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
	private function __construct() {
18
		add_action( 'load-settings_page_sharing', array( __CLASS__, 'admin_page_load' ), 9 );
19
	}
20
21
	/**
22
	 * Gets a URL to the public-api actions. Works like WP's admin_url
23
	 *
24
	 * @param string $service Shortname of a specific service.
25
	 *
26
	 * @return URL to specific public-api process
27
	 */
28
	// on WordPress.com this is/calls Keyring::admin_url
29
	static function api_url( $service = false, $params = array() ) {
30
		/**
31
		 * Filters the API URL used to interact with WordPress.com.
32
		 *
33
		 * @since 2.0.0
34
		 *
35
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
36
		 */
37
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );
38
39
		if ( $service ) {
40
			$url = add_query_arg( array( 'service' => $service ), $url );
41
		}
42
43
		if ( count( $params ) ) {
44
			$url = add_query_arg( $params, $url );
45
		}
46
47
		return $url;
48
	}
49
50 View Code Duplication
	static function connect_url( $service_name ) {
51
		return add_query_arg( array(
52
			'action'   => 'request',
53
			'service'  => $service_name,
54
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
55
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
56
		), menu_page_url( 'sharing', false ) );
57
	}
58
59
	static function refresh_url( $service_name ) {
60
		return add_query_arg( array(
61
			'action'   => 'request',
62
			'service'  => $service_name,
63
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
64
			'refresh'  => 1,
65
			'for'      => 'publicize',
66
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
67
		), admin_url( 'options-general.php?page=sharing' ) );
68
	}
69
70 View Code Duplication
	static function disconnect_url( $service_name, $id ) {
71
		return add_query_arg( array(
72
			'action'   => 'delete',
73
			'service'  => $service_name,
74
			'id'       => $id,
75
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
76
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
77
		), menu_page_url( 'sharing', false ) );
78
	}
79
80
	static function admin_page_load() {
81
		if ( isset( $_GET['action'] ) ) {
82
			if ( isset( $_GET['service'] ) ) {
83
				$service_name = $_GET['service'];
84
			}
85
86
			switch ( $_GET['action'] ) {
87
88
				case 'request':
89
					check_admin_referer( 'keyring-request', 'kr_nonce' );
90
					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...
91
92
					$verification = Jetpack::generate_secrets( 'publicize' );
93
					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...
94
						$url = Jetpack::admin_url( 'jetpack#/settings' );
95
						wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) );
96
97
					}
98
					$stats_options = get_option( 'stats_options' );
99
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
100
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];
101
102
					$user     = wp_get_current_user();
103
					$redirect = Jetpack_Keyring_Service_Helper::api_url( $service_name, urlencode_deep( array(
104
						'action'       => 'request',
105
						'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
106
						'for'          => 'publicize',
107
						// required flag that says this connection is intended for publicize
108
						'siteurl'      => site_url(),
109
						'state'        => $user->ID,
110
						'blog_id'      => $wpcom_blog_id,
111
						'secret_1'     => $verification['secret_1'],
112
						'secret_2'     => $verification['secret_2'],
113
						'eol'          => $verification['exp'],
114
					) ) );
115
					wp_redirect( $redirect );
116
					exit;
117
					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...
118
119
				case 'completed':
120
					Jetpack::load_xml_rpc_client();
121
					$xml = new Jetpack_IXR_Client();
122
					$xml->query( 'jetpack.fetchPublicizeConnections' );
123
124
					if ( ! $xml->isError() ) {
125
						$response = $xml->getResponse();
126
						Jetpack_Options::update_option( 'publicize_connections', $response );
127
					}
128
129
					break;
130
131
				case 'delete':
132
					$id = $_GET['id'];
133
134
					check_admin_referer( 'keyring-request', 'kr_nonce' );
135
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
136
137
					Jetpack_Keyring_Service_Helper::disconnect( $service_name, $id );
138
139
					do_action( 'connection_disconnected', $service_name );
140
					break;
141
			}
142
		}
143
	}
144
145
	/**
146
	 * Remove a Publicize connection
147
	 */
148
	static function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) {
149
		Jetpack::load_xml_rpc_client();
150
		$xml = new Jetpack_IXR_Client();
151
		$xml->query( 'jetpack.deletePublicizeConnection', $connection_id );
152
153
		if ( ! $xml->isError() ) {
154
			Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() );
155
		} else {
156
			return false;
157
		}
158
	}
159
160
}
161