Completed
Push — add/cli-generate-gitaction ( f8af7a...9d2eaa )
by
unknown
44:31 queued 34:06
created

Jetpack_Keyring_Service_Helper::connect_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 9
loc 9
rs 9.9666
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
	/**
42
	 * Constructor
43
	 */
44
	private function __construct() {
45
		add_action( 'admin_menu', array( __CLASS__, 'add_sharing_menu' ), 21 );
46
47
		add_action( 'load-settings_page_sharing', array( __CLASS__, 'admin_page_load' ), 9 );
48
	}
49
50
	/**
51
	 * We need a `sharing` submenu page to be able to connect and disconnect services.
52
	 */
53
	public static function add_sharing_menu() {
54
		global $submenu;
55
56
		if (
57
			! isset( $submenu['options-general.php'] )
58
			|| ! is_array( $submenu['options-general.php'] )
59
		) {
60
			return;
61
		}
62
63
		$general_settings_names = array_map(
64
			function ( $menu ) {
65
				return array_values( $menu )[0];
66
			},
67
			$submenu['options-general.php']
68
		);
69
		if ( ! in_array( 'Sharing', $general_settings_names, true ) ) {
70
			add_submenu_page( 'options-general.php', '', '', 'manage_options', 'sharing', '__return_empty_string' );
71
		}
72
	}
73
74
	function get_services( $filter = 'all' ) {
75
		$services = array();
76
77 View Code Duplication
		if ( 'all' === $filter ) {
78
			return $services;
79
		} else {
80
			$connected_services = array();
81
			foreach ( $services as $service => $empty ) {
82
				$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...
83
				if ( $connections ) {
84
					$connected_services[ $service ] = $connections;
85
				}
86
			}
87
			return $connected_services;
88
		}
89
	}
90
91
	/**
92
	 * Gets a URL to the public-api actions. Works like WP's admin_url
93
	 *
94
	 * @param string $service Shortname of a specific service.
95
	 *
96
	 * @return URL to specific public-api process
97
	 */
98
	// on WordPress.com this is/calls Keyring::admin_url
99
	static function api_url( $service = false, $params = array() ) {
100
		/**
101
		 * Filters the API URL used to interact with WordPress.com.
102
		 *
103
		 * @since 2.0.0
104
		 *
105
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
106
		 */
107
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );
108
109
		if ( $service ) {
110
			$url = add_query_arg( array( 'service' => $service ), $url );
111
		}
112
113
		if ( count( $params ) ) {
114
			$url = add_query_arg( $params, $url );
115
		}
116
117
		return $url;
118
	}
119
120 View Code Duplication
	static function connect_url( $service_name, $for ) {
121
		return add_query_arg( array(
122
			'action'   => 'request',
123
			'service'  => $service_name,
124
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
125
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
126
			'for'      => $for,
127
		), menu_page_url( 'sharing', false ) );
128
	}
129
130 View Code Duplication
	static function refresh_url( $service_name, $for ) {
131
		return add_query_arg( array(
132
			'action'   => 'request',
133
			'service'  => $service_name,
134
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
135
			'refresh'  => 1,
136
			'for'      => $for,
137
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
138
		), admin_url( 'options-general.php?page=sharing' ) );
139
	}
140
141 View Code Duplication
	static function disconnect_url( $service_name, $id ) {
142
		return add_query_arg( array(
143
			'action'   => 'delete',
144
			'service'  => $service_name,
145
			'id'       => $id,
146
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
147
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
148
		), menu_page_url( 'sharing', false ) );
149
	}
150
151
	static function admin_page_load() {
152
		if ( isset( $_GET['action'] ) ) {
153
			if ( isset( $_GET['service'] ) ) {
154
				$service_name = $_GET['service'];
155
			}
156
157
			switch ( $_GET['action'] ) {
158
159
				case 'request':
160
					check_admin_referer( 'keyring-request', 'kr_nonce' );
161
					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...
162
163
					$verification = Jetpack::generate_secrets( 'publicize' );
164
					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...
165
						$url = Jetpack::admin_url( 'jetpack#/settings' );
166
						wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) );
167
168
					}
169
					$stats_options = get_option( 'stats_options' );
170
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
171
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];
172
173
					$user     = wp_get_current_user();
174
					$redirect = Jetpack_Keyring_Service_Helper::api_url( $service_name, urlencode_deep( array(
175
						'action'       => 'request',
176
						'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
177
						'for'          => 'publicize',
178
						// required flag that says this connection is intended for publicize
179
						'siteurl'      => site_url(),
180
						'state'        => $user->ID,
181
						'blog_id'      => $wpcom_blog_id,
182
						'secret_1'     => $verification['secret_1'],
183
						'secret_2'     => $verification['secret_2'],
184
						'eol'          => $verification['exp'],
185
					) ) );
186
					wp_redirect( $redirect );
187
					exit;
188
					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...
189
190
				case 'completed':
191
					$xml = new Jetpack_IXR_Client();
192
					$xml->query( 'jetpack.fetchPublicizeConnections' );
193
194
					if ( ! $xml->isError() ) {
195
						$response = $xml->getResponse();
196
						Jetpack_Options::update_option( 'publicize_connections', $response );
197
					}
198
199
					break;
200
201
				case 'delete':
202
					$id = $_GET['id'];
203
204
					check_admin_referer( 'keyring-request', 'kr_nonce' );
205
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
206
207
					Jetpack_Keyring_Service_Helper::disconnect( $service_name, $id );
208
209
					do_action( 'connection_disconnected', $service_name );
210
					break;
211
			}
212
		}
213
	}
214
215
	/**
216
	 * Remove a Publicize connection
217
	 */
218
	static function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) {
219
		$xml = new Jetpack_IXR_Client();
220
		$xml->query( 'jetpack.deletePublicizeConnection', $connection_id );
221
222
		if ( ! $xml->isError() ) {
223
			Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() );
224
		} else {
225
			return false;
226
		}
227
	}
228
229
}
230