diff --git a/github-embed.php b/github-embed.php index e0ec02b..060084c 100644 --- a/github-embed.php +++ b/github-embed.php @@ -154,9 +154,22 @@ 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]; @@ -164,17 +177,17 @@ public function handle_oembed() { $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 ); } } @@ -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 ); @@ -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(); @@ -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 ); @@ -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(); @@ -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, @@ -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(); @@ -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 ); @@ -294,7 +313,9 @@ 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(); @@ -302,6 +323,7 @@ private function oembed_github_author( $owner ) { } require_once( 'github-api.php' ); +require_once (dirname(__FILE__)."/widget.php"); $github_api = new github_api(); $github_embed = new github_embed( $github_api ); diff --git a/widget.php b/widget.php new file mode 100644 index 0000000..d6bc28e --- /dev/null +++ b/widget.php @@ -0,0 +1,84 @@ + '','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 '