Die Idee gefiel mir und so erklärte ich mich bereit auf die Schnelle eine solche Version davon zu erstellen. Diese wurde von Guido getestet und noch etwas verbessert, unter anderem erhielt die Stand-Alone Version von ihm noch eine Abfrage auf den Transient-Cache der Werte über WordPress, da ansonsten die entsprechenden APIs nach einer gewissen Zeit dicht gemacht haben.
Lange Rede, gar kein Sinn, hier ist die PHP-Klasse der Stand-Alone Version vom Social Impact.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | <?php /** * Class Social Impact * Stand-Alone Version * * @author ppfeufer * @author muehlwitz */ if(!class_exists('Social_Impact_Standalone')) { class Social_Impact_Standalone { public $var_sFacebookFanpageId; public $var_sGoogleplusId; public $var_sTwitterId; public $var_sFeedburnerId; public $var_sUserAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0 - Social Impact'; /** * <[ Helper for cURL ]> * * @author ppfeufer * * @param string $var_sUrl * @return mixed */ private function _helper_curl($var_sUrl) { if(ini_get('allow_url_fopen')) { $cUrl_Data = file_get_contents($var_sUrl); } else { if(function_exists('curl_init')) { $cUrl_Channel = curl_init($var_sUrl); curl_setopt($cUrl_Channel, CURLOPT_RETURNTRANSFER, true); curl_setopt($cUrl_Channel, CURLOPT_HEADER, 0); curl_setopt($cUrl_Channel, CURLOPT_USERAGENT, $this->var_sUserAgent); curl_setopt($cUrl_Channel, CURLOPT_TIMEOUT, 10); $cUrl_Data = curl_exec($cUrl_Channel); if(curl_errno($cUrl_Channel) !== 0 || curl_getinfo($cUrl_Channel, CURLINFO_HTTP_CODE) !== 200) { $cUrl_Data === false; } // END if(curl_errno($ch) !== 0 || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) curl_close($cUrl_Channel); } // END if(function_exists('curl_init')) } // END if(ini_get('allow_url_fopen')) return $cUrl_Data; } // END private function helper_curl($var_sUrl = '') /** * <[ Helper ]> * * Getting Googleplus Count */ private function _get_googleplus_data() { $curl_GooglePlus = $this->_helper_curl('https://plus.google.com/_/socialgraph/lookup/incoming/?o=[null,null,%22' . $this->var_sGoogleplusId . '%22]&n=1'); try { preg_match('/]\s]\s,,(\d+)]\s,/s', $curl_GooglePlus, $matches); $var_iCircles = array_pop($matches); if(!empty($var_iCircles)) { $return_Googleplus['circles'] = (int) $var_iCircles; } // END if(!empty($var_iCircles)) $return_Googleplus['url'] = 'https://plus.google.com/' . $this->var_sGoogleplusId; return $return_Googleplus; } catch(Exception $e) {} return false; } // END private function _get_googleplus_data() /** * <[ Helper ]> * * Getting Facebook Fancount * * @return array */ private function _get_facebook_data() { $curl_Facebook = $this->_helper_curl('https://graph.facebook.com/' . $this->var_sFacebookFanpageId); try { $json_Facebook = json_decode($curl_Facebook); if($json_Facebook) { if(!empty($json_Facebook->likes)) { $return_Facebook['likes'] = $json_Facebook->likes; } // END if(!empty($json_Facebook->likes)) if(!empty($json_Facebook->link)) { $return_Facebook['url'] = $json_Facebook->link; } // END if(!empty($json_Facebook->link)) return $return_Facebook; } // ENDif($json_Facebook) } catch(Exception $e) {} return false; } // END private function _get_facebook_data() /** * <[ Helper ]> * * Getting Twitter Follower Count */ private function _get_twitter_data() { $curl_Twitter = $this->_helper_curl('http://twitter.com/users/show.xml?screen_name=' . $this->var_sTwitterId); try { $xml_Twitter = new SimpleXmlElement($curl_Twitter, LIBXML_NOCDATA); if($xml_Twitter) { if(!empty($xml_Twitter->followers_count)) { $return_Twitter['follower'] = (int) $xml_Twitter->followers_count; } // END if(!empty($xml->followers_count)) $return_Twitter['url'] = 'http://twitter.com/#!/' . $this->var_sTwitterId; return $return_Twitter; } // ENDif($xml_Twitter) } catch(Exception $e) {} return false; } // END private function _get_twitter_data() /** * <[ Helper ]> * * Getting Feedburner Reader Count */ private function _get_feedburner_data() { $curl_Feedburner = $this->_helper_curl('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $this->var_sFeedburnerId); try { $xml_Feedburner = new SimpleXmlElement($curl_Feedburner, LIBXML_NOCDATA); if($xml_Feedburner) { if(!empty($xml_Feedburner->feed->entry['circulation'])) { $return_Feedburner['reader'] = (int) $xml_Feedburner->feed->entry['circulation']; } // END if(!empty($xml->feed->entry['circulation'])) $return_Feedburner['url'] = 'http://feeds.feedburner.com/' . $this->var_sFeedburnerId; return $return_Feedburner; } // END if($xml_Feedburner) } catch(Exception $e) {} return false; } // END private function _get_feedburner_data() /** * returning the HTML */ function get_html() { $array_Feedburner = $this->_get_feedburner_data(); $array_Twitter = $this->_get_twitter_data(); $array_Facebook = $this->_get_facebook_data(); $array_Gogleplus = $this->_get_googleplus_data(); $var_sHtml = '<ul class="social-impact">'; $var_sHtml .= '<li class="feedburner-count"><a href="' . $array_Feedburner['url'] . '"><span>' . $array_Feedburner['reader'] . '</span></a></li>'; $var_sHtml .= '<li class="twitter-follower-count"><a href="' . $array_Twitter['url'] . '"><span>' . $array_Twitter['follower'] . '</span></a></li>'; $var_sHtml .= '<li class="facebook-fancount"><a href="' . $array_Facebook['url'] . '"><span>' . $array_Facebook['likes'] . '</span></a></li>'; $var_sHtml .= '<li class="googleplus-circles"><a href="' . $array_Gogleplus['url'] . '"><span>' . $array_Gogleplus['circles'] . '</span></a></li>'; $var_sHtml .= '</ul>'; return $var_sHtml; } // END function get_html() /** * <[ Helper ]> * * Transients als Fallback, falls API keine Infos mehr liefern * * @author muehlwitz */ private function _failover($count, $id) { if($count == '' || $count == 0) { $count = get_transient('sic_failover_' . $id); if($count === false) { $count = ''; } // END if($count === false) } else { // Transient neu Setzen set_transient('sic_failover_' . $id, $count, 60 * 60 * 72); // 72h Cachetime } // END if($count == '' || $count == 0) return $count; } // END function _failover($count, $id) /** * Returning the data as Array * * @return array */ function get_data() { $array_Feedburner = $this->_get_feedburner_data(); $array_Twitter = $this->_get_twitter_data(); $array_Facebook = $this->_get_facebook_data(); $array_Googleplus = $this->_get_googleplus_data(); $array_SocialImpactData = array( 'feedburner' => array( 'count' => $this->_failover($array_Feedburner['reader'],'feedburner'), 'url' => $array_Feedburner['url'] ), 'twitter' => array( 'count' => $this->_failover($array_Twitter['follower'],'twitter'), 'url' => $array_Twitter['url'] ), 'facebook' => array( 'count' => $this->_failover($array_Facebook['likes'],'facebook'), 'url' => $array_Facebook['url'] ), 'googleplus' => array( 'count' => $this->_failover($array_Googleplus['circles'],'googleplus'), 'url' => $array_Googleplus['url'] ) ); return $array_SocialImpactData; } // END function get_data() } // END class Social_Impact /** * Aufruf und Konfiguration der Klasse * * @param string $var_sFacebookFanpageId * @param string $var_sGooglePlusId * @param string $var_sTwitterId * @param string $var_sFeedburnerId */ function social_impact($var_sFacebookFanpageId = NULL, $var_sGooglePlusId = NULL, $var_sTwitterId = NULL, $var_sFeedburnerId = NULL, $var_sReturnHtml = true) { $socialImpact = new Social_Impact_Standalone(); $socialImpact->var_sFacebookFanpageId = $var_sFacebookFanpageId; $socialImpact->var_sGoogleplusId = $var_sGooglePlusId; $socialImpact->var_sTwitterId = $var_sTwitterId; $socialImpact->var_sFeedburnerId = $var_sFeedburnerId; if($var_sReturnHtml === true) { return $socialImpact->get_html(); } else { return $socialImpact->get_data(); } } // END function social_impact($var_sFacebookFanpageId = NULL, $var_sGooglePlusId = NULL, $var_sTwitterId = NULL, $var_sFeedburnerId = NULL) } // END if(!class_exists('Social_Impact_Standalone')) |
Einbindung in das WordPress-Theme
Die Einbindung in ein Theme ist gar nicht so schwer. Dazu wird einfach die PHP-Datei im Ordner des Themes abgelegt und in der functions.php eingebunden und die Klasse initialisiert.
1 2 3 4 5 6 7 8 | get_template_part('social-impact.class'); $var_sFacebookFanpageId = ''; $var_sGooglePlusId = ''; $var_sTwitterId = ''; $var_sFeedburnerId = ''; $var_sReturnHtml = false; // Return Data as HTML unordered list (true) or as an PHP-Array (false) $social_impact_data = social_impact($var_sFacebookFanpageId, $var_sGooglePlusId, $var_sTwitterId, $var_sFeedburnerId, $var_sReturnHtml); |
Nun kann das Array – $social_impact_data – überall im Theme verwendet werden. Dieses hat folgenden Aufbau
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Array ( [feedburner] => Array ( [count] => [url] => ) [twitter] => Array ( [count] => [url] => ) [facebook] => Array ( [count] => [url] => ) [googleplus] => Array ( [count] => [url] => ) ) |
Natürlich kann die Ausgabe auch direkt erfolgen, ohne vorher alles in einem Array unterzubringen. Hierfür muss einfach die letzte Option der aufrufenden Funktion den Wert true haben. In diesem Falle sollte diese Funktion allerdings nicht in der functions.php aufgerufen werden, sondern an der Stelle im Theme, an der man diese auch verwenden möchte.
Nun steht es jedem frei, dies nach eigenen Belieben zu formatieren und zu stylen. Per CSS ist das ja eigentlich kein Problem mehr, oder?
Danksagung
Ich möchte mich an dieser Stelle beim Guido Mühlwitz bedanken. Von ihm stammte letztlich die Idee das Plugin auch als Stand-Alone nutzen zu können und auch die Funktion für den Transient-Cache.
Update
23. August 2012
Kleinen Bug im Failover behoben. Danke an Guido :-)
Erkennung der Google+ Kreise überarbeitet und an die neuen Rückgabewerte von Google angepasst.
Geänderter Quelltext veröffentlicht.

Eine schöne Sache. Gibt es das Widget auch für 2er-Versionen?
Ich empfehle da eher ganz dringend das WordPress zu aktualisieren.
Ich versteh garnicht, wie man auf so alten CMS Versionen noch arbeiten kann. Und dann wundern, wenn Hacker den Webspace übernehmen…