PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/sharpmachine/wakeupmedia.com
PHP | 431 lines | 390 code | 38 blank | 3 comment | 40 complexity | 5ea97b243f837014c0b8236ec3ace495 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, WP_SHARING_PLUGIN_VERSION );
  22. wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL.'sharing.css', false, WP_SHARING_PLUGIN_VERSION );
  23. wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array( ), 2 );
  24. add_thickbox();
  25. }
  26. public function admin_init() {
  27. if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) )
  28. $this->process_requests();
  29. }
  30. public function process_requests() {
  31. if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
  32. $sharer = new Sharing_Service();
  33. $sharer->set_global_options( $_POST );
  34. do_action( 'sharing_admin_update' );
  35. wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
  36. die();
  37. }
  38. }
  39. public function subscription_menu( $user ) {
  40. add_submenu_page( 'options-general.php', __( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( &$this, 'management_page' ) );
  41. }
  42. public function ajax_save_services() {
  43. if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) && isset( $_POST['hidden'] ) && isset( $_POST['visible'] ) ) {
  44. $sharer = new Sharing_Service();
  45. $sharer->set_blog_services( explode( ',', $_POST['visible'] ), explode( ',', $_POST['hidden'] ) );
  46. die();
  47. }
  48. }
  49. public function ajax_new_service() {
  50. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['sharing_name'] ) && isset( $_POST['sharing_url'] ) && isset( $_POST['sharing_icon'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-new_service' ) ) {
  51. $sharer = new Sharing_Service();
  52. if ( $service = $sharer->new_service( $_POST['sharing_name'], $_POST['sharing_url'], $_POST['sharing_icon'] ) ) {
  53. $this->output_service( $service->get_id(), $service );
  54. echo '<!--->';
  55. $service->button_style = 'icon-text';
  56. $this->output_preview( $service );
  57. die();
  58. }
  59. }
  60. // Fail
  61. die( '1' );
  62. }
  63. public function ajax_delete_service() {
  64. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_'.$_POST['service'] ) ) {
  65. $sharer = new Sharing_Service();
  66. $sharer->delete_service( $_POST['service'] );
  67. }
  68. }
  69. public function ajax_save_options() {
  70. if ( isset( $_POST['_wpnonce'] ) && isset( $_POST['service'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options_'.$_POST['service'] ) ) {
  71. $sharer = new Sharing_Service();
  72. $service = $sharer->get_service( $_POST['service'] );
  73. if ( $service && $service instanceof Sharing_Advanced_Source ) {
  74. $service->update_options( $_POST );
  75. $sharer->set_service( $_POST['service'], $service );
  76. }
  77. $this->output_service( $service->get_id(), $service, true );
  78. echo '<!--->';
  79. $service->button_style = 'icon-text';
  80. $this->output_preview( $service );
  81. die();
  82. }
  83. }
  84. public function output_preview( $service ) {
  85. $klasses = array( 'advanced', 'preview-item' );
  86. if (
  87. 'googleplus1' == $service->shortname
  88. ||
  89. $service->button_style != 'text'
  90. ||
  91. $service->has_custom_button_style()
  92. ) {
  93. $klasses[] = 'preview-'.$service->get_class();
  94. $klasses[] = 'share-'.$service->get_class();
  95. if ( $service->get_class() != $service->get_id() )
  96. $klasses[] = 'preview-'.$service->get_id();
  97. }
  98. echo '<li class="'.implode( ' ', $klasses ).'">';
  99. echo $service->display_preview();
  100. echo '</li>';
  101. }
  102. public function output_service( $id, $service, $show_dropdown = false ) {
  103. ?>
  104. <li class="service advanced share-<?php echo $service->get_class(); ?>" id="<?php echo $service->get_id(); ?>">
  105. <span class="options-left"><?php echo esc_html( $service->get_name() ); ?></span>
  106. <?php if ( 0 === strpos( $service->get_id(), 'custom-' ) || $service->has_advanced_options() ) : ?>
  107. <span class="close"><a href="#" class="remove">&times;</a></span>
  108. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>">
  109. <input type="hidden" name="action" value="sharing_delete_service" />
  110. <input type="hidden" name="service" value="<?php echo esc_attr( $id ); ?>" />
  111. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options_'.$id );?>" />
  112. </form>
  113. <?php endif; ?>
  114. </li>
  115. <?php
  116. }
  117. public function management_page() {
  118. $sharer = new Sharing_Service();
  119. $enabled = $sharer->get_blog_services();
  120. $global = $sharer->get_global_options();
  121. $shows = array_values( get_post_types( array( 'public' => true ) ) );
  122. array_unshift( $shows, 'index' );
  123. if ( false == function_exists( 'mb_stripos' ) ) {
  124. echo '<div id="message" class="updated fade"><h3>' . __( 'Warning! Multibyte support missing!', 'jetpack' ) . '</h3>';
  125. 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>';
  126. }
  127. if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' )
  128. echo '<div class="updated"><p>'.__( 'Settings have been saved', 'jetpack' ).'</p></div>';
  129. ?>
  130. <div class="wrap">
  131. <div class="icon32" id="icon-options-general"><br /></div>
  132. <h2><?php _e( 'Sharing Settings', 'jetpack' ); ?></h2>
  133. <?php do_action( 'pre_admin_screen_sharing' ) ?>
  134. <?php if ( current_user_can( 'manage_options' ) ) : ?>
  135. <h3><?php _e( 'Sharing Buttons', 'jetpack' ) ?></h3>
  136. <p><?php _e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ) ?></p>
  137. <div id="services-config">
  138. <table id="available-services">
  139. <tr>
  140. <td class="description">
  141. <h3><?php _e( 'Available Services', 'jetpack' ); ?></h3>
  142. <p><?php _e( "Drag and drop the services you'd like to enable into the box below.", 'jetpack' ); ?></p>
  143. <p><a href="#TB_inline?height=395&amp;width=600&amp;inlineId=new-service" title="<?php echo esc_attr( __( 'Add a new service', 'jetpack' ) ); ?>" class="thickbox" id="add-a-new-service"><?php _e( 'Add a new service', 'jetpack' ); ?></a></p>
  144. </td>
  145. <td class="services">
  146. <ul class="services-available" style="height: 100px;">
  147. <?php foreach ( $sharer->get_all_services_blog() AS $id => $service ) : ?>
  148. <?php
  149. if ( !isset( $enabled['all'][$id] ) )
  150. $this->output_service( $id, $service );
  151. ?>
  152. <?php endforeach; ?>
  153. </ul>
  154. <br class="clearing" />
  155. </td>
  156. </tr>
  157. </table>
  158. <table id="enabled-services">
  159. <tr>
  160. <td class="description">
  161. <h3>
  162. <?php _e( 'Enabled Services', 'jetpack' ); ?>
  163. <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
  164. </h3>
  165. <p><?php _e( 'Services dragged here will appear individually.', 'jetpack' ); ?></p>
  166. </td>
  167. <td class="services" id="share-drop-target">
  168. <h2 id="drag-instructions" <?php if ( count( $enabled['visible'] ) > 0 ) echo ' style="display: none"'; ?>><?php _e( 'Drag and drop available services here.', 'jetpack' ); ?></h2>
  169. <ul class="services-enabled">
  170. <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
  171. <?php $this->output_service( $id, $service, true ); ?>
  172. <?php endforeach; ?>
  173. <li class="end-fix"></li>
  174. </ul>
  175. </td>
  176. <td id="hidden-drop-target" class="services">
  177. <p><?php _e( 'Services dragged here will be hidden behind a share button.', 'jetpack' ); ?></p>
  178. <ul class="services-hidden">
  179. <?php foreach ( $enabled['hidden'] as $id => $service ) : ?>
  180. <?php $this->output_service( $id, $service, true ); ?>
  181. <?php endforeach; ?>
  182. <li class="end-fix"></li>
  183. </ul>
  184. </td>
  185. </tr>
  186. </table>
  187. <table id="live-preview">
  188. <tr>
  189. <td class="description">
  190. <h3><?php _e( 'Live Preview', 'jetpack' ); ?></h3>
  191. </td>
  192. <td class="services">
  193. <h2<?php if ( count( $enabled['all'] ) > 0 ) echo ' style="display: none"'; ?>><?php _e( 'Sharing is off. Add services above to enable.', 'jetpack' ); ?></h2>
  194. <div class="sharedaddy sd-sharing-enabled">
  195. <?php if ( count( $enabled['all'] ) > 0 ) : ?>
  196. <h3 class="sd-title"><?php echo esc_html( $global['sharing_label'] ); ?></h3>
  197. <?php endif; ?>
  198. <div class="sd-content">
  199. <ul class="preview">
  200. <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
  201. <?php $this->output_preview( $service ); ?>
  202. <?php endforeach; ?>
  203. <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
  204. <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
  205. <?php endif; ?>
  206. </ul>
  207. <?php if ( count( $enabled['hidden'] ) > 0 ) : ?>
  208. <div class="sharing-hidden">
  209. <div class="inner" style="display: none; <?php echo count( $enabled['hidden'] ) == 1 ? 'width:150px;' : ''; ?>">
  210. <?php if ( count( $enabled['hidden'] ) == 1 ) : ?>
  211. <ul style="background-image:none;">
  212. <?php else: ?>
  213. <ul>
  214. <?php endif; ?>
  215. <?php foreach ( $enabled['hidden'] as $id => $service ) {
  216. $this->output_preview( $service );
  217. }?>
  218. </ul>
  219. </div>
  220. </div>
  221. <?php endif; ?>
  222. <ul class="archive" style="display:none;">
  223. <?php
  224. foreach ( $sharer->get_all_services_blog() as $id => $service ) :
  225. if ( isset( $enabled['visible'][$id] ) )
  226. $service = $enabled['visible'][$id];
  227. elseif ( isset( $enabled['hidden'][$id] ) )
  228. $service = $enabled['hidden'][$id];
  229. $service->button_style = 'icon-text'; // The archive needs the full text, which is removed in JS later
  230. $service->smart = false;
  231. $this->output_preview( $service );
  232. endforeach; ?>
  233. <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php _e( 'More', 'jetpack' ); ?></span></a></li>
  234. </ul>
  235. <div class="sharing-clear"></div>
  236. </div>
  237. </div>
  238. <br class="clearing" />
  239. </td>
  240. </tr>
  241. </table>
  242. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="save-enabled-shares">
  243. <input type="hidden" name="action" value="sharing_save_services" />
  244. <input type="hidden" name="visible" value="<?php echo implode( ',', array_keys( $enabled['visible'] ) ); ?>" />
  245. <input type="hidden" name="hidden" value="<?php echo implode( ',', array_keys( $enabled['hidden'] ) ); ?>" />
  246. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
  247. </form>
  248. </div>
  249. <form method="post" action="">
  250. <table class="form-table">
  251. <tbody>
  252. <tr valign="top">
  253. <th scope="row"><label><?php _e( 'Button style', 'jetpack' ); ?></label></th>
  254. <td>
  255. <select name="button_style" id="button_style">
  256. <option<?php if ( $global['button_style'] == 'icon-text' ) echo ' selected="selected"';?> value="icon-text"><?php _e( 'Icon + text', 'jetpack' ); ?></option>
  257. <option<?php if ( $global['button_style'] == 'icon' ) echo ' selected="selected"';?> value="icon"><?php _e( 'Icon only', 'jetpack' ); ?></option>
  258. <option<?php if ( $global['button_style'] == 'text' ) echo ' selected="selected"';?> value="text"><?php _e( 'Text only', 'jetpack' ); ?></option>
  259. <option<?php if ( $global['button_style'] == 'official' ) echo ' selected="selected"';?> value="official"><?php _e( 'Official buttons', 'jetpack' ); ?></option>
  260. </select>
  261. </td>
  262. </tr>
  263. <tr valign="top">
  264. <th scope="row"><label><?php _e( 'Sharing label', 'jetpack' ); ?></label></th>
  265. <td>
  266. <input type="text" name="sharing_label" value="<?php echo esc_attr( $global['sharing_label'] ); ?>" />
  267. </td>
  268. </tr>
  269. <tr valign="top">
  270. <th scope="row"><label><?php _e( 'Open links in', 'jetpack' ); ?></label></th>
  271. <td>
  272. <select name="open_links">
  273. <option<?php if ( $global['open_links'] == 'new' ) echo ' selected="selected"';?> value="new"><?php _e( 'New window', 'jetpack' ); ?></option>
  274. <option<?php if ( $global['open_links'] == 'same' ) echo ' selected="selected"';?> value="same"><?php _e( 'Same window', 'jetpack' ); ?></option>
  275. </select>
  276. </td>
  277. </tr>
  278. <tr valign="top">
  279. <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
  280. <td>
  281. <?php
  282. $br = false;
  283. foreach ( $shows as $show ) :
  284. if ( 'index' == $show ) {
  285. $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
  286. } else {
  287. $post_type_object = get_post_type_object( $show );
  288. $label = $post_type_object->labels->name;
  289. }
  290. ?>
  291. <?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>
  292. <?php $br = true; endforeach; ?>
  293. </td>
  294. </tr>
  295. <?php do_action( 'sharing_global_options' ); ?>
  296. </tbody>
  297. </table>
  298. <p class="submit">
  299. <input type="submit" name="submit" class="button-primary" value="<?php _e( 'Save Changes', 'jetpack' ); ?>" />
  300. </p>
  301. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
  302. </form>
  303. <div id="new-service" style="display: none">
  304. <form method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" id="new-service-form">
  305. <table class="form-table">
  306. <tbody>
  307. <tr valign="top">
  308. <th scope="row" width="100"><label><?php _e( 'Service name', 'jetpack' ); ?></label></th>
  309. <td>
  310. <input type="text" name="sharing_name" id="new_sharing_name" size="40" />
  311. </td>
  312. </tr>
  313. <tr valign="top">
  314. <th scope="row" width="100"><label><?php _e( 'Sharing URL', 'jetpack' ); ?></label></th>
  315. <td>
  316. <input type="text" name="sharing_url" id="new_sharing_url" size="40" />
  317. <p><?php _e( 'You can add the following variables to your service sharing URL:', 'jetpack' ); ?><br/>
  318. <code>%post_title%</code>, <code>%post_url%</code>, <code>%post_full_url%</code>, <code>%post_excerpt%</code>, <code>%post_tags%</code></p>
  319. </td>
  320. </tr>
  321. <tr valign="top">
  322. <th scope="row" width="100"><label><?php _e( 'Icon URL', 'jetpack' ); ?></label></th>
  323. <td>
  324. <input type="text" name="sharing_icon" id="new_sharing_icon" size="40" />
  325. <p><?php _e( 'Enter the URL of a 16x16px icon you want to use for this service.', 'jetpack' ); ?></p>
  326. </td>
  327. </tr>
  328. <tr valign="top" width="100">
  329. <th scope="row"></th>
  330. <td>
  331. <input type="submit" class="button-primary" value="<?php _e( 'Create Share Button', 'jetpack' ); ?>" />
  332. <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" />
  333. </td>
  334. </tr>
  335. <?php do_action( 'sharing_new_service_form' ); ?>
  336. </tbody>
  337. </table>
  338. <?php do_action( 'post_admin_screen_sharing' ) ?>
  339. <div class="inerror" style="display: none; margin-top: 15px">
  340. <p><?php _e( 'An error occurred creating your new sharing service - please check you gave valid details.', 'jetpack' ); ?></p>
  341. </div>
  342. <input type="hidden" name="action" value="sharing_new_service" />
  343. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-new_service' );?>" />
  344. </form>
  345. </div>
  346. <?php endif; ?>
  347. </div>
  348. <script type="text/javascript">
  349. var sharing_loading_icon = '<?php echo esc_js( admin_url( "/images/loading.gif" ) ); ?>';
  350. <?php if ( isset( $_GET['create_new_service'] ) && 'true' == $_GET['create_new_service'] ) : ?>
  351. jQuery(document).ready(function() {
  352. // Prefill new service box and then open it
  353. jQuery( '#new_sharing_name' ).val( '<?php echo esc_js( $_GET['name'] ); ?>' );
  354. jQuery( '#new_sharing_url' ).val( '<?php echo esc_js( $_GET['url'] ); ?>' );
  355. jQuery( '#new_sharing_icon' ).val( '<?php echo esc_js( $_GET['icon'] ); ?>' );
  356. jQuery( '#add-a-new-service' ).click();
  357. });
  358. <?php endif; ?>
  359. </script>
  360. <?php
  361. }
  362. }
  363. function sharing_admin_init() {
  364. global $sharing_admin;
  365. $sharing_admin = new Sharing_Admin();
  366. }
  367. add_action( 'init', 'sharing_admin_init' );