Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions github-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,40 @@ public function handle_oembed() {
die( 'Octocat is lost, and afraid' );
}

$this->mechanicUrl($url);
}


/**
* Return or echo json info on url GitHub
*
* @param string url
* @param boolean return object or echo json. Default echo json
* @return object version,size,template
*/

public function mechanicUrl($url,$echos=false){
// Issues / Milestones
if ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
$this->oembed_github_repo_contributors( $matches[1], $matches[2] );
return $this->oembed_github_repo_contributors( $matches[1], $matches[2], $echos );
} elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
if ( preg_match( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
$milestone = $milestones[1];
} else {
$milestone = null;
}
if ( $milestone ) {
$this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
return $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone, $echos );
}
} elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) {
// New style milestone URL, e.g. https://github.com/example/example/milestone/1.
$this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] );
return $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3], $echos );
} elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
// Repository.
$this->oembed_github_repo( $matches[1], $matches[2] );
return $this->oembed_github_repo( $matches[1], $matches[2], $echos );
} elseif ( preg_match( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
// User.
$this->oembed_github_author( $matches[1] );
return $this->oembed_github_author( $matches[1], $echos );
}
}

Expand All @@ -201,7 +214,7 @@ private function process_template( $template, $data ) {
* @param string $owner The owner of the repository
* @param string $repository The repository name
*/
private function oembed_github_repo_contributors( $owner, $repository ) {
private function oembed_github_repo_contributors( $owner, $repository,$ret=false ) {
$data = [];
$data['repo'] = $this->api->get_repo( $owner, $repository );
$data['contributors'] = $this->api->get_repo_contributors( $owner, $repository );
Expand All @@ -216,7 +229,9 @@ private function oembed_github_repo_contributors( $owner, $repository ) {
$response->title = $data['repo']->description;
$response->html = $this->process_template(
'repository_contributors.php', $data );

if ($ret){
return $response;
}
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
Expand All @@ -226,7 +241,7 @@ private function oembed_github_repo_contributors( $owner, $repository ) {
* Retrieve the summary information for a repo's milestone, and
* output it as an oembed response
*/
private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) {
private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone, $ret=false ) {
$data = [];
$data['repo'] = $this->api->get_repo( $owner, $repository );
$data['summary'] = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone );
Expand All @@ -240,7 +255,9 @@ private function oembed_github_repo_milestone_summary( $owner, $repository, $mil
$response->title = $data['repo']->description;
$response->html = $this->process_template(
'repository_milestone_summary.php', $data );

if ($ret){
return $response;
}
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
Expand All @@ -251,7 +268,7 @@ private function oembed_github_repo_milestone_summary( $owner, $repository, $mil
* Retrieve the information from github for a repo, and
* output it as an oembed response
*/
private function oembed_github_repo( $owner, $repository ) {
private function oembed_github_repo( $owner, $repository, $ret=false ) {
$data = [
'owner_slug' => $owner,
'repo_slug' => $repository,
Expand All @@ -269,7 +286,9 @@ private function oembed_github_repo( $owner, $repository ) {
$response->html = $this->process_template(
'repository.php', $data );


if ($ret){
return $response;
}
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
Expand All @@ -279,7 +298,7 @@ private function oembed_github_repo( $owner, $repository ) {
* Retrieve the information from github for an author, and output
* it as an oembed response
*/
private function oembed_github_author( $owner ) {
private function oembed_github_author( $owner, $ret=false ) {
$data = [];
$data["owner"] = $owner;
$data["owner_info"] = $this->api->get_user( $owner );
Expand All @@ -294,14 +313,17 @@ private function oembed_github_author( $owner ) {
$response->title = $data['owner_info']->name;
$response->html = $this->process_template(
'author.php', $data );

if ($ret){
return $response;
}
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
}
}

require_once( 'github-api.php' );
require_once (dirname(__FILE__)."/widget.php");

$github_api = new github_api();
$github_embed = new github_embed( $github_api );
84 changes: 84 additions & 0 deletions widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// Register and load the widget
function wpb_load_widget() {
register_widget( 'gitHub' );
}
add_action( 'widgets_init', 'wpb_load_widget' );


/**
* Widget class
*
*/
class gitHub extends WP_Widget {

private $fields = array('title' => '','url'=>'');
private $descrField = array('title' => 'Title widget','url'=>'Url to get info');

public function __construct() {//конструктор
parent::__construct("gitHub", "Echo info on url GitHub", array("description" => "Echo info on url GitHub"));
}

/**
* Work on frontend
*
* @param array $args
* @param array $instance
*/
public function widget($args, $instance) {
global $github_embed;
//$instance = defaultValue($instance, $this->fields);
echo $instance['title'];
//echo $instance['url'];
echo $github_embed->mechanicUrl($instance['url'],true)->html;
}

/**
* Work on backend interface
*
* @param array $instance
*/
public function form($instance) {
$this->genField($instance);
}

/**
* Update Settings
*
* @param array $newInstance
* @param array $oldInstance
* @return array
*/
public function update($newInstance, $oldInstance) {//
return $this->genField($newInstance, true);
}

/**
* Gen fields in backend
*
* @param array $instance
* @param array $update
* @return boolean
*/
private function genField($instance, $update = false) {
if ($update) {
$ret = array();
foreach ($this->fields as $k => $v) {
$ret[$k] = $instance[$k];
}
return $ret;
}

foreach ($this->fields as $k => $v) {
$tableId = $this->get_field_id($k);
$tableName = $this->get_field_name($k);
if (isset($instance[$k]))
$value = $instance[$k];
else
$value = '';
echo '<p><label>' . $this->descrField[$k] . '</label><br><input id="' . $tableId . '" type="text" name="' . $tableName . '" value="' . $value . '" placeholder="' . $v . '" class="widefat"></p>';
}
}

}
?>