'. $msg. ''; } static function format_since($since) { preg_match('/^([0-9]+)\s+(.*)$/', $since, $m); $num = $m[1]; $unit = $m[2]; if($unit=='days') { $unit = __('days', 'linuxfoo-gitlab'); } elseif($unit=='months') { $unit = __('months', 'linuxfoo-gitlab'); } elseif($unit=='years') { $unit = __('years', 'linuxfoo-gitlab'); } /* translators: %d: count, %s unit of time interval*/ return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit); } static function commits_list($atts, $project_url) { $since = null; if($atts['commits']=='none') { return ''; } elseif(!is_null($atts['commits']) && $atts['commits']!='all') { return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitlab')); } elseif(!is_null($atts['since'])) { if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) { return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitlab')); } else { $since = date(DATE_ISO8601, strtotime('-'.$atts['since'])); } } $out = ''; $commits_url = $project_url.'/repository/commits?'; if(!is_null($since)) { $commits_url .= '&since='.$since; } if(!is_null($atts['ref_name'])) { $commits_url .= '&ref_name='.$atts['ref_name']; $branch = $atts['ref_name']; } elseif(!is_null($atts['default_branch'])) { $commits_url .= '&ref_name='.$project->default_branch; $branch = $project->default_branch; } else { $branch = null; } $commits_json = file_get_contents($commits_url); if(is_null($commits_json)) { $out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab')); } else { $commits = json_decode($commits_json); if(is_null($commits)) { $out .= self::error(__('Commits information not readable.', 'linuxfoo-gitlab')); } else { $commits_count = count($commits); $commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']); $commits = array_slice($commits, 0, $commits_count); if($commits_count==0) { $out .= ''. __('No commits', 'linuxfoo-gitlab'). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($atts['since']) ? '' : ' '.('since '.$atts['since'].' ago')).'.'. ''; } else { $out .= ''. /* translators: %d: maximum number of commits displayed */ sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':'. ''. ''; } } } return $out; } static function releases_list($atts, $project_url) { $out = ''; if(is_null($atts['releases']) || $atts['releases']=='none') { $out .= ''; } elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) { $out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab')); } else { $releases_url = $project_url.'/releases?'; $releases_json = file_get_contents($releases_url); if(is_null($releases_json)) { $out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitlab')); } else { $releases = json_decode($releases_json); if(is_null($releases)) { $out .= self::error(__('Releases information not readable.', 'linuxfoo-gitlab')); } else { if(count($releases)==0) { $out .= '

'. __('This project has currently no releases.', 'linuxfoo-gitlab'). '

'; } elseif($atts['releases']=='latest') { $out .= '

'. __('Latest release', 'linuxfoo-gitlab').': '. ''.$releases[0]->name.''. '

'; } elseif($atts['releases']=='all') { $out .= '

'. __('Releases', 'linuxfoo-gitlab').': '. '

'. ''; } } } } return $out; } static function list_commits($atts, $content, $tag) { return self::show_project($atts, $content, $tag); } static function show_project($atts, $content, $tag) { global $post; $out = '
'; if( is_null($atts['url']) || is_null($atts['project_id']) ) { $out .= self::error(__('Required parameter missing.', 'linuxfoo-gitlab')); } elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { $out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab')); } elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) { $out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitlab')); } elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { $out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab')); } else { $project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id']; $project_json = file_get_contents($project_url); if(is_null($project_json)) { $out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitlab')); } else { $project = json_decode($project_json); if(is_null($project)) { $out .= self::error('Project information not readable.'); } else { $out .= ''. __('Gitlab project', 'linuxfoo-gitlab').' '. ''.$project->name.''. ''; $out .= self::commits_list($atts, $project_url); $out .= self::releases_list($atts, $project_url); } } } $out .= '
'; return $out; } } add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain'); add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' ); add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits'); add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project');