Tag Archives: plugin

stackoverflow plugin for wordpress (stackoverflow+)

Recently I wanted to display my stackoverflow account details on my blog – I found the plugin stackoverflow+ which I liked from the screenshots. After installing, it didn’t work. The widget itself displayed, but all the fields it should receive from stackoverflow stayed empty. After viewing the sources I noticed that my hosting didn’t provide zlib, so I implemented and alternative implementation to use curl, in case zlib is not available. The file wsp-functions.php will now look like this:

<?php

function sanitizeObject($d)
{
    if (is_object($d)) {
        $d = get_object_vars($d);
    }
    if (is_array($d)) {
        return array_map(__FUNCTION__, $d);
    } else {
        return $d;
    }
}

/**
 * Uses the Stackoverflow API to get certain data
 *
 * @param string $ids
 * @param string $type
 * @return array|mixed
 */
function getUserData($ids, $type = '')
{
    // Define Local Variables
    $site = 'stackoverflow';
    $baseurl = 'http://api.stackexchange.com/2.2/users/';
    $apikey = 'sbI47iM9fyMqLqD0sA2T8A((';

    //Construct Url and Get Data off SO and use Sanitizer.
    if ($type == '') {
        $url = $baseurl . $ids . '?key=' . $apikey . '&site=' . $site . '&order=desc&sort=reputation&filter=default';
    } else if ($type == 'byQuestionids') {
        $url = 'http://api.stackexchange.com/2.2/questions/' . $ids . '?key=' . $apikey . '&site=' . $site . '&order=desc&sort=activity';
    } else {
        $url = $baseurl . $ids . '/' . $type . '?key=' . $apikey . '&site=' . $site . '&order=desc&sort=activity&filter=default';
    }
    if (function_exists('curl_version') === true)
        $results = json_decode(getJsonFromUrl_curl($url));
    else
        $results = json_decode(getJsonFromUrl_zlib($url));
    $results = sanitizeObject($results);
    return $results;
}

/**
 * Gets the json data from the given URL (curl implementation)
 *
 * @param string $url URL as string
 * @return string returns the json data as string
 */
function getJsonFromUrl_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

/**
 * Gets the json data from the given URL (zlib implementation)
 *
 * @param string $url URL as string
 * @return string returns the json data as string
 */
function getJsonFromUrl_zlib($url) {
    $context = array('http'=>array('header'=>"Accept-Encoding: gzip\r\n"));
    $url = "compress.zlib://" . $url;
    return file_get_contents($url, false, $context);
}

?>

What happens: In line 37 we do a check to see, if curl is available on the server. If yes, the function “getJsonFromUrl_curl($url)” is called, so the stackexchange API  is consumed using curl. If curl is not available, it will use “getJsonFromUrl_zlib($url)”, doing the same using zlib. This procedure requires either curl or zlib to be installed.

In the file “stackoverflow-plus.php” I implemented a check for zlib and curl on plugin activation. If neither is available, plugin activation will fail with a message to the user “Either zlib or curl needs to be installed on the server!”.

<?php
/*
Plugin Name: Stackoverflow+
Plugin URI: http://techstricks.com/wp-plugins/
Description: Stackoverflow Plus integreates your <a href="http://www.stackoverflow.com">Stackoverflow</a> profile with your word press website. Show your Stackoverflow profile, Questions, Answers, Reputation, Badges and much more through a easy to configure widget.
Version: 1.0
Author: Amyth Arora
Author URI: http://www.techstricks.com
*/

load_plugin_textdomain('wordpress-stackoverflow-plus');

if (!defined('WSP_VERSION')) {
    define('WSP_VERSION','1.0');
}
if (!defined('WSP_AUTHOR')) {
    define('WSP_AUTHOR','Amyth Arora');
}
// import the Widget Class
require_once( plugin_dir_path( __FILE__ ) . 'wsp-widget.php');

//Add the Stylesheet
add_action('wp_enqueue_scripts', 'add_wsp_style');
function add_wsp_style(){
    wp_register_style('wsp-style', plugins_url('css/stackoverflow-plus.css', __FILE__));
    wp_enqueue_style('wsp-style');
}

function stackoverflowplus_activate() {
    if (function_exists('curl_version') === false && function_exists('gzcompress') === false ) {
        echo 'Either zlib or curl needs to be installed on the server!';
        die;
    }
}
register_activation_hook( __FILE__, 'stackoverflowplus_activate' );
?>

I also turned off the accept rate from the widget settings, it doesn’t seem to be provided anymore, see this link. Please note that I only did the modifications for curl – the plugin itself was written by @mytharora.