暂无描述

publicize-jetpack.php 25KB

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