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