Completed
Push — add/crowdsignal-shortcode ( 65c42e...1b4a63 )
by Kuba
14:46 queued 06:22
created

Jetpack_Keyring_Service_Helper::admin_page_load()   B

Complexity

Conditions 9
Paths 17

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 17
nop 0
dl 0
loc 64
rs 7.2298
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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