Publicize

The Jetpack by WordPress.com Publicize class.

Defined (1)

The class is defined in the following location(s).

/modules/publicize/publicize-jetpack.php  
  1. class Publicize extends Publicize_Base { 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. add_filter( 'jetpack_xmlrpc_methods', array( $this, 'register_update_publicize_connections_xmlrpc_method' ) ); 
  7.  
  8. add_action( 'load-settings_page_sharing', array( $this, 'admin_page_load' ), 9 ); 
  9.  
  10. add_action( 'wp_ajax_publicize_tumblr_options_page', array( $this, 'options_page_tumblr' ) ); 
  11. add_action( 'wp_ajax_publicize_facebook_options_page', array( $this, 'options_page_facebook' ) ); 
  12. add_action( 'wp_ajax_publicize_twitter_options_page', array( $this, 'options_page_twitter' ) ); 
  13. add_action( 'wp_ajax_publicize_linkedin_options_page', array( $this, 'options_page_linkedin' ) ); 
  14. add_action( 'wp_ajax_publicize_path_options_page', array( $this, 'options_page_path' ) ); 
  15. add_action( 'wp_ajax_publicize_google_plus_options_page', array( $this, 'options_page_google_plus' ) ); 
  16.  
  17. add_action( 'wp_ajax_publicize_tumblr_options_save', array( $this, 'options_save_tumblr' ) ); 
  18. add_action( 'wp_ajax_publicize_facebook_options_save', array( $this, 'options_save_facebook' ) ); 
  19. add_action( 'wp_ajax_publicize_twitter_options_save', array( $this, 'options_save_twitter' ) ); 
  20. add_action( 'wp_ajax_publicize_linkedin_options_save', array( $this, 'options_save_linkedin' ) ); 
  21. add_action( 'wp_ajax_publicize_path_options_save', array( $this, 'options_save_path' ) ); 
  22. add_action( 'wp_ajax_publicize_google_plus_options_save', array( $this, 'options_save_google_plus' ) ); 
  23.  
  24. add_action( 'load-settings_page_sharing', array( $this, 'force_user_connection' ) ); 
  25.  
  26. add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 ); 
  27.  
  28. add_action( 'transition_post_status', array( $this, 'save_publicized' ), 10, 3 ); 
  29.  
  30. add_filter( 'jetpack_twitter_cards_site_tag', array( $this, 'enhaced_twitter_cards_site_tag' ) ); 
  31.  
  32. add_action( 'publicize_save_meta', array( $this, 'save_publicized_twitter_account' ), 10, 4 ); 
  33. add_action( 'publicize_save_meta', array( $this, 'save_publicized_facebook_account' ), 10, 4 ); 
  34.  
  35. add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 ); 
  36.  
  37. include_once ( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' ); 
  38.  
  39. function force_user_connection() { 
  40. global $current_user; 
  41. $user_token = Jetpack_Data::get_access_token( $current_user->ID ); 
  42. $is_user_connected = $user_token && !is_wp_error( $user_token ); 
  43.  
  44. // If the user is already connected via Jetpack, then we're good 
  45. if ( $is_user_connected ) 
  46. return; 
  47.  
  48. // If they're not connected, then remove the Publicize UI and tell them they need to connect first 
  49. global $publicize_ui; 
  50. remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) ); 
  51.  
  52. // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. 
  53. // Jetpack::init()->admin_styles(); 
  54. add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 ); 
  55.  
  56. function admin_page_warning() { 
  57. $jetpack = Jetpack::init(); 
  58. $blog_name = get_bloginfo( 'blogname' ); 
  59. if ( empty( $blog_name ) ) { 
  60. $blog_name = home_url( '/' ); 
  61.  
  62. ?> 
  63. <div id="message" class="updated jetpack-message jp-connect"> 
  64. <div class="jetpack-wrap-container"> 
  65. <div class="jetpack-text-container"> 
  66. <h4> 
  67. <p><?php printf( 
  68. esc_html( wptexturize( __( "To use Publicize, you'll need to link your %s account to your WordPress.com account using the link below.", 'jetpack' ) ) ),  
  69. '<strong>' . esc_html( $blog_name ) . '</strong>' 
  70. ); ?></p> 
  71. <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> 
  72. </h4> 
  73. </div> 
  74. <div class="jetpack-install-container"> 
  75. <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> 
  76. </div> 
  77. </div> 
  78. </div> 
  79. <?php 
  80.  
  81. /** 
  82. * Remove a Publicize connection 
  83. */ 
  84. function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) { 
  85. Jetpack::load_xml_rpc_client(); 
  86. $xml = new Jetpack_IXR_Client(); 
  87. $xml->query( 'jetpack.deletePublicizeConnection', $connection_id ); 
  88.  
  89. if ( ! $xml->isError() ) { 
  90. Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() ); 
  91. } else { 
  92. return false; 
  93.  
  94. function receive_updated_publicize_connections( $publicize_connections ) { 
  95. Jetpack_Options::update_option( 'publicize_connections', $publicize_connections ); 
  96. return true; 
  97.  
  98. function register_update_publicize_connections_xmlrpc_method( $methods ) { 
  99. return array_merge( $methods, array( 
  100. 'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ),  
  101. ) ); 
  102.  
  103. function get_connections( $service_name, $_blog_id = false, $_user_id = false ) { 
  104. $connections = Jetpack_Options::get_option( 'publicize_connections' ); 
  105. $connections_to_return = array(); 
  106. if ( !empty( $connections ) && is_array( $connections ) ) { 
  107. if ( !empty( $connections[$service_name] ) ) { 
  108. foreach( $connections[$service_name] as $id => $connection ) { 
  109. if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) { 
  110. $connections_to_return[$id] = $connection; 
  111. return $connections_to_return; 
  112. return false; 
  113.  
  114. function get_connection_id( $connection ) { 
  115. return $connection['connection_data']['id']; 
  116.  
  117. function get_connection_meta( $connection ) { 
  118. $connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections 
  119. return $connection; 
  120.  
  121. function admin_page_load() { 
  122. if ( isset( $_GET['action'] ) ) { 
  123. if ( isset( $_GET['service'] ) ) 
  124. $service_name = $_GET['service']; 
  125.  
  126. switch ( $_GET['action'] ) { 
  127. case 'error': 
  128. add_action( 'pre_admin_screen_sharing', array( $this, 'display_connection_error' ), 9 ); 
  129. break; 
  130.  
  131. case 'request': 
  132. check_admin_referer( 'keyring-request', 'kr_nonce' ); 
  133. check_admin_referer( "keyring-request-$service_name", 'nonce' ); 
  134.  
  135. $verification = Jetpack::create_nonce( 'publicize' ); 
  136.  
  137. $stats_options = get_option( 'stats_options' ); 
  138. $wpcom_blog_id = Jetpack_Options::get_option('id'); 
  139. $wpcom_blog_id = !empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id']; 
  140.  
  141. $user = wp_get_current_user(); 
  142. $redirect = $this->api_url( $service_name, urlencode_deep( array( 
  143. 'action' => 'request',  
  144. 'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),  
  145. 'for' => 'publicize', // required flag that says this connection is intended for publicize 
  146. 'siteurl' => site_url(),  
  147. 'state' => $user->ID,  
  148. 'blog_id' => $wpcom_blog_id,  
  149. 'secret_1' => $verification['secret_1'],  
  150. 'secret_2' => $verification['secret_2'],  
  151. 'eol' => $verification['eol'],  
  152. ) ) ); 
  153. wp_redirect( $redirect ); 
  154. exit; 
  155. break; 
  156.  
  157. case 'completed': 
  158. Jetpack::load_xml_rpc_client(); 
  159. $xml = new Jetpack_IXR_Client(); 
  160. $xml->query( 'jetpack.fetchPublicizeConnections' ); 
  161.  
  162. if ( ! $xml->isError() ) { 
  163. $response = $xml->getResponse(); 
  164. Jetpack_Options::update_option( 'publicize_connections', $response ); 
  165.  
  166. break; 
  167.  
  168. case 'delete': 
  169. $id = $_GET['id']; 
  170.  
  171. check_admin_referer( 'keyring-request', 'kr_nonce' ); 
  172. check_admin_referer( "keyring-request-$service_name", 'nonce' ); 
  173.  
  174. $this->disconnect( $service_name, $id ); 
  175.  
  176. add_action( 'admin_notices', array( $this, 'display_disconnected' ) ); 
  177. break; 
  178.  
  179. // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. 
  180. // Errors encountered on WordPress.com's end are passed back as a code 
  181. /** 
  182. if ( isset( $_GET['action'] ) && 'error' == $_GET['action'] ) { 
  183. // Load Jetpack's styles to handle the box 
  184. Jetpack::init()->admin_styles(); 
  185. */ 
  186.  
  187. function display_connection_error() { 
  188. $code = false; 
  189. if ( isset( $_GET['service'] ) ) { 
  190. $service_name = $_GET['service']; 
  191. $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 ) ); 
  192. } else { 
  193. if ( isset( $_GET['publicize_error'] ) ) { 
  194. $code = strtolower( $_GET['publicize_error'] ); 
  195. switch ( $code ) { 
  196. case '400': 
  197. $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' ); 
  198. break; 
  199. case 'secret_mismatch': 
  200. $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' ); 
  201. break; 
  202. case 'empty_blog_id': 
  203. $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' ); 
  204. break; 
  205. case 'empty_state': 
  206. $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 <a href="%s">Jetpack page</a> within wp-admin.', 'jetpack' ), Jetpack::admin_url() ); 
  207. break; 
  208. default: 
  209. $error = __( 'Something which should never happen, happened. Sorry about that. If you try again, maybe it will work.', 'jetpack' ); 
  210. break; 
  211. } else { 
  212. $error = __( 'There was a problem connecting with Publicize. Please try again in a moment.', 'jetpack' ); 
  213. // Using the same formatting/style as Jetpack::admin_notices() error 
  214. ?> 
  215. <div id="message" class="jetpack-message jetpack-err"> 
  216. <div class="squeezer"> 
  217. <h4><?php echo wp_kses( $error, array( 'a' => array( 'href' => true ), 'code' => true, 'strong' => true, 'br' => true, 'b' => true ) ); ?></h4> 
  218. <?php if ( $code ) : ?> 
  219. <p><?php printf( __( 'Error code: %s', 'jetpack' ), esc_html( stripslashes( $code ) ) ); ?></p> 
  220. <?php endif; ?> 
  221. </div> 
  222. </div> 
  223. <?php 
  224.  
  225. function display_disconnected() { 
  226. echo "<div class='updated'>\n"; 
  227. echo '<p>' . esc_html( __( 'That connection has been removed.', 'jetpack' ) ) . "</p>\n"; 
  228. echo "</div>\n\n"; 
  229.  
  230. function globalization() { 
  231. if ( 'on' == $_REQUEST['global'] ) { 
  232. $id = $_REQUEST['connection']; 
  233.  
  234. if ( !current_user_can( $this->GLOBAL_CAP ) ) 
  235. return; 
  236.  
  237. Jetpack::load_xml_rpc_client(); 
  238. $xml = new Jetpack_IXR_Client(); 
  239. $xml->query( 'jetpack.globalizePublicizeConnection', $id, 'globalize' ); 
  240.  
  241. if ( !$xml->isError() ) { 
  242. $response = $xml->getResponse(); 
  243. Jetpack_Options::update_option( 'publicize_connections', $response ); 
  244.  
  245. /** 
  246. * Gets a URL to the public-api actions. Works like WP's admin_url 
  247. * @param string $service Shortname of a specific service. 
  248. * @return URL to specific public-api process 
  249. */ 
  250. // on WordPress.com this is/calls Keyring::admin_url 
  251. function api_url( $service = false, $params = array() ) { 
  252. /** 
  253. * Filters the API URL used to interact with WordPress.com. 
  254. * @module publicize 
  255. * @since 2.0.0 
  256. * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL. 
  257. */ 
  258. $url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' ); 
  259.  
  260. if ( $service ) 
  261. $url = add_query_arg( array( 'service' => $service ), $url ); 
  262.  
  263. if ( count ( $params ) ) 
  264. $url = add_query_arg( $params, $url ); 
  265.  
  266. return $url; 
  267.  
  268. function connect_url( $service_name ) { 
  269. return add_query_arg( array( 
  270. 'action' => 'request',  
  271. 'service' => $service_name,  
  272. 'kr_nonce' => wp_create_nonce( 'keyring-request' ),  
  273. 'nonce' => wp_create_nonce( "keyring-request-$service_name" ),  
  274. ), menu_page_url( 'sharing', false ) ); 
  275.  
  276. function refresh_url( $service_name ) { 
  277. return add_query_arg( array( 
  278. 'action' => 'request',  
  279. 'service' => $service_name,  
  280. 'kr_nonce' => wp_create_nonce( 'keyring-request' ),  
  281. 'refresh' => 1,  
  282. 'for' => 'publicize',  
  283. 'nonce' => wp_create_nonce( "keyring-request-$service_name" ),  
  284. ), admin_url( 'options-general.php?page=sharing' ) ); 
  285.  
  286. function disconnect_url( $service_name, $id ) { 
  287. return add_query_arg( array ( 
  288. 'action' => 'delete',  
  289. 'service' => $service_name,  
  290. 'id' => $id,  
  291. 'kr_nonce' => wp_create_nonce( 'keyring-request' ),  
  292. 'nonce' => wp_create_nonce( "keyring-request-$service_name" ),  
  293. ), menu_page_url( 'sharing', false ) ); 
  294.  
  295. function get_services( $filter ) { 
  296. if ( !in_array( $filter, array( 'all', 'connected' ) ) ) 
  297. $filter = 'all'; 
  298.  
  299. $services = array( 
  300. 'facebook' => array(),  
  301. 'twitter' => array(),  
  302. 'linkedin' => array(),  
  303. 'tumblr' => array(),  
  304. 'path' => array(),  
  305. 'google_plus' => array(),  
  306. ); 
  307.  
  308. if ( 'all' == $filter ) { 
  309. return $services; 
  310. } else { 
  311. $connected_services = array(); 
  312. foreach ( $services as $service => $empty ) { 
  313. $connections = $this->get_connections( $service ); 
  314. if ( $connections ) 
  315. $connected_services[$service] = $connections; 
  316. return $connected_services; 
  317.  
  318. function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) { 
  319. // Stub 
  320.  
  321. function flag_post_for_publicize( $new_status, $old_status, $post ) { 
  322. // Stub only. Doesn't need to do anything on Jetpack Client 
  323.  
  324. function test_connection( $service_name, $connection ) { 
  325. $connection_test_passed = true; 
  326. $connection_test_message = ''; 
  327. $user_can_refresh = false; 
  328.  
  329. $id = $this->get_connection_id( $connection ); 
  330.  
  331. Jetpack::load_xml_rpc_client(); 
  332. $xml = new Jetpack_IXR_Client(); 
  333. $xml->query( 'jetpack.testPublicizeConnection', $id ); 
  334.  
  335. if ( $xml->isError() ) { 
  336. $xml_response = $xml->getResponse(); 
  337. $connection_test_message = $xml_response['faultString']; 
  338. $connection_test_passed = false; 
  339.  
  340. // Bail if all is well 
  341. if ( $connection_test_passed ) { 
  342. return true; 
  343.  
  344. // Set up refresh if the user can 
  345. $user_can_refresh = current_user_can( $this->GLOBAL_CAP ); 
  346. if ( $user_can_refresh ) { 
  347. $nonce = wp_create_nonce( "keyring-request-" . $service_name ); 
  348. $refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) ); 
  349. $refresh_url = $this->refresh_url( $service_name ); 
  350.  
  351. $error_data = array( 
  352. 'user_can_refresh' => $user_can_refresh,  
  353. 'refresh_text' => $refresh_text,  
  354. 'refresh_url' => $refresh_url 
  355. ); 
  356.  
  357. return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data ); 
  358.  
  359. /** 
  360. * Save a flag locally to indicate that this post has already been Publicized via the selected 
  361. * connections. 
  362. */ 
  363. function save_publicized( $new_status, $old_status, $post ) { 
  364. // Only do this when a post transitions to being published 
  365. if ( 'publish' == $new_status && 'publish' != $old_status ) { 
  366. update_post_meta( $post->ID, $this->POST_DONE . 'all', true ); 
  367.  
  368. /** 
  369. * Options Code 
  370. */ 
  371.  
  372. function options_page_facebook() { 
  373. $connected_services = Jetpack_Options::get_option( 'publicize_connections' ); 
  374. $connection = $connected_services['facebook'][$_REQUEST['connection']]; 
  375. $options_to_show = ( ! empty( $connection['connection_data']['meta']['options_responses'] ) ? $connection['connection_data']['meta']['options_responses'] : false ); 
  376.  
  377. // Nonce check 
  378. check_admin_referer( 'options_page_facebook_' . $_REQUEST['connection'] ); 
  379.  
  380. $me = ( ! empty( $options_to_show[0] ) ? $options_to_show[0] : false ); 
  381. $pages = ( ! empty( $options_to_show[1]['data'] ) ? $options_to_show[1]['data'] : false ); 
  382.  
  383. $profile_checked = true; 
  384. $page_selected = false; 
  385.  
  386. if ( !empty( $connection['connection_data']['meta']['facebook_page'] ) ) { 
  387. $found = false; 
  388. if ( is_array( $pages->data ) ) { 
  389. foreach ( $pages->data as $page ) { 
  390. if ( $page->id == $connection['connection_data']['meta']['facebook_page'] ) { 
  391. $found = true; 
  392. break; 
  393.  
  394. if ( $found ) { 
  395. $profile_checked = false; 
  396. $page_selected = $connection['connection_data']['meta']['facebook_page']; 
  397.  
  398. ?> 
  399.  
  400. <div id="thickbox-content"> 
  401.  
  402. <?php 
  403. ob_start(); 
  404. Publicize_UI::connected_notice( 'Facebook' ); 
  405. $update_notice = ob_get_clean(); 
  406.  
  407. if ( ! empty( $update_notice ) ) 
  408. echo $update_notice; 
  409. ?> 
  410.  
  411. <?php if ( !empty( $me['name'] ) ) : ?> 
  412. <p><?php printf( 
  413. esc_html__( 'Publicize to my %s:', 'jetpack' ),  
  414. '<strong>' . esc_html__( 'Facebook Wall', 'jetpack' ) . '</strong>' 
  415. ); ?></p> 
  416. <table id="option-profile"> 
  417. <tbody> 
  418. <tr> 
  419. <td class="radio"><input type="radio" name="option" data-type="profile" id="<?php echo esc_attr( $me['id'] ) ?>" value="" <?php checked( $profile_checked, true ); ?> /></td> 
  420. <td class="thumbnail"><label for="<?php echo esc_attr( $me['id'] ) ?>"><img src="<?php echo esc_url( $me['picture']['data']['url'] ) ?>" width="50" height="50" /></label></td> 
  421. <td class="details"><label for="<?php echo esc_attr( $me['id'] ) ?>"><?php echo esc_html( $me['name'] ) ?></label></td> 
  422. </tr> 
  423. </tbody> 
  424. </table> 
  425. <?php endif; ?> 
  426.  
  427. <?php if ( $pages ) : ?> 
  428.  
  429. <p><?php printf( 
  430. esc_html__( 'Publicize to my %s:', 'jetpack' ),  
  431. '<strong>' . esc_html__( 'Facebook Page', 'jetpack' ) . '</strong>' 
  432. ); ?></p> 
  433. <table id="option-fb-fanpage"> 
  434. <tbody> 
  435.  
  436. <?php foreach ( $pages as $i => $page ) : ?> 
  437. <?php if ( ! ( $i % 2 ) ) : ?> 
  438. <tr> 
  439. <?php endif; ?> 
  440. <td class="radio"><input type="radio" name="option" data-type="page" id="<?php echo esc_attr( $page['id'] ) ?>" value="<?php echo esc_attr( $page['id'] ) ?>" <?php checked( $page_selected && $page_selected == $page['id'], true ); ?> /></td> 
  441. <td class="thumbnail"><label for="<?php echo esc_attr( $page['id'] ) ?>"><img src="<?php echo esc_url( str_replace( '_s', '_q', $page['picture']['data']['url'] ) ) ?>" width="50" height="50" /></label></td> 
  442. <td class="details"> 
  443. <label for="<?php echo esc_attr( $page['id'] ) ?>"> 
  444. <span class="name"><?php echo esc_html( $page['name'] ) ?></span><br/> 
  445. <span class="category"><?php echo esc_html( $page['category'] ) ?></span> 
  446. </label> 
  447. </td> 
  448. <?php if ( ( $i % 2 ) || ( $i == count( $pages ) - 1 ) ): ?> 
  449. </tr> 
  450. <?php endif; ?> 
  451. <?php endforeach; ?> 
  452.  
  453. </tbody> 
  454. </table> 
  455.  
  456. <?php endif; ?> 
  457.  
  458. <?php Publicize_UI::global_checkbox( 'facebook', $_REQUEST['connection'] ); ?> 
  459.  
  460. <p style="text-align: center;"> 
  461. <input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>" class="button fb-options save-options" name="save" data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>" rel="<?php echo wp_create_nonce('save_fb_token_' . $_REQUEST['connection'] ) ?>" /> 
  462. </p><br/> 
  463. </div> 
  464.  
  465. <?php 
  466.  
  467. function options_save_facebook() { 
  468. // Nonce check 
  469. check_admin_referer( 'save_fb_token_' . $_REQUEST['connection'] ); 
  470.  
  471. $id = $_POST['connection']; 
  472.  
  473. // Check for a numeric page ID 
  474. $page_id = $_POST['selected_id']; 
  475. if ( !ctype_digit( $page_id ) ) 
  476. die( 'Security check' ); 
  477.  
  478. if ( isset( $_POST['selected_id'] ) && 'profile' == $_POST['type'] ) { 
  479. // Publish to User Wall/Profile 
  480. $options = array( 
  481. 'facebook_page' => null,  
  482. 'facebook_profile' => true 
  483. ); 
  484.  
  485. } else { 
  486. if ( 'page' != $_POST['type'] || !isset( $_POST['selected_id'] ) ) { 
  487. return; 
  488.  
  489. // Publish to Page 
  490. $options = array( 
  491. 'facebook_page' => $page_id,  
  492. 'facebook_profile' => null 
  493. ); 
  494.  
  495. Jetpack::load_xml_rpc_client(); 
  496. $xml = new Jetpack_IXR_Client(); 
  497. $xml->query( 'jetpack.setPublicizeOptions', $id, $options ); 
  498.  
  499. if ( !$xml->isError() ) { 
  500. $response = $xml->getResponse(); 
  501. Jetpack_Options::update_option( 'publicize_connections', $response ); 
  502.  
  503. $this->globalization(); 
  504.  
  505. function options_page_tumblr() { 
  506. // Nonce check 
  507. check_admin_referer( 'options_page_tumblr_' . $_REQUEST['connection'] ); 
  508.  
  509. $connected_services = Jetpack_Options::get_option( 'publicize_connections' ); 
  510. $connection = $connected_services['tumblr'][$_POST['connection']]; 
  511. $options_to_show = $connection['connection_data']['meta']['options_responses']; 
  512. $request = $options_to_show[0]; 
  513.  
  514. $blogs = $request['response']['user']['blogs']; 
  515.  
  516. $blog_selected = false; 
  517.  
  518. if ( !empty( $connection['connection_data']['meta']['tumblr_base_hostname'] ) ) { 
  519. foreach ( $blogs as $blog ) { 
  520. if ( $connection['connection_data']['meta']['tumblr_base_hostname'] == $this->get_basehostname( $blog['url'] ) ) { 
  521. $blog_selected = $connection['connection_data']['meta']['tumblr_base_hostname']; 
  522. break; 
  523.  
  524.  
  525. // Use their Primary blog if they haven't selected one yet 
  526. if ( !$blog_selected ) { 
  527. foreach ( $blogs as $blog ) { 
  528. if ( $blog['primary'] ) 
  529. $blog_selected = $this->get_basehostname( $blog['url'] ); 
  530. } ?> 
  531.  
  532. <div id="thickbox-content"> 
  533.  
  534. <?php 
  535. ob_start(); 
  536. Publicize_UI::connected_notice( 'Tumblr' ); 
  537. $update_notice = ob_get_clean(); 
  538.  
  539. if ( ! empty( $update_notice ) ) 
  540. echo $update_notice; 
  541. ?> 
  542.  
  543. <p><?php printf( 
  544. esc_html__( 'Publicize to my %s:', 'jetpack' ),  
  545. '<strong>' . esc_html__( 'Tumblr blog', 'jetpack' ) . '</strong>' 
  546. ); ?></p> 
  547.  
  548. <ul id="option-tumblr-blog"> 
  549.  
  550. <?php 
  551. foreach ( $blogs as $blog ) { 
  552. $url = $this->get_basehostname( $blog['url'] ); ?> 
  553. <li> 
  554. <input type="radio" name="option" data-type="blog" id="<?php echo esc_attr( $url ) ?>" value="<?php echo esc_attr( $url ) ?>" <?php checked( $blog_selected == $url, true ); ?> /> 
  555. <label for="<?php echo esc_attr( $url ) ?>"><span class="name"><?php echo esc_html( $blog['title'] ) ?></span></label> 
  556. </li> 
  557. <?php } ?> 
  558.  
  559. </ul> 
  560.  
  561. <?php Publicize_UI::global_checkbox( 'tumblr', $_REQUEST['connection'] ); ?> 
  562.  
  563. <p style="text-align: center;"> 
  564. <input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>" class="button tumblr-options save-options" name="save" data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>" rel="<?php echo wp_create_nonce( 'save_tumblr_blog_' . $_REQUEST['connection'] ) ?>" /> 
  565. </p> <br /> 
  566. </div> 
  567.  
  568. <?php 
  569.  
  570. function get_basehostname( $url ) { 
  571. return parse_url( $url, PHP_URL_HOST ); 
  572.  
  573. function options_save_tumblr() { 
  574. // Nonce check 
  575. check_admin_referer( 'save_tumblr_blog_' . $_REQUEST['connection'] ); 
  576.  
  577. $id = $_POST['connection']; 
  578.  
  579. $options = array( 'tumblr_base_hostname' => $_POST['selected_id'] ); 
  580.  
  581. Jetpack::load_xml_rpc_client(); 
  582. $xml = new Jetpack_IXR_Client(); 
  583. $xml->query( 'jetpack.setPublicizeOptions', $id, $options ); 
  584.  
  585. if ( !$xml->isError() ) { 
  586. $response = $xml->getResponse(); 
  587. Jetpack_Options::update_option( 'publicize_connections', $response ); 
  588.  
  589. $this->globalization(); 
  590.  
  591. function options_page_twitter() { Publicize_UI::options_page_other( 'twitter' ); } 
  592. function options_page_linkedin() { Publicize_UI::options_page_other( 'linkedin' ); } 
  593. function options_page_path() { Publicize_UI::options_page_other( 'path' ); } 
  594. function options_page_google_plus() { Publicize_UI::options_page_other( 'google_plus' ); } 
  595.  
  596. function options_save_twitter() { $this->options_save_other( 'twitter' ); } 
  597. function options_save_linkedin() { $this->options_save_other( 'linkedin' ); } 
  598. function options_save_path() { $this->options_save_other( 'path' ); } 
  599. function options_save_google_plus() { $this->options_save_other( 'google_plus' ); } 
  600.  
  601. function options_save_other( $service_name ) { 
  602. // Nonce check 
  603. check_admin_referer( 'save_' . $service_name . '_token_' . $_REQUEST['connection'] ); 
  604. $this->globalization(); 
  605.  
  606. /** 
  607. * Already-published posts should not be Publicized by default. This filter sets checked to 
  608. * false if a post has already been published. 
  609. */ 
  610. function publicize_checkbox_default( $checked, $post_id, $name, $connection ) { 
  611. if ( 'publish' == get_post_status( $post_id ) ) 
  612. return false; 
  613.  
  614. return $checked; 
  615.  
  616. /** 
  617. * If there's only one shared connection to Twitter set it as twitter:site tag. 
  618. */ 
  619. function enhaced_twitter_cards_site_tag( $tag ) { 
  620. $custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' ); 
  621. if( ! empty( $custom_site_tag ) ) 
  622. return $tag; 
  623. if ( ! $this->is_enabled('twitter') ) 
  624. return $tag; 
  625. $connections = $this->get_connections( 'twitter' ); 
  626. foreach ( $connections as $connection ) { 
  627. $connection_meta = $this->get_connection_meta( $connection ); 
  628. if ( 0 == $connection_meta['connection_data']['user_id'] ) { 
  629. // If the connection is shared 
  630. return $this->get_display_name( 'twitter', $connection ); 
  631. return $tag; 
  632.  
  633. function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) { 
  634. if ( 'twitter' == $service_name && $submit_post ) { 
  635. $connection_meta = $this->get_connection_meta( $connection ); 
  636. $publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' ); 
  637. if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) { 
  638. update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) ); 
  639.  
  640. function get_publicized_twitter_account( $account, $post_id ) { 
  641. if ( ! empty( $account ) ) { 
  642. return $account; 
  643. $account = get_post_meta( $post_id, '_publicize_twitter_user', true ); 
  644. if ( ! empty( $account ) ) { 
  645. return $account; 
  646. return ''; 
  647.  
  648. /** 
  649. * Save the Publicized Facebook account when publishing a post 
  650. * Use only Personal accounts, not Facebook Pages 
  651. */ 
  652. function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) { 
  653. $connection_meta = $this->get_connection_meta( $connection ); 
  654. if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) { 
  655. $publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' ); 
  656. if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) { 
  657. $profile_link = $this->get_profile_link( 'facebook', $connection ); 
  658.  
  659. if ( false !== $profile_link ) { 
  660. update_post_meta( $post_id, '_publicize_facebook_user', $profile_link );