publicize.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. abstract class Publicize_Base {
  3. /**
  4. * Services that are currently connected to the given user
  5. * through publicize.
  6. */
  7. public $connected_services = array();
  8. /**
  9. * Services that are supported by publicize. They don't
  10. * necessarily need to be connected to the current user.
  11. */
  12. public $services;
  13. /**
  14. * key names for post meta
  15. */
  16. public $ADMIN_PAGE = 'wpas';
  17. public $POST_MESS = '_wpas_mess';
  18. public $POST_SKIP = '_wpas_skip_'; // connection id appended to indicate that a connection should NOT be publicized to
  19. public $POST_DONE = '_wpas_done_'; // connection id appended to indicate a connection has already been publicized to
  20. public $USER_AUTH = 'wpas_authorize';
  21. public $USER_OPT = 'wpas_';
  22. public $PENDING = '_publicize_pending'; // ready for Publicize to do its thing
  23. public $POST_SERVICE_DONE = '_publicize_done_external'; // array of external ids where we've Publicized
  24. /**
  25. * default pieces of the message used in constructing the
  26. * content pushed out to other social networks
  27. */
  28. public $default_prefix = '';
  29. public $default_message = '%title%';
  30. public $default_suffix = ' ';
  31. /**
  32. * What WP capability is require to create/delete global connections?
  33. * All users with this cap can un-globalize all other global connections, and globalize any of their own
  34. * Globalized connections cannot be unselected by users without this capability when publishing
  35. */
  36. public $GLOBAL_CAP = 'edit_others_posts';
  37. /**
  38. * Sets up the basics of Publicize
  39. */
  40. function __construct() {
  41. $this->default_message = self::build_sprintf( array(
  42. /**
  43. * Filter the default Publicize message.
  44. *
  45. * @module publicize
  46. *
  47. * @since 2.0.0
  48. *
  49. * @param string $this->default_message Publicize's default message. Default is the post title.
  50. */
  51. apply_filters( 'wpas_default_message', $this->default_message ),
  52. 'title',
  53. 'url',
  54. ) );
  55. $this->default_prefix = self::build_sprintf( array(
  56. /**
  57. * Filter the message prepended to the Publicize custom message.
  58. *
  59. * @module publicize
  60. *
  61. * @since 2.0.0
  62. *
  63. * @param string $this->default_prefix String prepended to the Publicize custom message.
  64. */
  65. apply_filters( 'wpas_default_prefix', $this->default_prefix ),
  66. 'url',
  67. ) );
  68. $this->default_suffix = self::build_sprintf( array(
  69. /**
  70. * Filter the message appended to the Publicize custom message.
  71. *
  72. * @module publicize
  73. *
  74. * @since 2.0.0
  75. *
  76. * @param string $this->default_suffix String appended to the Publicize custom message.
  77. */
  78. apply_filters( 'wpas_default_suffix', $this->default_suffix ),
  79. 'url',
  80. ) );
  81. /**
  82. * Filter the capability to change global Publicize connection options.
  83. *
  84. * All users with this cap can un-globalize all other global connections, and globalize any of their own
  85. * Globalized connections cannot be unselected by users without this capability when publishing.
  86. *
  87. * @module publicize
  88. *
  89. * @since 2.2.1
  90. *
  91. * @param string $this->GLOBAL_CAP default capability in control of global Publicize connection options. Default to edit_others_posts.
  92. */
  93. $this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP );
  94. // stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta,
  95. // then check meta and publicize based on that. stage 3 implemented on wpcom
  96. add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 );
  97. add_action( 'save_post', array( &$this, 'save_meta' ), 20, 2 );
  98. add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 );
  99. // Connection test callback
  100. add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) );
  101. }
  102. /**
  103. * Functions to be implemented by the extended class (publicize-wpcom or publicize-jetpack)
  104. */
  105. abstract function get_connection_id( $connection );
  106. abstract function connect_url( $service_name );
  107. abstract function disconnect_url( $service_name, $id );
  108. abstract function get_connection_meta( $connection );
  109. abstract function get_services( $filter = 'all' );
  110. abstract function get_connections( $service, $_blog_id = false, $_user_id = false );
  111. abstract function get_connection( $service, $id, $_blog_id = false, $_user_id = false );
  112. abstract function flag_post_for_publicize( $new_status, $old_status, $post );
  113. abstract function test_connection( $service_name, $connection );
  114. abstract function disconnect( $service, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false );
  115. /**
  116. * Shared Functions
  117. */
  118. /**
  119. * Returns an external URL to the connection's profile
  120. */
  121. function get_profile_link( $service_name, $connection ) {
  122. $cmeta = $this->get_connection_meta( $connection );
  123. if ( isset( $cmeta['connection_data']['meta']['link'] ) ) {
  124. if ( 'facebook' == $service_name && 0 === strpos( parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) {
  125. // App-scoped Facebook user IDs are not usable profile links
  126. return false;
  127. }
  128. return $cmeta['connection_data']['meta']['link'];
  129. } elseif ( 'facebook' == $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
  130. return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page'];
  131. } elseif ( 'tumblr' == $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
  132. return 'http://' . $cmeta['connection_data']['meta']['tumblr_base_hostname'];
  133. } elseif ( 'twitter' == $service_name ) {
  134. return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'
  135. } elseif ( 'google_plus' == $service_name && isset( $cmeta['connection_data']['meta']['google_plus_page'] ) ) {
  136. return 'https://plus.google.com/' . $cmeta['connection_data']['meta']['google_plus_page'];
  137. } elseif ( 'google_plus' == $service_name ) {
  138. return 'https://plus.google.com/' . $cmeta['external_id'];
  139. } else if ( 'linkedin' == $service_name ) {
  140. if ( !isset( $cmeta['connection_data']['meta']['profile_url'] ) ) {
  141. return false;
  142. }
  143. $profile_url_query = parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY );
  144. wp_parse_str( $profile_url_query, $profile_url_query_args );
  145. if ( isset( $profile_url_query_args['key'] ) ) {
  146. $id = $profile_url_query_args['key'];
  147. } elseif ( isset( $profile_url_query_args['id'] ) ) {
  148. $id = $profile_url_query_args['id'];
  149. } else {
  150. return false;
  151. }
  152. return esc_url_raw( add_query_arg( 'id', urlencode( $id ), 'http://www.linkedin.com/profile/view' ) );
  153. } else {
  154. return false; // no fallback. we just won't link it
  155. }
  156. }
  157. /**
  158. * Returns a display name for the connection
  159. */
  160. function get_display_name( $service_name, $connection ) {
  161. $cmeta = $this->get_connection_meta( $connection );
  162. if ( 'facebook' === $service_name ) {
  163. if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
  164. return $cmeta['connection_data']['meta']['display_name'];
  165. }
  166. return __( 'Connecting...', 'jetpack' );
  167. }
  168. if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
  169. return $cmeta['connection_data']['meta']['display_name'];
  170. } elseif ( $service_name == 'tumblr' && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
  171. return $cmeta['connection_data']['meta']['tumblr_base_hostname'];
  172. } elseif ( $service_name == 'twitter' ) {
  173. return $cmeta['external_display'];
  174. } else {
  175. $connection_display = $cmeta['external_display'];
  176. if ( empty( $connection_display ) )
  177. $connection_display = $cmeta['external_name'];
  178. return $connection_display;
  179. }
  180. }
  181. public static function get_service_label( $service_name ) {
  182. switch ( $service_name ) {
  183. case 'linkedin':
  184. return 'LinkedIn';
  185. break;
  186. case 'google_plus':
  187. return 'Google+';
  188. break;
  189. case 'twitter':
  190. case 'facebook':
  191. case 'tumblr':
  192. default:
  193. return ucfirst( $service_name );
  194. break;
  195. }
  196. }
  197. function show_options_popup( $service_name, $connection ) {
  198. $cmeta = $this->get_connection_meta( $connection );
  199. // always show if no selection has been made for facebook
  200. if ( 'facebook' == $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) )
  201. return true;
  202. // always show if no selection has been made for tumblr
  203. if ( 'tumblr' == $service_name && empty ( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) )
  204. return true;
  205. // if we have the specific connection info..
  206. if ( isset( $_GET['id'] ) ) {
  207. if ( $cmeta['connection_data']['id'] == $_GET['id'] )
  208. return true;
  209. } else {
  210. // otherwise, just show if this is the completed step / first load
  211. if ( !empty( $_GET['action'] ) && 'completed' == $_GET['action'] && !empty( $_GET['service'] ) && $service_name == $_GET['service'] && ! in_array( $_GET['service'], array( 'facebook', 'tumblr' ) ) )
  212. return true;
  213. }
  214. return false;
  215. }
  216. function user_id() {
  217. global $current_user;
  218. return $current_user->ID;
  219. }
  220. function blog_id() {
  221. return get_current_blog_id();
  222. }
  223. /**
  224. * Returns true if a user has a connection to a particular service, false otherwise
  225. */
  226. function is_enabled( $service, $_blog_id = false, $_user_id = false ) {
  227. if ( !$_blog_id )
  228. $_blog_id = $this->blog_id();
  229. if ( !$_user_id )
  230. $_user_id = $this->user_id();
  231. $connections = $this->get_connections( $service, $_blog_id, $_user_id );
  232. return ( is_array( $connections ) && count( $connections ) > 0 ? true : false );
  233. }
  234. /**
  235. * Fires when a post is saved, checks conditions and saves state in postmeta so that it
  236. * can be picked up later by @see ::publicize_post() on WordPress.com codebase.
  237. */
  238. function save_meta( $post_id, $post ) {
  239. $cron_user = null;
  240. $submit_post = true;
  241. if ( ! $this->post_type_is_publicizeable( $post->post_type ) )
  242. return;
  243. // Don't Publicize during certain contexts:
  244. // - import
  245. if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
  246. $submit_post = false;
  247. }
  248. // - on quick edit, autosave, etc but do fire on p2, quickpress, and instapost ajax
  249. if (
  250. defined( 'DOING_AJAX' )
  251. &&
  252. DOING_AJAX
  253. &&
  254. !did_action( 'p2_ajax' )
  255. &&
  256. !did_action( 'wp_ajax_json_quickpress_post' )
  257. &&
  258. !did_action( 'wp_ajax_instapost_publish' )
  259. &&
  260. !did_action( 'wp_ajax_post_reblog' )
  261. &&
  262. !did_action( 'wp_ajax_press-this-save-post' )
  263. ) {
  264. $submit_post = false;
  265. }
  266. // - bulk edit
  267. if ( isset( $_GET['bulk_edit'] ) ) {
  268. $submit_post = false;
  269. }
  270. // - API/XML-RPC Test Posts
  271. if (
  272. (
  273. defined( 'XMLRPC_REQUEST' )
  274. &&
  275. XMLRPC_REQUEST
  276. ||
  277. defined( 'APP_REQUEST' )
  278. &&
  279. APP_REQUEST
  280. )
  281. &&
  282. 0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' )
  283. ) {
  284. $submit_post = false;
  285. }
  286. // only work with certain statuses (avoids inherits, auto drafts etc)
  287. if ( !in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) ) {
  288. $submit_post = false;
  289. }
  290. // don't publish password protected posts
  291. if ( '' !== $post->post_password ) {
  292. $submit_post = false;
  293. }
  294. // Did this request happen via wp-admin?
  295. $from_web = isset( $_SERVER['REQUEST_METHOD'] )
  296. &&
  297. 'post' == strtolower( $_SERVER['REQUEST_METHOD'] )
  298. &&
  299. isset( $_POST[$this->ADMIN_PAGE] );
  300. if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && isset( $_POST['wpas_title'] ) ) {
  301. if ( empty( $_POST['wpas_title'] ) ) {
  302. delete_post_meta( $post_id, $this->POST_MESS );
  303. } else {
  304. update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) );
  305. }
  306. }
  307. // change current user to provide context for get_services() if we're running during cron
  308. if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
  309. $cron_user = (int) $GLOBALS['user_ID'];
  310. wp_set_current_user( $post->post_author );
  311. }
  312. /**
  313. * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered,
  314. * it will Publicize to everything *except* those marked for skipping.
  315. */
  316. foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
  317. foreach ( $connections as $connection ) {
  318. $connection_data = '';
  319. if ( method_exists( $connection, 'get_meta' ) )
  320. $connection_data = $connection->get_meta( 'connection_data' );
  321. elseif ( ! empty( $connection['connection_data'] ) )
  322. $connection_data = $connection['connection_data'];
  323. /** This action is documented in modules/publicize/ui.php */
  324. if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) {
  325. delete_post_meta( $post_id, $this->PENDING );
  326. continue;
  327. }
  328. if ( !empty( $connection->unique_id ) )
  329. $unique_id = $connection->unique_id;
  330. else if ( !empty( $connection['connection_data']['token_id'] ) )
  331. $unique_id = $connection['connection_data']['token_id'];
  332. // This was a wp-admin request, so we need to check the state of checkboxes
  333. if ( $from_web ) {
  334. // delete stray service-based post meta
  335. delete_post_meta( $post_id, $this->POST_SKIP . $service_name );
  336. // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition
  337. if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) {
  338. // Also make sure that the service-specific input isn't there.
  339. // If the user connected to a new service 'in-page' then a hidden field with the service
  340. // name is added, so we just assume they wanted to Publicize to that service.
  341. if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) {
  342. // Nothing seems to be checked, so we're going to mark this one to be skipped
  343. update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 );
  344. continue;
  345. } else {
  346. // clean up any stray post meta
  347. delete_post_meta( $post_id, $this->POST_SKIP . $unique_id );
  348. }
  349. } else {
  350. // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it
  351. delete_post_meta( $post_id, $this->POST_SKIP . $unique_id );
  352. }
  353. }
  354. /**
  355. * Fires right before the post is processed for Publicize.
  356. * Users may hook in here and do anything else they need to after meta is written,
  357. * and before the post is processed for Publicize.
  358. *
  359. * @since 2.1.2
  360. *
  361. * @param bool $submit_post Should the post be publicized.
  362. * @param int $post->ID Post ID.
  363. * @param string $service_name Service name.
  364. * @param array $connection Array of connection details.
  365. */
  366. do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection );
  367. }
  368. }
  369. if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
  370. wp_set_current_user( $cron_user );
  371. }
  372. // Next up will be ::publicize_post()
  373. }
  374. public function update_published_message( $messages ) {
  375. global $post_type, $post_type_object, $post;
  376. if ( ! $this->post_type_is_publicizeable( $post_type ) ) {
  377. return $messages;
  378. }
  379. $view_post_link_html = '';
  380. $viewable = is_post_type_viewable( $post_type_object );
  381. if ( $viewable ) {
  382. $view_text = esc_html__( 'View post' ); // intentionally omitted domain
  383. if ( 'jetpack-portfolio' == $post_type ) {
  384. $view_text = esc_html__( 'View project', 'jetpack' );
  385. }
  386. $view_post_link_html = sprintf( ' <a href="%1$s">%2$s</a>',
  387. esc_url( get_permalink( $post ) ),
  388. $view_text
  389. );
  390. }
  391. $services = $this->get_publicizing_services( $post->ID );
  392. if ( empty( $services ) ) {
  393. return $messages;
  394. }
  395. $labels = array();
  396. foreach ( $services as $service => $display_names ) {
  397. $labels[] = sprintf(
  398. /* translators: Service name is %1$s, and account name is %2$s. */
  399. esc_html__( '%1$s (%2$s)', 'jetpack' ),
  400. esc_html( $service ),
  401. esc_html( implode( ', ', $display_names ) )
  402. );
  403. }
  404. $messages['post'][6] = sprintf(
  405. /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
  406. esc_html__( 'Post published and sharing on %1$s.', 'jetpack' ),
  407. implode( ', ', $labels )
  408. ) . $view_post_link_html;
  409. if ( $post_type == 'post' && class_exists('Jetpack_Subscriptions' ) ) {
  410. $subscription = Jetpack_Subscriptions::init();
  411. if ( $subscription->should_email_post_to_subscribers( $post ) ) {
  412. $messages['post'][6] = sprintf(
  413. /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
  414. esc_html__( 'Post published, sending emails to subscribers and sharing post on %1$s.', 'jetpack' ),
  415. implode( ', ', $labels )
  416. ) . $view_post_link_html;
  417. }
  418. }
  419. $messages['jetpack-portfolio'][6] = sprintf(
  420. /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */
  421. esc_html__( 'Project published and sharing project on %1$s.', 'jetpack' ),
  422. implode( ', ', $labels )
  423. ) . $view_post_link_html;
  424. return $messages;
  425. }
  426. function get_publicizing_services( $post_id ) {
  427. $services = array();
  428. foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
  429. // services have multiple connections.
  430. foreach ( $connections as $connection ) {
  431. $unique_id = '';
  432. if ( ! empty( $connection->unique_id ) )
  433. $unique_id = $connection->unique_id;
  434. else if ( ! empty( $connection['connection_data']['token_id'] ) )
  435. $unique_id = $connection['connection_data']['token_id'];
  436. // Did we skip this connection?
  437. if ( get_post_meta( $post_id, $this->POST_SKIP . $unique_id, true ) ) {
  438. continue;
  439. }
  440. $services[ $this->get_service_label( $service_name ) ][] = $this->get_display_name( $service_name, $connection );
  441. }
  442. }
  443. return $services;
  444. }
  445. /**
  446. * Is a given post type Publicize-able?
  447. *
  448. * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via
  449. * the publicize_post_types array filter.
  450. *
  451. * @param string $post_type The post type to check.
  452. * @return bool True if the post type can be Publicized.
  453. */
  454. function post_type_is_publicizeable( $post_type ) {
  455. if ( 'post' == $post_type )
  456. return true;
  457. return post_type_supports( $post_type, 'publicize' );
  458. }
  459. function is_valid_facebook_connection( $connection ) {
  460. if ( $this->is_connecting_connection( $connection ) ) {
  461. return true;
  462. }
  463. return isset( $connection['connection_data']['meta']['facebook_page'] );
  464. }
  465. function is_connecting_connection( $connection ) {
  466. return isset( $connection['connection_data']['meta']['options_responses'] );
  467. }
  468. /**
  469. * Runs tests on all the connections and returns the results to the caller
  470. */
  471. function test_publicize_conns() {
  472. $test_results = array();
  473. foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
  474. foreach ( $connections as $connection ) {
  475. $id = $this->get_connection_id( $connection );
  476. $connection_test_passed = true;
  477. $connection_test_message = __( 'This connection is working correctly.' , 'jetpack' );
  478. $user_can_refresh = false;
  479. $refresh_text = '';
  480. $refresh_url = '';
  481. $connection_test_result = true;
  482. if ( method_exists( $this, 'test_connection' ) ) {
  483. $connection_test_result = $this->test_connection( $service_name, $connection );
  484. }
  485. if ( is_wp_error( $connection_test_result ) ) {
  486. $connection_test_passed = false;
  487. $connection_test_message = $connection_test_result->get_error_message();
  488. $error_data = $connection_test_result->get_error_data();
  489. $user_can_refresh = $error_data['user_can_refresh'];
  490. $refresh_text = $error_data['refresh_text'];
  491. $refresh_url = $error_data['refresh_url'];
  492. }
  493. // Mark facebook profiles as deprecated
  494. if ( 'facebook' === $service_name && ! $this->is_valid_facebook_connection( $connection ) ) {
  495. $connection_test_passed = false;
  496. $user_can_refresh = false;
  497. $connection_test_message = __( 'Facebook no longer supports Publicize connections to Facebook Profiles, but you can still connect Facebook Pages. Please select a Facebook Page to publish updates to.', 'jetpack');
  498. }
  499. $unique_id = null;
  500. if ( ! empty( $connection->unique_id ) ) {
  501. $unique_id = $connection->unique_id;
  502. } else if ( ! empty( $connection['connection_data']['token_id'] ) ) {
  503. $unique_id = $connection['connection_data']['token_id'];
  504. }
  505. $test_results[] = array(
  506. 'connectionID' => $id,
  507. 'serviceName' => $service_name,
  508. 'connectionTestPassed' => $connection_test_passed,
  509. 'connectionTestMessage' => esc_attr( $connection_test_message ),
  510. 'userCanRefresh' => $user_can_refresh,
  511. 'refreshText' => esc_attr( $refresh_text ),
  512. 'refreshURL' => $refresh_url,
  513. 'unique_id' => $unique_id,
  514. );
  515. }
  516. }
  517. wp_send_json_success( $test_results );
  518. }
  519. protected static function build_sprintf( $args ) {
  520. $search = array();
  521. $replace = array();
  522. foreach ( $args as $k => $arg ) {
  523. if ( 0 == $k ) {
  524. $string = $arg;
  525. continue;
  526. }
  527. $search[] = "%$arg%";
  528. $replace[] = "%$k\$s";
  529. }
  530. return str_replace( $search, $replace, $string );
  531. }
  532. }
  533. function publicize_calypso_url() {
  534. $calypso_sharing_url = 'https://wordpress.com/sharing/';
  535. if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
  536. $site_suffix = Jetpack::build_raw_urls( home_url() );
  537. } elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
  538. $site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
  539. }
  540. if ( $site_suffix ) {
  541. return $calypso_sharing_url . $site_suffix;
  542. } else {
  543. return $calypso_sharing_url;
  544. }
  545. }