PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/sharedaddy/sharing.php

https://gitlab.com/Gashler/sg
PHP | 482 lines | 429 code | 40 blank | 13 comment | 45 complexity | 689183a9457fcb6655ac8a6405226987 MD5 | raw file
  1. <?php
  2. class Sharing_Admin {
  3. public function __construct() {
  4. if ( !defined( 'WP_SHARING_PLUGIN_URL' ) ) {
  5. define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  6. define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
  7. }
  8. require_once WP_SHARING_PLUGIN_DIR.'sharing-service.php';
  9. add_action( 'admin_init', array( &$this, 'admin_init' ) );
  10. add_action( 'admin_menu', array( &$this, 'subscription_menu' ) );
  11. // Insert our CSS and JS
  12. add_action( 'load-settings_page_sharing', array( &$this, 'sharing_head' ) );
  13. // Catch AJAX
  14. add_action( 'wp_ajax_sharing_save_services', array( &$this, 'ajax_save_services' ) );
  15. add_action( 'wp_ajax_sharing_save_options', array( &$this, 'ajax_save_options' ) );
  16. add_action( 'wp_ajax_sharing_new_service', array( &$this, 'ajax_new_service' ) );
  17. add_action( 'wp_ajax_sharing_delete_service', array( &$this, 'ajax_delete_service' ) );
  18. }
  19. public function sharing_head() {
  20. wp_enqueue_script( 'sharing-js', WP_SHARING_PLUGIN_URL.'admin-sharing.js', array( 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-form' ), 2 );
  21. wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL.'admin-sharing.css', false, JETPACK__VERSION );
  22. wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL.'sharing.css', false, JETPACK__VERSION );
  23. wp_enqueue_style( 'genericons' );
  24. wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array( ), 4 );
  25. add_thickbox();
  26. }
  27. public function admin_init() {
  28. if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) )
  29. $this->process_requests();
  30. }
  31. public function process_requests() {
  32. if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
  33. $sharer = new Sharing_Service();
  34. $sharer->set_global_options( $_POST );
  35. /**
  36. * Fires when updating sharing settings.
  37. *
  38. * @since 1.1.0
  39. */
  40. do_action( 'sharing_admin_update' );
  41. wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
  42. die();
  43. }
  44. }
  45. public function subscription_menu( $user ) {
  46. if ( !defined( 'IS_WPCOM' ) || !IS_WPCOM ) {
  47. $active = Jetpack::get_active_modules();
  48. if ( !in_array( 'publicize', $active ) && !current_user_can( 'manage_options' ) )
  49. return;
  50. }
  51. add_submenu_page( 'options-general.php', __( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'sharing', array( &$this, 'management_page' ) );
  52. }
  53. public function ajax_save_services() {
  54. if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) && isset( $_POST['hidden'] ) && isset( $_POST['visible'] ) ) {
  55. $sharer = new Sharing_Service();
  56. $sharer->set_blog_services( explode( ',', $_POST['visible'] ), explode( ',', $_POST['hidden'] ) );
  57. die();
  58. }
  59. }
  60. public function ajax_new_service() {
  61. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['sharing_name'] ) && isset( $_POST['sharing_url'] ) && isset( $_POST['sharing_icon'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-new_service' ) ) {
  62. $sharer = new Sharing_Service();
  63. if ( $service = $sharer->new_service( stripslashes( $_POST['sharing_name'] ), stripslashes( $_POST['sharing_url'] ), stripslashes( $_POST['sharing_icon'] ) ) ) {
  64. $this->output_service( $service->get_id(), $service );
  65. echo '<!--->';
  66. $service->button_style = 'icon-text';
  67. $this->output_preview( $service );
  68. die();
  69. }
  70. }
  71. // Fail
  72. die( '1' );
  73. }
  74. public function ajax_delete_service() {
  75. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_'.$_POST['service'] ) ) {
  76. $sharer = new Sharing_Service();
  77. $sharer->delete_service( $_POST['service'] );
  78. }
  79. }
  80. public function ajax_save_options() {
  81. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_'.$_POST['service'] ) ) {
  82. $sharer = new Sharing_Service();
  83. $service = $sharer->get_service( $_POST['service'] );
  84. if ( $service && $service instanceof Sharing_Advanced_Source ) {
  85. $service->update_options( $_POST );
  86. $sharer->set_service( $_POST['service'], $service );
  87. }
  88. $this->output_service( $service->get_id(), $service, true );
  89. echo '<!--->';
  90. $service->button_style = 'icon-text';
  91. $this->output_preview( $service );
  92. die();
  93. }
  94. }
  95. public function output_preview( $service ) {
  96. $klasses = array( 'advanced', 'preview-item' );
  97. if ( $service->button_style != 'text' || $service->has_custom_button_style() ) {
  98. $klasses[] = 'preview-'.$service->get_class();
  99. $klasses[] = 'share-'.$service->get_class();
  100. if ( $service->get_class() != $service->get_id() )
  101. $klasses[] = 'preview-'.$service->get_id();
  102. }
  103. echo '<li class="'.implode( ' ', $klasses ).'">';
  104. $service->display_preview();
  105. echo '</li>';
  106. }
  107. public function output_service( $id, $service, $show_dropdown = false ) {
  108. ?>
  109. <li class="service advanced share-<?php echo $service->get_class(); ?>" id="<?php echo $service->get_id(); ?>" tabindex="0">
  110. <span class="options-left"><?php echo esc_html( $service->get_name() ); ?></span>
  111. <?php if ( 0 === strpos( $service->get_id(), 'custom-' ) || $service->has_advanced_options() ) : ?>
  112. <span class="close"><a href="#" class="remove">&times;</a></span>
  113. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>">
  114. <input type="hidden" name="action" value="sharing_delete_service" />
  115. <input type="hidden" name="service" value="<?php echo esc_attr( $id ); ?>" />
  116. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options_'.$id );?>" />
  117. </form>
  118. <?php endif; ?>
  119. </li>
  120. <?php
  121. }
  122. public function management_page() {
  123. $sharer = new Sharing_Service();
  124. $enabled = $sharer->get_blog_services();
  125. $global = $sharer->get_global_options();
  126. $shows = array_values( get_post_types( array( 'public' => true ) ) );
  127. array_unshift( $shows, 'index' );
  128. if ( false == function_exists( 'mb_stripos' ) ) {
  129. echo '<div id="message" class="updated fade"><h3>' . __( 'Warning! Multibyte support missing!', 'jetpack' ) . '</h3>';
  130. echo "<p>" . sprintf( __( 'This plugin will work without it, but multibyte support is used <a href="%s">if available</a>. You may see minor problems with Tweets and other sharing services.', 'jetpack' ), "http://www.php.net/manual/en/mbstring.installation.php" ) . '</p></div>';
  131. }
  132. if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' )
  133. echo '<div class="updated"><p>'.__( 'Settings have been saved', 'jetpack' ).'</p></div>';
  134. if( ! isset( $global['sharing_label'] ) ) {
  135. $global['sharing_label'] = __( 'Share this:', 'jetpack' );
  136. }
  137. ?>
  138. <div class="wrap">
  139. <div class="icon32" id="icon-options-general"><br /></div>
  140. <h2><?php _e( 'Sharing Settings', 'jetpack' ); ?></h2>
  141. <?php
  142. /**
  143. * Fires at the top of the admin sharing settings screen.
  144. *
  145. * @since 1.6.0
  146. */
  147. do_action( 'pre_admin_screen_sharing' );
  148. ?>
  149. <?php if ( current_user_can( 'manage_options' ) ) : ?>
  150. <div class="share_manage_options">
  151. <h3><?php _e( 'Sharing Buttons', 'jetpack' ) ?></h3>
  152. <p><?php _e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ) ?></p>
  153. <div id="services-config">
  154. <table id="available-services">
  155. <tr>
  156. <td class="description">
  157. <h3><?php _e( 'Available Services', 'jetpack' ); ?></h3>
  158. <p><?php _e( "Drag and drop the services you'd like to enable into the box below.", 'jetpack' ); ?></p>
  159. <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>
  160. </td>
  161. <td class="services">
  162. <ul class="services-available" style="height: 100px;">
  163. <?php foreach ( $sharer->get_all_services_blog() AS $id => $service ) : ?>
  164. <?php
  165. if ( !isset( $enabled['all'][$id] ) )
  166. $this->output_service( $id, $service );
  167. ?>
  168. <?php endforeach; ?>
  169. </ul>
  170. <?php
  171. if ( -1 == get_option( 'blog_public' ) )
  172. echo '<p><strong>'.__( 'Please note that your services have been restricted because your site is private.', 'jetpack' ).'</strong></p>';
  173. ?>
  174. <br class="clearing" />
  175. </td>
  176. </tr>
  177. </table>
  178. <table id="enabled-services">
  179. <tr>
  180. <td class="description">
  181. <h3>
  182. <?php _e( 'Enabled Services', 'jetpack' ); ?>
  183. <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
  184. </h3>
  185. <p><?php _e( 'Services dragged here will appear individually.', 'jetpack' ); ?></p>
  186. </td>
  187. <td class="services" id="share-drop-target">
  188. <h2 id="drag-instructions" <?php if ( count( $enabled['visible'] ) > 0 ) echo ' style="display: none"'; ?>><?php _e( 'Drag and drop available services here.', 'jetpack' ); ?></h2>
  189. <ul class="services-enabled">
  190. <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
  191. <?php $this->output_service( $id, $service, true ); ?>
  192. <?php endforeach; ?>
  193. <li class="end-fix"></li>
  194. </ul>
  195. </td>
  196. <td id="hidden-drop-target" class="services">
  197. <p><?php _e( 'Services dragged here will be hidden behind a share button.', 'jetpack' ); ?></p>
  198. <ul class="services-hidden">
  199. <?php foreach ( $enabled['hidden'] as $id => $service ) : ?>
  200. <?php $this->output_service( $id, $service, true ); ?>
  201. <?php endforeach; ?>
  202. <li class="end-fix"></li>
  203. </ul>
  204. </td>
  205. </tr>
  206. </table>
  207. <table id="live-preview">
  208. <tr>
  209. <td class="description">
  210. <h3><?php _e( 'Live Preview', 'jetpack' ); ?></h3>
  211. </td>
  212. <td class="services">
  213. <h2<?php if ( count( $enabled['all'] ) > 0 ) echo ' style="display: none"'; ?>><?php _e( 'Sharing is off. Add services above to enable.', 'jetpack' ); ?></h2>
  214. <div class="sharedaddy sd-sharing-enabled">
  215. <?php if ( count( $enabled['all'] ) > 0 ) : ?>
  216. <h3 class="sd-title"><?php echo esc_html( $global['sharing_label'] ); ?></h3>
  217. <?php endif; ?>
  218. <div class="sd-content">
  219. <ul class="preview">
  220. <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
  221. <?php $this->output_preview( $service ); ?>
  222. <?php endforeach; ?>
  223. <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
  224. <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
  225. <?php endif; ?>
  226. </ul>
  227. <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
  228. <div class="sharing-hidden">
  229. <div class="inner" style="display: none; <?php echo count( $enabled['hidden'] ) == 1 ? 'width:150px;' : ''; ?>">
  230. <?php if ( count( $enabled['hidden'] ) == 1 ) : ?>
  231. <ul style="background-image:none;">
  232. <?php else: ?>
  233. <ul>
  234. <?php endif; ?>
  235. <?php foreach ( $enabled['hidden'] as $id => $service ) {
  236. $this->output_preview( $service );
  237. }?>
  238. </ul>
  239. </div>
  240. </div>
  241. <?php endif; ?>
  242. <ul class="archive" style="display:none;">
  243. <?php
  244. foreach ( $sharer->get_all_services_blog() as $id => $service ) :
  245. if ( isset( $enabled['visible'][$id] ) )
  246. $service = $enabled['visible'][$id];
  247. elseif ( isset( $enabled['hidden'][$id] ) )
  248. $service = $enabled['hidden'][$id];
  249. $service->button_style = 'icon-text'; // The archive needs the full text, which is removed in JS later
  250. $service->smart = false;
  251. $this->output_preview( $service );
  252. endforeach; ?>
  253. <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
  254. </ul>
  255. </div>
  256. </div>
  257. <br class="clearing" />
  258. </td>
  259. </tr>
  260. </table>
  261. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="save-enabled-shares">
  262. <input type="hidden" name="action" value="sharing_save_services" />
  263. <input type="hidden" name="visible" value="<?php echo implode( ',', array_keys( $enabled['visible'] ) ); ?>" />
  264. <input type="hidden" name="hidden" value="<?php echo implode( ',', array_keys( $enabled['hidden'] ) ); ?>" />
  265. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
  266. </form>
  267. </div>
  268. <form method="post" action="">
  269. <table class="form-table">
  270. <tbody>
  271. <tr valign="top">
  272. <th scope="row"><label><?php _e( 'Button style', 'jetpack' ); ?></label></th>
  273. <td>
  274. <select name="button_style" id="button_style">
  275. <option<?php if ( $global['button_style'] == 'icon-text' ) echo ' selected="selected"';?> value="icon-text"><?php _e( 'Icon + text', 'jetpack' ); ?></option>
  276. <option<?php if ( $global['button_style'] == 'icon' ) echo ' selected="selected"';?> value="icon"><?php _e( 'Icon only', 'jetpack' ); ?></option>
  277. <option<?php if ( $global['button_style'] == 'text' ) echo ' selected="selected"';?> value="text"><?php _e( 'Text only', 'jetpack' ); ?></option>
  278. <option<?php if ( $global['button_style'] == 'official' ) echo ' selected="selected"';?> value="official"><?php _e( 'Official buttons', 'jetpack' ); ?></option>
  279. </select>
  280. </td>
  281. </tr>
  282. <tr valign="top">
  283. <th scope="row"><label><?php _e( 'Sharing label', 'jetpack' ); ?></label></th>
  284. <td>
  285. <input type="text" name="sharing_label" value="<?php echo esc_attr( $global['sharing_label'] ); ?>" />
  286. </td>
  287. </tr>
  288. <?php
  289. /**
  290. * Filters the HTML at the beginning of the "Show button on" row.
  291. *
  292. * @since 2.1.0
  293. *
  294. * @param string $var Opening HTML tag at the beginning of the "Show button on" row.
  295. */
  296. echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
  297. ?>
  298. <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
  299. <td>
  300. <?php
  301. $br = false;
  302. foreach ( $shows as $show ) :
  303. if ( 'index' == $show ) {
  304. $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
  305. } else {
  306. $post_type_object = get_post_type_object( $show );
  307. $label = $post_type_object->labels->name;
  308. }
  309. ?>
  310. <?php if ( $br ) echo '<br />'; ?><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>
  311. <?php $br = true; endforeach; ?>
  312. </td>
  313. <?php
  314. /**
  315. * Filters the HTML at the end of the "Show button on" row.
  316. *
  317. * @since 2.1.0
  318. *
  319. * @param string $var Closing HTML tag at the end of the "Show button on" row.
  320. */
  321. echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
  322. ?>
  323. <?php
  324. /**
  325. * Fires at the end of the sharing global options settings table.
  326. *
  327. * @since 1.1.0
  328. */
  329. do_action( 'sharing_global_options' );
  330. ?>
  331. </tbody>
  332. </table>
  333. <p class="submit">
  334. <input type="submit" name="submit" class="button-primary" value="<?php _e( 'Save Changes', 'jetpack' ); ?>" />
  335. </p>
  336. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
  337. </form>
  338. <div id="new-service" style="display: none">
  339. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="new-service-form">
  340. <table class="form-table">
  341. <tbody>
  342. <tr valign="top">
  343. <th scope="row" width="100"><label><?php _e( 'Service name', 'jetpack' ); ?></label></th>
  344. <td>
  345. <input type="text" name="sharing_name" id="new_sharing_name" size="40" />
  346. </td>
  347. </tr>
  348. <tr valign="top">
  349. <th scope="row" width="100"><label><?php _e( 'Sharing URL', 'jetpack' ); ?></label></th>
  350. <td>
  351. <input type="text" name="sharing_url" id="new_sharing_url" size="40" />
  352. <p><?php _e( 'You can add the following variables to your service sharing URL:', 'jetpack' ); ?><br/>
  353. <code>%post_title%</code>, <code>%post_url%</code>, <code>%post_full_url%</code>, <code>%post_excerpt%</code>, <code>%post_tags%</code></p>
  354. </td>
  355. </tr>
  356. <tr valign="top">
  357. <th scope="row" width="100"><label><?php _e( 'Icon URL', 'jetpack' ); ?></label></th>
  358. <td>
  359. <input type="text" name="sharing_icon" id="new_sharing_icon" size="40" />
  360. <p><?php _e( 'Enter the URL of a 16x16px icon you want to use for this service.', 'jetpack' ); ?></p>
  361. </td>
  362. </tr>
  363. <tr valign="top" width="100">
  364. <th scope="row"></th>
  365. <td>
  366. <input type="submit" class="button-primary" value="<?php _e( 'Create Share Button', 'jetpack' ); ?>" />
  367. <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
  368. </td>
  369. </tr>
  370. <?php
  371. /**
  372. * Fires after the custom sharing service form
  373. *
  374. * @since 1.1.0
  375. */
  376. do_action( 'sharing_new_service_form' );
  377. ?>
  378. </tbody>
  379. </table>
  380. <?php
  381. /**
  382. * Fires at the bottom of the admin sharing settings screen.
  383. *
  384. * @since 1.6.0
  385. */
  386. do_action( 'post_admin_screen_sharing' );
  387. ?>
  388. <div class="inerror" style="display: none; margin-top: 15px">
  389. <p><?php _e( 'An error occurred creating your new sharing service - please check you gave valid details.', 'jetpack' ); ?></p>
  390. </div>
  391. <input type="hidden" name="action" value="sharing_new_service" />
  392. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-new_service' );?>" />
  393. </form>
  394. </div>
  395. </div>
  396. <?php endif; ?>
  397. </div>
  398. <script type="text/javascript">
  399. var sharing_loading_icon = '<?php echo esc_js( admin_url( "/images/loading.gif" ) ); ?>';
  400. <?php if ( isset( $_GET['create_new_service'] ) && 'true' == $_GET['create_new_service'] ) : ?>
  401. jQuery(document).ready(function() {
  402. // Prefill new service box and then open it
  403. jQuery( '#new_sharing_name' ).val( '<?php echo esc_js( $_GET['name'] ); ?>' );
  404. jQuery( '#new_sharing_url' ).val( '<?php echo esc_js( $_GET['url'] ); ?>' );
  405. jQuery( '#new_sharing_icon' ).val( '<?php echo esc_js( $_GET['icon'] ); ?>' );
  406. jQuery( '#add-a-new-service' ).click();
  407. });
  408. <?php endif; ?>
  409. </script>
  410. <?php
  411. }
  412. }
  413. function sharing_admin_init() {
  414. global $sharing_admin;
  415. $sharing_admin = new Sharing_Admin();
  416. }
  417. add_action( 'init', 'sharing_admin_init' );