Completed
Push — remove/wp-admin-publicize-conn... ( 3e2162...45a0c0 )
by
unknown
18:28 queued 09:13
created

Publicize::get_publicized_twitter_account()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
nc 3
cc 3
eloc 7
nop 2
1
<?php
2
3
class Publicize extends Publicize_Base {
4
5
	function __construct() {
6
		parent::__construct();
7
8
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'register_update_publicize_connections_xmlrpc_method' ) );
9
10
		add_action( 'load-settings_page_sharing', array( $this, 'force_user_connection' ) );
11
12
		add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 );
13
14
		add_action( 'transition_post_status', array( $this, 'save_publicized' ), 10, 3 );
15
16
		add_filter( 'jetpack_twitter_cards_site_tag', array( $this, 'enhaced_twitter_cards_site_tag' ) );
17
18
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_twitter_account' ), 10, 4 );
19
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_facebook_account' ), 10, 4 );
20
21
		add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 );
22
23
		include_once ( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' );
24
	}
25
26
	function force_user_connection() {
27
		global $current_user;
28
		$user_token = Jetpack_Data::get_access_token( $current_user->ID );
29
		$is_user_connected = $user_token && !is_wp_error( $user_token );
30
31
		// If the user is already connected via Jetpack, then we're good
32
		if ( $is_user_connected )
33
			return;
34
35
		// If they're not connected, then remove the Publicize UI and tell them they need to connect first
36
		global $publicize_ui;
37
		remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) );
38
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 );
39
	}
40
41
	function admin_page_warning() {
42
		$jetpack = Jetpack::init();
43
		$blog_name = get_bloginfo( 'blogname' );
44
		if ( empty( $blog_name ) ) {
45
			$blog_name = home_url( '/' );
46
		}
47
48
		?>
49
		<div id="message" class="updated jetpack-message jp-connect">
50
			<div class="jetpack-wrap-container">
51
				<div class="jetpack-text-container">
52
					<p><?php printf(
53
						esc_html( wptexturize( __( "To use Publicize, you'll need to link your %s account to your WordPress.com account using the link below.", 'jetpack' ) ) ),
54
						'<strong>' . esc_html( $blog_name ) . '</strong>'
55
					); ?></p>
56
					<p><?php echo esc_html( wptexturize( __( "If you don't have a WordPress.com account yet, you can sign up for free in just a few seconds.", 'jetpack' ) ) ); ?></p>
57
				</div>
58
				<div class="jetpack-install-container">
59
					<p class="submit"><a href="<?php echo $jetpack->build_connect_url( false, menu_page_url( 'sharing', false ) ); ?>" class="button-connector" id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a></p>
60
				</div>
61
			</div>
62
		</div>
63
		<?php
64
	}
65
66
67
	function receive_updated_publicize_connections( $publicize_connections ) {
68
		Jetpack_Options::update_option( 'publicize_connections', $publicize_connections );
69
		return true;
70
	}
71
72
	function register_update_publicize_connections_xmlrpc_method( $methods ) {
73
		return array_merge( $methods, array(
74
			'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ),
75
		) );
76
	}
77
78
	function get_connections( $service_name, $_blog_id = false, $_user_id = false ) {
79
		$connections = Jetpack_Options::get_option( 'publicize_connections' );
80
		$connections_to_return = array();
81
		if ( !empty( $connections ) && is_array( $connections ) ) {
82
			if ( !empty( $connections[$service_name] ) ) {
83
				foreach( $connections[$service_name] as $id => $connection ) {
84
					if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) {
85
						$connections_to_return[$id] = $connection;
86
					}
87
				}
88
			}
89
			return $connections_to_return;
90
		}
91
		return false;
92
	}
93
94
	function get_connection_id( $connection ) {
95
		return $connection['connection_data']['id'];
96
	}
97
98
	function get_connection_meta( $connection ) {
99
		$connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections
100
		return $connection;
101
	}
102
103
	function get_services( $filter ) {
104
		if ( !in_array( $filter, array( 'all', 'connected' ) ) )
105
			$filter = 'all';
106
107
		$services = array(
108
				'facebook'        => array(),
109
				'twitter'         => array(),
110
				'linkedin'        => array(),
111
				'tumblr'          => array(),
112
				'path'            => array(),
113
				'google_plus'     => array(),
114
		);
115
116
		if ( 'all' == $filter ) {
117
			return $services;
118
		} else {
119
			$connected_services = array();
120
			foreach ( $services as $service => $empty ) {
121
				$connections = $this->get_connections( $service );
122
				if ( $connections )
123
					$connected_services[$service] = $connections;
124
			}
125
			return $connected_services;
126
		}
127
	}
128
129
	function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) {
130
		// Stub
131
	}
