From 1010a20e84f94acad4b301b065edb15f1ab5e515 Mon Sep 17 00:00:00 2001 From: artem Date: Thu, 3 Oct 2019 10:37:21 +0300 Subject: [PATCH 1/3] Add processing function --- github-embed.php | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/github-embed.php b/github-embed.php index e0ec02b..9352f80 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); + } + + + /** + * Функция обработки URL, вернет JSON или объект ссылки, если $echos будет true + * + * @param string url + * @param boolean вывод 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 ); From b14c8050a8e29791037bcf7dc74e62c78a273b64 Mon Sep 17 00:00:00 2001 From: artem Date: Thu, 3 Oct 2019 10:38:06 +0300 Subject: [PATCH 2/3] Add widget --- widget.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 widget.php diff --git a/widget.php b/widget.php new file mode 100644 index 0000000..9d20ab0 --- /dev/null +++ b/widget.php @@ -0,0 +1,85 @@ + '','url'=>''); + private $descrField = array('title' => 'Заголовок блока','url'=>'Ссылка на гитхаб'); + + public function __construct() {//конструктор + parent::__construct("gitHub", "Виджет вывода инфы из гитхаба", array("description" => "Выводит инфу из гитхаба на сайт")); + } + + /** + * Основной класс виджета + * + * @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; + } + + /** + * вывод в админке + * + * @param array $instance + */ + public function form($instance) { + $this->genField($instance); + } + + /** + * обновление настроек + * + * @param array $newInstance + * @param array $oldInstance + * @return array + */ + public function update($newInstance, $oldInstance) {// + return $this->genField($newInstance, true); + } + + /** + * Генератор полей для админки + * + * @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 '


'; + } + } + +} +?> \ No newline at end of file From 023aff30ca1573e6ca85bfcfe9abe935c760461a Mon Sep 17 00:00:00 2001 From: artem Date: Thu, 3 Oct 2019 10:55:56 +0300 Subject: [PATCH 3/3] translate on english for pull request --- github-embed.php | 4 ++-- widget.php | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/github-embed.php b/github-embed.php index 9352f80..060084c 100644 --- a/github-embed.php +++ b/github-embed.php @@ -159,10 +159,10 @@ public function handle_oembed() { /** - * Функция обработки URL, вернет JSON или объект ссылки, если $echos будет true + * Return or echo json info on url GitHub * * @param string url - * @param boolean вывод json или возврат объекта + * @param boolean return object or echo json. Default echo json * @return object version,size,template */ diff --git a/widget.php b/widget.php index 9d20ab0..d6bc28e 100644 --- a/widget.php +++ b/widget.php @@ -7,21 +7,20 @@ function wpb_load_widget() { /** - * Виджет бокового вида группы ВК + * Widget class * - * @package Minecraft\widgets */ class gitHub extends WP_Widget { private $fields = array('title' => '','url'=>''); - private $descrField = array('title' => 'Заголовок блока','url'=>'Ссылка на гитхаб'); + private $descrField = array('title' => 'Title widget','url'=>'Url to get info'); public function __construct() {//конструктор - parent::__construct("gitHub", "Виджет вывода инфы из гитхаба", array("description" => "Выводит инфу из гитхаба на сайт")); + parent::__construct("gitHub", "Echo info on url GitHub", array("description" => "Echo info on url GitHub")); } /** - * Основной класс виджета + * Work on frontend * * @param array $args * @param array $instance @@ -35,7 +34,7 @@ public function widget($args, $instance) { } /** - * вывод в админке + * Work on backend interface * * @param array $instance */ @@ -44,7 +43,7 @@ public function form($instance) { } /** - * обновление настроек + * Update Settings * * @param array $newInstance * @param array $oldInstance @@ -55,7 +54,7 @@ public function update($newInstance, $oldInstance) {// } /** - * Генератор полей для админки + * Gen fields in backend * * @param array $instance * @param array $update @@ -82,4 +81,4 @@ private function genField($instance, $update = false) { } } -?> \ No newline at end of file +?>