PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9
Jetpack – WP Security, Backup, Speed, & Growth v7.9
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 7.9, at modules/sharedaddy/sharing.php

643 lines 23.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Automattic\Jetpack\Assets;
4
5 if ( ! defined( 'WP_SHARING_PLUGIN_URL' ) ) {
6 define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
7 define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
8 }
9
10 class Sharing_Admin {
11
12 public function __construct() {
13 require_once WP_SHARING_PLUGIN_DIR . 'sharing-service.php';
14
15 add_action( 'admin_init', array( &$this, 'admin_init' ) );
16 add_action( 'admin_menu', array( &$this, 'subscription_menu' ) );
17
18 // Insert our CSS and JS
19 add_action( 'load-settings_page_sharing', array( &$this, 'sharing_head' ) );
20
21 // Catch AJAX
22 add_action( 'wp_ajax_sharing_save_services', array( &$this, 'ajax_save_services' ) );
23 add_action( 'wp_ajax_sharing_save_options', array( &$this, 'ajax_save_options' ) );
24 add_action( 'wp_ajax_sharing_new_service', array( &$this, 'ajax_new_service' ) );
25 add_action( 'wp_ajax_sharing_delete_service', array( &$this, 'ajax_delete_service' ) );
26 }
27
28 public function sharing_head() {
29 wp_enqueue_script(
30 'sharing-js',
31 Assets::get_file_url_for_environment(
32 '_inc/build/sharedaddy/admin-sharing.min.js',
33 'modules/sharedaddy/admin-sharing.js'
34 ),
35 array( 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-form' ),
36 2
37 );
38
39 /**
40 * Filters the switch that if set to true allows Jetpack to use minified assets. Defaults to true
41 * if the SCRIPT_DEBUG constant is not set or set to false. The filter overrides it.
42 *
43 * @since 6.2.0
44 *
45 * @param boolean $var should Jetpack use minified assets.
46 */
47 $postfix = apply_filters( 'jetpack_should_use_minified_assets', true ) ? '.min' : '';
48 if ( is_rtl() ) {
49 wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing-rtl' . $postfix . '.css', false, JETPACK__VERSION );
50 } else {
51 wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing' . $postfix . '.css', false, JETPACK__VERSION );
52 }
53 wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL . 'sharing.css', false, JETPACK__VERSION );
54
55 wp_enqueue_style( 'social-logos' );
56 wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array(), 4 );
57 add_thickbox();
58
59 // On Jetpack sites, make sure we include CSS to style the admin page.
60 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
61 Jetpack_Admin_Page::load_wrapper_styles();
62 }
63 }
64
65 public function admin_init() {
66 if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
67 $this->process_requests();
68 }
69 }
70
71 public function process_requests() {
72 if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
73 $sharer = new Sharing_Service();
74 $sharer->set_global_options( $_POST );
75 /**
76 * Fires when updating sharing settings.
77 *
78 * @module sharedaddy
79 *
80 * @since 1.1.0
81 */
82 do_action( 'sharing_admin_update' );
83
84 wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
85 die();
86 }
87 }
88
89 public function subscription_menu( $user ) {
90 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
91 $active = Jetpack::get_active_modules();
92 if ( ! in_array( 'publicize', $active ) && ! current_user_can( 'manage_options' ) ) {
93 return;
94 }
95 }
96
97 add_submenu_page(
98 'options-general.php',
99 __( 'Sharing Settings', 'jetpack' ),
100 __( 'Sharing', 'jetpack' ),
101 'publish_posts',
102 'sharing',
103 array( &$this, 'wrapper_admin_page' )
104 );
105 }
106
107 public function ajax_save_services() {
108 if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) && isset( $_POST['hidden'] ) && isset( $_POST['visible'] ) ) {
109 $sharer = new Sharing_Service();
110
111 $sharer->set_blog_services( explode( ',', $_POST['visible'] ), explode( ',', $_POST['hidden'] ) );
112 die();
113 }
114 }
115
116 public function ajax_new_service() {
117 if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['sharing_name'] ) && isset( $_POST['sharing_url'] ) && isset( $_POST['sharing_icon'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-new_service' ) ) {
118 $sharer = new Sharing_Service();
119 if ( $service = $sharer->new_service( stripslashes( $_POST['sharing_name'] ), stripslashes( $_POST['sharing_url'] ), stripslashes( $_POST['sharing_icon'] ) ) ) {
120 $this->output_service( $service->get_id(), $service );
121 echo '<!--->';
122 $service->button_style = 'icon-text';
123 $this->output_preview( $service );
124
125 die();
126 }
127 }
128
129 // Fail
130 die( '1' );
131 }
132
133 public function ajax_delete_service() {
134 if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_' . $_POST['service'] ) ) {
135 $sharer = new Sharing_Service();
136 $sharer->delete_service( $_POST['service'] );
137 }
138 }
139
140 public function ajax_save_options() {
141 if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_' . $_POST['service'] ) ) {
142 $sharer = new Sharing_Service();
143 $service = $sharer->get_service( $_POST['service'] );
144
145 if ( $service && $service instanceof Sharing_Advanced_Source ) {
146 $service->update_options( $_POST );
147
148 $sharer->set_service( $_POST['service'], $service );
149 }
150
151 $this->output_service( $service->get_id(), $service, true );
152 echo '<!--->';
153 $service->button_style = 'icon-text';
154 $this->output_preview( $service );
155 die();
156 }
157 }
158
159 public function output_preview( $service ) {
160
161 $klasses = array( 'advanced', 'preview-item' );
162
163 if ( $service->button_style != 'text' || $service->has_custom_button_style() ) {
164 $klasses[] = 'preview-' . $service->get_class();
165 $klasses[] = 'share-' . $service->get_class();
166 if ( $service->is_deprecated() ) {
167 $klasses[] = 'share-deprecated';
168 }
169
170 if ( $service->get_class() != $service->get_id() ) {
171 $klasses[] = 'preview-' . $service->get_id();
172 }
173 }
174
175 echo '<li class="' . implode( ' ', $klasses ) . '">';
176 $service->display_preview();
177 echo '</li>';
178 }
179
180 public function output_service( $id, $service, $show_dropdown = false ) {
181 $title = '';
182 $klasses = array( 'service', 'advanced', 'share-' . $service->get_class() );
183 if ( $service->is_deprecated() ) {
184 $title = sprintf( __( 'The %1$s service has shut down. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $service->get_name() );
185 $klasses[] = 'share-deprecated';
186 }
187
188 ?>
189 <li class="<?php echo implode( ' ', $klasses ); ?>" id="<?php echo $service->get_id(); ?>" tabindex="0" title="<?php echo esc_attr( $title ); ?>">
190 <span class="options-left"><?php echo esc_html( $service->get_name() ); ?></span>
191 <?php if ( 0 === strpos( $service->get_id(), 'custom-' ) || $service->has_advanced_options() ) : ?>
192 <span class="close"><a href="#" class="remove">&times;</a></span>
193 <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>">
194 <input type="hidden" name="action" value="sharing_delete_service" />
195 <input type="hidden" name="service" value="<?php echo esc_attr( $id ); ?>" />
196 <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options_' . $id );?>" />
197 </form>
198 <?php endif; ?>
199 </li>
200 <?php
201 }
202
203 public function wrapper_admin_page() {
204 Jetpack_Admin_Page::wrap_ui( array( &$this, 'management_page' ), array( 'is-wide' =>true ) );
205 }
206
207 public function management_page() {
208 $sharer = new Sharing_Service();
209 $enabled = $sharer->get_blog_services();
210 $global = $sharer->get_global_options();
211
212 $shows = array_values( get_post_types( array( 'public' => true ) ) );
213 array_unshift( $shows, 'index' );
214
215 if ( false == function_exists( 'mb_stripos' ) ) {
216 echo '<div id="message" class="updated fade"><h3>' . __( 'Warning! Multibyte support missing!', 'jetpack' ) . '</h3>';
217 echo '<p>' . sprintf( __( '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' ), 'https://www.php.net/manual/en/mbstring.installation.php' ) . '</p></div>';
218 }
219
220 if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ) {
221 echo '<div class="updated"><p>' . __( 'Settings have been saved', 'jetpack' ) . '</p></div>';
222 }
223
224 if ( ! isset( $global['sharing_label'] ) ) {
225 $global['sharing_label'] = __( 'Share this:', 'jetpack' );
226 }
227 ?>
228
229 <div class="wrap">
230 <div class="icon32" id="icon-options-general"><br /></div>
231 <h1><?php _e( 'Sharing Settings', 'jetpack' ); ?></h1>
232
233 <?php
234 /**
235 * Fires at the top of the admin sharing settings screen.
236 *
237 * @module sharedaddy
238 *
239 * @since 1.6.0
240 */
241 do_action( 'pre_admin_screen_sharing' );
242 ?>
243
244 <?php if ( current_user_can( 'manage_options' ) ) : ?>
245
246 <div class="share_manage_options">
247 <h2><?php _e( 'Sharing Buttons', 'jetpack' ) ?></h2>
248 <p><?php _e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ) ?></p>
249
250 <div id="services-config">
251 <table id="available-services">
252 <tr>
253 <td class="description">
254 <h3><?php _e( 'Available Services', 'jetpack' ); ?></h3>
255 <p><?php _e( "Drag and drop the services you'd like to enable into the box below.", 'jetpack' ); ?></p>
256 <p><a href="#TB_inline?height=395&amp;width=600&amp;inlineId=new-service" class="thickbox" id="add-a-new-service"><?php _e( 'Add a new service', 'jetpack' ); ?></a></p>
257 </td>
258 <td class="services">
259 <ul class="services-available" style="height: 100px;">
260 <?php foreach ( $sharer->get_all_services_blog() as $id => $service ) : ?>
261 <?php
262 if ( ! isset( $enabled['all'][ $id ] ) ) {
263 $this->output_service( $id, $service );
264 }
265 ?>
266 <?php endforeach; ?>
267 </ul>
268 <?php
269 if ( -1 == get_option( 'blog_public' ) ) {
270 echo '<p><strong>' . __( 'Please note that your services have been restricted because your site is private.', 'jetpack' ) . '</strong></p>';
271 }
272 ?>
273 <br class="clearing" />
274 </td>
275 </tr>
276 </table>
277
278 <table id="enabled-services">
279 <tr>
280 <td class="description">
281 <h3>
282 <?php _e( 'Enabled Services', 'jetpack' ); ?>
283 <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
284 </h3>
285 <p><?php _e( 'Services dragged here will appear individually.', 'jetpack' ); ?></p>
286 </td>
287 <td class="services" id="share-drop-target">
288 <h2 id="drag-instructions" <?php if ( count( $enabled['visible'] ) > 0 ) { echo ' style="display: none"';} ?>><?php _e( 'Drag and drop available services here.', 'jetpack' ); ?></h2>
289
290 <ul class="services-enabled">
291 <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
292 <?php $this->output_service( $id, $service, true ); ?>
293 <?php endforeach; ?>
294
295 <li class="end-fix"></li>
296 </ul>
297 </td>
298 <td id="hidden-drop-target" class="services">
299 <p><?php _e( 'Services dragged here will be hidden behind a share button.', 'jetpack' ); ?></p>
300
301 <ul class="services-hidden">
302 <?php foreach ( $enabled['hidden'] as $id => $service ) : ?>
303 <?php $this->output_service( $id, $service, true ); ?>
304 <?php endforeach; ?>
305 <li class="end-fix"></li>
306 </ul>
307 </td>
308 </tr>
309 </table>
310
311 <table id="live-preview">
312 <tr>
313 <td class="description">
314 <h3><?php _e( 'Live Preview', 'jetpack' ); ?></h3>
315 </td>
316 <td class="services">
317 <h2 <?php echo ( count( $enabled['all'] ) > 0 ) ? ' style="display: none"' : ''; ?>><?php _e( 'Sharing is off. Add services above to enable.', 'jetpack' ); ?></h2>
318 <div class="sharedaddy sd-sharing-enabled">
319 <?php if ( count( $enabled['all'] ) > 0 ) : ?>
320 <h3 class="sd-title"><?php echo esc_html( $global['sharing_label'] ); ?></h3>
321 <?php endif; ?>
322 <div class="sd-content">
323 <ul class="preview">
324 <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
325 <?php $this->output_preview( $service ); ?>
326 <?php endforeach; ?>
327
328 <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
329 <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
330 <?php endif; ?>
331 </ul>
332
333 <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
334 <div class="sharing-hidden">
335 <div class="inner" style="display: none; <?php echo count( $enabled['hidden'] ) == 1 ? 'width:150px;' : ''; ?>">
336 <?php if ( count( $enabled['hidden'] ) == 1 ) : ?>
337 <ul style="background-image:none;">
338 <?php else : ?>
339 <ul>
340 <?php endif; ?>
341
342 <?php
343 foreach ( $enabled['hidden'] as $id => $service ) {
344 $this->output_preview( $service );
345 }
346 ?>
347 </ul>
348 </div>
349 </div>
350 <?php endif; ?>
351
352 <ul class="archive" style="display:none;">
353 <?php
354 foreach ( $sharer->get_all_services_blog() as $id => $service ) :
355 if ( isset( $enabled['visible'][ $id ] ) ) {
356 $service = $enabled['visible'][ $id ];
357 } elseif ( isset( $enabled['hidden'][ $id ] ) ) {
358 $service = $enabled['hidden'][ $id ];
359 }
360
361 $service->button_style = 'icon-text'; // The archive needs the full text, which is removed in JS later
362 $service->smart = false;
363 $this->output_preview( $service );
364 endforeach; ?>
365 <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
366 </ul>
367 </div>
368 </div>
369 <br class="clearing" />
370 </td>
371 </tr>
372 </table>
373
374 <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="save-enabled-shares">
375 <input type="hidden" name="action" value="sharing_save_services" />
376 <input type="hidden" name="visible" value="<?php echo implode( ',', array_keys( $enabled['visible'] ) ); ?>" />
377 <input type="hidden" name="hidden" value="<?php echo implode( ',', array_keys( $enabled['hidden'] ) ); ?>" />
378 <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
379 </form>
380 </div>
381
382 <form method="post" action="">
383 <table class="form-table">
384 <tbody>
385 <tr valign="top">
386 <th scope="row"><label><?php _e( 'Button style', 'jetpack' ); ?></label></th>
387 <td>
388 <select name="button_style" id="button_style">
389 <option<?php echo ( $global['button_style'] == 'icon-text' ) ? ' selected="selected"' : ''; ?> value="icon-text"><?php _e( 'Icon + text', 'jetpack' ); ?></option>
390 <option<?php echo ( $global['button_style'] == 'icon' ) ? ' selected="selected"' : ''; ?> value="icon"><?php _e( 'Icon only', 'jetpack' ); ?></option>
391 <option<?php echo ( $global['button_style'] == 'text' ) ? ' selected="selected"' : ''; ?> value="text"><?php _e( 'Text only', 'jetpack' ); ?></option>
392 <option<?php echo ( $global['button_style'] == 'official' ) ? ' selected="selected"' : ''; ?> value="official"><?php _e( 'Official buttons', 'jetpack' ); ?></option>
393 </select>
394 </td>
395 </tr>
396 <tr valign="top">
397 <th scope="row"><label><?php _e( 'Sharing label', 'jetpack' ); ?></label></th>
398 <td>
399 <input type="text" name="sharing_label" value="<?php echo esc_attr( $global['sharing_label'] ); ?>" />
400 </td>
401 </tr>
402 <?php
403 /**
404 * Filters the HTML at the beginning of the "Show button on" row.
405 *
406 * @module sharedaddy
407 *
408 * @since 2.1.0
409 *
410 * @param string $var Opening HTML tag at the beginning of the "Show button on" row.
411 */
412 echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
413 ?>
414 <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
415 <td>
416 <?php
417 $br = false;
418 foreach ( $shows as $show ) :
419 if ( 'index' == $show ) {
420 $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
421 } else {
422 $post_type_object = get_post_type_object( $show );
423 $label = $post_type_object->labels->name;
424 }
425 ?>
426 <?php
427 if ( $br ) {
428 echo '<br />';
429 }
430 ?>
431 <label><input type="checkbox"<?php checked( in_array( $show, $global['show'] ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label>
432 <?php
433 $br = true;
434 endforeach;
435 ?>
436 </td>
437 <?php
438 /**
439 * Filters the HTML at the end of the "Show button on" row.
440 *
441 * @module sharedaddy
442 *
443 * @since 2.1.0
444 *
445 * @param string $var Closing HTML tag at the end of the "Show button on" row.
446 */
447 echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
448 ?>
449
450 <?php
451 /**
452 * Fires at the end of the sharing global options settings table.
453 *
454 * @module sharedaddy
455 *
456 * @since 1.1.0
457 */
458 do_action( 'sharing_global_options' );
459 ?>
460 </tbody>
461 </table>
462
463 <p class="submit">
464 <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
465 </p>
466
467 <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
468 </form>
469
470 <div id="new-service" style="display: none">
471 <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="new-service-form">
472 <table class="form-table">
473 <tbody>
474 <tr valign="top">
475 <th scope="row" width="100"><label><?php _e( 'Service name', 'jetpack' ); ?></label></th>
476 <td>
477 <input type="text" name="sharing_name" id="new_sharing_name" size="40" />
478 </td>
479 </tr>
480 <tr valign="top">
481 <th scope="row" width="100"><label><?php _e( 'Sharing URL', 'jetpack' ); ?></label></th>
482 <td>
483 <input type="text" name="sharing_url" id="new_sharing_url" size="40" />
484
485 <p><?php _e( 'You can add the following variables to your service sharing URL:', 'jetpack' ); ?><br/>
486 <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>
487 </td>
488 </tr>
489 <tr valign="top">
490 <th scope="row" width="100"><label><?php _e( 'Icon URL', 'jetpack' ); ?></label></th>
491 <td>
492 <input type="text" name="sharing_icon" id="new_sharing_icon" size="40" />
493 <p><?php _e( 'Enter the URL of a 16x16px icon you want to use for this service.', 'jetpack' ); ?></p>
494 </td>
495 </tr>
496 <tr valign="top" width="100">
497 <th scope="row"></th>
498 <td>
499 <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Create Share Button', 'jetpack' ); ?>" />
500 <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
501 </td>
502 </tr>
503
504 <?php
505 /**
506 * Fires after the custom sharing service form
507 *
508 * @module sharedaddy
509 *
510 * @since 1.1.0
511 */
512 do_action( 'sharing_new_service_form' );
513 ?>
514 </tbody>
515 </table>
516
517 <?php
518 /**
519 * Fires at the bottom of the admin sharing settings screen.
520 *
521 * @module sharedaddy
522 *
523 * @since 1.6.0
524 */
525 do_action( 'post_admin_screen_sharing' );
526 ?>
527
528 <div class="inerror" style="display: none; margin-top: 15px">
529 <p><?php _e( 'An error occurred creating your new sharing service - please check you gave valid details.', 'jetpack' ); ?></p>
530 </div>
531
532 <input type="hidden" name="action" value="sharing_new_service" />
533 <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-new_service' );?>" />
534 </form>
535 </div>
536 </div>
537
538 <?php endif; ?>
539
540
541 </div>
542
543 <script type="text/javascript">
544 var sharing_loading_icon = '<?php echo esc_js( admin_url( '/images/loading.gif' ) ); ?>';
545 <?php if ( isset( $_GET['create_new_service'] ) && 'true' == $_GET['create_new_service'] ) : ?>
546 jQuery(document).ready(function() {
547 // Prefill new service box and then open it
548 jQuery( '#new_sharing_name' ).val( '<?php echo esc_js( $_GET['name'] ); ?>' );
549 jQuery( '#new_sharing_url' ).val( '<?php echo esc_js( $_GET['url'] ); ?>' );
550 jQuery( '#new_sharing_icon' ).val( '<?php echo esc_js( $_GET['icon'] ); ?>' );
551 jQuery( '#add-a-new-service' ).click();
552 });
553 <?php endif; ?>
554 </script>
555 <?php
556 }
557 }
558
559 /**
560 * Callback to get the value for the jetpack_sharing_enabled field.
561 *
562 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
563 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
564 * It is not possible to enable sharing on a post if it is disabled globally.
565 */
566 function jetpack_post_sharing_get_value( array $post ) {
567 // if sharing IS disabled on this post, enabled=false, so negate the meta
568 return (bool) ! get_post_meta( $post['id'], 'sharing_disabled', true );
569 }
570
571 /**
572 * Callback to set sharing_disabled post_meta when the
573 * jetpack_sharing_enabled field is updated.
574 *
575 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
576 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
577 * It is not possible to enable sharing on a post if it is disabled globally.
578 *
579 */
580 function jetpack_post_sharing_update_value( $enable_sharing, $post_object ) {
581 if ( $enable_sharing ) {
582 // delete the override if we want to enable sharing
583 return delete_post_meta( $post_object->ID, 'sharing_disabled' );
584 } else {
585 return update_post_meta( $post_object->ID, 'sharing_disabled', true );
586 }
587 }
588
589 /**
590 * Add Sharing post_meta to the REST API Post response.
591 *
592 * @action rest_api_init
593 * @uses register_rest_field
594 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
595 */
596 function jetpack_post_sharing_register_rest_field() {
597 $post_types = get_post_types( array( 'public' => true ) );
598 foreach ( $post_types as $post_type ) {
599 register_rest_field(
600 $post_type,
601 'jetpack_sharing_enabled',
602 array(
603 'get_callback' => 'jetpack_post_sharing_get_value',
604 'update_callback' => 'jetpack_post_sharing_update_value',
605 'schema' => array(
606 'description' => __( 'Are sharing buttons enabled?', 'jetpack' ),
607 'type' => 'boolean',
608 ),
609 )
610 );
611
612 /**
613 * Ensures all public internal post-types support `sharing`
614 * This feature support flag is used by the REST API and Gutenberg.
615 */
616 add_post_type_support( $post_type, 'jetpack-sharing-buttons' );
617 }
618 }
619
620 // Add Sharing post_meta to the REST API Post response.
621 add_action( 'rest_api_init', 'jetpack_post_sharing_register_rest_field' );
622
623 // Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
624 // restapi_theme_init because they depend on theme support, so let's also hook to that
625 add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
626
627 function sharing_admin_init() {
628 global $sharing_admin;
629
630 $sharing_admin = new Sharing_Admin();
631 }
632
633 /**
634 * Set the Likes and Sharing Gutenberg extension as available
635 */
636 function jetpack_sharing_set_extension_availability() {
637 Jetpack_Gutenberg::set_extension_available( 'sharing' );
638 }
639
640 add_action( 'jetpack_register_gutenberg_extensions', 'jetpack_sharing_set_extension_availability' );
641
642 add_action( 'init', 'sharing_admin_init' );
643