132
133
	function flag_post_for_publicize( $new_status, $old_status, $post ) {
134
		// Stub only. Doesn't need to do anything on Jetpack Client
135
	}
136
137
	function test_connection( $service_name, $connection ) {
138
		$connection_test_passed = true;
139
		$connection_test_message = '';
140
		$user_can_refresh = false;
0 ignored issues
show
Unused Code introduced by
$user_can_refresh is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
142
		$id = $this->get_connection_id( $connection );
143
144
		Jetpack::load_xml_rpc_client();
145
		$xml = new Jetpack_IXR_Client();
146
		$xml->query( 'jetpack.testPublicizeConnection', $id );
147
148
		if ( $xml->isError() ) {
149
			$xml_response = $xml->getResponse();
150
			$connection_test_message = $xml_response['faultString'];
151
			$connection_test_passed = false;
152
		}
153
154
		// Bail if all is well
155
		if ( $connection_test_passed ) {
156
			return true;
157
		}
158
159
		// Set up refresh if the user can
160
		$user_can_refresh = current_user_can( $this->GLOBAL_CAP );
161
		if ( $user_can_refresh ) {
162
			$nonce = wp_create_nonce( "keyring-request-" . $service_name );
0 ignored issues
show
Unused Code introduced by
$nonce is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
			$refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) );
164
			$refresh_url = $this->refresh_url( $service_name );
0 ignored issues
show
Bug introduced by
The method refresh_url() does not seem to exist on object<Publicize>.

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...
165
		}
166
167
		$error_data = array(
168
			'user_can_refresh' => $user_can_refresh,
169
			'refresh_text' => $refresh_text,
0 ignored issues
show
Bug introduced by
The variable $refresh_text 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...
170
			'refresh_url' => $refresh_url
0 ignored issues
show
Bug introduced by
The variable $refresh_url 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...
171
		);
172
173
		return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data );
174
	}
175
176
	/**
177
	 * Save a flag locally to indicate that this post has already been Publicized via the selected
178
	 * connections.
179
	 */
180
	function save_publicized( $new_status, $old_status, $post ) {
181
		// Only do this when a post transitions to being published
182
		if ( 'publish' == $new_status && 'publish' != $old_status ) {
183
			update_post_meta( $post->ID, $this->POST_DONE . 'all', true );
184
		}
185
	}
186
187
	/**
188
	* Already-published posts should not be Publicized by default. This filter sets checked to
189
	* false if a post has already been published.
190
	*/
191
	function publicize_checkbox_default( $checked, $post_id, $name, $connection ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $connection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
192
		if ( 'publish' == get_post_status( $post_id ) )
193
			return false;
194
195
		return $checked;
196
	}
197
198
	/**
199
	* If there's only one shared connection to Twitter set it as twitter:site tag.
200
	*/
201
	function enhaced_twitter_cards_site_tag( $tag ) {
202
		$custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' );
203
		if( ! empty( $custom_site_tag ) )
204
			return $tag;
205
		if ( ! $this->is_enabled('twitter') )
206
			return $tag;
207
		$connections = $this->get_connections( 'twitter' );
208
		foreach ( $connections as $connection ) {
0 ignored issues
show
Bug introduced by
The expression $connections of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
209
			$connection_meta = $this->get_connection_meta( $connection );
210
			if ( 0 == $connection_meta['connection_data']['user_id'] ) {
211
				// If the connection is shared
212
				return $this->get_display_name( 'twitter', $connection );
213
			}
214
		}
215
		return $tag;
216
	}
217
218
	function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) {
219
		if ( 'twitter' == $service_name && $submit_post ) {
220
			$connection_meta = $this->get_connection_meta( $connection );
221
			$publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' );
222
			if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
223
				update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) );
224
			}
225
		}
226
	}
227
228
	function get_publicized_twitter_account( $account, $post_id ) {
229
		if ( ! empty( $account ) ) {
230
			return $account;
231
		}
232
		$account = get_post_meta( $post_id, '_publicize_twitter_user', true );
233
		if ( ! empty( $account ) ) {
234
			return $account;
235
		}
236
		return '';
237
	}
238
239
	/**
240
	* Save the Publicized Facebook account when publishing a post
241
	* Use only Personal accounts, not Facebook Pages
242
	*/
243
	function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) {
244
		$connection_meta = $this->get_connection_meta( $connection );
245
		if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) {
246
			$publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' );
247
			if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
248
				$profile_link = $this->get_profile_link( 'facebook', $connection );
249
250
				if ( false !== $profile_link ) {
251
					update_post_meta( $post_id, '_publicize_facebook_user', $profile_link );
252
				}
253
			}
254
		}
255
	}
256
}
257