Last active
November 26, 2019 07:01
-
-
Save raazon/b5c34367c19d1b8b74f1768b15edb3fe to your computer and use it in GitHub Desktop.
WordPress Rest API Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/************************************************************************** | |
* ALL CUSTOM REST API ROUTES FOR 3rd PARTY USE | |
* @reference: https://developer.wordpress.org/rest-api/ | |
* @since 1.0 | |
* @author Razon | |
/**************************************************************************/ | |
// ALL CUSTOM REST ROUTE'S | |
add_action( 'rest_api_init', function () { | |
/** | |
* Get all user data | |
* @method: GET | |
* @example url: http://example.com/wp-json/api/v1/users-data | |
*/ | |
register_rest_route( 'api/v1', '/users-data', array( | |
'methods' => WP_REST_Server::READABLE, // READABLE = 'GET', CREATABLE = 'POST', EDITABLE = 'POST, PUT, PATCH'; DELETABLE = 'DELETE' | |
'callback' => 'wp_custom_api_v1_get_users_data', | |
'permission_callback' => 'wp_custom_api_v1_get_users_data_permission', | |
) ); | |
/** | |
* Get single user data | |
* @method: POST | |
* @example url: http://example.com/wp-json/api/v1/get-user-data | |
*/ | |
register_rest_route( 'api/v1', '/get-user-data', array( | |
'methods' => WP_REST_Server::CREATABLE, | |
'callback' => 'wp_custom_api_v1_get_single_user_data', | |
) ); | |
/** | |
* Get single user data by user id | |
* @method: GET | |
* @example url: http://example.com/wp-json/api/v1/user-data/100 | |
*/ | |
register_rest_route( 'api/v1', '/user-data/(?P<id>[0-9]+)', array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'wp_custom_api_v1_get_users_data', | |
) ); | |
/** | |
* Get Properties | |
* @method: GET | |
* @example url: http://example.com/wp-json/api/v1/get-properties/SEARCHTEXT/PROPERTYTYPE/ | |
*/ | |
register_rest_route( 'api/v1', '/get-properties/(?P<search>[\s\S]+)/(?P<property_type>[\s\S]+)', array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'wp_custom_api_v1_get_properties', | |
) ); | |
/** | |
* Get Properties Aother way | |
* @method: GET | |
* @example url: http://example.com/wp-json/api/v1/get-properties/?search=SEARCH&property_type=SALE | |
*/ | |
register_rest_route('api/v1', '/get-properties/', array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => 'wp_custom_api_v1_get_properties', | |
)); | |
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get users data | |
* @return array List of all user data via secret key | |
*/ | |
function wp_custom_api_v1_get_users_data( $data ) { | |
$headers = getallheaders(); | |
$apiAuthorizationToken = get_theme_mod('api_authorization_token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'); | |
if(isset($headers['Authorization']) && trim($headers['Authorization']) === $apiAuthorizationToken){ | |
$userID = $data['id']; | |
$users = []; | |
$args = array( | |
'role__in' => ['agent', 'subscriber', 'author', 'contributor', 'editor'], | |
); | |
if($userID){ | |
$args['include'] = [$userID]; | |
} | |
$user_query = new WP_User_Query( $args ); | |
if ( ! empty( $user_query->results ) ) { | |
foreach ( $user_query->results as $key=>$user ) { | |
$userAvatarID = get_user_meta($user->ID, 'user_avatar', true); | |
$mobile_numbers = get_field("mobile_numbers", "user_$user->ID"); | |
$fax_numbers = get_field("fax_numbers", "user_$user->ID"); | |
$users[$key] = [ | |
'ID' => $user->ID, | |
'username' => $user->user_login, | |
'email' => $user->user_email, | |
'first_name' => $user->first_name, | |
'last_name' => $user->last_name, | |
'nickname' => $user->nickname, | |
'display_name' => $user->display_name, | |
'user_url' => $user->user_url, | |
'user_registered' => $user->user_registered, | |
'avatar_url' => $userAvatarID ? esc_url(wp_get_attachment_url($userAvatarID)) : esc_url(get_avatar_url($user->ID, ['size' => '255'])), | |
'OfficePhone' => $user->OfficePhone, | |
'LinkedIn' => $user->LinkedIn, | |
'Twitter' => $user->Twitter, | |
'Skype' => $user->Skype, | |
'description' => $user->description, | |
'role' => $user->roles[0], | |
'company_name' => get_user_meta( $user->ID, 'company_name' , true ), | |
'title' => get_user_meta( $user->ID, 'title' , true ), | |
'mobile_numbers' => $mobile_numbers, | |
'fax_numbers' => $fax_numbers, | |
'license_number' => get_user_meta( $user->ID, 'license_number' , true ), | |
]; | |
} | |
return rest_ensure_response( $users ); | |
} else { | |
return new WP_Error( 'not_found', esc_html__( 'No user found', 'ichelper' ), array( 'status' => 404 ) ); | |
} | |
}else{ | |
return new WP_Error( 'unauthorized', esc_html__( 'Authorization token invalid.', 'ichelper' ), array( 'status' => 401 ) ); | |
} | |
} | |
/** | |
* Get single user data | |
* @return array Single user data by user access details | |
*/ | |
function wp_custom_api_v1_get_single_user_data($data){ | |
$headers = getallheaders(); | |
$username = $data['userlogin']; | |
$password = $data['userpass']; | |
$isAuthenticate = wp_authenticate($username, $password); // ref: https://developer.wordpress.org/reference/functions/wp_authenticate/ | |
if($isAuthenticate->ID){ | |
$userAvatarID = get_user_meta($isAuthenticate->ID, 'user_avatar', true); | |
$mobile_numbers = get_field("mobile_numbers", "user_$isAuthenticate->ID"); | |
$fax_numbers = get_field("fax_numbers", "user_$isAuthenticate->ID"); | |
$response = [ | |
'ID' => $isAuthenticate->ID, | |
'username' => $isAuthenticate->user_login, | |
'email' => $isAuthenticate->user_email, | |
'first_name' => $isAuthenticate->first_name, | |
'last_name' => $isAuthenticate->last_name, | |
'nickname' => $isAuthenticate->nickname, | |
'display_name' => $isAuthenticate->display_name, | |
'user_url' => $isAuthenticate->user_url, | |
'user_registered' => $isAuthenticate->user_registered, | |
'avatar_url' => $userAvatarID ? esc_url(wp_get_attachment_url($userAvatarID)) : esc_url(get_avatar_url($isAuthenticate->ID, ['size' => '255'])), | |
'OfficePhone' => $isAuthenticate->OfficePhone, | |
'LinkedIn' => $isAuthenticate->LinkedIn, | |
'Twitter' => $isAuthenticate->Twitter, | |
'Skype' => $isAuthenticate->Skype, | |
'description' => $isAuthenticate->description, | |
'role' => $isAuthenticate->roles[0], | |
'company_name' => get_user_meta( $isAuthenticate->ID, 'company_name' , true ), | |
'title' => get_user_meta( $isAuthenticate->ID, 'title' , true ), | |
'mobile_numbers' => $mobile_numbers, | |
'fax_numbers' => $fax_numbers, | |
'license_number' => get_user_meta( $isAuthenticate->ID, 'license_number' , true ), | |
]; | |
return rest_ensure_response( $response ); | |
}else{ | |
$response = []; | |
foreach($isAuthenticate->errors as $key => $error){ | |
$errorMessage = wp_strip_all_tags($error[0], true); | |
$errorMessageReplace = str_replace('Lost your password?', '', $errorMessage); | |
$response[$key] = trim($errorMessageReplace); | |
} | |
return new WP_Error( 'unauthorized', $response, array( 'status' => 401 ) ); | |
} | |
} | |
/** | |
* Get users data permission | |
* @return array List of menus with slug and description | |
*/ | |
function wp_custom_api_v1_get_users_data_permission($request){ | |
return is_user_logged_in(); | |
} | |
/** | |
* Get Properties | |
* @return array Search properties | |
*/ | |
function wp_custom_api_v1_get_properties($request){ | |
$search = isset($request['search']) ? urldecode($request['search']) : ''; | |
$property_type = isset($request['property_type']) ? urldecode($request['property_type']) : ''; | |
// all of this just for test | |
var_dump($request->get_query_params()); | |
var_dump($request->get_body_params()); | |
var_dump($request->get_json_params()); | |
var_dump($request->get_default_params()); | |
var_dump($request->get_file_params()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment