ui.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. <?php
  2. /**
  3. * Only user facing pieces of Publicize are found here.
  4. */
  5. class Publicize_UI {
  6. /**
  7. * Contains an instance of class 'publicize' which loads Keyring, sets up services, etc.
  8. */
  9. public $publicize;
  10. /**
  11. * Hooks into WordPress to display the various pieces of UI and load our assets
  12. */
  13. function __construct() {
  14. global $publicize;
  15. $this->publicize = $publicize = new Publicize;
  16. add_action( 'init', array( $this, 'init' ) );
  17. }
  18. function init() {
  19. // Show only to users with the capability required to manage their Publicize connections.
  20. /**
  21. * Filter what user capability is required to use the publicize form on the edit post page. Useful if publish post capability has been removed from role.
  22. *
  23. * @module publicize
  24. *
  25. * @since 4.1.0
  26. *
  27. * @param string $capability User capability needed to use publicize
  28. */
  29. $capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' );
  30. if ( ! current_user_can( $capability ) ) {
  31. return;
  32. }
  33. // assets (css, js)
  34. add_action( 'load-settings_page_sharing', array( &$this, 'load_assets' ) );
  35. add_action( 'admin_head-post.php', array( &$this, 'post_page_metabox_assets' ) );
  36. add_action( 'admin_head-post-new.php', array( &$this, 'post_page_metabox_assets' ) );
  37. // management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen)
  38. add_action( 'pre_admin_screen_sharing', array( &$this, 'admin_page' ) );
  39. add_action( 'post_submitbox_misc_actions', array( &$this, 'post_page_metabox' ) );
  40. }
  41. /**
  42. * If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
  43. */
  44. function sharing_menu() {
  45. add_submenu_page( 'options-general.php', __( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'sharing', array( &$this, 'management_page' ) );
  46. }
  47. /**
  48. * Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
  49. */
  50. function management_page() { ?>
  51. <div class="wrap">
  52. <div class="icon32" id="icon-options-general"><br /></div>
  53. <h1><?php _e( 'Sharing Settings', 'jetpack' ); ?></h1>
  54. <?php
  55. /** This action is documented in modules/sharedaddy/sharing.php */
  56. do_action( 'pre_admin_screen_sharing' );
  57. ?>
  58. </div> <?php
  59. }
  60. /**
  61. * styling for the sharing screen and popups
  62. * JS for the options and switching
  63. */
  64. function load_assets() {
  65. wp_enqueue_script(
  66. 'publicize',
  67. Jetpack::get_file_url_for_environment(
  68. '_inc/build/publicize/assets/publicize.min.js',
  69. 'modules/publicize/assets/publicize.js'
  70. ),
  71. array( 'jquery', 'thickbox' ),
  72. '20121019'
  73. );
  74. if ( is_rtl() ) {
  75. wp_enqueue_style( 'publicize', plugins_url( 'assets/rtl/publicize-rtl.css', __FILE__ ), array(), '20180301' );
  76. } else {
  77. wp_enqueue_style( 'publicize', plugins_url( 'assets/publicize.css', __FILE__ ), array(), '20180301' );
  78. }
  79. wp_enqueue_style( 'social-logos' );
  80. add_thickbox();
  81. }
  82. public static function connected_notice( $service_name ) { ?>
  83. <div class='updated'>
  84. <p><?php
  85. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  86. $platform = __( 'WordPress.com', 'jetpack' );
  87. } else {
  88. $platform = __( 'Jetpack', 'jetpack' );
  89. }
  90. printf(
  91. __( 'You have successfully connected your %1$s account with %2$s.', '1: Service Name (Facebook, Twitter, ...), 2. WordPress.com or Jetpack', 'jetpack' ),
  92. Publicize::get_service_label( $service_name ),
  93. $platform
  94. ); ?></p>
  95. </div><?php
  96. }
  97. public static function denied_notice() { ?>
  98. <div class='updated'>
  99. <p><?php _e ( "You have chosen not to connect your blog. Please click 'accept' when prompted if you wish to connect your accounts.", 'jetpack' ); ?></p>
  100. </div><?php
  101. }
  102. /**
  103. * Lists the current user's publicized accounts for the blog
  104. * looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
  105. */
  106. function admin_page() {
  107. $_blog_id = get_current_blog_id();
  108. ?>
  109. <form action="" id="publicize-form">
  110. <h2 id="publicize"><?php _e( 'Publicize', 'jetpack' ) ?></h2>
  111. <?php
  112. if ( ! empty( $_GET['action'] ) && 'deny' == $_GET['action'] ) {
  113. $this->denied_notice();
  114. }
  115. ?>
  116. <p>
  117. <?php esc_html_e( 'Connect your blog to popular social networking sites and automatically share new posts with your friends.', 'jetpack' ) ?>
  118. <?php esc_html_e( 'You can make a connection for just yourself or for all users on your blog. Shared connections are marked with the (Shared) text.', 'jetpack' ); ?>
  119. </p>
  120. <?php
  121. if ( $this->in_jetpack ) {
  122. $doc_link = "http://jetpack.com/support/publicize/";
  123. } else {
  124. $doc_link = "http://en.support.wordpress.com/publicize/";
  125. }
  126. ?>
  127. <p>&rarr; <a href="<?php echo esc_url( $doc_link ); ?>" rel="noopener noreferrer" target="_blank"><?php esc_html_e( 'More information on using Publicize.', 'jetpack' ); ?></a></p>
  128. <div id="publicize-services-block">
  129. <?php
  130. $services = $this->publicize->get_services( 'all' );
  131. $total_num_of_services = count ( $services );
  132. $service_num = 0;?>
  133. <div class='left'>
  134. <?php
  135. foreach ( $services as $name => $service ) :
  136. $connect_url = $this->publicize->connect_url( $name );
  137. if ( $service_num == ( round ( ( $total_num_of_services / 2 ), 0 ) ) )
  138. echo "</div><div class='right'>";
  139. $service_num++;
  140. ?>
  141. <div class="publicize-service-entry" <?php if ( $service_num > 0 ): ?>class="connected"<?php endif; ?> >
  142. <div id="<?php echo esc_attr( $name ); ?>" class="publicize-service-left">
  143. <a href="<?php echo esc_url( $connect_url ); ?>" id="service-link-<?php echo esc_attr( $name ); ?>" target="_top"><?php echo $this->publicize->get_service_label( $name ); ?></a>
  144. </div>
  145. <div class="publicize-service-right">
  146. <?php if ( $this->publicize->is_enabled( $name ) && $connections = $this->publicize->get_connections( $name ) ) : ?>
  147. <ul>
  148. <?php
  149. foreach( $connections as $c ) :
  150. $id = $this->publicize->get_connection_id( $c );
  151. $disconnect_url = $this->publicize->disconnect_url( $name, $id );
  152. $cmeta = $this->publicize->get_connection_meta( $c );
  153. $profile_link = $this->publicize->get_profile_link( $name, $c );
  154. $connection_display = $this->publicize->get_display_name( $name, $c );
  155. $options_nonce = wp_create_nonce( 'options_page_' . $name . '_' . $id ); ?>
  156. <?php if ( $this->publicize->show_options_popup( $name, $c ) ): ?>
  157. <script type="text/javascript">
  158. jQuery(document).ready( function($) {
  159. showOptionsPage.call(
  160. this,
  161. '<?php echo esc_js( $name ); ?>',
  162. '<?php echo esc_js( $options_nonce ); ?>',
  163. '<?php echo esc_js( $id ); ?>'
  164. );
  165. } );
  166. </script>
  167. <?php endif; ?>
  168. <li class="publicize-connection" data-connection-id="<?php echo esc_attr( $id ); ?>">
  169. <?php esc_html_e( 'Connected as:', 'jetpack' ); ?>
  170. <?php
  171. if ( !empty( $profile_link ) ) : ?>
  172. <a class="publicize-profile-link" href="<?php echo esc_url( $profile_link ); ?>" target="_top">
  173. <?php echo esc_html( $connection_display ); ?>
  174. </a><?php
  175. else :
  176. echo esc_html( $connection_display );
  177. endif;
  178. ?>
  179. <?php if ( 0 == $cmeta['connection_data']['user_id'] ) : ?>
  180. <small>(<?php esc_html_e( 'Shared', 'jetpack' ); ?>)</small>
  181. <?php if ( current_user_can( $this->publicize->GLOBAL_CAP ) ) : ?>
  182. <a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
  183. <?php endif; ?>
  184. <?php else : ?>
  185. <a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
  186. <?php endif; ?>
  187. <br/>
  188. <div class="pub-connection-test test-in-progress" id="pub-connection-test-<?php echo esc_attr( $id ); ?>" >
  189. </div>
  190. </li>
  191. <?php
  192. endforeach;
  193. ?>
  194. </ul>
  195. <?php endif; ?>
  196. <?php
  197. $connections = $this->publicize->get_connections( $name );
  198. if ( empty ( $connections ) ) { ?>
  199. <a id="<?php echo esc_attr( $name ); ?>" class="publicize-add-connection button" href="<?php echo esc_url( $connect_url ); ?>" target="_top"><?php echo esc_html( __( 'Connect', 'jetpack' ) ); ?></a>
  200. <?php } else { ?>
  201. <a id="<?php echo esc_attr( $name ); ?>" class="publicize-add-connection button add-new" href="<?php echo esc_url( $connect_url ); ?>" target="_top"><?php echo esc_html( __( 'Add New', 'jetpack' ) ); ?></a>
  202. <?php } ?>
  203. </div>
  204. </div>
  205. <?php endforeach; ?>
  206. </div>
  207. <script>
  208. (function($){
  209. $('.pub-disconnect-button').on('click', function(e){ if ( confirm( '<?php echo esc_js( __( 'Are you sure you want to stop Publicizing posts to this connection?', 'jetpack' ) ); ?>' ) ) {
  210. return true;
  211. } else {
  212. e.preventDefault();
  213. return false;
  214. }
  215. })
  216. })(jQuery);
  217. </script>
  218. </div>
  219. <?php wp_nonce_field( "wpas_posts_{$_blog_id}", "_wpas_posts_{$_blog_id}_nonce" ); ?>
  220. <input type="hidden" id="wpas_ajax_blog_id" name="wpas_ajax_blog_id" value="<?php echo $_blog_id; ?>" />
  221. </form><?php
  222. }
  223. public static function global_checkbox( $service_name, $id ) {
  224. global $publicize;
  225. if ( current_user_can( $publicize->GLOBAL_CAP ) ) : ?>
  226. <p>
  227. <input id="globalize_<?php echo $service_name; ?>" type="checkbox" name="global" value="<?php echo wp_create_nonce( 'publicize-globalize-' . $id ) ?>" />
  228. <label for="globalize_<?php echo $service_name; ?>"><?php _e( 'Make this connection available to all users of this blog?', 'jetpack' ); ?></label>
  229. </p>
  230. <?php endif;
  231. }
  232. function broken_connection( $service_name, $id ) { ?>
  233. <div id="thickbox-content">
  234. <div class='error'>
  235. <p><?php printf( __( 'There was a problem connecting to %s. Please disconnect and try again.', 'jetpack' ), Publicize::get_service_label( $service_name ) ); ?></p>
  236. </div>
  237. </div><?php
  238. }
  239. public static function options_page_other( $service_name ) {
  240. // Nonce check
  241. check_admin_referer( "options_page_{$service_name}_" . $_REQUEST['connection'] );
  242. ?>
  243. <div id="thickbox-content">
  244. <?php
  245. ob_start();
  246. Publicize_UI::connected_notice( $service_name );
  247. $update_notice = ob_get_clean();
  248. if ( ! empty( $update_notice ) )
  249. echo $update_notice;
  250. ?>
  251. <?php Publicize_UI::global_checkbox( $service_name, $_REQUEST['connection'] ); ?>
  252. <p style="text-align: center;">
  253. <input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>" class="button <?php echo $service_name; ?>-options save-options" name="save" data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>" rel="<?php echo wp_create_nonce( 'save_'.$service_name.'_token_' . $_REQUEST['connection'] ) ?>" />
  254. </p> <br />
  255. </div>
  256. <?php
  257. }
  258. /**
  259. * CSS for styling the publicize message box and counter that displays on the post page.
  260. * There is also some JavaScript for length counting and some basic display effects.
  261. */
  262. function post_page_metabox_assets() {
  263. global $post;
  264. $user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author;
  265. $default_prefix = $this->publicize->default_prefix;
  266. $default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) );
  267. $default_message = $this->publicize->default_message;
  268. $default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) );
  269. $default_suffix = $this->publicize->default_suffix;
  270. $default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) ); ?>
  271. <script type="text/javascript">
  272. jQuery( function($) {
  273. var wpasTitleCounter = $( '#wpas-title-counter' ),
  274. wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length,
  275. wpasTitle = $('#wpas-title').keyup( function() {
  276. var length = wpasTitle.val().length;
  277. wpasTitleCounter.text( length );
  278. if ( wpasTwitterCheckbox && length > 256 ) {
  279. wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
  280. } else {
  281. wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
  282. }
  283. } ),
  284. authClick = false;
  285. $('#publicize-disconnected-form-show').click( function() {
  286. $('#publicize-form').slideDown( 'fast' );
  287. $(this).hide();
  288. } );
  289. $('#publicize-disconnected-form-hide').click( function() {
  290. $('#publicize-form').slideUp( 'fast' );
  291. $('#publicize-disconnected-form-show').show();
  292. } );
  293. $('#publicize-form-edit').click( function() {
  294. $('#publicize-form').slideDown( 'fast', function() {
  295. wpasTitle.focus();
  296. if ( !wpasTitle.text() ) {
  297. var url = $('#shortlink').length ? $('#shortlink').val() : '';
  298. var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, '$("#title").val()', 'url' ); printf( $default_suffix, 'url' ); ?>' );
  299. wpasTitle.append( defaultMessage.replace( /<[^>]+>/g,'') );
  300. var selBeg = defaultMessage.indexOf( $("#title").val() );
  301. if ( selBeg < 0 ) {
  302. selBeg = 0;
  303. selEnd = 0;
  304. } else {
  305. selEnd = selBeg + $("#title").val().length;
  306. }
  307. var domObj = wpasTitle.get(0);
  308. if ( domObj.setSelectionRange ) {
  309. domObj.setSelectionRange( selBeg, selEnd );
  310. } else if ( domObj.createTextRange ) {
  311. var r = domObj.createTextRange();
  312. r.moveStart( 'character', selBeg );
  313. r.moveEnd( 'character', selEnd );
  314. r.select();
  315. }
  316. }
  317. wpasTitle.keyup();
  318. } );
  319. $('#publicize-defaults').hide();
  320. $(this).hide();
  321. return false;
  322. } );
  323. $('#publicize-form-hide').click( function() {
  324. var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
  325. return $.trim( $(el).parent( 'label' ).text() );
  326. } );
  327. $('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
  328. $('#publicize-form-edit').show();
  329. return false;
  330. } );
  331. $('.authorize-link').click( function() {
  332. if ( authClick ) {
  333. return false;
  334. }
  335. authClick = true;
  336. $(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
  337. $.ajaxSetup( { async: false } );
  338. if ( window.wp && window.wp.autosave ) {
  339. window.wp.autosave.server.triggerSave();
  340. } else {
  341. autosave();
  342. }
  343. return true;
  344. } );
  345. $( '.pub-service' ).click( function() {
  346. var service = $(this).data( 'service' ),
  347. fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
  348. $( '#add-publicize-check' ).append( fakebox );
  349. } );
  350. publicizeConnTestStart = function() {
  351. $( '#pub-connection-tests' )
  352. .removeClass( 'below-h2' )
  353. .removeClass( 'error' )
  354. .removeClass( 'publicize-token-refresh-message' )
  355. .addClass( 'test-in-progress' )
  356. .html( '' );
  357. $.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
  358. }
  359. publicizeConnRefreshClick = function( event ) {
  360. event.preventDefault();
  361. var popupURL = event.currentTarget.href;
  362. var popupTitle = event.currentTarget.title;
  363. // open a popup window
  364. // when it is closed, kick off the tests again
  365. var popupWin = window.open( popupURL, popupTitle, '' );
  366. var popupWinTimer= window.setInterval( function() {
  367. if ( popupWin.closed !== false ) {
  368. window.clearInterval( popupWinTimer );
  369. publicizeConnTestStart();
  370. }
  371. }, 500 );
  372. }
  373. publicizeConnTestComplete = function( response ) {
  374. var testsSelector = $( '#pub-connection-tests' );
  375. testsSelector
  376. .removeClass( 'test-in-progress' )
  377. .removeClass( 'below-h2' )
  378. .removeClass( 'error' )
  379. .removeClass( 'publicize-token-refresh-message' )
  380. .html( '' );
  381. // If any of the tests failed, show some stuff
  382. var somethingShownAlready = false;
  383. var facebookNotice = false;
  384. $.each( response.data, function( index, testResult ) {
  385. // find the li for this connection
  386. if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) {
  387. if ( ! somethingShownAlready ) {
  388. testsSelector
  389. .addClass( 'below-h2' )
  390. .addClass( 'error' )
  391. .addClass( 'publicize-token-refresh-message' )
  392. .append( "<p><?php echo esc_html( __( 'Before you hit Publish, please refresh the following connection(s) to make sure we can Publicize your post:', 'jetpack' ) ); ?></p>" );
  393. somethingShownAlready = true;
  394. }
  395. if ( testResult.userCanRefresh ) {
  396. testsSelector.append( '<p/>' );
  397. $( '<a/>', {
  398. 'class' : 'pub-refresh-button button',
  399. 'title' : testResult.refreshText,
  400. 'href' : testResult.refreshURL,
  401. 'text' : testResult.refreshText,
  402. 'target' : '_refresh_' + testResult.serviceName
  403. } )
  404. .appendTo( testsSelector.children().last() )
  405. .click( publicizeConnRefreshClick );
  406. }
  407. }
  408. if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) {
  409. $( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true );
  410. if ( ! facebookNotice ) {
  411. var message = '<p>'
  412. + testResult.connectionTestMessage
  413. + '</p><p>'
  414. + ' <a class="button" href="<?php echo esc_url( admin_url( 'options-general.php?page=sharing' ) ); ?>" rel="noopener noreferrer" target="_blank">'
  415. + '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>'
  416. + '</a>'
  417. + '<p>';
  418. testsSelector
  419. .addClass( 'below-h2' )
  420. .addClass( 'error' )
  421. .addClass( 'publicize-token-refresh-message' )
  422. .append( message );
  423. facebookNotice = true;
  424. }
  425. }
  426. } );
  427. }
  428. $( document ).ready( function() {
  429. // If we have the #pub-connection-tests div present, kick off the connection test
  430. if ( $( '#pub-connection-tests' ).length ) {
  431. publicizeConnTestStart();
  432. }
  433. } );
  434. } );
  435. </script>
  436. <style type="text/css">
  437. #publicize {
  438. line-height: 1.5;
  439. }
  440. #publicize ul {
  441. margin: 4px 0 4px 6px;
  442. }
  443. #publicize li {
  444. margin: 0;
  445. }
  446. #publicize textarea {
  447. margin: 4px 0 0;
  448. width: 100%
  449. }
  450. #publicize ul.not-connected {
  451. list-style: square;
  452. padding-left: 1em;
  453. }
  454. #publicize-title:before {
  455. content: "\f237";
  456. font: normal 20px/1 dashicons;
  457. speak: none;
  458. margin-left: -1px;
  459. padding-right: 3px;
  460. vertical-align: top;
  461. -webkit-font-smoothing: antialiased;
  462. color: #82878c;
  463. }
  464. .post-new-php .authorize-link, .post-php .authorize-link {
  465. line-height: 1.5em;
  466. }
  467. .post-new-php .authorize-message, .post-php .authorize-message {
  468. margin-bottom: 0;
  469. }
  470. #poststuff #publicize .updated p {
  471. margin: .5em 0;
  472. }
  473. .wpas-twitter-length-limit {
  474. color: red;
  475. }
  476. </style><?php
  477. }
  478. /**
  479. * Controls the metabox that is displayed on the post page
  480. * Allows the user to customize the message that will be sent out to the social network, as well as pick which
  481. * networks to publish to. Also displays the character counter and some other information.
  482. */
  483. function post_page_metabox() {
  484. global $post;
  485. if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) )
  486. return;
  487. $user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author;
  488. $services = $this->publicize->get_services( 'connected' );
  489. $available_services = $this->publicize->get_services( 'all' );
  490. if ( ! is_array( $available_services ) )
  491. $available_services = array();
  492. if ( ! is_array( $services ) )
  493. $services = array();
  494. ?>
  495. <div id="publicize" class="misc-pub-section misc-pub-section-last">
  496. <span id="publicize-title">
  497. <?php esc_html_e( 'Publicize:', 'jetpack' ); ?>
  498. <?php if ( 0 < count( $services ) ) : ?>
  499. <?php list( $publicize_form, $active ) = $this->get_metabox_form_connected( $services ); ?>
  500. <span id="publicize-defaults">
  501. <?php foreach ( $active as $item ) : ?>
  502. <strong><?php echo esc_html( $item ); ?></strong>
  503. <?php endforeach; ?>
  504. </span>
  505. <a href="#" id="publicize-form-edit"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a>&nbsp;<a href="<?php echo esc_url( admin_url( 'options-general.php?page=sharing' ) ); ?>" rel="noopener noreferrer" target="_blank"><?php _e( 'Settings', 'jetpack' ); ?></a><br />
  506. <?php else : ?>
  507. <?php $publicize_form = $this->get_metabox_form_disconnected( $available_services ); ?>
  508. <strong><?php echo __( 'Not Connected', 'jetpack' ); ?></strong>
  509. <a href="#" id="publicize-disconnected-form-show"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a><br />
  510. <?php endif; ?>
  511. </span>
  512. <?php
  513. /**
  514. * Filter the Publicize details form.
  515. *
  516. * @module publicize
  517. *
  518. * @since 2.0.0
  519. *
  520. * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
  521. */
  522. echo apply_filters( 'publicize_form', $publicize_form );
  523. ?>
  524. </div> <?php // #publicize
  525. }
  526. private function get_metabox_form_connected( $services ) {
  527. global $post;
  528. $active = array();
  529. ob_start();
  530. ?> <div id="publicize-form" class="hide-if-js">
  531. <ul>
  532. <?php
  533. // We can set an _all flag to indicate that this post is completely done as
  534. // far as Publicize is concerned. Jetpack uses this approach. All published posts in Jetpack
  535. // have Publicize disabled.
  536. $all_done = get_post_meta( $post->ID, $this->publicize->POST_DONE . 'all', true ) || ( $this->in_jetpack && 'publish' == $post->post_status );
  537. // We don't allow Publicizing to the same external id twice, to prevent spam
  538. $service_id_done = (array) get_post_meta( $post->ID, $this->publicize->POST_SERVICE_DONE, true );
  539. foreach ( $services as $name => $connections ) {
  540. foreach ( $connections as $connection ) {
  541. $connection_data = '';
  542. if ( method_exists( $connection, 'get_meta' ) )
  543. $connection_data = $connection->get_meta( 'connection_data' );
  544. elseif ( ! empty( $connection['connection_data'] ) )
  545. $connection_data = $connection['connection_data'];
  546. /**
  547. * Filter whether a post should be publicized to a given service.
  548. *
  549. * @module publicize
  550. *
  551. * @since 2.0.0
  552. *
  553. * @param bool true Should the post be publicized to a given service? Default to true.
  554. * @param int $post->ID Post ID.
  555. * @param string $name Service name.
  556. * @param array $connection_data Array of information about all Publicize details for the site.
  557. */
  558. if ( ! $continue = apply_filters( 'wpas_submit_post?', true, $post->ID, $name, $connection_data ) ) {
  559. continue;
  560. }
  561. if ( ! empty( $connection->unique_id ) ) {
  562. $unique_id = $connection->unique_id;
  563. } else if ( ! empty( $connection['connection_data']['token_id'] ) ) {
  564. $unique_id = $connection['connection_data']['token_id'];
  565. }
  566. // Should we be skipping this one?
  567. $skip = (
  568. (
  569. in_array( $post->post_status, array( 'publish', 'draft', 'future' ) )
  570. &&
  571. get_post_meta( $post->ID, $this->publicize->POST_SKIP . $unique_id, true )
  572. )
  573. ||
  574. (
  575. is_array( $connection )
  576. &&
  577. (
  578. ( isset( $connection['meta']['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['meta']['external_id'] ] ) )
  579. ||
  580. // Jetpack's connection data looks a little different.
  581. ( isset( $connection['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['external_id'] ] ) )
  582. )
  583. )
  584. );
  585. // Was this connections (OR, old-format service) already Publicized to?
  586. $done = ( 1 == get_post_meta( $post->ID, $this->publicize->POST_DONE . $unique_id, true ) || 1 == get_post_meta( $post->ID, $this->publicize->POST_DONE . $name, true ) ); // New and old style flags
  587. // If this one has already been publicized to, don't let it happen again
  588. $disabled = '';
  589. if ( $done ) {
  590. $disabled = ' disabled="disabled"';
  591. }
  592. // If this is a global connection and this user doesn't have enough permissions to modify
  593. // those connections, don't let them change it
  594. $cmeta = $this->publicize->get_connection_meta( $connection );
  595. $hidden_checkbox = false;
  596. if ( !$done && ( 0 == $cmeta['connection_data']['user_id'] && !current_user_can( $this->publicize->GLOBAL_CAP ) ) ) {
  597. $disabled = ' disabled="disabled"';
  598. /**
  599. * Filters the checkboxes for global connections with non-prilvedged users.
  600. *
  601. * @module publicize
  602. *
  603. * @since 3.7.0
  604. *
  605. * @param bool $checked Indicates if this connection should be enabled. Default true.
  606. * @param int $post->ID ID of the current post
  607. * @param string $name Name of the connection (Facebook, Twitter, etc)
  608. * @param array $connection Array of data about the connection.
  609. */
  610. $hidden_checkbox = apply_filters( 'publicize_checkbox_global_default', true, $post->ID, $name, $connection );
  611. }
  612. // Determine the state of the checkbox (on/off) and allow filtering
  613. $checked = $skip != 1 || $done;
  614. /**
  615. * Filter the checkbox state of each Publicize connection appearing in the post editor.
  616. *
  617. * @module publicize
  618. *
  619. * @since 2.0.1
  620. *
  621. * @param bool $checked Should the Publicize checkbox be enabled for a given service.
  622. * @param int $post->ID Post ID.
  623. * @param string $name Service name.
  624. * @param array $connection Array of connection details.
  625. */
  626. $checked = apply_filters( 'publicize_checkbox_default', $checked, $post->ID, $name, $connection );
  627. // Force the checkbox to be checked if the post was DONE, regardless of what the filter does
  628. if ( $done ) {
  629. $checked = true;
  630. }
  631. $disabled = false;
  632. // This post has been handled, so disable everything
  633. if ( $all_done ) {
  634. $disabled = ' disabled="disabled"';
  635. }
  636. $label = sprintf(
  637. _x( '%1$s: %2$s', 'Service: Account connected as', 'jetpack' ),
  638. esc_html( $this->publicize->get_service_label( $name ) ),
  639. esc_html( $this->publicize->get_display_name( $name, $connection ) )
  640. );
  641. if (
  642. $name === 'facebook'
  643. && ! $this->publicize->is_valid_facebook_connection( $connection )
  644. && $this->publicize->is_connecting_connection( $connection )
  645. ) {
  646. $skip = true;
  647. $disabled = ' disabled="disabled"';
  648. $checked = false;
  649. $hidden_checkbox = false;
  650. }
  651. if ( ( !$skip || $done ) && ! $disabled ) {
  652. $active[] = $label;
  653. }
  654. ?>
  655. <li>
  656. <label for="wpas-submit-<?php echo esc_attr( $unique_id ); ?>">
  657. <input type="checkbox" name="wpas[submit][<?php echo $unique_id; ?>]" id="wpas-submit-<?php echo $unique_id; ?>" class="wpas-submit-<?php echo $name; ?>" value="1" <?php
  658. checked( true, $checked );
  659. echo $disabled;
  660. ?> />
  661. <?php
  662. if ( $hidden_checkbox ) {
  663. // Need to submit a value to force a global connection to post
  664. echo '<input type="hidden" name="wpas[submit][' . $unique_id . ']" value="1" />';
  665. }
  666. echo esc_html( $label );
  667. ?>
  668. </label>
  669. </li>
  670. <?php
  671. }
  672. }
  673. if ( $title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true ) ) {
  674. $title = esc_html( $title );
  675. } else {
  676. $title = '';
  677. }
  678. ?>
  679. </ul>
  680. <label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label>
  681. <span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
  682. <textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo $title; ?></textarea>
  683. <a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
  684. <input type="hidden" name="wpas[0]" value="1" />
  685. </div>
  686. <?php if ( ! $all_done ) : ?>
  687. <div id="pub-connection-tests"></div>
  688. <?php endif; ?>
  689. <?php // #publicize-form
  690. return array( ob_get_clean(), $active );
  691. }
  692. private function get_metabox_form_disconnected( $available_services ) {
  693. ob_start();
  694. ?><div id="publicize-form" class="hide-if-js">
  695. <div id="add-publicize-check" style="display: none;"></div>
  696. <?php _e( 'Connect to', 'jetpack' ); ?>:
  697. <ul class="not-connected">
  698. <?php foreach ( $available_services as $service_name => $service ) : ?>
  699. <li>
  700. <a class="pub-service" data-service="<?php echo esc_attr( $service_name ); ?>" title="<?php echo esc_attr( sprintf( __( 'Connect and share your posts on %s', 'jetpack' ), $this->publicize->get_service_label( $service_name ) ) ); ?>" rel="noopener noreferrer" target="_blank" href="<?php echo esc_url( $this->publicize->connect_url( $service_name ) ); ?>">
  701. <?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?>
  702. </a>
  703. </li>
  704. <?php endforeach; ?>
  705. </ul>
  706. <a href="#" class="hide-if-no-js button" id="publicize-disconnected-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
  707. </div><?php // #publicize-form
  708. return ob_get_clean();
  709. }
  710. }