class.gogs-api-wpp.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require GAWPP_DIR . "gogs-php-api-client/src/gpac.php";
  3. class Gogs_API_Plugin {
  4. private static $versions_supported = array(
  5. array("name" => "v1", "path" => "/api/v1")
  6. );
  7. private static $client = false;
  8. private static $user = false;
  9. public static function init() {
  10. if (!self::$client && !empty(self::get_url()) && !empty(self::get_token())) {
  11. if(($resp = self::verify()) !== true)
  12. Gogs_API_Plugin_Admin::notify_setup_missing($resp);
  13. } else {
  14. Gogs_API_Plugin_Admin::notify_setup_missing();
  15. }
  16. }
  17. public static function user() {
  18. return self::$user;
  19. }
  20. public static function get_url() {
  21. return self::get_option("url", false);
  22. }
  23. public static function get_token() {
  24. return self::get_option("token", false);
  25. }
  26. public static function get_user() {
  27. return self::get_option("usr", false);
  28. }
  29. public static function get_api_path() {
  30. return self::get_option("apiv", self::$versions_supported[0]["path"]);
  31. }
  32. public static function get_credit(bool $force = false, bool $formatted = false) {
  33. if (($cred = self::get_option("credit", "yes")) && ($force || !$cred || $cred == 'yes')) {
  34. $data = get_plugin_data(GAWPP_DIR . "gogs-api-wpp.php");
  35. $credit = array(
  36. "plugin" => array(
  37. "name" => esc_html($data["Name"]),
  38. "uri" => esc_attr($data["PluginURI"]),
  39. "version" => esc_html($data["Version"]),
  40. "fyear" => 2017,
  41. "nyear" => (int)date("Y"),
  42. ), "author" => array(
  43. "name" => esc_html($data["AuthorName"]),
  44. "uri" => esc_attr($data["AuthorURI"])
  45. )
  46. );
  47. if ($formatted)
  48. return sprintf('<a href="%1$s" title="%2$s">%2$s</a> &copy; %3$s <a href="%4$s" title="%5$s">%5$s</a>',
  49. $credit["plugin"]["uri"],
  50. $credit["plugin"]["name"],
  51. (function() use ($credit) {
  52. if ($credit["plugin"]["fyear"] != $credit["plugin"]["nyear"])
  53. return sprintf("%d - %d", $credit["plugin"]["fyear"], $credit["plugin"]["nyear"]);
  54. return sprintf("%d", $credit["plugin"]["fyear"]);
  55. })(),
  56. $credit["author"]["uri"],
  57. $credit["author"]["name"]
  58. );
  59. return $credit;
  60. }
  61. return null;
  62. }
  63. public static function get_api_supported() {
  64. return self::$versions_supported;
  65. }
  66. public static function get_option(string $name, $default = false) {
  67. return !empty($ret = get_option("gawpp_" . $name, $default)) ? $ret : $default;
  68. }
  69. private static function set_option(string $name, string $value, string $autol = 'yes') {
  70. return update_option("gawpp_" . $name, $value, $autol);
  71. }
  72. private static function del_option(string $name) {
  73. return delete_option("gawpp_" . $name);
  74. }
  75. public static function verify() {
  76. self::$client = new Gogs\API\Client(
  77. self::get_url() . self::get_api_path(),
  78. self::get_token()
  79. );
  80. try {
  81. self::$user = self::$client->user(self::get_user())->load();
  82. } catch (Gogs\Lib\Curl\Exception\HTTPUnexpectedResponse $e) {
  83. return (string)$e->getMessage();
  84. }
  85. return true;
  86. }
  87. public static function on_activate() {
  88. register_setting("gawpp", "gawpp_url");
  89. register_setting("gawpp", "gawpp_token");
  90. register_setting("gawpp", "gawpp_usr");
  91. }
  92. public static function on_deactivate() {
  93. self::del_option("url");
  94. self::del_option("token");
  95. self::del_option("usr");
  96. unregister_setting("gawpp", "gawpp_url");
  97. unregister_setting("gawpp", "gawpp_token");
  98. unregister_setting("gawpp", "gawpp_usr");
  99. }
  100. }