collecting medal for hitxy members
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

148 行
4.6KB

  1. <?php
  2. /*
  3. Plugin Name: Collecting Medal for membmers
  4. Plugin URI: https://biukop.com.au/
  5. Description: A backend for collecting memorial medal for HITxy Medal for 100 anuversary
  6. Text Domain: member
  7. Author: Patrick
  8. Twitter: @lawipac
  9. Author URI: https://lawipac.com/
  10. Version: 1.0.1
  11. License: GPL
  12. Copyright: All rights reserved.
  13. */
  14. namespace Member;
  15. //require_once(dirname(__FILE__) . '/autoload.php');
  16. require_once (ABSPATH . 'wp-includes/pluggable.php');
  17. class Member{
  18. private $token = "";
  19. private $nonce = "";
  20. public function __construct() {
  21. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  22. add_shortcode( 'mm_workspace', array($this, 'shortcode_workspace'));
  23. add_shortcode( 'mm_token', array($this, 'shortcode_token'));
  24. // hook add_rewrite_rules function into rewrite_rules_array
  25. add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
  26. // hook add_query_vars function into query_vars
  27. add_filter('query_vars', array($this,'add_query_vars'));
  28. //
  29. $this->ajax_hook('list_users');
  30. $this->ajax_hook('search_users');
  31. }
  32. private function ajax_hook($code, $admin_only = false)
  33. {
  34. add_action("wp_ajax_$code", array($this,"ajax_$code" ));
  35. if (!$admin_only) {
  36. add_action("wp_ajax_nopriv_$code", array($this,"ajax_$code"));
  37. }
  38. }
  39. public function shortcode_workspace($attrs) {
  40. if ($this->token != "" )
  41. return "";
  42. $str = file_get_contents(plugin_dir_path(__FILE__) . "/html/workspace.html");
  43. $css = file_get_contents(plugin_dir_path(__FILE__) . "/css/workspace.css");
  44. return $css . "\n" . $str;
  45. }
  46. public function shortcode_token($attrs) {
  47. $login = get_query_var( 'token' );
  48. return "<h1 id='test'> $login </h1>";
  49. }
  50. //for customer profile and broker trans
  51. public function my_add_rewrite_rules($aRules) {
  52. $aNewRules = array(
  53. 'medal/([^/]+)/?$' => 'index.php?pagename=medal&token=$matches[1]',
  54. );
  55. $aRules = $aNewRules + $aRules;
  56. return $aRules;
  57. }
  58. //
  59. //query var
  60. public function add_query_vars($aVars) {
  61. $aVars[] = "token"; // represents the receiption of this medal
  62. return $aVars;
  63. }
  64. public function register_js_css() {
  65. $this->nonce = wp_create_nonce('medal');
  66. $this->token = get_query_var( 'token' );
  67. $this->register_medal_js();
  68. }
  69. private function register_medal_js()
  70. {
  71. //wp_enqueue_style( 'mm', plugins_url('css/workspace.css', __FILE__));
  72. wp_enqueue_script('mm', plugins_url('js/workspace.js', __FILE__), array('jquery', 'jquery-ui-core'));
  73. wp_enqueue_script('typeahead', plugins_url('js/typeahead.bundle.min.js', __FILE__), array('jquery'));
  74. wp_localize_script( 'mm', 'mm', array(
  75. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  76. 'nonce' => $this->nonce, // It is common practice to comma after
  77. 'display_name' => wp_get_current_user()->display_name,
  78. 'loading' => plugins_url('img/loading.gif', __FILE__),
  79. 'done' => plugins_url('img/done.gif', __FILE__),
  80. 'search_user' => plugins_url('img/loading_user.gif', __FILE__),
  81. 'anonymous' => !is_user_logged_in(),
  82. ) );
  83. }
  84. function ajax_list_users()
  85. {
  86. //check_ajax_referer('medal');
  87. $client = $_POST['client'];
  88. $url = $_POST["action"];
  89. $response = array(
  90. 'status' => 'success',
  91. 'users' => [
  92. 1,2,3,4,5,6,7
  93. ],
  94. 'id' => $client,
  95. 'url' => $url,
  96. );
  97. wp_send_json($response);
  98. }
  99. public function ajax_search_users(){
  100. //check_ajax_referer('medal');
  101. $pattern = $_GET['pattern'];
  102. $args= array(
  103. 'search' => "*$pattern*", // or login or nicename in this example
  104. 'search_fields' => array('display_name'),
  105. );
  106. $users = new \WP_User_Query($args);
  107. $count = $users->get_total();
  108. //build response
  109. $response = array(
  110. 'count' => $count,
  111. 'date' => date('Y-m-d H:i:s'),
  112. 'users' => array(),
  113. );
  114. foreach ( $users->results as $u ) {
  115. $response['users'][] = array(
  116. 'userid' => $u->ID,
  117. 'username' => html_entity_decode($u->display_name),
  118. );
  119. }
  120. wp_send_json($response['users']);
  121. }
  122. }
  123. $mm = new Member();