publicize-jetpack.php 29 KB

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