collecting medal for hitxy members
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

147 lines
4.5KB

  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. 'anonymous' => !is_user_logged_in(),
  81. ) );
  82. }
  83. function ajax_list_users()
  84. {
  85. //check_ajax_referer('medal');
  86. $client = $_POST['client'];
  87. $url = $_POST["action"];
  88. $response = array(
  89. 'status' => 'success',
  90. 'users' => [
  91. 1,2,3,4,5,6,7
  92. ],
  93. 'id' => $client,
  94. 'url' => $url,
  95. );
  96. wp_send_json($response);
  97. }
  98. public function ajax_search_users(){
  99. //check_ajax_referer('medal');
  100. $pattern = $_GET['pattern'];
  101. $args= array(
  102. 'search' => "*$pattern*", // or login or nicename in this example
  103. 'search_fields' => array('display_name'),
  104. );
  105. $users = new \WP_User_Query($args);
  106. $count = $users->get_total();
  107. //build response
  108. $response = array(
  109. 'count' => $count,
  110. 'date' => date('Y-m-d H:i:s'),
  111. 'users' => array(),
  112. );
  113. foreach ( $users->results as $u ) {
  114. $response['users'][] = array(
  115. 'userid' => $u->ID,
  116. 'username' => html_entity_decode($u->display_name),
  117. );
  118. }
  119. wp_send_json($response['users']);
  120. }
  121. }
  122. $mm = new Member();