Publicize

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