Completed
Push — fix/murial-studio ( dc6c26...a8f618 )
by
unknown
61:47 queued 55:35
created

Jetpack_Keyring_Service_Helper::add_sharing_menu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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