Created
March 10, 2016 21:00
-
-
Save bradyvercher/b541b6b0b39e0bef5a89 to your computer and use it in GitHub Desktop.
A starter WP REST API controller for creating a Dynamic App for Help Scout. http://developer.helpscout.net/custom-apps/dynamic/
This file contains 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 | |
/** | |
* Help Scout REST controller class. | |
* | |
* @license GPL-2.0+ | |
* @link http://developer.helpscout.net/custom-apps/dynamic/ | |
*/ | |
class HelpScout_REST_Controller { | |
/** | |
* Secret key. | |
* | |
* @todo Enter the secret key for your custom app. If you need to create | |
* one, use `wp_generate_password( 40 );`. | |
* | |
* @var string | |
*/ | |
const SECRET_KEY = '{secret key goes here}'; | |
/** | |
* Register routes. | |
*/ | |
public function register_routes() { | |
register_rest_route( 'helpscout', '/v1', [ | |
[ | |
'methods' => WP_REST_Server::CREATABLE, | |
'callback' => [ $this, 'handle_request' ], | |
'permission_callback' => [ $this, 'check_permissions' ], | |
'args' => [ | |
'customer' => array( | |
'required' => true, | |
), | |
'mailbox' => array( | |
'required' => true, | |
), | |
'ticket' => array( | |
'required' => true, | |
), | |
], | |
], | |
] ); | |
} | |
/** | |
* Verify the signature. | |
* | |
* @since 1.0.0 | |
* | |
* @param WP_REST_Request $request Request instance. | |
* @return boolean | |
*/ | |
public function check_permissions( $request ) { | |
global $wp_rest_server; | |
$raw_data = $wp_rest_server->get_raw_data(); | |
$signature = base64_encode( hash_hmac( 'sha1', $raw_data, self::SECRET_KEY, true ) ); | |
return hash_equals( $request->get_header( 'x-helpscout-signature' ), $signature ); | |
} | |
/** | |
* Handle the request. | |
* | |
* @param WP_REST_Request $request Request instance. | |
* @return array|WP_REST_Response | |
*/ | |
public function handle_request( $request ) { | |
$user = $this->find_user( $request['customer'] ); | |
if ( is_wp_error( $user ) ) { | |
return $this->error_to_response( $user ); | |
} | |
$data = array( | |
'html' => $this->get_html( $user ), | |
); | |
return $data; | |
} | |
/** | |
* Build the HTML for the custom app. | |
* | |
* @since 1.0.0 | |
* | |
* @param WP_User $user User object. | |
* @return string | |
*/ | |
protected function get_html( $user ) { | |
return ''; // @todo Build the HTML. | |
} | |
/** | |
* Find a user by email address. | |
* | |
* @param array $customer Array of customer data. | |
* @return WP_User|WP_Error | |
*/ | |
protected function find_user( $customer ) { | |
$emails = array_unique( array_merge( array( $customer['email'] ), $customer['emails'] ) ); | |
foreach ( $emails as $email ) { | |
$user = get_user_by( 'email', $email ); | |
if ( $user ) { | |
return $user; | |
} | |
} | |
return new WP_Error( 'unknown_user', 'A user with that email address was not found.' ); | |
} | |
/** | |
* Convert a WP_Error object into a response. | |
* | |
* @param WP_Error $error Error object. | |
* @return WP_REST_Response | |
*/ | |
protected function error_to_response( $error ) { | |
$data = array( 'html' => $error->get_error_message() ); | |
return new WP_REST_Response( $data, 200 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment