class.gogs-api-wpp-admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. class Gogs_API_Plugin_Admin {
  3. private static $init = false;
  4. public static function init() {
  5. if (self::$init)
  6. return;
  7. self::init_hooks();
  8. }
  9. private static function init_hooks() {
  10. self::$init = true;
  11. add_action("admin_init", array(__CLASS__, "admin_init"), 5);
  12. add_action("admin_menu", array(__CLASS__, "admin_menu"), 5);
  13. }
  14. public static function admin_init() {
  15. load_plugin_textdomain(GAWPP_TEXT_DOMAIN);
  16. register_setting("gawpp", "gawpp_url", function(string $url) {
  17. $rurl = esc_url_raw($url);
  18. if (empty($rurl))
  19. add_settings_error(
  20. "gawpp_messages",
  21. "gawpp_message",
  22. esc_html(
  23. sprintf(__("Url %s is invalid", GAWPP_TEXT_DOMAIN),
  24. $url
  25. )
  26. ),
  27. "error"
  28. );
  29. if (($pos = strrpos($rurl, "/api/v")))
  30. $rurl = substr($rurl, 0, $pos);
  31. return empty($rurl = rtrim($rurl, "/")) ? false : $rurl;
  32. });
  33. register_setting("gawpp", "gawpp_token", function(string $token) {
  34. if(preg_match("/^[0-9a-f]{40}$/i", $token))
  35. return $token;
  36. add_settings_error(
  37. "gawpp_messages",
  38. "gawpp_message",
  39. esc_html(
  40. sprintf(__("The token \"%s\" is not a valid sha1-hash", GAWPP_TEXT_DOMAIN), $token)
  41. ),
  42. "error"
  43. );
  44. return false;
  45. });
  46. register_setting("gawpp", "gawpp_apiv", function(string $apiv) {
  47. $apiv = explode(":", $apiv);
  48. if (count($apiv) == 2 && is_numeric($apiv[0])) {
  49. $idx = intval($apiv[0]);
  50. if ($idx >= 0) {
  51. $opts = Gogs_API_Plugin::get_api_supported();
  52. if (isset($opts[$idx]) && $opts[$idx]["path"] == $apiv[1])
  53. return $opts[$idx]["path"];
  54. }
  55. }
  56. add_settings_error(
  57. "gawpp_messages",
  58. "gawpp_message",
  59. esc_html(
  60. sprintf(__("The api version selected \"%s\" is not supported", GAWPP_TEXT_DOMAIN), join(":", $apiv))
  61. ),
  62. "error"
  63. );
  64. return false;
  65. });
  66. register_setting("gawpp", "gawpp_usr", function(string $user){
  67. if (empty(trim($user)))
  68. return false;
  69. if (preg_match("/[a-z0-9\\_\\-i\\.]+$/i", $user))
  70. return $user;
  71. add_settings_error(
  72. "gawpp_messages",
  73. "gawpp_message",
  74. esc_html(
  75. __("The username must be valid alpha or numeric or dash(-_) or dot characters", GAWPP_TEXT_DOMAIN)
  76. ),
  77. "error"
  78. );
  79. });
  80. register_setting("gawpp", "gawpp_credit", function(string $opt){
  81. if (empty(trim($opt)))
  82. return "yes";
  83. if (in_array($opt, array("yes", "no")))
  84. return $opt;
  85. return "yes";
  86. });
  87. add_settings_section(
  88. "gawpp_config",
  89. __("Required", GAWPP_TEXT_DOMAIN),
  90. function(array $args) {
  91. echo sprintf(
  92. '<p id="%s">%s</p>',
  93. esc_attr($args["id"]),
  94. esc_html__(
  95. "Information about your Gogs-site that Gogs API WPPlugin need to know to make queries. It's recommended to fill in these fields first, to ensure you have entered the required fields correctly before filling out the optional fields.",
  96. GAWPP_TEXT_DOMAIN
  97. )
  98. );
  99. },
  100. "gawpp"
  101. );
  102. $field = function(array $args) {
  103. $options = get_option("gawpp_" . $args["id"]);
  104. echo sprintf(
  105. '<input type="text" id="%1$s" name="gawpp_%1$s" class="%2$s"%3$s%4$s%5$s/>',
  106. esc_attr($args["label_for"]),
  107. esc_attr($args["class"]),
  108. (isset($args["placeholder"]) && !empty($args["placeholder"]) ?
  109. sprintf(' placeholder="%s"', esc_attr($args["placeholder"])) : null
  110. ),
  111. (isset($args["data"]) && !empty($args["data"]) ?
  112. sprintf(' data-custom="%s"', esc_attr($args["data"])) : null
  113. ),
  114. (($value = Gogs_API_Plugin::get_option($args["label_for"])) != false ?
  115. sprintf(' value="%s"', $value) : null
  116. )
  117. );
  118. };
  119. add_settings_field(
  120. "gawpp_url",
  121. __("Gogs URL", GAWPP_TEXT_DOMAIN),
  122. $field,
  123. "gawpp",
  124. "gawpp_config",
  125. array(
  126. 'label_for' => "url",
  127. 'class' => "regular-text code",
  128. 'data' => "",
  129. 'placeholder' => 'https://git.domain.tld'
  130. ),
  131. trim(GAWPP_GOGS_LOGO)
  132. );
  133. add_settings_field(
  134. "gawpp_token",
  135. __("Application token", GAWPP_TEXT_DOMAIN),
  136. $field,
  137. "gawpp",
  138. "gawpp_config",
  139. array(
  140. 'label_for' => "token",
  141. 'class' => "regular-text code",
  142. 'data' => "",
  143. 'placeholder' => 'e.g c7397d798d241d6462ac992680fa6648d83132ab'
  144. )
  145. );
  146. add_settings_field(
  147. "gawpp_apiv",
  148. __("API version", GAWPP_TEXT_DOMAIN),
  149. function (array $args) {
  150. $selected = Gogs_API_Plugin::get_api_path();
  151. echo sprintf('<select id="%1$s" name="gawpp_%1$s" class="%2$s">%3$s</select>',
  152. $args["label_for"],
  153. $args["class"],
  154. (function() use ($args, $selected) {
  155. $opts = array();
  156. foreach(Gogs_API_Plugin::get_api_supported() as $key => $opt)
  157. $opts[] = sprintf('<option value="%d:%s"%s>API %s: %s</value>',
  158. (int)$key,
  159. esc_attr($opt["path"]),
  160. $selected == $opt["path"] ? " selected" : "",
  161. esc_html($opt["name"]),
  162. esc_html($opt["path"])
  163. );
  164. return join($opts);
  165. })()
  166. );
  167. },
  168. "gawpp",
  169. "gawpp_config",
  170. array(
  171. 'label_for' => "apiv",
  172. 'class' => "regular-text code"
  173. )
  174. );
  175. add_settings_section(
  176. "gawpp_config_optional",
  177. __("Optional", GAWPP_TEXT_DOMAIN),
  178. function(array $args) {
  179. echo sprintf(
  180. '<p id="%s">%s</p>',
  181. esc_attr($args["id"]),
  182. esc_html__(
  183. "Information you can add to change the experience and/or behaviour, but you should first fill out (and save!) the required fields to ensure you have entered them correctly. Otherwise it can be tricky to find out which field you have entered incorrectly, e.g if the username is unknown for your Gogs-site, Gogs API will return \"Not found\" - which is the same status code if the API URL is wrong.",
  184. GAWPP_TEXT_DOMAIN
  185. )
  186. );
  187. },
  188. "gawpp"
  189. );
  190. add_settings_field(
  191. "gawpp_usr",
  192. __("Gogs user", GAWPP_TEXT_DOMAIN),
  193. $field,
  194. "gawpp",
  195. "gawpp_config_optional",
  196. array(
  197. 'label_for' => "usr",
  198. 'class' => "regular-text code",
  199. 'data' => "",
  200. 'placeholder' => 'optional'
  201. )
  202. );
  203. add_settings_field(
  204. "gawpp_credit",
  205. __("Allow credit", GAWPP_TEXT_DOMAIN),
  206. function(array $args) {
  207. $selected = Gogs_API_Plugin::get_option($args["label_for"]);
  208. $inputs = array();
  209. foreach(array("yes", "no") as $opt)
  210. $inputs[] = sprintf('<input type="radio" name="gawpp_%1$s" id="%2$s_%1$s" value="%2$s"%3$s%4$s/><label for="%2$s_%1$s">%5$s</label>',
  211. $args["label_for"],
  212. $opt,
  213. !empty($args["class"]) ? sprintf(' class="%s"', esc_attr($args["class"])) : null,
  214. (function() use ($selected, $opt) {
  215. if (empty($selected) && $opt == "yes")
  216. return ' checked';
  217. else if ($selected == $opt)
  218. return ' checked';
  219. return null;
  220. })(),
  221. ucfirst(esc_html__($opt, GAWPP_TEXT_DOMAIN)) . ", " . (
  222. $opt == "yes" ?
  223. __("of course I'll give you credit!", GAWPP_TEXT_DOMAIN) :
  224. sprintf('%s <a href="https://www.paypal.me/joachimmgok/86nok">%s</a>!',
  225. __("but I'll consider to", GAWPP_TEXT_DOMAIN),
  226. __("buy you a beer", GAWP_TEXT_DOMAIN)
  227. )
  228. )
  229. );
  230. echo '<p>' . join("<br />", $inputs) . "</p><p class=\"description\">Similar to the credit shown below.</p>";
  231. },
  232. "gawpp",
  233. "gawpp_config_optional",
  234. array(
  235. 'label_for' => "credit",
  236. 'class' => "regular-text code",
  237. )
  238. );
  239. }
  240. public static function admin_menu() {
  241. $hook = add_menu_page(
  242. "Gogs API",
  243. "Gogs API options",
  244. "manage_options",
  245. "gawpp",
  246. array(__CLASS__, "display_page"),
  247. GAWPP_GOGS_LOGO
  248. );
  249. add_action("load-$hook", array(__CLASS__, "display_help"));
  250. }
  251. public static function get_page_url($action = 'config') {
  252. $args = array('page' => 'gawpp');
  253. return add_query_arg( $args, admin_url('admin.php'));
  254. }
  255. public static function display_page() {
  256. include GAWPP_DIR . "views/config.php";
  257. }
  258. public static function display_help() {
  259. $screen = get_current_screen();
  260. if (current_user_can("manage_options")) {
  261. $screen->add_help_tab(
  262. array(
  263. "id" => "overview",
  264. "title" => __("Overview", GAWPP_TEXT_DOMAIN),
  265. "content" =>
  266. "<p><strong>" . esc_html(__("Gogs API WPP", GAWPP_TEXT_DOMAIN)) . "</strong></p>" .
  267. "<p>Gogs API Wordpress Plugin lets you query you Gogs installation and show this data in you blog.</p>"
  268. )
  269. );
  270. $screen->add_help_tab(
  271. array(
  272. "id" => "setup",
  273. "title" => __("Setup", GAWPP_TEXT_DOMAIN),
  274. "content" =>
  275. "<p><strong>" . esc_html(__("Gogs API WPP Setup", GAWPP_TEXT_DOMAIN)) . "</strong></p>" . <<<EOF
  276. <p>To setup Gogs API WPP you must have an application token generated from your Gogs-account.</p>
  277. <ol>
  278. <li>Generate application token from you Gogs settings and store it securely. You will only be able to read it once!</li>
  279. <li>Ensure your Gogs installation supports Gogs API v1.</li>
  280. <li>Enter the URL for you Gogs page, without the /api/v1/ postfix - we'll add it for you, and your application token.</li>
  281. <li>If you want to list things for another user than the owner of the application token, specify the username, but you can leave it blank to operate on the authorized user. <strong>Please note</strong> that only public content (repos, organization/team memberships etc) will be listet on a specified user. <strong>This also if applies to your user, if you specify the username</strong>.</li>
  282. </ol>
  283. EOF
  284. )
  285. );
  286. }
  287. }
  288. public static function notify_setup_missing(string $resp = null) {
  289. add_action("admin_notices", function() use ($resp) {
  290. $screen = get_current_screen();
  291. if ($screen->id == "toplevel_page_gawpp")
  292. return;
  293. echo sprintf('<div class="notice notice-warning"><p class="wp-clearfix"><strong>%1$s</strong> %2$s%5$s <a href="%3$s" title="%4$s" class="button button-primary alignright">%4$s</a></p></div>',
  294. __("Almost done!", GAWPP_TEXT_DOMAIN),
  295. __("Gogs API Wordpress Plugin needs additional setup to work.", GAWPP_TEXT_DOMAIN),
  296. self::get_page_url("config"),
  297. __("Complete setup", GAWPP_TEXT_DOMAIN),
  298. !empty($resp) ? (
  299. " <em>" . esc_html($resp) . "</em>"
  300. ) : null
  301. );
  302. });
  303. }
  304. }
  305. ?>