ID ); $is_user_connected = $user_token && !is_wp_error( $user_token ); // If the user is already connected via Jetpack, then we're good if ( $is_user_connected ) return; // If they're not connected, then remove the Publicize UI and tell them they need to connect first global $publicize_ui; remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) ); // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. // Jetpack::init()->admin_styles(); add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 ); } function admin_page_warning() { $jetpack = Jetpack::init(); $blog_name = get_bloginfo( 'blogname' ); if ( empty( $blog_name ) ) { $blog_name = home_url( '/' ); } ?>

' . esc_html( $blog_name ) . '' ); ?>

query( 'jetpack.deletePublicizeConnection', $connection_id ); if ( ! $xml->isError() ) { Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() ); } else { return false; } } function receive_updated_publicize_connections( $publicize_connections ) { Jetpack_Options::update_option( 'publicize_connections', $publicize_connections ); return true; } function register_update_publicize_connections_xmlrpc_method( $methods ) { return array_merge( $methods, array( 'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ), ) ); } function get_connections( $service_name, $_blog_id = false, $_user_id = false ) { $connections = Jetpack_Options::get_option( 'publicize_connections' ); $connections_to_return = array(); if ( !empty( $connections ) && is_array( $connections ) ) { if ( !empty( $connections[$service_name] ) ) { foreach( $connections[$service_name] as $id => $connection ) { if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) { $connections_to_return[$id] = $connection; } } } return $connections_to_return; } return false; } function get_connection_id( $connection ) { return $connection['connection_data']['id']; } function get_connection_meta( $connection ) { $connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections return $connection; } function admin_page_load() { if ( isset( $_GET['action'] ) ) { if ( isset( $_GET['service'] ) ) $service_name = $_GET['service']; switch ( $_GET['action'] ) { case 'error': add_action( 'pre_admin_screen_sharing', array( $this, 'display_connection_error' ), 9 ); break; case 'request': check_admin_referer( 'keyring-request', 'kr_nonce' ); check_admin_referer( "keyring-request-$service_name", 'nonce' ); $verification = Jetpack::create_nonce( 'publicize' ); $stats_options = get_option( 'stats_options' ); $wpcom_blog_id = Jetpack_Options::get_option('id'); $wpcom_blog_id = !empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id']; $user = wp_get_current_user(); $redirect = $this->api_url( $service_name, urlencode_deep( array( 'action' => 'request', 'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ), 'for' => 'publicize', // required flag that says this connection is intended for publicize 'siteurl' => site_url(), 'state' => $user->ID, 'blog_id' => $wpcom_blog_id, 'secret_1' => $verification['secret_1'], 'secret_2' => $verification['secret_2'], 'eol' => $verification['eol'], ) ) ); wp_redirect( $redirect ); exit; break; case 'completed': Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query( 'jetpack.fetchPublicizeConnections' ); if ( ! $xml->isError() ) { $response = $xml->getResponse(); Jetpack_Options::update_option( 'publicize_connections', $response ); } break; case 'delete': $id = $_GET['id']; check_admin_referer( 'keyring-request', 'kr_nonce' ); check_admin_referer( "keyring-request-$service_name", 'nonce' ); $this->disconnect( $service_name, $id ); add_action( 'admin_notices', array( $this, 'display_disconnected' ) ); break; } } // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. // Errors encountered on WordPress.com's end are passed back as a code /* if ( isset( $_GET['action'] ) && 'error' == $_GET['action'] ) { // Load Jetpack's styles to handle the box Jetpack::init()->admin_styles(); } */ } function display_connection_error() { $code = false; if ( isset( $_GET['service'] ) ) { $service_name = $_GET['service']; $error = sprintf( __( 'There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'jetpack' ), Publicize::get_service_label( $service_name ) ); } else { if ( isset( $_GET['publicize_error'] ) ) { $code = strtolower( $_GET['publicize_error'] ); switch ( $code ) { case '400': $error = __( 'An invalid request was made. This normally means that something intercepted or corrupted the request from your server to the Jetpack Server. Try again and see if it works this time.', 'jetpack' ); break; case 'secret_mismatch': $error = __( 'We could not verify that your server is making an authorized request. Please try again, and make sure there is nothing interfering with requests from your server to the Jetpack Server.', 'jetpack' ); break; case 'empty_blog_id': $error = __( 'No blog_id was included in your request. Please try disconnecting Jetpack from WordPress.com and then reconnecting it. Once you have done that, try connecting Publicize again.', 'jetpack' ); break; case 'empty_state': $error = sprintf( __( 'No user information was included in your request. Please make sure that your user account has connected to Jetpack. Connect your user account by going to the Jetpack page within wp-admin.', 'jetpack' ), Jetpack::admin_url() ); break; default: $error = __( 'Something which should never happen, happened. Sorry about that. If you try again, maybe it will work.', 'jetpack' ); break; } } else { $error = __( 'There was a problem connecting with Publicize. Please try again in a moment.', 'jetpack' ); } } // Using the same formatting/style as Jetpack::admin_notices() error ?>

array( 'href' => true ), 'code' => true, 'strong' => true, 'br' => true, 'b' => true ) ); ?>

\n"; echo '

' . esc_html( __( 'That connection has been removed.', 'jetpack' ) ) . "

\n"; echo "\n\n"; } function globalization() { if ( 'on' == $_REQUEST['global'] ) { $id = $_REQUEST['connection']; if ( !current_user_can( $this->GLOBAL_CAP ) ) return; Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query( 'jetpack.globalizePublicizeConnection', $id, 'globalize' ); if ( !$xml->isError() ) { $response = $xml->getResponse(); Jetpack_Options::update_option( 'publicize_connections', $response ); } } } /** * Gets a URL to the public-api actions. Works like WP's admin_url * * @param string $service Shortname of a specific service. * @return URL to specific public-api process */ // on WordPress.com this is/calls Keyring::admin_url function api_url( $service = false, $params = array() ) { /** * Filters the API URL used to interact with WordPress.com. * * @module publicize * * @since 2.0.0 * * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL. */ $url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' ); if ( $service ) $url = add_query_arg( array( 'service' => $service ), $url ); if ( count ( $params ) ) $url = add_query_arg( $params, $url ); return $url; } function connect_url( $service_name ) { return add_query_arg( array( 'action' => 'request', 'service' => $service_name, 'kr_nonce' => wp_create_nonce( 'keyring-request' ), 'nonce' => wp_create_nonce( "keyring-request-$service_name" ), ), menu_page_url( 'sharing', false ) ); } function refresh_url( $service_name ) { return add_query_arg( array( 'action' => 'request', 'service' => $service_name, 'kr_nonce' => wp_create_nonce( 'keyring-request' ), 'refresh' => 1, 'for' => 'publicize', 'nonce' => wp_create_nonce( "keyring-request-$service_name" ), ), admin_url( 'options-general.php?page=sharing' ) ); } function disconnect_url( $service_name, $id ) { return add_query_arg( array ( 'action' => 'delete', 'service' => $service_name, 'id' => $id, 'kr_nonce' => wp_create_nonce( 'keyring-request' ), 'nonce' => wp_create_nonce( "keyring-request-$service_name" ), ), menu_page_url( 'sharing', false ) ); } function get_services( $filter ) { if ( !in_array( $filter, array( 'all', 'connected' ) ) ) $filter = 'all'; $services = array( 'facebook' => array(), 'twitter' => array(), 'linkedin' => array(), 'tumblr' => array(), 'path' => array(), 'google_plus' => array(), ); if ( 'all' == $filter ) { return $services; } else { $connected_services = array(); foreach ( $services as $service => $empty ) { $connections = $this->get_connections( $service ); if ( $connections ) $connected_services[$service] = $connections; } return $connected_services; } } function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) { // Stub } function flag_post_for_publicize( $new_status, $old_status, $post ) { // Stub only. Doesn't need to do anything on Jetpack Client } function test_connection( $service_name, $connection ) { $connection_test_passed = true; $connection_test_message = ''; $user_can_refresh = false; $id = $this->get_connection_id( $connection ); Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query( 'jetpack.testPublicizeConnection', $id ); if ( $xml->isError() ) { $xml_response = $xml->getResponse(); $connection_test_message = $xml_response['faultString']; $connection_test_passed = false; } // Bail if all is well if ( $connection_test_passed ) { return true; } // Set up refresh if the user can $user_can_refresh = current_user_can( $this->GLOBAL_CAP ); if ( $user_can_refresh ) { $nonce = wp_create_nonce( "keyring-request-" . $service_name ); $refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) ); $refresh_url = $this->refresh_url( $service_name ); } $error_data = array( 'user_can_refresh' => $user_can_refresh, 'refresh_text' => $refresh_text, 'refresh_url' => $refresh_url ); return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data ); } /** * Save a flag locally to indicate that this post has already been Publicized via the selected * connections. */ function save_publicized( $new_status, $old_status, $post ) { // Only do this when a post transitions to being published if ( 'publish' == $new_status && 'publish' != $old_status ) { update_post_meta( $post->ID, $this->POST_DONE . 'all', true ); } } /** * Options Code */ function options_page_facebook() { $connected_services = Jetpack_Options::get_option( 'publicize_connections' ); $connection = $connected_services['facebook'][$_REQUEST['connection']]; $options_to_show = ( ! empty( $connection['connection_data']['meta']['options_responses'] ) ? $connection['connection_data']['meta']['options_responses'] : false ); // Nonce check check_admin_referer( 'options_page_facebook_' . $_REQUEST['connection'] ); $me = ( ! empty( $options_to_show[0] ) ? $options_to_show[0] : false ); $pages = ( ! empty( $options_to_show[1]['data'] ) ? $options_to_show[1]['data'] : false ); $profile_checked = true; $page_selected = false; if ( !empty( $connection['connection_data']['meta']['facebook_page'] ) ) { $found = false; if ( is_array( $pages->data ) ) { foreach ( $pages->data as $page ) { if ( $page->id == $connection['connection_data']['meta']['facebook_page'] ) { $found = true; break; } } } if ( $found ) { $profile_checked = false; $page_selected = $connection['connection_data']['meta']['facebook_page']; } } ?>

' . esc_html__( 'Facebook Wall', 'jetpack' ) . '' ); ?>

/>

' . esc_html__( 'Facebook Page', 'jetpack' ) . '' ); ?>

$page ) : ?>
/>


null, 'facebook_profile' => true ); } else { if ( 'page' != $_POST['type'] || !isset( $_POST['selected_id'] ) ) { return; } // Publish to Page $options = array( 'facebook_page' => $page_id, 'facebook_profile' => null ); } Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query( 'jetpack.setPublicizeOptions', $id, $options ); if ( !$xml->isError() ) { $response = $xml->getResponse(); Jetpack_Options::update_option( 'publicize_connections', $response ); } $this->globalization(); } function options_page_tumblr() { // Nonce check check_admin_referer( 'options_page_tumblr_' . $_REQUEST['connection'] ); $connected_services = Jetpack_Options::get_option( 'publicize_connections' ); $connection = $connected_services['tumblr'][$_POST['connection']]; $options_to_show = $connection['connection_data']['meta']['options_responses']; $request = $options_to_show[0]; $blogs = $request['response']['user']['blogs']; $blog_selected = false; if ( !empty( $connection['connection_data']['meta']['tumblr_base_hostname'] ) ) { foreach ( $blogs as $blog ) { if ( $connection['connection_data']['meta']['tumblr_base_hostname'] == $this->get_basehostname( $blog['url'] ) ) { $blog_selected = $connection['connection_data']['meta']['tumblr_base_hostname']; break; } } } // Use their Primary blog if they haven't selected one yet if ( !$blog_selected ) { foreach ( $blogs as $blog ) { if ( $blog['primary'] ) $blog_selected = $this->get_basehostname( $blog['url'] ); } } ?>

' . esc_html__( 'Tumblr blog', 'jetpack' ) . '' ); ?>


$_POST['selected_id'] ); Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query( 'jetpack.setPublicizeOptions', $id, $options ); if ( !$xml->isError() ) { $response = $xml->getResponse(); Jetpack_Options::update_option( 'publicize_connections', $response ); } $this->globalization(); } function options_page_twitter() { Publicize_UI::options_page_other( 'twitter' ); } function options_page_linkedin() { Publicize_UI::options_page_other( 'linkedin' ); } function options_page_path() { Publicize_UI::options_page_other( 'path' ); } function options_page_google_plus() { Publicize_UI::options_page_other( 'google_plus' ); } function options_save_twitter() { $this->options_save_other( 'twitter' ); } function options_save_linkedin() { $this->options_save_other( 'linkedin' ); } function options_save_path() { $this->options_save_other( 'path' ); } function options_save_google_plus() { $this->options_save_other( 'google_plus' ); } function options_save_other( $service_name ) { // Nonce check check_admin_referer( 'save_' . $service_name . '_token_' . $_REQUEST['connection'] ); $this->globalization(); } /** * Already-published posts should not be Publicized by default. This filter sets checked to * false if a post has already been published. */ function publicize_checkbox_default( $checked, $post_id, $name, $connection ) { if ( 'publish' == get_post_status( $post_id ) ) return false; return $checked; } /** * If there's only one shared connection to Twitter set it as twitter:site tag. */ function enhaced_twitter_cards_site_tag( $tag ) { $custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' ); if( ! empty( $custom_site_tag ) ) return $tag; if ( ! $this->is_enabled('twitter') ) return $tag; $connections = $this->get_connections( 'twitter' ); foreach ( $connections as $connection ) { $connection_meta = $this->get_connection_meta( $connection ); if ( 0 == $connection_meta['connection_data']['user_id'] ) { // If the connection is shared return $this->get_display_name( 'twitter', $connection ); } } return $tag; } function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) { if ( 'twitter' == $service_name && $submit_post ) { $connection_meta = $this->get_connection_meta( $connection ); $publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' ); if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) { update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) ); } } } function get_publicized_twitter_account( $account, $post_id ) { if ( ! empty( $account ) ) { return $account; } $account = get_post_meta( $post_id, '_publicize_twitter_user', true ); if ( ! empty( $account ) ) { return $account; } return ''; } /** * Save the Publicized Facebook account when publishing a post * Use only Personal accounts, not Facebook Pages */ function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) { $connection_meta = $this->get_connection_meta( $connection ); if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) { $publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' ); if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) { $profile_link = $this->get_profile_link( 'facebook', $connection ); if ( false !== $profile_link ) { update_post_meta( $post_id, '_publicize_facebook_user', $profile_link ); } } } } }