class.gogs-api-wpp-widget.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. class TimeAgo extends DateInterval {
  3. public static function set(DateInterval $obj) {
  4. return unserialize(sprintf(
  5. 'O:%d:"%s"%s',
  6. strlen(__CLASS__),
  7. __CLASS__,
  8. strstr(strstr(serialize($obj), '"'), ':')
  9. ));
  10. }
  11. public function __toString() {
  12. $str = array();
  13. if (($y = $this->format("%y")) > 0)
  14. $str["y"] = sprintf("%d %s", $y, _n("year", "years", $y, GAWPP_TEXT_DOMAIN));
  15. if (($m = $this->format("%m")) > 0)
  16. $str["m"] = sprintf("%d %s", $m, _n("month", "months", $m, GAWPP_TEXT_DOMAIN));
  17. if (($d = $this->format("%d")) > 0)
  18. $str["d"] = sprintf("%d %s", $d, _n("day", "days", $d, GAWPP_TEXT_DOMAIN));
  19. if (empty($str) && ($h = $this->format("%h")) > 0)
  20. $str["h"] = sprintf("%d %s", $h, _n("hour", "hours", $h, GAWPP_TEXT_DOMAIN));
  21. if ((empty($str) || isset($str["h"])) && ($i = $this->format("%i")) > 0)
  22. $str["i"] = sprintf("%d %s", $i, _n("minute", "minutes", $i, GAWPP_TEXT_DOMAIN));
  23. if ((empty($str) || isset($str["i"])) && ($s = $this->format("%s")) > 0)
  24. $str["s"] = sprintf("%d %s", $s, _n("second", "seconds", $s, GAWPP_TEXT_DOMAIN));
  25. return join($str);
  26. }
  27. }
  28. class Gogs_API_Plugin_Widget extends WP_Widget {
  29. const DEFAULT_AMOUNT = 10;
  30. private static $types = array(
  31. "repos" => "Show repositories",
  32. "user" => "Show user profile"
  33. );
  34. public function __construct() {
  35. parent::__construct(
  36. "gawpp_widget",
  37. esc_html__("Gogs API WPPlugin", GAWPP_TEXT_DOMAIN),
  38. array(
  39. "classname" => "gawpp_widget",
  40. "description" => __("Gogs GIT", GAWPP_TEXT_DOMAIN),
  41. )
  42. );
  43. }
  44. public function widget($args, $instance) {
  45. wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
  46. wp_enqueue_style($this->name, plugins_url("_inc/widget.css", __FILE__));
  47. try {
  48. $user = Gogs_API_Plugin::user();
  49. switch($this->instance_get($instance, "type")) {
  50. case "repos":
  51. $limit = (int)$this->instance_get($instance, "amount", self::DEFAULT_AMOUNT);
  52. $i = 0;
  53. $out = array();
  54. $now = new DateTime();
  55. foreach($user->repos()->load()->all() as $key => $repo) {
  56. $out[] = $this->partial("repo",
  57. $repo->html_url,
  58. $repo->name,
  59. $repo->description,
  60. $repo->stars_count,
  61. $repo->forks_count,
  62. !$repo->private ? " hidden" : "",
  63. TimeAgo::set(
  64. $now->diff(
  65. new DateTime(!empty($repo->updated_at) ? $repo->updated_at : $repo->created_ad)
  66. )
  67. ) . $repo->updated_at
  68. );
  69. }
  70. echo $this->view("repos", $args,
  71. $this->instance_get($instance, "title", $args["widget_name"]),
  72. join($out)
  73. );
  74. break;
  75. case "user":
  76. echo $user->username;
  77. break;
  78. }
  79. } catch (Exception $e) {
  80. echo $e->getMessage();
  81. }
  82. }
  83. private function partial(string $type, ...$args) {
  84. ob_start();
  85. include(GAWPP_DIR . "views/widget-" . $type . ".php");
  86. $page = ob_get_contents();
  87. ob_end_clean();
  88. $matches = array();
  89. $count = count($args);
  90. if ($matches_count = preg_match_all('/[\\%][a-z0-9][\\$a-z]{0,2}/s', $page, $matches)) {
  91. $identicals = array();
  92. foreach($matches[0] as $match)
  93. $identicals[$match] = true;
  94. $matches_count = count($identicals);
  95. if ($count == $matches_count)
  96. return sprintf($page, ...$args);
  97. }
  98. return __CLASS__ . "::template::partial error (" . $matches_count . "!=" . $count .")";
  99. }
  100. public function view(string $type, array $tmpl_args, ...$args) {
  101. ob_start();
  102. include(GAWPP_DIR . "views/widget-" . $type . ".php");
  103. $page = ob_get_contents();
  104. ob_end_clean();
  105. $tmpl_args_arr = array(
  106. isset($tmpl_args["before_widget"]) ? $tmpl_args["before_widget"] : sprintf('<section id="%s" class="widget %s">', $this->id, $this->id_base),
  107. isset($tmpl_args["before_title"]) ? $tmpl_args["before_title"] : '<h2 class="widget-title">',
  108. isset($tmpl_args["after_title"]) ? $tmpl_args["after_title"] : '</h2>',
  109. isset($tmpl_args["after_widget"]) ? $tmpl_args["after_widget"] : '</section>'
  110. );
  111. $matches = array();
  112. $count = count($tmpl_args_arr) + count($args);
  113. if ($matches_count = preg_match_all('/[\\%][a-z0-9][\\$a-z]{0,2}/s', $page, $matches)) {
  114. $identicals = array();
  115. foreach($matches[0] as $match)
  116. $identicals[$match] = true;
  117. $matches_count = count($identicals);
  118. if ($count == $matches_count)
  119. return sprintf($page, ...$tmpl_args_arr, ...$args);
  120. }
  121. return __CLASS__ . "::template error (" . $matches_count . "!=" . $count .")";
  122. }
  123. public function get_field_id($name) {
  124. return parent::get_field_id("widget_" . $name);
  125. }
  126. public function get_field_name($name) {
  127. return parent::get_field_name("widget_" . $name);
  128. }
  129. public function instance_get(array $instance, $name, $default = false) {
  130. return isset($instance["widget_" . $name]) ? $instance["widget_" . $name] : $default;
  131. }
  132. public function form($instance) {
  133. echo sprintf(
  134. '<p><label for="%1$s">%2$s</label><input class="widefat" type="text" id="%1$s" name="%3$s" placeholder="%4$s: %5$s"%6$s/></p>',
  135. esc_attr($this->get_field_id("title")),
  136. esc_attr__("Widget title:", GAWPP_TEXT_DOMAIN),
  137. esc_attr($this->get_field_name("title")),
  138. __("Default", GAWPP_TEXT_DOMAIN),
  139. esc_html($this->name),
  140. !empty($title = $this->instance_get($instance, "title", false)) ? (
  141. sprintf(' value="%s"', $title)
  142. ) : null
  143. );
  144. echo sprintf(
  145. '<p><label for="%1$s">%2$s</label><select class="widefat" id="%1$s" name="%3$s">%4$s</select></p>',
  146. esc_attr($this->get_field_id("type")),
  147. esc_attr__("Widget type:", GAWPP_TEXT_DOMAIN),
  148. esc_attr($this->get_field_name("type")),
  149. (function() use ($instance) {
  150. $opts = array();
  151. foreach (self::$types as $key => $type)
  152. $opts[] = sprintf('<option value="%s"%s>%s</option>',
  153. $key,
  154. !$this->instance_get($instance, "type") || $this->instance_get($instance, "type") == $key ? " selected" : "",
  155. $type
  156. );
  157. return join($opts);
  158. })()
  159. );
  160. echo sprintf(
  161. '<p><label for="%1$s">%2$s</label><input class="widefat" type="number" id="%1$s" name="%3$s" value="%4$s" /></p>',
  162. esc_attr($this->get_field_id("amount")),
  163. esc_attr__("Amount:", GAWPP_TEXT_DOMAIN),
  164. esc_attr($this->get_field_name("amount")),
  165. esc_html($this->instance_get($instance, "amount", 10))
  166. );
  167. }
  168. }