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.

436 lignes
13KB

  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. use function Nextgenthemes\ARVE\Common\Admin\label_text;
  16. //require_once(dirname(__FILE__) . '/autoload.php');
  17. require_once (ABSPATH . 'wp-includes/pluggable.php');
  18. class Member{
  19. private $token = "";
  20. private $nonce = "";
  21. private $db;
  22. public function __construct() {
  23. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  24. add_shortcode( 'mm_workspace', array($this, 'shortcode_workspace'));
  25. add_shortcode( 'mm_token', array($this, 'shortcode_token'));
  26. // hook add_rewrite_rules function into rewrite_rules_array
  27. add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
  28. // hook add_query_vars function into query_vars
  29. add_filter('query_vars', array($this,'add_query_vars'));
  30. //
  31. $this->ajax_hook('list_users');
  32. $this->ajax_hook('search_users');
  33. $this->ajax_hook('verify_user');
  34. global $wpdb;
  35. $this->db = $wpdb;
  36. }
  37. private function ajax_hook($code, $admin_only = false)
  38. {
  39. add_action("wp_ajax_$code", array($this,"ajax_$code" ));
  40. if (!$admin_only) {
  41. add_action("wp_ajax_nopriv_$code", array($this,"ajax_$code"));
  42. }
  43. }
  44. public function shortcode_workspace($attrs) {
  45. if ($this->token != "" )
  46. return "";
  47. $str = file_get_contents(plugin_dir_path(__FILE__) . "/html/workspace.html");
  48. $css = file_get_contents(plugin_dir_path(__FILE__) . "/css/workspace.css");
  49. return $css . "\n" . $str;
  50. }
  51. public function shortcode_token($attrs) {
  52. $token = get_query_var( 'token' );
  53. if (trim($token) == "")
  54. return "";
  55. $user = $this->getUserByToken($token);
  56. update_user_meta($user->ID, "hit100collected", "5");
  57. $subject = file_get_contents(plugin_dir_path(__FILE__) . "/html/medal_received.html");
  58. $subject = str_replace("#NAME#", $user->display_name, $subject);
  59. $result = str_replace("#SRC#", plugins_url('img/done.gif', __FILE__), $subject);
  60. return $result;
  61. }
  62. //for customer profile and broker trans
  63. public function my_add_rewrite_rules($aRules) {
  64. $aNewRules = array(
  65. 'medal/([^/]+)/?$' => 'index.php?pagename=medal&token=$matches[1]',
  66. );
  67. $aRules = $aNewRules + $aRules;
  68. return $aRules;
  69. }
  70. //
  71. //query var
  72. public function add_query_vars($aVars) {
  73. $aVars[] = "token"; // represents the receiption of this medal
  74. return $aVars;
  75. }
  76. public function register_js_css() {
  77. $this->nonce = wp_create_nonce('medal');
  78. $this->token = get_query_var( 'token' );
  79. if ($this->token == "edit")
  80. $this->house_keeping();
  81. if ($this->token == "test")
  82. $this->test();
  83. $this->register_medal_js();
  84. }
  85. private function register_medal_js()
  86. {
  87. //wp_enqueue_style( 'mm', plugins_url('css/workspace.css', __FILE__));
  88. wp_enqueue_script('mm', plugins_url('js/workspace.js', __FILE__), array('jquery', 'jquery-ui-core'));
  89. wp_enqueue_script('typeahead', plugins_url('js/typeahead.bundle.min.js', __FILE__), array('jquery'));
  90. wp_localize_script( 'mm', 'mm', array(
  91. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  92. 'nonce' => $this->nonce, // It is common practice to comma after
  93. 'display_name' => wp_get_current_user()->display_name,
  94. 'loading' => plugins_url('img/loading.gif', __FILE__),
  95. 'done' => plugins_url('img/done.gif', __FILE__),
  96. 'search_user' => plugins_url('img/loading_user.gif', __FILE__),
  97. 'anonymous' => !is_user_logged_in(),
  98. 'user' => $this->getUserByToken($this->token),
  99. 'imgurl' => plugins_url('img/box/', __FILE__),
  100. ) );
  101. }
  102. function ajax_list_users()
  103. {
  104. check_ajax_referer('medal');
  105. $client = $_POST['client'];
  106. $name = $_POST['name'];
  107. $user = false;
  108. if ($name != ""){
  109. $user =$this->getUserByDisplayName($name);
  110. }else{
  111. $user = get_user_by("ID", $client);
  112. }
  113. if ($user == false){
  114. $response = array(
  115. 'status' => 'error',
  116. 'errMsg' => "User not found",
  117. );
  118. wp_send_json($response);
  119. }
  120. $phone = get_user_meta($user->ID, "tel-mobile", true);
  121. $response = array(
  122. 'status' => 'success',
  123. 'userID' => $user->ID,
  124. 'email' => $this->mask_email($user->user_email),
  125. 'phone' => $this->mask_phone($phone),
  126. );
  127. wp_send_json($response);
  128. }
  129. private function mask_phone($phone)
  130. {
  131. if ($phone != "" && strlen($phone) > 4){
  132. return substr($phone, 0, -4) . "####";
  133. }else
  134. return "no valid phone";
  135. }
  136. private function mask_email($email)
  137. {
  138. $pos = stripos($email,"@");
  139. return substr($email,0,1) . "*****" . substr($email, $pos);
  140. }
  141. public function ajax_search_users()
  142. {
  143. check_ajax_referer('medal');
  144. $pattern = $_GET['pattern'];
  145. $args= array(
  146. 'search' => "*$pattern*", // or login or nicename in this example
  147. 'search_fields' => array('display_name'),
  148. 'role__in' => array('subscriber')
  149. );
  150. $users = new \WP_User_Query($args);
  151. $count = $users->get_total();
  152. //build response
  153. $response = array(
  154. 'count' => $count,
  155. 'date' => date('Y-m-d H:i:s'),
  156. 'users' => array(),
  157. );
  158. foreach ( $users->results as $u ) {
  159. $response['users'][] = array(
  160. 'userid' => $u->ID,
  161. 'username' => html_entity_decode($u->display_name),
  162. );
  163. }
  164. wp_send_json($response['users']);
  165. }
  166. public function ajax_verify_user()
  167. {
  168. check_ajax_referer('medal');
  169. $client = $_POST['client'];
  170. $verifycode = $_POST['verifycode'];
  171. $method = $_POST['method'];
  172. $user = get_user_by("ID", $client);
  173. if ($user == false){
  174. $response = array(
  175. 'status' => 'error',
  176. 'errMsg' => "User not found",
  177. );
  178. wp_send_json($response);
  179. }
  180. $pickup = $this->get_pickup($user);
  181. $response = array(
  182. 'status' => 'success',
  183. 'userID' => $user->ID,
  184. 'pass' => $this->verify_code($method, $verifycode, $user),
  185. 'addr' => $this->getUserPostalAddress($user),
  186. 'state' =>$this->getUserState($user),
  187. 'delivery'=> $this->get_delivery($this->getUserState($user)),
  188. 'pickup' => $pickup["name"],
  189. 'pp' => $pickup["phone"],
  190. 'ppwechat'=>$pickup["wechat"],
  191. 'loc' =>$pickup["loc"],
  192. 'time' =>$pickup["time"],
  193. 'step' => get_user_meta($client, "hit100collected", true),
  194. );
  195. wp_send_json($response);
  196. }
  197. private function get_delivery($state)
  198. {
  199. switch ($state){
  200. case "na":
  201. return "na"; //not available
  202. case "NT":
  203. case "TAS":
  204. case "ACT":
  205. case "SA":
  206. return "dp"; //direct post
  207. case "NSW":
  208. case "VIC":
  209. case "QLD":
  210. case "WA":
  211. return "pp";
  212. }
  213. }
  214. private function get_pickup($user)
  215. {
  216. $state = $this->getUserState($user);
  217. switch ($state){
  218. case "na":
  219. case "NT":
  220. case "TAS":
  221. case "ACT":
  222. case "SA":
  223. return array(
  224. "name" => "n/a",
  225. "phone"=>"n/a",
  226. "wechat" =>"n/a",
  227. "loc" => "n/a",
  228. "time" => "n/a",
  229. );
  230. case "NSW":
  231. return array(
  232. "name" => "孙鹏",
  233. "phone" => "0422896020",
  234. "wechat" => "lawipac",
  235. "loc" => "2020-12-13 15:30 - 18:00 : QVB Level 2 Coffee Shop, Sydney.\n" .
  236. "2020-12-15 17:00 - 18:00 : Strathfield Station Exit (Square). \n" .
  237. "2020-12-19 15:30 - 18:00 : Central Station Main Exit",
  238. "time" => "2020-12-13 ~ 19",
  239. );
  240. case "VIC":
  241. return array(
  242. "name" => "杨欧",
  243. "phone" => "0450673987",
  244. "wechat" => "yangou627478",
  245. "loc" => "n/a",
  246. "time" => "n/a",
  247. );
  248. case "QLD":
  249. return array(
  250. "name" => "于启华",
  251. "phone" => "0405928939",
  252. "wechat" => "cloudfisher321",
  253. "loc" => "n/a",
  254. "time" => "n/a",
  255. );
  256. case "WA":
  257. return array(
  258. "name" => "赵健 (Perth-15-外语)",
  259. "phone" => "0452008130",
  260. "wechat" => "Shmilyxiaorenyu",
  261. "loc" => "n/a",
  262. "time" => "n/a",
  263. );
  264. }
  265. }
  266. private function getUserPostalAddress($user)
  267. {
  268. $addr = get_user_meta($user->ID, 'postal-address', true);
  269. if ( $addr == "" )
  270. return "No valid address provided";
  271. else{
  272. return str_replace("," , "\r\n", $addr);
  273. }
  274. }
  275. private function getUserState($user)
  276. {
  277. $valid = array("NSW", "VIC", "TAS", "NT", "WA", "SA", "ACT", "QLD");
  278. $s = get_user_meta($user->ID, 'state', true);
  279. $s = strtoupper($s);
  280. if (in_array($s, $valid) )
  281. {
  282. return $s;
  283. }else {
  284. return "na";
  285. }
  286. }
  287. private function verify_code($method, $verifycode, $user)
  288. {
  289. $phone = get_user_meta($user->ID, "tel-mobile", true);
  290. if ($method=="mobile" && stripos($phone, $verifycode) != false && strlen($verifycode) ==4 )
  291. return true;
  292. if ($method=="email"){
  293. $mas = $this->mask_email($user->user_email);
  294. $newEmail = str_replace("*****", $verifycode, $mas );
  295. return $newEmail == $user->user_email;
  296. }
  297. return false;
  298. }
  299. private function getUserByDisplayName($pattern)
  300. {
  301. $args= array(
  302. 'search' => "*$pattern*", // or login or nicename in this example
  303. 'search_fields' => array('display_name'),
  304. );
  305. $users = new \WP_User_Query($args);
  306. if ($users->get_total() >=1){
  307. return $users->results[0];
  308. }else
  309. return false;
  310. }
  311. public function getUserByToken($token)
  312. {
  313. $user = get_users(array(
  314. 'meta_key' => 'token',
  315. 'meta_value' => "$token"
  316. ));
  317. return $user[0];
  318. }
  319. //for development purpose only
  320. public function test()
  321. {
  322. //$this->ajax_list_users();
  323. }
  324. public function house_keeping()
  325. {
  326. return;
  327. $args= array(
  328. 'search' => "**", // or login or nicename in this example
  329. 'search_fields' => array('display_name'),
  330. 'role__in' => array("subscriber"),
  331. );
  332. $users = new \WP_User_Query($args);
  333. $count = $users->get_total();
  334. foreach ( $users->results as $u ) {
  335. $post_addr = get_user_meta($u->ID, 'postal-address', true);
  336. $card = trim($post_addr) == ""? -1: 0;
  337. $country = get_user_meta($u->ID, 'country', true);
  338. $step = ($country != "Australia") ? 0: 2;
  339. $state = get_user_meta($u->ID, 'state', true);
  340. $step = (strtoupper($state) == "NSW") ? 4: $step;
  341. update_user_meta($u->ID, "hit100collected", $step);
  342. }
  343. }
  344. private function update_medal($u)
  345. {
  346. $this->db->update('sp_medal_100', array(
  347. 'medal' => 1,
  348. 'card' => 0,
  349. 'card_posted' =>0,
  350. 'card_delivered' => 0,
  351. 'medal_delivered' => 0,
  352. ),array(
  353. 'uid' => $u->ID,
  354. ));
  355. }
  356. }
  357. $mm = new Member();