Guest User

simple-social-buttons.php

a guest
Feb 11th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.64 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Simple Social Buttons
  4. Plugin URI: http://www.rabinek.pl/simple-social-buttons-wordpress/
  5. Description: Insert social buttons into posts and archives: Facebook "Like it", Google Plus One "+1", Twitter share and Pinterest.
  6. Author: Paweł Rabinek
  7. Version: 1.7.7
  8. Author URI: http://www.rabinek.pl/
  9. */
  10.  
  11. /* Copyright 2011, PaweĹ‚ Rabinek (xradar) (email : [email protected])
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License, version 2, as
  15. published by the Free Software Foundation.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26.  
  27. /**
  28. * HACK: Converted to class, added buttons ordering, improve saving settings
  29. * @author Fabian Wolf
  30. * @link http://usability-idealist.de/
  31. * @since 1.3
  32. * @requires PHP 5
  33. */
  34.  
  35.  
  36. class SimpleSocialButtonsPR {
  37. var $pluginName = 'Simple Social Buttons';
  38. var $pluginVersion = '1.7.7';
  39. var $pluginPrefix = 'ssb_pr_';
  40. var $hideCustomMetaKey = '_ssb_hide';
  41.  
  42. // plugin default settings
  43. var $pluginDefaultSettings = array(
  44. 'googleplus' => '1',
  45. 'fblike' => '2',
  46. 'twitter' => '3',
  47. 'pinterest' => '0',
  48. 'beforepost' => '1',
  49. 'afterpost' => '0',
  50. 'beforepage' => '1',
  51. 'afterpage' => '0',
  52. 'beforearchive' => '0',
  53. 'afterarchive' => '0'
  54. );
  55.  
  56. // defined buttons
  57. var $arrKnownButtons = array('fblike', 'googleplus', 'twitter', 'pinterest');
  58.  
  59. // an array to store current settings, to avoid passing them between functions
  60. var $settings = array();
  61.  
  62.  
  63. /**
  64. * Constructor
  65. */
  66. function __construct() {
  67. register_activation_hook( __FILE__, array(&$this, 'plugin_install') );
  68. register_deactivation_hook( __FILE__, array(&$this, 'plugin_uninstall') );
  69.  
  70. /**
  71. * Action hooks
  72. */
  73. add_action( 'create_ssb', array(&$this, 'direct_display'), 10 , 1);
  74.  
  75. /**
  76. * basic init
  77. */
  78. add_action( 'init', array(&$this, 'plugin_init') );
  79.  
  80. // get settings
  81. $this->settings = $this->get_settings();
  82.  
  83. // social JS + CSS data
  84. add_action( 'wp_footer', array(&$this, 'include_social_js') );
  85. if(!isset($this->settings['override_css'])) {
  86. add_action( 'wp_head', array(&$this, 'include_css') );
  87. }
  88.  
  89. /**
  90. * Filter hooks
  91. */
  92. add_filter( 'the_content', array(&$this, 'insert_buttons') );
  93. add_filter( 'the_excerpt', array(&$this, 'insert_buttons') );
  94. }
  95.  
  96. function plugin_init() {
  97. load_plugin_textdomain( 'simplesocialbuttons', '', dirname( plugin_basename( __FILE__ ) ).'/lang' );
  98. }
  99.  
  100. /**
  101. * Both avoids time-wasting https calls AND provides better SSL-protection if the current server is accessed using HTTPS
  102. */
  103. public function get_current_http( $echo = true ) {
  104. $return = 'http' . (strtolower(@$_SERVER['HTTPS']) == 'on' ? 's' : '') . '://';
  105.  
  106. if($echo != false) {
  107. echo $return;
  108. return;
  109. }
  110.  
  111. return $return;
  112. }
  113.  
  114. function include_social_js($force_include = false) {
  115. $lang = get_bloginfo('language');
  116. $lang_g = strtolower(substr($lang, 0, 2));
  117. $lang_fb = str_replace('-', '_', $lang);
  118.  
  119. // most common problem with incorrect WPLANG in /wp-config.php
  120. if($lang_fb == "en" || empty($lang_fb)) {
  121. $lang_fb = "en_US";
  122. }
  123.  
  124. /**
  125. * Disable loading of social network JS if disabled for specific post type
  126. *
  127. * NOTE: Conditional tags seem to work only AFTER the page has loaded, thus the code has been added here instead of at the plugin init
  128. * @author Fabian Wolf
  129. * @link http://usability-idealist.de/
  130. * @date Di 20. Dez 17:50:01 CET 2011
  131. */
  132. if($this->where_to_insert() != false || $force_include == true) {
  133. ?>
  134.  
  135. <!-- Simple Social Buttons plugin -->
  136. <script type="text/javascript">
  137. //<![CDATA[
  138. <?php if ((int)$this->settings['googleplus'] != 0):?>
  139. // google plus
  140. window.___gcfg = {lang: '<?php echo $lang_g; ?>'};
  141. (function() {
  142. var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  143. po.src = 'https://apis.google.com/js/plusone.js';
  144. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  145. })();
  146. <?php endif;?>
  147. <?php if ((int)$this->settings['fblike'] != 0):?>
  148. // facebook
  149. (function(d, s, id) {
  150. var js, fjs = d.getElementsByTagName(s)[0];
  151. if (d.getElementById(id)) return;
  152. js = d.createElement(s); js.id = id;
  153. js.src = "//connect.facebook.net/<?php echo $lang_fb; ?>/all.js#xfbml=1";
  154. fjs.parentNode.insertBefore(js, fjs);
  155. }(document, 'script', 'facebook-jssdk'));
  156. <?php endif;?>
  157. <?php if ((int)$this->settings['twitter'] != 0):?>
  158. // twitter
  159. !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
  160. <?php endif;?>
  161. // ]]>
  162. </script>
  163. <?php if ((int)$this->settings['pinterest'] != 0):?>
  164. <script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>
  165. <?php endif;?>
  166. <!-- /End of Simple Social Buttons -->
  167.  
  168. <?php
  169. }
  170. }
  171.  
  172.  
  173. function include_css() {
  174. ?>
  175.  
  176. <!-- Simple Social Buttons style sheet -->
  177. <style type="text/css">
  178. div.simplesocialbuttons { height: 20px; margin: 10px auto 10px 0; text-align: left; clear: left; }
  179. div.simplesocialbutton { float: left; }
  180. div.ssb-button-googleplus { width: 100px; }
  181. div.ssb-button-fblike { width: 140px; line-height: 1; }
  182. div.ssb-button-twitter { width: 130px; }
  183. div.ssb-button-pinterest { width: 100px; }
  184. .fb-like iframe { max-width: none !important; }
  185. </style>
  186. <!-- End of Simple Social Buttons -->
  187.  
  188. <?php
  189. }
  190.  
  191. /**
  192. * Called when installing = activating the plugin
  193. */
  194. function plugin_install() {
  195. $defaultSettings = $this->check_old_settings();
  196.  
  197. /**
  198. * @see http://codex.wordpress.org/Function_Reference/add_option
  199. * @param string $name Name of the option to be added. Use underscores to separate words, and do not use uppercase - this is going to be placed into the database.
  200. * @param mixed $value Value for this option name. Limited to 2^32 bytes of data.
  201. * @param string $deprecated Deprecated in WordPress Version 2.3.
  202. * @param string $autoload Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no. Default: yes
  203. */
  204. add_option( $this->pluginPrefix . 'settings', $defaultSettings, '', 'yes' );
  205. add_option( $this->pluginPrefix . 'version', $this->pluginVersion, '', 'yes' ); // for backward-compatiblity checks
  206.  
  207. }
  208.  
  209. /**
  210. * Backward compatiblity for newer versions
  211. */
  212. function check_old_settings() {
  213. $return = $this->pluginDefaultSettings;
  214.  
  215. $oldSettings = get_option( $this->pluginPrefix . 'settings', array() );
  216.  
  217. if( !empty($oldSettings) && is_array($oldSettings) != false) {
  218. $return = wp_parse_args( $oldSettings, $this->pluginDefaultSettings );
  219. }
  220.  
  221. return $return;
  222. }
  223.  
  224. /**
  225. * Plugin unistall and database clean up
  226. */
  227. function plugin_uninstall() {
  228. if( !defined( 'ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') ) {
  229. exit();
  230. }
  231.  
  232. delete_option( $this->pluginPrefix . 'settings' );
  233. delete_option( $this->pluginPrefix . 'version' );
  234.  
  235. }
  236.  
  237.  
  238. /**
  239. * Get settings from database
  240. */
  241. public function get_settings() {
  242. $return = get_option($this->pluginPrefix . 'settings' );
  243. if(empty($return) != false) {
  244. $return = $this->pluginDefaultSettings;
  245. }
  246.  
  247. return $return;
  248. }
  249.  
  250. /**
  251. * Update settings
  252. */
  253. function update_settings( $newSettings = array() ) {
  254. $return = false;
  255.  
  256. // compile settings
  257. $currentSettings = $this->get_settings();
  258.  
  259. /**
  260. * Compile settings array
  261. * @see http://codex.wordpress.org/Function_Reference/wp_parse_args
  262. * @param mixed $args
  263. * @param mixed $defaults
  264. */
  265. $updatedSettings = wp_parse_args( $newSettings, $currentSettings );
  266.  
  267. if($currentSettings != $updatedSettings ) {
  268. $return = update_option( $this->pluginPrefix . 'settings', $newSettings );
  269. }
  270.  
  271. return $return;
  272. }
  273.  
  274. /**
  275. * Returns true on pages where buttons should be shown
  276. */
  277. function where_to_insert() {
  278. $return = false;
  279.  
  280. // display on single post?
  281. if(is_single() && ($this->settings['beforepost'] || $this->settings['afterpost']) && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) != 'true') {
  282. $return = true;
  283. }
  284.  
  285. // display on single page?
  286. if(is_page() && ($this->settings['beforepage'] || $this->settings['afterpage']) && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) != 'true') {
  287. $return = true;
  288. }
  289.  
  290. // display on frontpage?
  291. if((is_front_page() || is_home()) && $this->settings['showfront']) {
  292. $return = true;
  293. }
  294.  
  295. // display on category archive?
  296. if(is_category() && $this->settings['showcategory']) {
  297. $return = true;
  298. }
  299.  
  300. // display on date archive?
  301. if(is_date() && $this->settings['showarchive'])
  302. {
  303. $return = true;
  304. }
  305.  
  306. // display on tag archive?
  307. if(is_tag() && $this->settings['showtag']) {
  308. $return = true;
  309. }
  310. return $return;
  311. }
  312.  
  313. /**
  314. * Insert the buttons to the content
  315. */
  316. function insert_buttons($content) {
  317. // Insert or not?
  318. if(!$this->where_to_insert() ) {
  319. return $content;
  320. }
  321.  
  322. // creating order
  323. $order = array();
  324. foreach ($this->arrKnownButtons as $button_name) {
  325. $order[$button_name] = $this->settings[$button_name];
  326. }
  327. $ssb_buttonscode = $this->generate_buttons_code($order);
  328.  
  329. if(is_single()) {
  330. if($this->settings['beforepost']) {
  331. $content = $ssb_buttonscode.$content;
  332. }
  333. if($this->settings['afterpost']) {
  334. $content = $content.$ssb_buttonscode;
  335. }
  336. } else if(is_page()) {
  337. if($this->settings['beforepage']) {
  338. $content = $ssb_buttonscode.$content;
  339. }
  340. if($this->settings['afterpage']) {
  341. $content = $content.$ssb_buttonscode;
  342. }
  343. } else {
  344. if($this->settings['beforearchive']) {
  345. $content = $ssb_buttonscode.$content;
  346. }
  347. if($this->settings['afterarchive']) {
  348. $content = $content.$ssb_buttonscode;
  349. }
  350. }
  351.  
  352. return $content;
  353.  
  354. }
  355.  
  356. function direct_display($order = null)
  357. {
  358. // Return false if hide SSB for this page/post is disabled
  359. if (is_single() && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) == 'true') return false;
  360.  
  361. // Display buttons and scripts
  362. $buttons_code = $this->generate_buttons_code($order);
  363. echo $buttons_code;
  364. $this->include_social_js(true);
  365. }
  366.  
  367. /**
  368. * Generate buttons html code with specified order
  369. *
  370. * @param mixed $order - order of social buttons
  371. */
  372. function generate_buttons_code($order = null)
  373. {
  374. foreach ($this->arrKnownButtons as $button_name) {
  375. $defaultOrder[$button_name] = $this->pluginDefaultSettings[$button_name];
  376. }
  377.  
  378. $order = wp_parse_args($order, $defaultOrder);
  379.  
  380. // define empty buttons code to use
  381. $ssb_buttonscode = '';
  382.  
  383. // get post permalink and title
  384. $permalink = get_permalink();
  385. $title = get_the_title();
  386.  
  387. //Sorting the buttons
  388. $arrButtons = array();
  389. foreach($this->arrKnownButtons as $button_name) {
  390. if(!empty($order[$button_name]) && (int)$order[$button_name] != 0) {
  391. $arrButtons[$button_name] = $order[$button_name];
  392. }
  393. }
  394. @asort($arrButtons);
  395.  
  396. $arrButtonsCode = array();
  397. foreach($arrButtons as $button_name => $button_sort) {
  398. switch($button_name) {
  399. case 'googleplus':
  400. $arrButtonsCode[] = '<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><div class="g-plusone" data-size="medium" data-href="'.$permalink.'"></div></div>';
  401. break;
  402. case 'fblike':
  403. $arrButtonsCode[] = '<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><div class="fb-like" data-href="'.$permalink.'" data-send="false" data-layout="button_count" data-show-faces="false"></div></div>';
  404. break;
  405. case 'twitter':
  406. $arrButtonsCode[] = '<div class="simplesocialbutton ssb-button-twitter"><!-- Twitter--><a href="https://twitter.com/share" class="twitter-share-button" data-text="'.$title.'" data-url="'.$permalink.'" ' . ((!empty($this->settings['twitterusername'])) ? 'data-via="'.$this->settings['twitterusername'].'" ' : '') . 'rel="nofollow"></a></div>';
  407. break;
  408. case 'pinterest':
  409. $thumb_id = get_post_thumbnail_id($post->ID);
  410.  
  411. // Don't show 'Pin It' button, if post doesn't have thumbnail
  412. if (empty($thumb_id)) break;
  413.  
  414. // Getting thumbnail url
  415. $thumb = wp_get_attachment_image_src($thumb_id, 'thumbnail_size' );
  416. $thumb_src = (isset($thumb[0])) ? $thumb[0] : null;
  417. $thumb_alt = get_post_meta($thumb_id , '_wp_attachment_image_alt', true);
  418.  
  419. // if there isn't thumbnail alt, take a post title as a description
  420. $description = (!empty($thumb_alt)) ? $thumb_alt : $title ;
  421.  
  422. $arrButtonsCode[] = '<div class="simplesocialbutton ssb-button-pinterest"><!-- Pinterest--><a href="http://pinterest.com/pin/create/button/?url='.urlencode($permalink).'&media='.urlencode($thumb_src).'&description='.urlencode($description).'" data-pin-do="buttonPin" data-pin-config="beside" rel="nofollow"><img border="0" src="<a href="//www.pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg&description=Next%20stop%3A%20Pinterest" data-pin-do="buttonPin" data-pin-config="beside"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" title="Pin It" /></a></div>';
  423. break;
  424. }
  425. }
  426.  
  427. if(count($arrButtonsCode) > 0) {
  428. $ssb_buttonscode = '<div class="simplesocialbuttons">'."\n";
  429. $ssb_buttonscode .= implode("\n", $arrButtonsCode) . "\n";
  430. $ssb_buttonscode .= '</div>'."\n";
  431. }
  432.  
  433. return $ssb_buttonscode;
  434. }
  435. } // end class
  436.  
  437.  
  438. /**
  439. * Admin class
  440. *
  441. * Gets only initiated if this plugin is called inside the admin section ;)
  442. */
  443. class SimpleSocialButtonsPR_Admin extends SimpleSocialButtonsPR {
  444.  
  445. function __construct() {
  446. parent::__construct();
  447.  
  448. add_action('admin_menu', array(&$this, 'admin_actions') );
  449. add_action('add_meta_boxes', array(&$this, 'ssb_meta_box'));
  450. add_action('save_post', array(&$this, 'ssb_save_meta'), 10, 2);
  451.  
  452. add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
  453. }
  454.  
  455. public function admin_actions() {
  456. if (current_user_can('install_plugins'))
  457. add_options_page('Simple Social Buttons ', 'Simple Social Buttons ', "install_plugins", 'simple-social-buttons', array(&$this, 'admin_page') );
  458. }
  459.  
  460. public function admin_page() {
  461. global $wpdb;
  462.  
  463. include dirname( __FILE__ ).'/ssb-admin.php';
  464. }
  465.  
  466.  
  467.  
  468. public function plugin_action_links($links, $file) {
  469. static $this_plugin;
  470.  
  471. if (!$this_plugin) {
  472. $this_plugin = plugin_basename(__FILE__);
  473. }
  474.  
  475. if ($file == $this_plugin) {
  476. $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=simple-social-buttons">'.__('Settings', 'simplesocialbuttons').'</a>';
  477. array_unshift($links, $settings_link);
  478. }
  479.  
  480. return $links;
  481. }
  482.  
  483. /**
  484. * Register meta box to hide/show SSB plugin on single post or page
  485. */
  486. public function ssb_meta_box()
  487. {
  488. $postId = $_GET['post'];
  489. $postType = get_post_type($postId);
  490.  
  491. if ($postType != 'page' && $postType != 'post') return false;
  492.  
  493. $currentSsbHide = get_post_custom_values($this->hideCustomMetaKey, $postId);
  494.  
  495. if ($currentSsbHide[0] == 'true') {
  496. $checked = true;
  497. } else {
  498. $checked = false;
  499. }
  500.  
  501. // Rendering meta box
  502. if (!function_exists('add_meta_box')) include('includes/template.php');
  503. add_meta_box('ssb_meta_box', __('SSB Settings', 'simplesocialbuttons'), array(&$this, 'render_ssb_meta_box'), $postType, 'side', 'default', array('type' => $postType, 'checked' => $checked));
  504. }
  505.  
  506. /**
  507. * Showing custom meta field
  508. */
  509. public function render_ssb_meta_box($post, $metabox)
  510. {
  511. wp_nonce_field( plugin_basename( __FILE__ ), 'ssb_noncename' );
  512. ?>
  513.  
  514. <label for="<?php echo $this->hideCustomMetaKey;?>"><input type="checkbox" id="<?php echo $this->hideCustomMetaKey;?>" name="<?php echo $this->hideCustomMetaKey;?>" value="true" <?php if ($metabox['args']['checked']):?>checked="checked"<?php endif;?>/>&nbsp;<?php echo __('Hide Simple Social Buttons', 'simplesocialbuttons');?></label>
  515.  
  516. <?php
  517. }
  518.  
  519.  
  520. /**
  521. * Saving custom meta value
  522. */
  523. public function ssb_save_meta($post_id, $post)
  524. {
  525. $postId = (int)$post_id;
  526.  
  527. // Verify if this is an auto save routine.
  528. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  529. return;
  530.  
  531. // Verify this came from the our screen and with proper authorization
  532. if ( !wp_verify_nonce( $_POST['ssb_noncename'], plugin_basename( __FILE__ ) ) )
  533. return;
  534.  
  535. // Check permissions
  536. if ( 'page' == $_POST['post_type'] ) {
  537. if ( !current_user_can( 'edit_page', $post_id ) )
  538. return;
  539. } else {
  540. if ( !current_user_can( 'edit_post', $post_id ) )
  541. return;
  542. }
  543.  
  544. // Saving data
  545. $newValue = (isset($_POST[$this->hideCustomMetaKey])) ? $_POST[$this->hideCustomMetaKey] : 'false';
  546.  
  547. update_post_meta($postId, $this->hideCustomMetaKey, $newValue);
  548. }
  549.  
  550.  
  551. } // end SimpleSocialButtonsPR_Admin
  552.  
  553. if(is_admin() ) {
  554. $_ssb_pr = new SimpleSocialButtonsPR_Admin();
  555. } else {
  556. $_ssb_pr = new SimpleSocialButtonsPR();
  557. }
  558.  
  559. /**
  560. * Function to insert Simple Social Buttons directly in template.
  561. *
  562. * @param mixed $order - order of the buttons in array or string (parsed by wp_parse_args())
  563. *
  564. * @example 1 - use in template with default order
  565. * get_ssb();
  566. *
  567. * @example 2 - use in template with specified order
  568. * get_ssb('googleplus=3&fblike=2&twitter=1');
  569. *
  570. * @example 3 - hiding button by setting order to 0. By using code below googleplus button won't be displayed
  571. * get_ssb('googleplus=0&fblike=1&twitter=2');
  572. *
  573. *
  574. */
  575. function get_ssb($order = null)
  576. {
  577. do_action('create_ssb', $order);
  578. }
  579.  
  580. ?>
Advertisement
Add Comment
Please, Sign In to add comment