collecting medal for hitxy members
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

325 lignes
9.2KB

  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. private $db;
  21. public function __construct() {
  22. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  23. add_shortcode( 'mm_workspace', array($this, 'shortcode_workspace'));
  24. add_shortcode( 'mm_token', array($this, 'shortcode_token'));
  25. // hook add_rewrite_rules function into rewrite_rules_array
  26. add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
  27. // hook add_query_vars function into query_vars
  28. add_filter('query_vars', array($this,'add_query_vars'));
  29. //
  30. $this->ajax_hook('list_users');
  31. $this->ajax_hook('search_users');
  32. $this->ajax_hook('verify_user');
  33. global $wpdb;
  34. $this->db = $wpdb;
  35. }
  36. private function ajax_hook($code, $admin_only = false)
  37. {
  38. add_action("wp_ajax_$code", array($this,"ajax_$code" ));
  39. if (!$admin_only) {
  40. add_action("wp_ajax_nopriv_$code", array($this,"ajax_$code"));
  41. }
  42. }
  43. public function shortcode_workspace($attrs) {
  44. if ($this->token != "" )
  45. return "";
  46. $str = file_get_contents(plugin_dir_path(__FILE__) . "/html/workspace.html");
  47. $css = file_get_contents(plugin_dir_path(__FILE__) . "/css/workspace.css");
  48. return $css . "\n" . $str;
  49. }
  50. public function shortcode_token($attrs) {
  51. $token = get_query_var( 'token' );
  52. $user = $this->getUserByToken($token);
  53. return "<h1 id='test'> ok ok " . $user[0]->display_name . " </h1>";
  54. }
  55. //for customer profile and broker trans
  56. public function my_add_rewrite_rules($aRules) {
  57. $aNewRules = array(
  58. 'medal/([^/]+)/?$' => 'index.php?pagename=medal&token=$matches[1]',
  59. );
  60. $aRules = $aNewRules + $aRules;
  61. return $aRules;
  62. }
  63. //
  64. //query var
  65. public function add_query_vars($aVars) {
  66. $aVars[] = "token"; // represents the receiption of this medal
  67. return $aVars;
  68. }
  69. public function register_js_css() {
  70. $this->nonce = wp_create_nonce('medal');
  71. $this->token = get_query_var( 'token' );
  72. if ($this->token == "edit")
  73. $this->house_keeping();
  74. if ($this->token == "test")
  75. $this->test();
  76. $this->register_medal_js();
  77. }
  78. private function register_medal_js()
  79. {
  80. //wp_enqueue_style( 'mm', plugins_url('css/workspace.css', __FILE__));
  81. wp_enqueue_script('mm', plugins_url('js/workspace.js', __FILE__), array('jquery', 'jquery-ui-core'));
  82. wp_enqueue_script('typeahead', plugins_url('js/typeahead.bundle.min.js', __FILE__), array('jquery'));
  83. wp_localize_script( 'mm', 'mm', array(
  84. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  85. 'nonce' => $this->nonce, // It is common practice to comma after
  86. 'display_name' => wp_get_current_user()->display_name,
  87. 'loading' => plugins_url('img/loading.gif', __FILE__),
  88. 'done' => plugins_url('img/done.gif', __FILE__),
  89. 'search_user' => plugins_url('img/loading_user.gif', __FILE__),
  90. 'anonymous' => !is_user_logged_in(),
  91. 'user' => $this->getUserByToken($this->token),
  92. ) );
  93. }
  94. function ajax_list_users()
  95. {
  96. //check_ajax_referer('medal');
  97. $client = $_POST['client'];
  98. $name = $_POST['name'];
  99. $user = false;
  100. if ($name != ""){
  101. $user =$this->getUserByDisplayName($name);
  102. }else{
  103. $user = get_user_by("ID", $client);
  104. }
  105. if ($user == false){
  106. $response = array(
  107. 'status' => 'error',
  108. 'errMsg' => "User not found",
  109. );
  110. wp_send_json($response);
  111. }
  112. $phone = get_user_meta($user->ID, "tel-mobile", true);
  113. $response = array(
  114. 'status' => 'success',
  115. 'userID' => $user->ID,
  116. 'email' => $this->mask_email($user->user_email),
  117. 'phone' => $this->mask_phone($phone),
  118. );
  119. wp_send_json($response);
  120. }
  121. private function mask_phone($phone)
  122. {
  123. if ($phone != "" && strlen($phone) > 4){
  124. return substr($phone, 0, -4) . "####";
  125. }else
  126. return "no valid phone";
  127. }
  128. private function mask_email($email)
  129. {
  130. $pos = stripos($email,"@");
  131. return substr($email,0,1) . "*****" . substr($email, $pos);
  132. }
  133. public function ajax_search_users()
  134. {
  135. //check_ajax_referer('medal');
  136. $pattern = $_GET['pattern'];
  137. $args= array(
  138. 'search' => "*$pattern*", // or login or nicename in this example
  139. 'search_fields' => array('display_name'),
  140. );
  141. $users = new \WP_User_Query($args);
  142. $count = $users->get_total();
  143. //build response
  144. $response = array(
  145. 'count' => $count,
  146. 'date' => date('Y-m-d H:i:s'),
  147. 'users' => array(),
  148. );
  149. foreach ( $users->results as $u ) {
  150. $response['users'][] = array(
  151. 'userid' => $u->ID,
  152. 'username' => html_entity_decode($u->display_name),
  153. );
  154. }
  155. wp_send_json($response['users']);
  156. }
  157. public function ajax_verify_user()
  158. {
  159. //check_ajax_referer('medal');
  160. $client = $_POST['client'];
  161. $verifycode = $_POST['verifycode'];
  162. $method = $_POST['method'];
  163. $user = get_user_by("ID", $client);
  164. if ($user == false){
  165. $response = array(
  166. 'status' => 'error',
  167. 'errMsg' => "User not found",
  168. );
  169. wp_send_json($response);
  170. }
  171. $response = array(
  172. 'status' => 'success',
  173. 'userID' => $user->ID,
  174. 'pass' => $this->verify_code($method, $verifycode, $user),
  175. );
  176. wp_send_json($response);
  177. }
  178. private function verify_code($method, $verifycode, $user)
  179. {
  180. $phone = get_user_meta($user->ID, "tel-mobile", true);
  181. if ($method=="mobile" && stripos($phone, $verifycode) != false && strlen($verifycode) ==4 )
  182. return true;
  183. if ($method=="email"){
  184. $mas = $this->mask_email($user->user_email);
  185. $newEmail = str_replace("*****", $verifycode, $mas );
  186. return $newEmail == $user->user_email;
  187. }
  188. return false;
  189. }
  190. private function getUserByDisplayName($pattern)
  191. {
  192. $args= array(
  193. 'search' => "*$pattern*", // or login or nicename in this example
  194. 'search_fields' => array('display_name'),
  195. );
  196. $users = new \WP_User_Query($args);
  197. if ($users->get_total() >=1){
  198. return $users->results[0];
  199. }else
  200. return false;
  201. }
  202. public function getUserByToken($token)
  203. {
  204. $user = get_users(array(
  205. 'meta_key' => 'token',
  206. 'meta_value' => "$token"
  207. ));
  208. return $user;
  209. }
  210. //for development purpose only
  211. public function test()
  212. {
  213. $this->ajax_list_users();
  214. }
  215. public function house_keeping()
  216. {
  217. $args= array(
  218. 'search' => "**", // or login or nicename in this example
  219. 'search_fields' => array('display_name'),
  220. 'role__in' => array("subscriber"),
  221. );
  222. $users = new \WP_User_Query($args);
  223. $count = $users->get_total();
  224. foreach ( $users->results as $u ) {
  225. $card = 0;
  226. $post_addr = get_user_meta($u->ID, 'postal-address', true);
  227. $card = trim($post_addr) == ""? -1: 0;
  228. $medal = 1;
  229. $country = get_user_meta($u->ID, 'country', true);
  230. if ($country != "Australia")
  231. $medal = 0;
  232. $this->db->update('sp_medal_100', array(
  233. 'medal' => $medal,
  234. 'card' => $card,
  235. 'card_posted' =>0,
  236. 'card_delivered' => 0,
  237. 'medal_delivered' => 0,
  238. ),array(
  239. 'uid' => $u->ID,
  240. ));
  241. }
  242. }
  243. private function update_medal($u)
  244. {
  245. $this->db->update('sp_medal_100', array(
  246. 'medal' => 1,
  247. 'card' => 0,
  248. 'card_posted' =>0,
  249. 'card_delivered' => 0,
  250. 'medal_delivered' => 0,
  251. ),array(
  252. 'uid' => $u->ID,
  253. ));
  254. }
  255. }
  256. $mm = new Member();