/modules/sharedaddy/sharing.php

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