PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-publicize / src / class-publicize-ui.php

class-publicize-ui.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-ui.php

636 lines 19.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Publicize_UI class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8 namespace Automattic\Jetpack\Publicize;
9
10 use Automattic\Jetpack\Assets;
11 use Automattic\Jetpack\Current_Plan;
12 use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
13 use Automattic\Jetpack\Status\Host;
14
15 /**
16 * Only user facing pieces of Publicize are found here.
17 */
18 class Publicize_UI {
19 /**
20 * Contains an instance of class 'Publicize' which loads Keyring, sets up services, etc.
21 *
22 * @var Publicize Instance of Publicize
23 */
24 public $publicize;
25
26 /**
27 * URL to Sharing settings page in wordpress.com
28 *
29 * @var string
30 */
31 protected $publicize_settings_url = '';
32
33 /**
34 * Hooks into WordPress to display the various pieces of UI and load our assets
35 */
36 public function __construct() {
37 global $publicize;
38 if ( ! is_object( $publicize ) ) {
39 $publicize = new Publicize();
40 }
41 $this->publicize = $publicize;
42
43 add_action( 'admin_init', array( $this, 'init' ) );
44 }
45
46 /**
47 * Initialize UI-related functionality.
48 */
49 public function init() {
50 // Show only to users with the capability required to manage their Publicize connections.
51 if ( ! Utils::is_publicize_active() || ! $this->publicize->current_user_can_access_publicize_data() ) {
52 return;
53 }
54
55 $this->publicize_settings_url = $this->publicize->publicize_connections_url();
56
57 // Assets (css, js).
58 add_action( 'admin_head-post.php', array( $this, 'post_page_metabox_assets' ) );
59 add_action( 'admin_head-post-new.php', array( $this, 'post_page_metabox_assets' ) );
60
61 // Management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen).
62 add_action( 'post_submitbox_misc_actions', array( $this, 'post_page_metabox' ) );
63 }
64
65 /**
66 * If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
67 *
68 * @deprecated 0.42.3
69 */
70 public function sharing_menu() {
71 add_submenu_page(
72 'options-general.php',
73 esc_html__( 'Sharing Settings', 'jetpack-publicize-pkg' ),
74 esc_html__( 'Sharing', 'jetpack-publicize-pkg' ),
75 'publish_posts',
76 'sharing',
77 array( $this, 'wrapper_admin_page' )
78 );
79 }
80
81 /**
82 * Add admin page with wrapper.
83 *
84 * @deprecated 0.42.3
85 */
86 public function wrapper_admin_page() {
87 if ( class_exists( 'Jetpack_Admin_Page' ) ) {
88 \Jetpack_Admin_Page::wrap_ui( array( $this, 'management_page' ) );
89 }
90 }
91
92 /**
93 * Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
94 *
95 * @deprecated 0.42.3
96 */
97 public function management_page() {
98 ?>
99 <div class="wrap">
100 <div class="icon32" id="icon-options-general"><br /></div>
101 <h1><?php esc_html_e( 'Sharing Settings', 'jetpack-publicize-pkg' ); ?></h1>
102
103 <?php
104 /** This action is documented in modules/sharedaddy/sharing.php */
105 do_action( 'pre_admin_screen_sharing' );
106 ?>
107 </div>
108 <?php
109 }
110
111 /**
112 * Styling for the sharing screen and popups
113 * JS for the options and switching
114 *
115 * @deprecated 0.42.3
116 */
117 public function load_assets() {
118 if ( class_exists( 'Jetpack_Admin_Page' ) ) {
119 \Jetpack_Admin_Page::load_wrapper_styles();
120 }
121 }
122
123 /**
124 * Lists the current user's publicized accounts for the blog
125 * looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
126 *
127 * @deprecated 0.42.3
128 */
129 public function admin_page() {
130 ?>
131 <h2 id="publicize"><?php esc_html_e( 'Jetpack Social', 'jetpack-publicize-pkg' ); ?></h2>
132 <p><?php esc_html_e( 'Connect social media services to automatically share new posts.', 'jetpack-publicize-pkg' ); ?></p>
133 <h4>
134 <?php
135 printf(
136 wp_kses(
137 /* translators: %s is the link to the Publicize page in Calypso */
138 __( "We've made some updates to Jetpack Social. Please visit the <a href='%s' class='jptracks' data-jptracks-name='legacy_publicize_settings'>WordPress.com sharing page</a> to manage your Jetpack Social connections or use the button below.", 'jetpack-publicize-pkg' ),
139 array(
140 'a' => array(
141 'href' => array(),
142 'class' => array(),
143 'data-jptracks-name' => array(),
144 ),
145 )
146 ),
147 esc_url( $this->publicize->publicize_connections_url() )
148 );
149 ?>
150 </h4>
151
152 <a href="<?php echo esc_url( $this->publicize->publicize_connections_url() ); ?>" class="button button-primary jptracks" data-jptracks-name='legacy_publicize_settings'><?php esc_html_e( 'Jetpack Social Settings', 'jetpack-publicize-pkg' ); ?></a>
153 <?php
154 }
155
156 /**
157 * CSS for styling the publicize message box and counter that displays on the post page.
158 * There is also some JavaScript for length counting and some basic display effects.
159 */
160 public function post_page_metabox_assets() {
161 // We don't need those assets for the block editor pages.
162 $current_screen = get_current_screen();
163 if ( $current_screen && $current_screen->is_block_editor ) {
164 return;
165 }
166
167 $is_atomic_site = ( new Host() )->is_woa_site();
168 $is_simple_site = ( new Host() )->is_wpcom_simple();
169 $site_type = $is_atomic_site ? 'atomic' : ( $is_simple_site ? 'simple' : 'jetpack' );
170
171 Assets::register_script(
172 'jetpack-social-classic-editor-options',
173 '../build/classic-editor.js',
174 __FILE__,
175 array(
176 'in_footer' => true,
177 'enqueue' => true,
178 'textdomain' => 'jetpack-publicize-pkg',
179 )
180 );
181
182 wp_add_inline_script(
183 'jetpack-social-classic-editor-options',
184 'var jetpackSocialClassicEditorOptions = ' . wp_json_encode(
185 array(
186 'connectionsUrl' => esc_url( $this->publicize_settings_url ),
187 'isEnhancedPublishingEnabled' => $this->publicize->has_enhanced_publishing_feature(),
188 'resharePath' => '/wpcom/v2/publicize/share-post/{postId}',
189 'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1',
190 'isReshareSupported' => Current_Plan::supports( 'republicize' ),
191 'siteType' => $site_type,
192 ),
193 JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
194 ),
195 'before'
196 );
197
198 $default_prefix = $this->publicize->default_prefix;
199 $default_prefix = preg_replace( '/%([0-9])\$s/', '" + %\\1$s + "', wp_json_encode( (string) $default_prefix, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) );
200
201 $default_message = $this->publicize->default_message;
202 $default_message = preg_replace( '/%([0-9])\$s/', '" + %\\1$s + "', wp_json_encode( (string) $default_message, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) );
203
204 $default_suffix = $this->publicize->default_suffix;
205 $default_suffix = preg_replace( '/%([0-9])\$s/', '" + %\\1$s + "', wp_json_encode( (string) $default_suffix, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) );
206
207 $max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280;
208 $max_length -= 24; // t.co link, space.
209
210 ?>
211
212 <script type="text/javascript">
213 jQuery( function($) {
214 var wpasTitleCounter = $( '#wpas-title-counter' ),
215 wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length,
216 postTitle = $( '#title' ),
217 wpasTitle = $( '#wpas-title' ).keyup( function() {
218 var postTitleVal,
219 length = wpasTitle.val().length;
220
221 if ( ! length ) {
222 length = wpasTitle.attr( 'placeholder' ).length;
223 }
224
225 wpasTitleCounter.text( length ).trigger( 'change' );
226 } ),
227 authClick = false;
228
229 wpasTitleCounter.on( 'change', function( e ) {
230 if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) {
231 wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
232 } else {
233 wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
234 }
235 } );
236
237 // Keep the postTitle and the placeholder in sync
238 postTitle.on( 'keyup', function( e ) {
239 var url = $( '#sample-permalink' ).text();
240 <?php // phpcs:ignore ?>
241 var defaultMessage = $.trim( <?php printf( $default_prefix, 'url' ); ?> + <?php printf( $default_message, 'e.currentTarget.value', 'url' ); ?> + <?php printf( $default_suffix, 'url' ); ?> )
242 .replace( /<[^>]+>/g,'');
243
244 wpasTitle.attr( 'placeholder', defaultMessage );
245 wpasTitle.trigger( 'keyup' );
246 } );
247
248 // set the initial placeholder
249 postTitle.trigger( 'keyup' );
250
251 // If a custom message has been provided, open the UI so the author remembers
252 if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) {
253 $( '#publicize-form' ).show();
254 $( '#publicize-defaults' ).hide();
255 $( '#publicize-form-edit' ).hide();
256 }
257
258 $('#publicize-disconnected-form-show').click( function() {
259 $('#publicize-form').slideDown( 'fast' );
260 $(this).hide();
261 } );
262
263 $('#publicize-disconnected-form-hide').click( function() {
264 $('#publicize-form').slideUp( 'fast' );
265 $('#publicize-disconnected-form-show').show();
266 } );
267
268 $('#publicize-form-edit').click( function() {
269 $('#publicize-form').slideDown( 'fast', function() {
270 var selBeg = 0, selEnd = 0;
271 wpasTitle.focus();
272
273 if ( ! wpasTitle.text() ) {
274 wpasTitle.text( wpasTitle.attr( 'placeholder' ) );
275
276 selBeg = wpasTitle.text().indexOf( postTitle.val() );
277 if ( selBeg < 0 ) {
278 selBeg = 0;
279 } else {
280 selEnd = selBeg + postTitle.val().length;
281 }
282
283 var domObj = wpasTitle.get(0);
284 if ( domObj.setSelectionRange ) {
285 domObj.setSelectionRange( selBeg, selEnd );
286 } else if ( domObj.createTextRange ) {
287 var r = domObj.createTextRange();
288 r.moveStart( 'character', selBeg );
289 r.moveEnd( 'character', selEnd );
290 r.select();
291 }
292 }
293 } );
294
295 $('#publicize-defaults').hide();
296 $(this).hide();
297 return false;
298 } );
299
300
301 $('#publicize-form-hide').click( function() {
302 var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
303 return $.trim( $(el).parent( 'label' ).text() );
304 } );
305 $('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
306 $('#publicize-form-edit').show();
307 return false;
308 } );
309
310 $('.authorize-link').click( function() {
311 if ( authClick ) {
312 return false;
313 }
314 authClick = true;
315 $(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
316 $.ajaxSetup( { async: false } );
317
318 if ( window.wp && window.wp.autosave ) {
319 window.wp.autosave.server.triggerSave();
320 } else {
321 autosave();
322 }
323
324 return true;
325 } );
326
327 $( '.pub-service' ).click( function() {
328 var service = $(this).data( 'service' ),
329 fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
330 $( '#add-publicize-check' ).append( fakebox );
331 } );
332 } );
333 </script>
334
335 <style type="text/css">
336 #publicize {
337 line-height: 1.5;
338 }
339 #publicize ul {
340 margin: 4px 0 4px 6px;
341 }
342 #publicize li {
343 margin: 0;
344 }
345 #publicize textarea {
346 margin: 4px 0 0;
347 width: 100%
348 }
349 #publicize ul.not-connected {
350 list-style: square;
351 padding-left: 1em;
352 }
353 .wpas-disabled {
354 color: #999;
355 }
356 .publicize__notice-warning {
357 display: block;
358 padding: 7px 10px;
359 margin: 5px 0;
360 border-left-width: 4px;
361 border-left-style: solid;
362 font-size: 12px;
363 box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
364 }
365 .publicize__notice-media-warning {
366 border-right: 1px solid #c3c4c7;
367 border-bottom: 1px solid #c3c4c7;
368 border-top: 1px solid #c3c4c7;
369 }
370 .publicize-external-link {
371 display: block;
372 text-decoration: none;
373 margin-top: 8px;
374 }
375 .publicize-external-link__text {
376 text-decoration: underline;
377 }
378 #publicize-title::before {
379 content: "\f237";
380 font: normal 20px/1 dashicons;
381 speak: none;
382 margin-left: -1px;
383 padding-right: 3px;
384 vertical-align: top;
385 -webkit-font-smoothing: antialiased;
386 color: #8c8f94;
387 }
388 .post-new-php .authorize-link, .post-php .authorize-link {
389 line-height: 1.5em;
390 }
391 .post-new-php .authorize-message, .post-php .authorize-message {
392 margin-bottom: 0;
393 }
394 #poststuff #publicize .updated p {
395 margin: .5em 0;
396 }
397 .wpas-twitter-length-limit {
398 color: red;
399 }
400 .publicize__notice-warning .dashicons {
401 font-size: 16px;
402 text-decoration: none;
403 }
404 .publicize-placeholders-help {
405 margin: 0.5rem 0 1rem;
406 }
407 </style>
408 <?php
409 }
410
411 /**
412 * Get the connection label.
413 *
414 * @param string $service_label Service's human-readable Label ("Facebook", "Twitter", ...).
415 * @param string $display_name Connection's human-readable Username ("@jetpack", ...).
416 * @return string
417 */
418 private function connection_label( $service_label, $display_name ) {
419 return sprintf(
420 /* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Username on Service (@jetpack, ...) */
421 __( '%1$s: %2$s', 'jetpack-publicize-pkg' ),
422 $service_label,
423 $display_name
424 );
425 }
426
427 /**
428 * Controls the metabox that is displayed on the post page
429 * Allows the user to customize the message that will be sent out to the social network, as well as pick which
430 * networks to publish to. Also displays the character counter and some other information.
431 */
432 public function post_page_metabox() {
433 global $post;
434
435 if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) ) {
436 return;
437 }
438
439 $connections_data = $this->publicize->get_filtered_connection_data();
440
441 if ( ! is_array( $connections_data ) ) {
442 $connections_data = array();
443 }
444 ?>
445 <div id="publicize" class="misc-pub-section misc-pub-section-last">
446 <span id="publicize-title">
447 <?php
448 esc_html_e( 'Jetpack Social:', 'jetpack-publicize-pkg' );
449
450 if ( ! empty( $connections_data ) ) :
451 $publicize_form = $this->get_metabox_form_connected( $connections_data );
452
453 $labels = array();
454
455 foreach ( $connections_data as $connection_data ) {
456 if ( ! $connection_data['enabled'] ) {
457 continue;
458 }
459
460 $labels[] = sprintf(
461 '<strong>%s</strong>',
462 esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) )
463 );
464 }
465
466 ?>
467 <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- labels are already escaped above ?>
468 <span id="publicize-defaults"><?php echo implode( ', ', $labels ); ?></span>
469 <a href="#" id="publicize-form-edit"><?php esc_html_e( 'Edit', 'jetpack-publicize-pkg' ); ?></a>&nbsp;<a href="<?php echo esc_url( $this->publicize->publicize_connections_url() ); ?>" rel="noopener noreferrer" target="_blank"><?php esc_html_e( 'Settings', 'jetpack-publicize-pkg' ); ?></a><br />
470 <?php
471 else :
472 $publicize_form = '';
473 ?>
474 <strong><?php esc_html_e( 'Not Connected', 'jetpack-publicize-pkg' ); ?></strong>
475 <a href="<?php echo esc_url( $this->publicize->publicize_connections_url() ); ?>" rel="noopener noreferrer" target="_blank"><?php esc_html_e( 'Settings', 'jetpack-publicize-pkg' ); ?></a><br />
476 <?php
477
478 endif;
479 ?>
480 </span>
481 <?php
482 /**
483 * Fires right before rendering the Publicize form in the Classic
484 * Editor.
485 *
486 * @since 0.14.0
487 */
488 do_action( 'publicize_classic_editor_form_before' );
489
490 /**
491 * Filter the Publicize details form.
492 *
493 * @since 0.1.0
494 * @since-jetpack 2.0.0
495 *
496 * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
497 */
498 echo apply_filters( 'publicize_form', $publicize_form ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Parts of the form are escaped individually in the code above.
499
500 /**
501 * Fires right after rendering the Publicize form in the Classic
502 * Editor.
503 *
504 * @since 0.14.0
505 */
506 do_action( 'publicize_classic_editor_form_after' );
507 ?>
508 </div>
509 <?php
510 }
511
512 /**
513 * Generates HTML content for connections form.
514 *
515 * @since 0.1.0
516 * @since-jetpack 6.7.0
517 *
518 * @global WP_Post $post The current post instance being published.
519 *
520 * @param array $connections_data Array of connections.
521 * @return array {
522 * Array of content for generating connection form.
523 *
524 * @type string HTML content of form
525 * @type array {
526 * Array of connection labels for active connections only.
527 *
528 * @type string Connection label string.
529 * }
530 * }
531 */
532 private function get_metabox_form_connected( $connections_data ) {
533 global $post;
534
535 ob_start();
536
537 ?>
538 <div id="publicize-form" class="hide-if-js">
539 <ul>
540 <?php
541
542 foreach ( $connections_data as $connection_data ) {
543 ?>
544
545 <li>
546 <label
547 for="wpas-submit-<?php echo esc_attr( $connection_data['connection_id'] ); ?>"
548 >
549 <input
550 type="checkbox"
551 name="wpas[submit][<?php echo esc_attr( $connection_data['connection_id'] ); ?>]"
552 id="wpas-submit-<?php echo esc_attr( $connection_data['connection_id'] ); ?>"
553 class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
554 value="1"
555 data-id="<?php echo esc_attr( $connection_data['connection_id'] ); ?>"
556 <?php
557 checked( true, $connection_data['enabled'] );
558 ?>
559 />
560 <?php echo esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) ); ?>
561
562 </label>
563 </li>
564 <?php
565 }
566
567 $title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true );
568 if ( ! $title ) {
569 $title = '';
570 }
571
572 $is_social_note = 'jetpack-social-note' === get_post_type( $post->ID );
573
574 $is_post_published = 'publish' === get_post_status( $post->ID );
575
576 $templates_enabled = $this->publicize->has_paid_features();
577
578 $placeholders = $this->get_message_placeholders();
579
580 ?>
581
582 </ul>
583
584 <?php if ( ! $is_social_note ) : ?>
585 <label for="wpas-title"><?php esc_html_e( 'Custom Message:', 'jetpack-publicize-pkg' ); ?></label>
586 <?php if ( ! $templates_enabled ) : ?>
587 <span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
588 <?php endif; ?>
589 <textarea name="wpas_title" id="wpas-title"><?php echo esc_textarea( $title ); ?></textarea>
590 <?php if ( $templates_enabled && ! empty( $placeholders ) ) : ?>
591 <details class="publicize-placeholders-help">
592 <summary><?php esc_html_e( 'Available placeholders', 'jetpack-publicize-pkg' ); ?></summary>
593 <p>
594 <?php esc_html_e( 'Use placeholders to automatically insert post details.', 'jetpack-publicize-pkg' ); ?>
595 </p>
596 <ul>
597 <?php foreach ( $placeholders as $placeholder ) : ?>
598 <li>
599 <code><?php echo esc_html( $placeholder['id'] ); ?></code>
600 &mdash; <?php echo esc_html( $placeholder['label'] ); ?>
601 </li>
602 <?php endforeach; ?>
603 </ul>
604 </details>
605 <?php endif; ?>
606 <a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack-publicize-pkg' ); ?></a>
607 <input type="hidden" name="wpas[0]" value="1" />
608 <?php endif; ?>
609 <?php if ( $is_post_published && Current_Plan::supports( 'republicize' ) ) : ?>
610 <button type="button" class="hide-if-no-js button" id="publicize-share-now">
611 <?php esc_html_e( 'Share now', 'jetpack-publicize-pkg' ); ?>
612 </button>
613 <span id="publicize-share-now-notice" class="hidden"></span>
614 <?php endif; ?>
615 </div>
616
617 <div id="pub-connection-needs-media"></div>
618 <div id="pub-connection-tests"></div>
619 <?php
620
621 return ob_get_clean();
622 }
623
624 /**
625 * Get the catalogue of placeholders supported in custom Publicize messages.
626 *
627 * Sourced from WPCOM via `Message_Templates_Placeholders` so the metabox
628 * hint can never drift from the resolver in `Template_Parser`.
629 *
630 * @return array<int, array{id: string, label: string}> Ordered list of placeholders.
631 */
632 private function get_message_placeholders() {
633 return Message_Templates_Placeholders::get_all();
634 }
635 }
636