SHARE
TWEET

Untitled

a guest Jul 17th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. URL
  2.     http://example.com/api/user_data/1
  3.  
  4.     JSON
  5.     {
  6.       "success": true,
  7.       "data": {
  8.         "id": 1,
  9.         "display_name": "your_username",
  10.         "user_registered": "2015-11-12 05:00:00",
  11.         "first": "First",
  12.         "last": "Last Name"
  13.       }
  14.     }
  15.    
  16. if ( ! class_exists( 'JSONEndpoint_UserData' ) ):
  17.     /**
  18.      * The code that registers the endpoint and handles the result
  19.      */
  20.     class JSONEndpoint_UserData {
  21.  
  22.         const ENDPOINT_NAME       = 'api/user_data'; // endpoint to capture
  23.         const ENDPOINT_QUERY_NAME = '__api_user_data'; // turns to param
  24.  
  25.         // WordPress hooks
  26.         public function run() {
  27.             add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
  28.             add_action( 'parse_request', array( $this, 'sniff_requests' ), 0 );
  29.             add_action( 'init', array( $this, 'add_endpoint' ), 0 );
  30.         }
  31.  
  32.         // Add public query vars
  33.         public function add_query_vars( $vars ) {
  34.             $vars[] = static::ENDPOINT_QUERY_NAME;
  35.             $vars[] = 'id';
  36.  
  37.             return $vars;
  38.         }
  39.  
  40.         // Add API Endpoint
  41.         public function add_endpoint() {
  42.             add_rewrite_rule( '^' . static::ENDPOINT_NAME . '/([^/]+)/?$', 'index.php?' . static::ENDPOINT_QUERY_NAME . '=1&id=$matches[1]', 'top' );
  43. // --->
  44.             flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
  45. // --->
  46.         }
  47.  
  48.         // Sniff Requests
  49.         public function sniff_requests( $wp_query ) {
  50.  
  51.             global $wp;
  52.  
  53.             if ( isset( $wp->query_vars[ static::ENDPOINT_QUERY_NAME ] ) ) {
  54.                 $this->handle_request(); // handle it
  55.             }
  56.         }
  57.  
  58.         // Handle Requests
  59.         protected function handle_request() {
  60.             global $wp;
  61.  
  62.             // we only deal with number$
  63.             $id = is_numeric( $wp->query_vars[ 'id' ] ) ? absint( $wp->query_vars[ 'id' ] ) : false;
  64.  
  65.             if ( ! is_numeric( $id ) || ! $user = get_user_by( 'id', $id ) ) {
  66.                 wp_send_json_error( array( 'message' => 'Invalid User ID' ) );
  67.             }
  68.  
  69.             // ALLOWING ACCESS FROM ANYWHERE --- WE MIGHT WANT TO RESTRICT THE PLACES THAT CAN USE THIS
  70.             header( "Access-Control-Allow-Origin: *" );
  71.  
  72.             // prep the response
  73.             $data = array(
  74.                 'id'              => $user->ID,
  75.                 'display_name'    => $user->data->display_name,
  76.                 'user_registered' => $user->data->user_registered,
  77.                 'first'           => $user->first_name,
  78.                 'last'            => $user->last_name,
  79.             );
  80.  
  81.             // write the response
  82.             wp_send_json_success( $data );
  83.  
  84.             die(); // just in case
  85.         }
  86.     }
  87.  
  88.     $ep = new JSONEndpoint_UserData();
  89.     $ep->run();
  90.  
  91. endif; // JSONEndpoint_UserData
RAW Paste Data
Top