Нет описания

sharing.php 23KB

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