|
- <?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;
- //require_once(dirname(__FILE__) . '/autoload.php');
- require_once (ABSPATH . 'wp-includes/pluggable.php');
-
- class Member{
- private $token = "";
- private $nonce = "";
-
- 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');
- }
-
- 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) {
- $login = get_query_var( 'token' );
- return "<h1 id='test'> $login </h1>";
- }
-
-
-
- //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' );
- $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(),
- ) );
- }
-
-
- function ajax_list_users()
- {
- //check_ajax_referer('medal');
- $client = $_POST['client'];
- $url = $_POST["action"];
-
- $response = array(
- 'status' => 'success',
- 'users' => [
- 1,2,3,4,5,6,7
- ],
- 'id' => $client,
- 'url' => $url,
- );
- wp_send_json($response);
- }
-
- 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'),
- );
-
- $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']);
- }
-
- }
-
- $mm = new Member();
|