Exemple #1
0
/**
 * List Commits shortcode.
 *
 * @param $atts
 * @return string
 */
function ghcommits_shortcode($atts)
{
    $a = shortcode_atts(array('username' => get_option('wpgithub_defaultuser', 'seinoxygen'), 'repository' => get_option('wpgithub_defaultrepo', 'wp-github'), 'limit' => '5'), $atts);
    // Init the cache system.
    $cache = new WpGithubCache();
    // Set custom timeout in seconds.
    $cache->timeout = get_option('wpgithub_cache_time', 600);
    $commits = $cache->get($a['username'] . '.' . $a['repository'] . '.commits.json');
    if ($commits == NULL) {
        $github = new Github($a['username'], $a['repository']);
        $commits = $github->get_commits();
        $cache->set($a['username'] . '.' . $a['repository'] . '.commits.json', $commits);
    }
    if (is_array($commits)) {
        $commits = array_slice($commits, 0, $a['limit']);
    }
    $html = '<ul class="wp-github wpg-commits">';
    foreach ($commits as $commit) {
        $html .= '<li><a target="_blank" href="' . $commit->html_url . '" title="' . $commit->commit->message . '">' . $commit->commit->message . '</a></li>';
    }
    $html .= '</ul>';
    return $html;
}
 function widget($args, $instance)
 {
     extract($args);
     $title = $this->get_title($instance);
     $username = $this->get_username($instance);
     $repository = $this->get_repository($instance);
     $commit_count = $this->get_commit_count($instance);
     echo $before_widget;
     echo $before_title . $title . $after_title;
     // Init the cache system.
     $cache = new Cache();
     // Set custom timeout in seconds.
     $cache->timeout = get_option('wpgithub_cache_time', 600);
     $commits = $cache->get($username . '.' . $repository . '.commits.json');
     if ($commits == null) {
         $github = new Github($username, $repository);
         $commits = $github->get_commits();
         $cache->set($username . '.' . $repository . '.commits.json', $commits);
     }
     if ($commits == null || count($commits) == 0) {
         echo $username . ' does not have any public commits.';
     } else {
         $commits = array_slice($commits, 0, $commit_count);
         echo '<ul>';
         foreach ($commits as $commit) {
             echo '<li><a href="' . $commit->html_url . '" title="' . $commit->commit->message . '">' . $commit->commit->message . '</a></li>';
         }
         echo '</ul>';
     }
     echo $after_widget;
 }