PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 15.0
Jetpack – WP Security, Backup, Speed, & Growth v15.0
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 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / sharedaddy / sharing.php

sharing.php in Jetpack – WP Security, Backup, Speed, & Growth 15.0, at modules/sharedaddy/sharing.php

892 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Set up Sharing functionality and management in wp-admin.
4 *
5 * @package automattic/jetpack
6 */
7
8 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
9
10 use Automattic\Jetpack\Assets;
11 use Automattic\Jetpack\Redirect;
12 use Automattic\Jetpack\Status;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 0 );
16 }
17
18 if ( ! defined( 'WP_SHARING_PLUGIN_URL' ) ) {
19 define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
20 define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
21 }
22
23 /**
24 * Utilities to manage sharing settings from wp-admin.
25 */
26 class Sharing_Admin {
27
28 /**
29 * Constructor.
30 * Hook into WordPress to add our functionality.
31 */
32 public function __construct() {
33 require_once WP_SHARING_PLUGIN_DIR . 'sharing-service.php';
34
35 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonces are handled in process_requests.
36 if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'sharing.php' || $_GET['page'] === 'sharing' ) ) {
37 add_action( 'admin_init', array( $this, 'admin_init' ) );
38 }
39
40 add_action( 'admin_menu', array( $this, 'subscription_menu' ) );
41
42 // Insert our CSS and JS
43 add_action( 'load-settings_page_sharing', array( $this, 'sharing_head' ) );
44
45 // Catch AJAX
46 add_action( 'wp_ajax_sharing_save_services', array( $this, 'ajax_save_services' ) );
47 add_action( 'wp_ajax_sharing_save_options', array( $this, 'ajax_save_options' ) );
48 add_action( 'wp_ajax_sharing_new_service', array( $this, 'ajax_new_service' ) );
49 add_action( 'wp_ajax_sharing_delete_service', array( $this, 'ajax_delete_service' ) );
50 }
51
52 /**
53 * Enqueue scripts and styles on the sharing settings page.
54 *
55 * @return void
56 */
57 public function sharing_head() {
58 wp_enqueue_script(
59 'sharing-js',
60 Assets::get_file_url_for_environment(
61 '_inc/build/sharedaddy/admin-sharing.min.js',
62 'modules/sharedaddy/admin-sharing.js'
63 ),
64 array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-form' ),
65 JETPACK__VERSION,
66 false
67 );
68
69 /**
70 * Filters the switch that if set to true allows Jetpack to use minified assets. Defaults to true
71 * if the SCRIPT_DEBUG constant is not set or set to false. The filter overrides it.
72 *
73 * @since 6.2.0
74 *
75 * @param boolean $var should Jetpack use minified assets.
76 */
77 $postfix = apply_filters( 'jetpack_should_use_minified_assets', true ) ? '.min' : '';
78 if ( is_rtl() ) {
79 wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing-rtl' . $postfix . '.css', false, JETPACK__VERSION );
80 } else {
81 wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing' . $postfix . '.css', false, JETPACK__VERSION );
82 }
83 wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL . 'sharing.css', false, JETPACK__VERSION );
84
85 wp_enqueue_style( 'social-logos' );
86 wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array(), 4, false );
87 add_thickbox();
88
89 // On Jetpack sites, make sure we include CSS to style the admin page.
90 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
91 Jetpack_Admin_Page::load_wrapper_styles();
92 }
93 }
94
95 /**
96 * Load the process that handles saving changes on the sharing settings page.
97 *
98 * @return void
99 */
100 public function admin_init() {
101 $this->process_requests();
102 }
103
104 /**
105 * Save changes to sharing settings.
106 *
107 * @return void
108 */
109 public function process_requests() {
110 if (
111 isset( $_POST['_wpnonce'] )
112 && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-options' )
113 ) {
114 $sharer = new Sharing_Service();
115 $sharer->set_global_options( $_POST );
116 /**
117 * Fires when updating sharing settings.
118 *
119 * @module sharedaddy
120 *
121 * @since 1.1.0
122 */
123 do_action( 'sharing_admin_update' );
124
125 wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
126 die( 0 );
127 }
128 }
129
130 /**
131 * Register Sharing settings menu page in Settings > Sharing.
132 */
133 public function subscription_menu() {
134 add_submenu_page(
135 'options-general.php',
136 __( 'Sharing Settings', 'jetpack' ),
137 __( 'Sharing', 'jetpack' ),
138 'manage_options',
139 'sharing',
140 array( $this, 'wrapper_admin_page' )
141 );
142 }
143
144 /**
145 * Save changes to sharing services via AJAX.
146 *
147 * @return void
148 */
149 public function ajax_save_services() {
150 if (
151 isset( $_POST['_wpnonce'] )
152 && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-options' )
153 && isset( $_POST['hidden'] )
154 && isset( $_POST['visible'] )
155 ) {
156 $sharer = new Sharing_Service();
157
158 $sharer->set_blog_services(
159 explode( ',', sanitize_text_field( wp_unslash( $_POST['visible'] ) ) ),
160 explode( ',', sanitize_text_field( wp_unslash( $_POST['hidden'] ) ) )
161 );
162 die( 0 );
163 }
164 }
165
166 /**
167 * Create a new custom sharing service via AJAX.
168 *
169 * @return never
170 */
171 public function ajax_new_service() {
172 if (
173 isset( $_POST['_wpnonce'] )
174 && isset( $_POST['sharing_name'] )
175 && isset( $_POST['sharing_url'] )
176 && isset( $_POST['sharing_icon'] )
177 && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-new_service' )
178 ) {
179 $sharer = new Sharing_Service();
180 $service = $sharer->new_service(
181 sanitize_text_field( wp_unslash( $_POST['sharing_name'] ) ),
182 esc_url_raw( wp_unslash( $_POST['sharing_url'] ) ),
183 esc_url_raw( wp_unslash( $_POST['sharing_icon'] ) )
184 );
185
186 if ( $service ) {
187 $this->output_service( $service->get_id(), $service );
188 echo '<!--->';
189 $service->button_style = 'icon-text';
190 $this->output_preview( $service );
191
192 die( 0 );
193 }
194 }
195
196 // Fail
197 die( '1' );
198 }
199
200 /**
201 * Delete a sharing service via AJAX.
202 *
203 * @return void
204 */
205 public function ajax_delete_service() {
206 if (
207 isset( $_POST['_wpnonce'] )
208 && isset( $_POST['service'] )
209 && wp_verify_nonce(
210 sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ),
211 'sharing-options_' . sanitize_text_field( wp_unslash( $_POST['service'] ) )
212 )
213 ) {
214 $sharer = new Sharing_Service();
215 $sharer->delete_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ) );
216 }
217 }
218
219 /**
220 * Save changes to sharing settings via AJAX.
221 *
222 * @return void
223 */
224 public function ajax_save_options() {
225 if (
226 isset( $_POST['_wpnonce'] )
227 && isset( $_POST['service'] )
228 && wp_verify_nonce(
229 sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ),
230 'sharing-options_' . sanitize_text_field( wp_unslash( $_POST['service'] ) )
231 )
232 ) {
233 $sharer = new Sharing_Service();
234 $service = $sharer->get_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ) );
235
236 if ( $service && $service instanceof Sharing_Advanced_Source ) {
237 $service->update_options( $_POST );
238
239 $sharer->set_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ), $service );
240 }
241
242 $this->output_service( $service->get_id(), $service, true );
243 echo '<!--->';
244 $service->button_style = 'icon-text';
245 $this->output_preview( $service );
246 die( 0 );
247 }
248 }
249
250 /**
251 * Display a preview of a sharing service.
252 *
253 * @param object $service Sharing service object.
254 *
255 * @return void
256 */
257 public function output_preview( $service ) {
258 $klasses = array( 'advanced', 'preview-item' );
259
260 if ( $service->button_style !== 'text' || $service->has_custom_button_style() ) {
261 $klasses[] = 'preview-' . $service->get_class();
262 $klasses[] = 'share-' . $service->get_class();
263 if ( $service->is_deprecated() ) {
264 $klasses[] = 'share-deprecated';
265 }
266
267 if ( $service->get_class() !== $service->get_id() ) {
268 $klasses[] = 'preview-' . $service->get_id();
269 }
270 }
271
272 echo '<li class="' . esc_attr( implode( ' ', $klasses ) ) . '">';
273 $service->display_preview();
274 echo '</li>';
275 }
276
277 /**
278 * Display a specific sharing service.
279 *
280 * @param int $id Service unique ID.
281 * @param object $service Sharing service.
282 * @param bool $show_dropdown Display a dropdown. Not in use at the moment.
283 *
284 * @return void
285 */
286 public function output_service( $id, $service, $show_dropdown = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
287 $title = '';
288 $klasses = array( 'service', 'advanced', 'share-' . $service->get_class() );
289 $displayed_klasses = implode( ' ', $klasses );
290
291 if ( $service->is_deprecated() ) {
292 /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
293 $title = sprintf( __( 'The %1$s sharing service has shut down or discontinued support for sharing buttons. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $service->get_name() );
294 $klasses[] = 'share-deprecated';
295 }
296
297 ?>
298 <li class="<?php echo esc_attr( $displayed_klasses ); ?>" id="<?php echo esc_attr( $service->get_id() ); ?>" tabindex="0" title="<?php echo esc_attr( $title ); ?>">
299 <span class="options-left"><?php echo esc_html( $service->get_name() ); ?></span>
300 <?php if ( str_starts_with( $service->get_id(), 'custom-' ) || $service->has_advanced_options() ) : ?>
301 <span class="close"><a href="#" class="remove">&times;</a></span>
302 <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
303 <input type="hidden" name="action" value="sharing_delete_service" />
304 <input type="hidden" name="service" value="<?php echo esc_attr( $id ); ?>" />
305 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-options_' . $id ) ); ?>" />
306 </form>
307 <?php endif; ?>
308 </li>
309 <?php
310 }
311
312 /**
313 * Display admin UI within a Jetpack header and footer.
314 *
315 * @return void
316 */
317 public function wrapper_admin_page() {
318 Jetpack_Admin_Page::wrap_ui( array( $this, 'management_page' ), array( 'is-wide' => true ) );
319 }
320
321 /**
322 * Sharing settings inner page structure.
323 *
324 * @return void
325 */
326 public function management_page() {
327
328 if ( ! function_exists( 'mb_stripos' ) ) {
329 echo '<div id="message" class="updated fade"><h3>' . esc_html__( 'Warning! Multibyte support missing!', 'jetpack' ) . '</h3>';
330 echo '<p>' . wp_kses(
331 sprintf(
332 /* Translators: placeholder is a link to a PHP support document. */
333 __( 'This plugin will work without it, but multibyte support is used <a href="%s" rel="noopener noreferrer" target="_blank">if available</a>. You may see minor problems with Tweets and other sharing services.', 'jetpack' ),
334 'https://www.php.net/manual/en/mbstring.installation.php'
335 ),
336 array(
337 'a' => array(
338 'href' => array(),
339 'rel' => array(),
340 'target' => array(),
341 ),
342 )
343 ) . '</p></div>';
344 }
345
346 if ( isset( $_GET['update'] ) && 'saved' === $_GET['update'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only used to display a message.
347 echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
348 }
349 ?>
350
351 <div class="wrap">
352 <div class="icon32" id="icon-options-general"><br /></div>
353 <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
354
355 <?php
356 /**
357 * Fires at the top of the admin sharing settings screen.
358 *
359 * @module sharedaddy
360 *
361 * @since 1.6.0
362 */
363 do_action( 'pre_admin_screen_sharing' );
364 ?>
365
366 <?php
367 $block_availability = Jetpack_Gutenberg::get_cached_availability();
368 $is_block_available = (bool) isset( $block_availability['sharing-buttons'] ) && $block_availability['sharing-buttons']['available'];
369 $is_block_theme = wp_is_block_theme();
370 $show_block_message = $is_block_available && $is_block_theme;
371
372 // We either show old services config or the sharing block message.
373 if ( current_user_can( 'manage_options' ) ) :
374 $show_block_message ? $this->sharing_block_display() : $this->services_config_display();
375 endif;
376 ?>
377 </div>
378
379 <script type="text/javascript">
380 var sharing_loading_icon = '<?php echo esc_js( admin_url( '/images/loading.gif' ) ); ?>';
381 <?php
382 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- we handle the nonce on the PHP side.
383 if (
384 isset( $_GET['create_new_service'] ) && isset( $_GET['name'] ) && isset( $_GET['url'] ) && isset( $_GET['icon'] )
385 && 'true' == $_GET['create_new_service'] // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
386 ) :
387 ?>
388 jQuery(document).ready(function() {
389 // Prefill new service box and then open it
390 jQuery( '#new_sharing_name' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['name'] ) ) ); ?>' );
391 jQuery( '#new_sharing_url' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['url'] ) ) ); ?>' );
392 jQuery( '#new_sharing_icon' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['icon'] ) ) ); ?>' );
393 jQuery( '#add-a-new-service' ).click();
394 });
395 <?php endif; ?>
396 </script>
397 <?php
398 // phpcs:enable WordPress.Security.NonceVerification.Recommended
399 }
400
401 /**
402 * Display services admin UI for settings.
403 *
404 * @return void
405 */
406 public function services_config_display() {
407 $sharer = new Sharing_Service();
408 $enabled = $sharer->get_blog_services();
409 $global = $sharer->get_global_options();
410
411 $shows = array_values( get_post_types( array( 'public' => true ) ) );
412 array_unshift( $shows, 'index' );
413 if ( ! isset( $global['sharing_label'] ) ) {
414 $global['sharing_label'] = __( 'Share this:', 'jetpack' );
415 }
416 ?>
417 <div class="share_manage_options">
418 <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
419 <p><?php esc_html_e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ); ?></p>
420
421 <div id="services-config">
422 <table id="available-services">
423 <tr>
424 <td class="description">
425 <h3><?php esc_html_e( 'Available Services', 'jetpack' ); ?></h3>
426 <p><?php esc_html_e( "Drag and drop the services you'd like to enable into the box below.", 'jetpack' ); ?></p>
427 <p><a href="#TB_inline?height=395&amp;width=600&amp;inlineId=new-service" class="thickbox" id="add-a-new-service"><?php esc_html_e( 'Add a new service', 'jetpack' ); ?></a></p>
428 </td>
429 <td class="services">
430 <ul class="services-available" style="height: 100px;">
431 <?php foreach ( $sharer->get_all_services_blog() as $id => $service ) : ?>
432 <?php
433 if ( ! isset( $enabled['all'][ $id ] ) ) {
434 $this->output_service( $id, $service );
435 }
436 ?>
437 <?php endforeach; ?>
438 </ul>
439 <?php
440 if ( ( new Status() )->is_private_site() ) {
441 echo '<p><strong>' . esc_html__( 'Please note that your services have been restricted because your site is private.', 'jetpack' ) . '</strong></p>';
442 }
443 ?>
444 <br class="clearing" />
445 </td>
446 </tr>
447 </table>
448
449 <table id="enabled-services">
450 <tr>
451 <td class="description">
452 <h3>
453 <?php esc_html_e( 'Enabled Services', 'jetpack' ); ?>
454 <img src="<?php echo esc_url( admin_url( 'images/loading.gif' ) ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
455 </h3>
456 <p><?php esc_html_e( 'Services dragged here will appear individually.', 'jetpack' ); ?></p>
457 </td>
458 <td class="services" id="share-drop-target">
459 <h2 id="drag-instructions"
460 <?php
461 if ( is_countable( $enabled['visible'] ) && count( $enabled['visible'] ) > 0 ) {
462 echo ' style="display: none"';}
463 ?>
464 ><?php esc_html_e( 'Drag and drop available services here.', 'jetpack' ); ?></h2>
465
466 <ul class="services-enabled">
467 <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
468 <?php $this->output_service( $id, $service, true ); ?>
469 <?php endforeach; ?>
470
471 <li class="end-fix"></li>
472 </ul>
473 </td>
474 <td id="hidden-drop-target" class="services">
475 <p><?php esc_html_e( 'Services dragged here will be hidden behind a share button.', 'jetpack' ); ?></p>
476
477 <ul class="services-hidden">
478 <?php foreach ( $enabled['hidden'] as $id => $service ) : ?>
479 <?php $this->output_service( $id, $service, true ); ?>
480 <?php endforeach; ?>
481 <li class="end-fix"></li>
482 </ul>
483 </td>
484 </tr>
485 </table>
486
487 <table id="live-preview">
488 <tr>
489 <td class="description">
490 <h3><?php esc_html_e( 'Live Preview', 'jetpack' ); ?></h3>
491 </td>
492 <td class="services">
493 <h2 <?php echo ( is_countable( $enabled['all'] ) && count( $enabled['all'] ) > 0 ) ? ' style="display: none"' : ''; ?>><?php esc_html_e( 'Sharing is off. Add services above to enable.', 'jetpack' ); ?></h2>
494 <div class="sharedaddy sd-sharing-enabled">
495 <?php if ( is_countable( $enabled['all'] ) && count( $enabled['all'] ) > 0 ) : ?>
496 <h3 class="sd-title"><?php echo esc_html( $global['sharing_label'] ); ?></h3>
497 <?php endif; ?>
498 <div class="sd-content">
499 <ul class="preview">
500 <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
501 <?php $this->output_preview( $service ); ?>
502 <?php endforeach; ?>
503
504 <?php if ( is_countable( $enabled['hidden'] ) && count( $enabled['hidden'] ) > 0 ) : ?>
505 <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php esc_html_e( 'More', 'jetpack' ); ?></span></a></li>
506 <?php endif; ?>
507 </ul>
508
509 <?php if ( is_countable( $enabled['hidden'] ) && count( $enabled['hidden'] ) > 0 ) : ?>
510 <div class="sharing-hidden">
511 <div class="inner" style="display: none; <?php echo count( $enabled['hidden'] ) === 1 ? 'width:150px;' : ''; ?>">
512 <?php if ( count( $enabled['hidden'] ) === 1 ) : ?>
513 <ul style="background-image:none;">
514 <?php else : ?>
515 <ul>
516 <?php endif; ?>
517
518 <?php
519 foreach ( $enabled['hidden'] as $id => $service ) {
520 $this->output_preview( $service );
521 }
522 ?>
523 </ul>
524 </div>
525 </div>
526 <?php endif; ?>
527
528 <ul class="archive" style="display:none;">
529 <?php
530 foreach ( $sharer->get_all_services_blog() as $id => $service ) :
531 if ( isset( $enabled['visible'][ $id ] ) ) {
532 $service = $enabled['visible'][ $id ];
533 } elseif ( isset( $enabled['hidden'][ $id ] ) ) {
534 $service = $enabled['hidden'][ $id ];
535 }
536
537 $service->button_style = 'icon-text'; // The archive needs the full text, which is removed in JS later.
538 $service->smart = false;
539 $this->output_preview( $service );
540 endforeach;
541 ?>
542 <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php esc_html_e( 'More', 'jetpack' ); ?></span></a></li>
543 </ul>
544 </div>
545 </div>
546 <br class="clearing" />
547 </td>
548 </tr>
549 </table>
550
551 <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" id="save-enabled-shares">
552 <input type="hidden" name="action" value="sharing_save_services" />
553 <input type="hidden" name="visible" value="<?php echo esc_attr( implode( ',', array_keys( $enabled['visible'] ) ) ); ?>" />
554 <input type="hidden" name="hidden" value="<?php echo esc_attr( implode( ',', array_keys( $enabled['hidden'] ) ) ); ?>" />
555 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-options' ) ); ?>" />
556 </form>
557 </div>
558
559 <form method="post" action="">
560 <table class="form-table">
561 <tbody>
562 <tr valign="top">
563 <th scope="row"><label><?php esc_html_e( 'Button style', 'jetpack' ); ?></label></th>
564 <td>
565 <select name="button_style" id="button_style">
566 <option<?php echo ( $global['button_style'] === 'icon-text' ) ? ' selected="selected"' : ''; ?> value="icon-text"><?php esc_html_e( 'Icon + text', 'jetpack' ); ?></option>
567 <option<?php echo ( $global['button_style'] === 'icon' ) ? ' selected="selected"' : ''; ?> value="icon"><?php esc_html_e( 'Icon only', 'jetpack' ); ?></option>
568 <option<?php echo ( $global['button_style'] === 'text' ) ? ' selected="selected"' : ''; ?> value="text"><?php esc_html_e( 'Text only', 'jetpack' ); ?></option>
569 <option<?php echo ( $global['button_style'] === 'official' ) ? ' selected="selected"' : ''; ?> value="official"><?php esc_html_e( 'Official buttons', 'jetpack' ); ?></option>
570 </select>
571 </td>
572 </tr>
573 <tr valign="top">
574 <th scope="row"><label><?php esc_html_e( 'Sharing label', 'jetpack' ); ?></label></th>
575 <td>
576 <input type="text" name="sharing_label" value="<?php echo esc_attr( $global['sharing_label'] ); ?>" />
577 </td>
578 </tr>
579 <?php
580 /**
581 * Filters the HTML at the beginning of the "Show button on" row.
582 *
583 * @module sharedaddy
584 *
585 * @since 2.1.0
586 *
587 * @param string $var Opening HTML tag at the beginning of the "Show button on" row.
588 */
589 echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
590 ?>
591 <th scope="row"><label><?php esc_html_e( 'Show buttons on', 'jetpack' ); ?></label></th>
592 <td>
593 <?php
594 $br = false;
595 foreach ( $shows as $show ) :
596 if ( 'index' === $show ) {
597 $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
598 } else {
599 $post_type_object = get_post_type_object( $show );
600 $label = $post_type_object->labels->name;
601 }
602 ?>
603 <?php
604 if ( $br ) {
605 echo '<br />';
606 }
607 ?>
608 <label><input type="checkbox"<?php checked( in_array( $show, $global['show'], true ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label>
609 <?php
610 $br = true;
611 endforeach;
612 ?>
613 </td>
614 <?php
615 /**
616 * Filters the HTML at the end of the "Show button on" row.
617 *
618 * @module sharedaddy
619 *
620 * @since 2.1.0
621 *
622 * @param string $var Closing HTML tag at the end of the "Show button on" row.
623 */
624 echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
625 ?>
626
627 <?php
628 /**
629 * Fires at the end of the sharing global options settings table.
630 *
631 * @module sharedaddy
632 *
633 * @since 1.1.0
634 */
635 do_action( 'sharing_global_options' );
636 ?>
637 </tbody>
638 </table>
639
640 <p class="submit">
641 <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
642 </p>
643
644 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-options' ) ); ?>" />
645 </form>
646
647 <div id="new-service" style="display: none">
648 <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" id="new-service-form">
649 <table class="form-table">
650 <tbody>
651 <tr valign="top">
652 <th scope="row" width="100"><label><?php esc_html_e( 'Service name', 'jetpack' ); ?></label></th>
653 <td>
654 <input type="text" name="sharing_name" id="new_sharing_name" size="40" />
655 </td>
656 </tr>
657 <tr valign="top">
658 <th scope="row" width="100"><label><?php esc_html_e( 'Sharing URL', 'jetpack' ); ?></label></th>
659 <td>
660 <input type="text" name="sharing_url" id="new_sharing_url" size="40" />
661
662 <p><?php esc_html_e( 'You can add the following variables to your service sharing URL:', 'jetpack' ); ?><br/>
663 <code>%post_id%</code>, <code>%post_title%</code>, <code>%post_slug%</code>, <code>%post_url%</code>, <code>%post_full_url%</code>, <code>%post_excerpt%</code>, <code>%post_tags%</code>, <code>%home_url%</code></p>
664 </td>
665 </tr>
666 <tr valign="top">
667 <th scope="row" width="100"><label><?php esc_html_e( 'Icon URL', 'jetpack' ); ?></label></th>
668 <td>
669 <input type="text" name="sharing_icon" id="new_sharing_icon" size="40" />
670 <p><?php esc_html_e( 'Enter the URL of a 16x16px icon you want to use for this service.', 'jetpack' ); ?></p>
671 </td>
672 </tr>
673 <tr valign="top" width="100">
674 <th scope="row"></th>
675 <td>
676 <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Create Share Button', 'jetpack' ); ?>" />
677 <img src="<?php echo esc_url( admin_url( 'images/loading.gif' ) ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
678 </td>
679 </tr>
680
681 <?php
682 /**
683 * Fires after the custom sharing service form
684 *
685 * @module sharedaddy
686 *
687 * @since 1.1.0
688 */
689 do_action( 'sharing_new_service_form' );
690 ?>
691 </tbody>
692 </table>
693
694 <?php
695 /**
696 * Fires at the bottom of the admin sharing settings screen.
697 *
698 * @module sharedaddy
699 *
700 * @since 1.6.0
701 */
702 do_action( 'post_admin_screen_sharing' );
703 ?>
704
705 <div class="inerror" style="display: none; margin-top: 15px">
706 <p><?php esc_html_e( 'An error occurred creating your new sharing service - please check you gave valid details.', 'jetpack' ); ?></p>
707 </div>
708
709 <input type="hidden" name="action" value="sharing_new_service" />
710 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-new_service' ) ); ?>" />
711 </form>
712 </div>
713 <?php
714 }
715
716 /**
717 * Display sharing block admin UI for settings.
718 *
719 * @return void
720 */
721 public function sharing_block_display() {
722 $showcase_services = array(
723 new Share_Tumblr( 'tumblr', array() ),
724 new Share_Facebook( 'facebook', array() ),
725 new Share_Email( 'email', array() ),
726 new Share_Reddit( 'reddit', array() ),
727 );
728
729 global $submenu;
730 // Hide the link to Jetpack Sharing settings if no Jetpack Settings found in submenu list
731 $show_jetpack_admin_settings_link = array_reduce(
732 $submenu['jetpack'],
733 function ( $carry, $item ) {
734 return $carry || ( isset( $item[2] ) && $item[2] === 'jetpack#/settings' );
735 },
736 false
737 );
738
739 $host = new Status\Host();
740
741 $wpcom_link = 'https://wordpress.com/support/wordpress-editor/blocks/sharing-buttons-block/';
742
743 if ( function_exists( 'localized_wpcom_url' ) ) {
744 $wpcom_link = localized_wpcom_url( $wpcom_link );
745 }
746
747 $link = $host->is_wpcom_platform() ? $wpcom_link : Redirect::get_url( 'jetpack-support-sharing-block' );
748 ?>
749
750 <div class="share_manage_options">
751 <br class="clearing" />
752 <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
753 <div class="sharing-block-message__items-wrapper">
754 <div>
755 <p><?php esc_html_e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ); ?></p>
756 <div class="sharing-block-message__buttons-wrapper">
757 <a href="<?php echo esc_url( admin_url( 'site-editor.php?path=%2Fwp_template' ) ); ?>" class="button button-primary">
758 <?php esc_html_e( 'Go to the site editor', 'jetpack' ); ?>
759 </a>
760 <a data-target="wpcom-help-center" href="<?php echo esc_url( $link ); ?>" class="button" target="_blank" rel="noopener noreferrer">
761 <?php esc_html_e( 'Learn how to add Sharing Buttons', 'jetpack' ); ?>
762 </a>
763 </div>
764 </div>
765 <div>
766 <p><?php esc_html_e( 'Sharing Buttons example:', 'jetpack' ); ?></p>
767 <div class="sharedaddy sd-sharing-enabled">
768 <div class="sd-content">
769 <ul class="preview">
770 <?php foreach ( $showcase_services as $service ) : ?>
771 <?php $this->output_preview( $service ); ?>
772 <?php endforeach; ?>
773 </ul>
774 </div>
775 </div>
776 </div>
777 <?php if ( $show_jetpack_admin_settings_link ) : ?>
778 <p class="settings-sharing__block-theme-description">
779 <?php
780 printf(
781 wp_kses(
782 /* translators: Link to Jetpack sharing settings. */
783 __( 'You are using a block-based theme. You can <a class="dops-card__link" href="%s">disable Jetpack’s legacy sharing buttons</a> and add a sharing block to your theme’s template instead.', 'jetpack' ),
784 array(
785 'a' => array( 'href' => array() ),
786 )
787 ),
788 esc_url( admin_url( 'admin.php?page=jetpack#/sharing' ) )
789 );
790 ?>
791 </p>
792 <?php endif; ?>
793 </div>
794 <br class="clearing" />
795 </div>
796 <?php
797 }
798 }
799
800 /**
801 * Callback to get the value for the jetpack_sharing_enabled field.
802 *
803 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
804 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
805 * It is not possible to enable sharing on a post if it is disabled globally.
806 *
807 * @param array $post The post object.
808 *
809 * @return bool
810 */
811 function jetpack_post_sharing_get_value( array $post ) {
812 if ( ! isset( $post['id'] ) ) {
813 return false;
814 }
815
816 // if sharing IS disabled on this post, enabled=false, so negate the meta
817 return (bool) ! get_post_meta( $post['id'], 'sharing_disabled', true );
818 }
819
820 /**
821 * Callback to set sharing_disabled post_meta when the
822 * jetpack_sharing_enabled field is updated.
823 *
824 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
825 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
826 * It is not possible to enable sharing on a post if it is disabled globally.
827 *
828 * @param bool $enable_sharing Should sharing be enabled on this post.
829 * @param WP_Post $post_object The post object.
830 *
831 * @return int|bool
832 */
833 function jetpack_post_sharing_update_value( $enable_sharing, $post_object ) {
834 if ( $enable_sharing ) {
835 // delete the override if we want to enable sharing
836 return delete_post_meta( $post_object->ID, 'sharing_disabled' );
837 } else {
838 return update_post_meta( $post_object->ID, 'sharing_disabled', true );
839 }
840 }
841
842 /**
843 * Add Sharing post_meta to the REST API Post response.
844 *
845 * @action rest_api_init
846 * @uses register_rest_field
847 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
848 */
849 function jetpack_post_sharing_register_rest_field() {
850 $post_types = get_post_types( array( 'public' => true ) );
851 foreach ( $post_types as $post_type ) {
852 register_rest_field(
853 $post_type,
854 'jetpack_sharing_enabled',
855 array(
856 'get_callback' => 'jetpack_post_sharing_get_value',
857 'update_callback' => 'jetpack_post_sharing_update_value',
858 'schema' => array(
859 'description' => __( 'Are sharing buttons enabled?', 'jetpack' ),
860 'type' => 'boolean',
861 ),
862 )
863 );
864
865 /**
866 * Ensures all public internal post-types support `sharing`
867 * This feature support flag is used by the REST API and Gutenberg.
868 */
869 add_post_type_support( $post_type, 'jetpack-sharing-buttons' );
870 }
871 }
872
873 // Add Sharing post_meta to the REST API Post response.
874 add_action( 'rest_api_init', 'jetpack_post_sharing_register_rest_field' );
875
876 // Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
877 // restapi_theme_init because they depend on theme support, so let's also hook to that
878 add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
879
880 /**
881 * Initialize sharing settings in WP Admin.
882 *
883 * @return void
884 */
885 function sharing_admin_init() {
886 global $sharing_admin;
887
888 $sharing_admin = new Sharing_Admin();
889 }
890
891 add_action( 'init', 'sharing_admin_init' );
892