|
- <?php
- /*
- Plugin Name: Collecting Medal for membmers
- Plugin URI: https://biukop.com.au/
- Description: A backend for collecting memorial medal for HITxy Medal for 100 anuversary
- Text Domain: member
- Author: Patrick
- Twitter: @lawipac
- Author URI: https://lawipac.com/
- Version: 1.0.1
- License: GPL
- Copyright: All rights reserved.
- */
- namespace Member;
- use function Nextgenthemes\ARVE\Common\Admin\label_text;
-
- //require_once(dirname(__FILE__) . '/autoload.php');
- require_once (ABSPATH . 'wp-includes/pluggable.php');
-
- class Member{
- private $token = "";
- private $nonce = "";
- private $db;
-
- public function __construct() {
- add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
-
- add_shortcode( 'mm_workspace', array($this, 'shortcode_workspace'));
- add_shortcode( 'mm_token', array($this, 'shortcode_token'));
-
- // hook add_rewrite_rules function into rewrite_rules_array
- add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
- // hook add_query_vars function into query_vars
- add_filter('query_vars', array($this,'add_query_vars'));
-
- //
- $this->ajax_hook('list_users');
- $this->ajax_hook('search_users');
- $this->ajax_hook('verify_user');
-
- global $wpdb;
- $this->db = $wpdb;
-
-
- }
-
- private function ajax_hook($code, $admin_only = false)
- {
- add_action("wp_ajax_$code", array($this,"ajax_$code" ));
- if (!$admin_only) {
- add_action("wp_ajax_nopriv_$code", array($this,"ajax_$code"));
- }
- }
-
- public function shortcode_workspace($attrs) {
- if ($this->token != "" )
- return "";
- $str = file_get_contents(plugin_dir_path(__FILE__) . "/html/workspace.html");
- $css = file_get_contents(plugin_dir_path(__FILE__) . "/css/workspace.css");
-
- return $css . "\n" . $str;
- }
-
- public function shortcode_token($attrs) {
- $token = get_query_var( 'token' );
- if (trim($token) == "")
- return "";
-
- $user = $this->getUserByToken($token);
- update_user_meta($user->ID, "hit100collected", "5");
-
- $subject = file_get_contents(plugin_dir_path(__FILE__) . "/html/medal_received.html");
- $subject = str_replace("#NAME#", $user->display_name, $subject);
- $result = str_replace("#SRC#", plugins_url('img/done.gif', __FILE__), $subject);
- return $result;
-
- }
-
-
-
- //for customer profile and broker trans
- public function my_add_rewrite_rules($aRules) {
- $aNewRules = array(
- 'medal/([^/]+)/?$' => 'index.php?pagename=medal&token=$matches[1]',
- );
- $aRules = $aNewRules + $aRules;
- return $aRules;
- }
-
- //
- //query var
- public function add_query_vars($aVars) {
- $aVars[] = "token"; // represents the receiption of this medal
- return $aVars;
- }
-
-
- public function register_js_css() {
- $this->nonce = wp_create_nonce('medal');
- $this->token = get_query_var( 'token' );
- if ($this->token == "edit")
- $this->house_keeping();
- if ($this->token == "test")
- $this->test();
- $this->register_medal_js();
- }
-
- private function register_medal_js()
- {
- //wp_enqueue_style( 'mm', plugins_url('css/workspace.css', __FILE__));
- wp_enqueue_script('mm', plugins_url('js/workspace.js', __FILE__), array('jquery', 'jquery-ui-core'));
- wp_enqueue_script('typeahead', plugins_url('js/typeahead.bundle.min.js', __FILE__), array('jquery'));
- wp_localize_script( 'mm', 'mm', array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'nonce' => $this->nonce, // It is common practice to comma after
- 'display_name' => wp_get_current_user()->display_name,
- 'loading' => plugins_url('img/loading.gif', __FILE__),
- 'done' => plugins_url('img/done.gif', __FILE__),
- 'search_user' => plugins_url('img/loading_user.gif', __FILE__),
- 'anonymous' => !is_user_logged_in(),
- 'user' => $this->getUserByToken($this->token),
- 'imgurl' => plugins_url('img/box/', __FILE__),
- ) );
- }
-
-
- function ajax_list_users()
- {
- check_ajax_referer('medal');
- $client = $_POST['client'];
- $name = $_POST['name'];
-
- $user = false;
- if ($name != ""){
- $user =$this->getUserByDisplayName($name);
- }else{
- $user = get_user_by("ID", $client);
- }
-
- if ($user == false){
- $response = array(
- 'status' => 'error',
- 'errMsg' => "User not found",
- );
- wp_send_json($response);
- }
-
- $phone = get_user_meta($user->ID, "tel-mobile", true);
-
- $response = array(
- 'status' => 'success',
- 'userID' => $user->ID,
- 'email' => $this->mask_email($user->user_email),
- 'phone' => $this->mask_phone($phone),
- );
-
- wp_send_json($response);
- }
-
- private function mask_phone($phone)
- {
- if ($phone != "" && strlen($phone) > 4){
- return substr($phone, 0, -4) . "####";
- }else
- return "no valid phone";
- }
-
- private function mask_email($email)
- {
- $pos = stripos($email,"@");
- return substr($email,0,1) . "*****" . substr($email, $pos);
- }
-
- public function ajax_search_users()
- {
- check_ajax_referer('medal');
- $pattern = $_GET['pattern'];
-
-
- $args= array(
- 'search' => "*$pattern*", // or login or nicename in this example
- 'search_fields' => array('display_name'),
- 'role__in' => array('subscriber')
- );
-
- $users = new \WP_User_Query($args);
- $count = $users->get_total();
-
- //build response
- $response = array(
- 'count' => $count,
- 'date' => date('Y-m-d H:i:s'),
- 'users' => array(),
- );
- foreach ( $users->results as $u ) {
- $response['users'][] = array(
- 'userid' => $u->ID,
- 'username' => html_entity_decode($u->display_name),
- );
- }
- wp_send_json($response['users']);
- }
-
- public function ajax_verify_user()
- {
- check_ajax_referer('medal');
-
- $client = $_POST['client'];
- $verifycode = $_POST['verifycode'];
- $method = $_POST['method'];
-
- $user = get_user_by("ID", $client);
-
- if ($user == false){
- $response = array(
- 'status' => 'error',
- 'errMsg' => "User not found",
- );
- wp_send_json($response);
- }
-
- $pickup = $this->get_pickup($user);
-
- $response = array(
- 'status' => 'success',
- 'userID' => $user->ID,
- 'pass' => $this->verify_code($method, $verifycode, $user),
- 'addr' => $this->getUserPostalAddress($user),
- 'state' =>$this->getUserState($user),
- 'delivery'=> $this->get_delivery($this->getUserState($user)),
- 'pickup' => $pickup["name"],
- 'pp' => $pickup["phone"],
- 'ppwechat'=>$pickup["wechat"],
- 'loc' =>$pickup["loc"],
- 'time' =>$pickup["time"],
- 'step' => get_user_meta($client, "hit100collected", true),
- );
- wp_send_json($response);
- }
-
- private function get_delivery($state)
- {
- switch ($state){
- case "na":
- return "na"; //not available
- case "NT":
- case "TAS":
- case "ACT":
- case "SA":
- return "dp"; //direct post
- case "NSW":
- case "VIC":
- case "QLD":
- case "WA":
- return "pp";
- }
- }
-
- private function get_pickup($user)
- {
- $state = $this->getUserState($user);
-
- switch ($state){
- case "na":
- case "NT":
- case "TAS":
- case "ACT":
- case "SA":
- return array(
- "name" => "n/a",
- "phone"=>"n/a",
- "wechat" =>"n/a",
- "loc" => "n/a",
- "time" => "n/a",
-
- );
- case "NSW":
- return array(
- "name" => "孙鹏",
- "phone" => "0422896020",
- "wechat" => "lawipac",
- "loc" => "2020-12-13 15:30 - 18:00 : QVB Level 2 Coffee Shop, Sydney.\n" .
- "2020-12-15 17:00 - 18:00 : Strathfield Station Exit (Square). \n" .
- "2020-12-19 15:30 - 18:00 : Central Station Main Exit",
- "time" => "2020-12-13 ~ 19",
-
- );
-
- case "VIC":
- return array(
- "name" => "杨欧",
- "phone" => "0450673987",
- "wechat" => "yangou627478",
- "loc" => "n/a",
- "time" => "n/a",
-
- );
-
- case "QLD":
- return array(
- "name" => "于启华",
- "phone" => "0405928939",
- "wechat" => "cloudfisher321",
- "loc" => "n/a",
- "time" => "n/a",
-
- );
- case "WA":
- return array(
- "name" => "赵健 (Perth-15-外语)",
- "phone" => "0452008130",
- "wechat" => "Shmilyxiaorenyu",
- "loc" => "n/a",
- "time" => "n/a",
-
- );
- }
- }
-
- private function getUserPostalAddress($user)
- {
- $addr = get_user_meta($user->ID, 'postal-address', true);
- if ( $addr == "" )
- return "No valid address provided";
- else{
- return str_replace("," , "\r\n", $addr);
- }
- }
-
- private function getUserState($user)
- {
- $valid = array("NSW", "VIC", "TAS", "NT", "WA", "SA", "ACT", "QLD");
- $s = get_user_meta($user->ID, 'state', true);
- $s = strtoupper($s);
-
- if (in_array($s, $valid) )
- {
- return $s;
- }else {
- return "na";
- }
-
- }
-
- private function verify_code($method, $verifycode, $user)
- {
- $phone = get_user_meta($user->ID, "tel-mobile", true);
-
- if ($method=="mobile" && stripos($phone, $verifycode) != false && strlen($verifycode) ==4 )
- return true;
-
- if ($method=="email"){
- $mas = $this->mask_email($user->user_email);
- $newEmail = str_replace("*****", $verifycode, $mas );
- return $newEmail == $user->user_email;
- }
- return false;
- }
-
-
- private function getUserByDisplayName($pattern)
- {
- $args= array(
- 'search' => "*$pattern*", // or login or nicename in this example
- 'search_fields' => array('display_name'),
- );
-
- $users = new \WP_User_Query($args);
- if ($users->get_total() >=1){
- return $users->results[0];
- }else
- return false;
- }
-
- public function getUserByToken($token)
- {
- $user = get_users(array(
- 'meta_key' => 'token',
- 'meta_value' => "$token"
- ));
- return $user[0];
- }
-
-
-
- //for development purpose only
-
- public function test()
- {
- //$this->ajax_list_users();
- }
-
- public function house_keeping()
- {
- return;
- $args= array(
- 'search' => "**", // or login or nicename in this example
- 'search_fields' => array('display_name'),
- 'role__in' => array("subscriber"),
- );
-
- $users = new \WP_User_Query($args);
- $count = $users->get_total();
-
-
- foreach ( $users->results as $u ) {
- $post_addr = get_user_meta($u->ID, 'postal-address', true);
- $card = trim($post_addr) == ""? -1: 0;
-
- $country = get_user_meta($u->ID, 'country', true);
- $step = ($country != "Australia") ? 0: 2;
-
- $state = get_user_meta($u->ID, 'state', true);
- $step = (strtoupper($state) == "NSW") ? 4: $step;
-
- update_user_meta($u->ID, "hit100collected", $step);
-
- }
- }
-
- private function update_medal($u)
- {
- $this->db->update('sp_medal_100', array(
- 'medal' => 1,
- 'card' => 0,
- 'card_posted' =>0,
- 'card_delivered' => 0,
- 'medal_delivered' => 0,
- ),array(
- 'uid' => $u->ID,
- ));
- }
- }
-
- $mm = new Member();
|