Advertisement
Guest User

Untitled

a guest
Apr 13th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. <?php
  2. /*
  3. Class Name: class.mashsharer.php
  4. Author: Rene Hermenau
  5. * version 1.0.6
  6. @scince 1.1.1
  7. Description: main class for mashsharer
  8. */
  9.  
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) exit;
  12. require_once 'class.debug.php';
  13. /*ln
  14. Sharer inspired by the Mashable one - get shares from the big four FB, Twitter, G+, LinkedIn
  15. */
  16.  
  17. class mashsharer {
  18. function __construct() {
  19.  
  20. #global $logme;
  21. #global $post;
  22. #global $wpdb;
  23. #$this->wpdb = $wpdb;
  24. //self::$instance = $this;
  25. add_shortcode('mashshare',array( $this, 'mashsharerShow'));
  26. add_filter('the_content', array( $this, 'mashsharer_filter_content'));
  27. add_filter('widget_text', 'do_shortcode');
  28. //add_shortcode('mashshare',array( $this, 'mashsharerGetTotal'));
  29. } // __construct
  30.  
  31.  
  32.  
  33. public function mashsharerGetTotal() {
  34. global $wpdb;
  35. global $post;
  36. global $url;
  37. $apikey = get_option('mashsharer_apikey');
  38.  
  39. if (!$url)
  40. $url = get_permalink($post->ID);
  41.  
  42. // We use curl instead file_get_contents for security purposes
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, "http://api.sharedcount.com/?url=" . rawurlencode($url) . "&apikey=" . $apikey);
  45. /* For debugging */
  46. //curl_setopt($ch, CURLOPT_URL, "http://api.sharedcount.com/?url=" . rawurlencode('http://www.google.de') . "&apikey=" . $apikey);
  47. /* For debugging */
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  49. $output = curl_exec($ch);
  50. curl_close($ch);
  51.  
  52. $counts = json_decode($output, true);
  53. //echo "This page has " . $counts["Twitter"] ." tweets, " . $counts["Facebook"]["like_count"] . " likes, and ". $counts["GooglePlusOne"] . "+1's";
  54. $total_count = $counts['Twitter'] + $counts['Facebook']['total_count'] + $counts['GooglePlusOne']; /* we can add a fake number here for smaller websites */
  55.  
  56. $sql = "select TOTAL_SHARES from ".MASHSHARER_TABLE." where URL='".$url."'";
  57. $results = $wpdb->get_results($sql);
  58.  
  59. if(count($results) == 0)
  60. {
  61. if ($total_count >= 0)
  62. {
  63. $sql = "INSERT INTO ".MASHSHARER_TABLE." (URL, TOTAL_SHARES, CHECK_TIMESTAMP) values('".$url."'," . $total_count . ",now())";
  64. }
  65. } else {
  66. $sql = "UPDATE ".MASHSHARER_TABLE." SET TOTAL_SHARES=".$total_count.", CHECK_TIMESTAMP=now() where URL='".$url."'";
  67.  
  68. }
  69.  
  70.  
  71. $wpdb->query($sql);
  72. /*echo "SQL ergebnisse" . count($results);
  73. echo "Anzahl shares" . $total_count;
  74. echo $sql;*/
  75. }
  76.  
  77. /* DEFINE ADDONS */
  78. public function mashload($place){
  79. global $addons;
  80. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  81. //if (class_exists('mashshare_networks') && is_plugin_active('mashshare-networks/mashshare-networks.php')) {
  82. if (class_exists('mashshare_networks')) {
  83. include_once(ABSPATH . 'wp-content/plugins/mashshare-networks/mashshare-networks.php');
  84. $networks = new mashshare_networks();
  85. $addons = $networks->mashshare_get_networks($place);
  86. return $addons;
  87. }
  88. return '';
  89. }
  90.  
  91. public function mashsharerShow($atts, $place) {
  92. global $wpdb;
  93. global $post;
  94. global $url;
  95. global $cacheexpire;
  96. global $addons;
  97. //global $place;
  98. $cacheexpire = get_option('mashsharer_cache_expire');
  99. //$cacheexpire = 1;
  100. $logme = new mashsharer_debug;
  101.  
  102. /* Load addons */
  103. $addons = $this->mashload($place);
  104.  
  105.  
  106. extract(shortcode_atts(array(
  107. 'cache' => '3600',
  108. 'url' => 0,
  109. 'f' => 1,
  110. 'bgcolor' => '',
  111. 'bordercolor' => '#ffffff',
  112. 'borderwidth' => '0',
  113. 'bordertype' => 'solid',
  114. 'fontcolor' => '#bf1e2d',
  115. 'fontsize' => '65',
  116. 'fontweight' => 'bold',
  117. 'padding' => '1',
  118. 'fontfamily' => 'Helvetica Neue,Helvetica,Arial,sans-serif'
  119. ), $atts));
  120.  
  121.  
  122. $title = addslashes(the_title_attribute('echo=0'));
  123.  
  124. if (!$url)
  125. $url = get_permalink($post->ID);
  126.  
  127. /* Create transient caching function */
  128. //$mashsharer_transient_key = "mashsharer_".$url;
  129. $mashsharer_transient_key = "mash_".md5($url);
  130. $results = get_transient($mashsharer_transient_key);
  131. //echo $mashsharer_transient_key;
  132. //delete_transient($mashsharer_transient_key);
  133. if ($results === false) {
  134. //echo "not cached";
  135. $logme->log_me('SQL data not cached');
  136. /* Get the share counts and write them to database when cache is expired */
  137. $this->mashsharerGetTotal();
  138. $sql = "select TOTAL_SHARES from " . MASHSHARER_TABLE . " where URL='" . $url . "'";
  139. $results = $wpdb->get_results($sql);
  140. set_transient($mashsharer_transient_key, $results, $cacheexpire);
  141. } else {
  142. //echo "its cached";
  143. $logme->log_me ('sql data cached');
  144. $results = get_transient($mashsharer_transient_key);
  145. //echo " cached results: ".$results;
  146. }
  147. //echo $mashsharer_transient_key;
  148.  
  149. if(empty($results))
  150. {
  151. $totalshares = 0;
  152. } else {
  153. $totalshares = $results[0]->TOTAL_SHARES;
  154. }
  155.  
  156. if ($totalshares > 1000000) { $totalshares = round($totalshares/1000000,1).'mln'; } elseif ($totalshares > 1000) { $totalshares = round($totalshares/1000,1).'k'; }
  157.  
  158. if ($f)
  159. $return = '
  160. <div style="line-height:10px;">&nbsp;</div>
  161. <div class="mashsharer-box">
  162. <div class="mashsharer-count"><font style="padding: ' . $padding . 'px ' . $padding . 'px ' . $padding . 'px ' . $padding . 'px; font-size: ' . $fontsize . 'px; font-weight:' . $fontweight . '; font-family: ' . $fontfamily . '; color: ' . $fontcolor . '; background-color: ' . $bgcolor . '; border: ' . $bordercolor . ' ' . $bordertype . ' ' . $borderwidth . 'px">' . $totalshares . '</font>
  163. <br><span class="mashsharer-sharetext">CONDIVISIONI</span>
  164. </div>
  165. <div class="mashsharer-buttons">
  166. <a class="facebook" onclick="javascript:mashFbSharrer(\'' . $url . '\',\'' . esc_html(htmlspecialchars(urlencode($title))) . '\', \'Facebook share popup\',\'http://goo.gl/dS52U\',520,350)" href="javascript:return(0);">Condividi su FB</a>
  167. <a class="twitter" onclick="javascript:mashTwSharrer(\'' . $url . '\', \'' . esc_html(htmlspecialchars(urlencode($title))) . '\', \'Twitter share popup\', \'http://goo.gl/dS52U\', 520, 350)"" href="javascript:return(0)">Condividi su TW</a>
  168. </div>'
  169. . $addons .
  170. '</div>
  171. <div style="clear:both;:"></div>
  172. ';
  173. return $return;
  174.  
  175. }
  176.  
  177. public function mashsharer_filter_content($content){
  178. global $atts;
  179. global $wp_current_filter;
  180. global $pages;
  181.  
  182.  
  183. $position = get_option('mashsharer_position');
  184. $option_posts = get_option('mashsharer_posts');
  185. $option_pages = get_option('mashsharer_pages');
  186. $pt = get_post_type();
  187. $frontpage = get_option('mashsharer_frontpage');
  188.  
  189.  
  190. if ($option_posts == '1')
  191. $option_posts = 'post';
  192. if ($option_pages == '1')
  193. $option_pages = 'page';
  194.  
  195. $post_types = array(
  196. $option_posts,
  197. $option_pages
  198. );
  199.  
  200. if( $post_types && !in_array($pt,$post_types)){
  201. return $content;
  202. }
  203.  
  204. /*if( $posts === $pt){
  205. return "posts".$content . "content is empty";
  206. }
  207. if( $pages === $pt){
  208. return "pages".$content;
  209. }*/
  210.  
  211.  
  212. if ( !is_singular() ){
  213. return $content;
  214. }
  215.  
  216. if( in_array('get_the_excerpt', $wp_current_filter) ) {
  217. return $content;
  218. }
  219.  
  220. if ($frontpage == 0 && is_front_page()== true){
  221. return $content;
  222. }
  223.  
  224.  
  225. if( is_feed() ) {
  226. return $content;
  227. }
  228.  
  229. switch($position){
  230. case 'manual':
  231. break;
  232.  
  233. case 'both':
  234. $content = $this->mashsharerShow($atts, '') . $content . $this->mashsharerShow($atts, "bottom");
  235. break;
  236.  
  237. case 'before':
  238. $content = $this->mashsharerShow($atts, '') . $content;
  239. break;
  240.  
  241. case 'after':
  242. $content .= $this->mashsharerShow($atts, '');
  243. break;
  244. }
  245.  
  246. return $content;
  247.  
  248. }
  249. }
  250. new mashsharer;
  251.  
  252. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement