Last active
February 7, 2021 15:11
-
-
Save devinsays/dd15de71e3c563a3e6577acea7fa2d9d to your computer and use it in GitHub Desktop.
Revisions
-
devinsays revised this gist
Apr 3, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -116,8 +116,8 @@ function easy_post_make_request( $params ) { $response = wp_remote_post( $api_url, $args ); if ( ! is_wp_error( $response ) ) { $response = json_decode( $response['body'] ); } return $response; } -
devinsays revised this gist
Apr 3, 2017 . 1 changed file with 6 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -60,12 +60,8 @@ function easy_post_return_rates( WP_REST_Request $request ) { $data = easy_post_make_request( $params ); $response = new WP_REST_Response( $data ); // Cache for 10 minutes set_transient( $key, $response, 60 * 10 ); } else { // Returns cached value return get_transient( $key ); @@ -118,7 +114,10 @@ function easy_post_make_request( $params ) { // Replace the EASYPOST_TEST_API_KEY constant with your own API Key $api_url = 'https://' . EASYPOST_TEST_API_KEY . ':@api.easypost.com/v2/shipments'; $response = wp_remote_post( $api_url, $args ); if ( ! is_wp_error( $response ) ) { $response = json_decode( $response['body'] ); } return $response; } -
devinsays revised this gist
Apr 3, 2017 . 1 changed file with 8 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -57,27 +57,22 @@ function easy_post_return_rates( WP_REST_Request $request ) { * This data will expire after 10 minutes. */ if ( false === ( $data = get_transient( $key ) ) ) { $data = easy_post_make_request( $params ); $response = new WP_REST_Response( $data ); if ( ! is_wp_error( $response ) ) { $response = json_decode( $response['body'] ); // Cache for 10 minutes set_transient( $key, $response, 60 * 10 ); } } else { // Returns cached value return get_transient( $key ); } return $response; } /** * Fetches data from the Easy Post API via wp_remote_post -
devinsays revised this gist
Apr 3, 2017 . 1 changed file with 74 additions and 74 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ */ function easy_post_api_init() { $namespace = 'easy-post/v1'; register_rest_route( $namespace, '/rates/', array( 'methods' => 'GET', 'callback' => 'easy_post_return_rates', @@ -28,102 +28,102 @@ function easy_post_api_init() { */ function easy_post_return_rates( WP_REST_Request $request ) { // Default shipping settings $defaults = array( 'zip' => 78701, 'width' => 10, 'length' => 10, 'height' => 10, 'weight' => 16, 'shipping_zip' => 94105 ); // Get query strings params from request $params = $request->get_query_params(); // Ensures all param keys are available $params = array_replace( $defaults, $params ); // Sanitizes each of the keys $params = array_map( "absint", $params ); // Creates unique key from values to set or retrieve transient $key = 'easy_post_' . implode( "_", $params ); /** * To avoid making the same request multiple times in a short time period * we store the response as a transient in the WordPress database. * * This data will expire after 10 minutes. */ if ( false === ( $data = get_transient( $key ) ) ) { $data = easy_post_make_request( $params ); $response = new WP_REST_Response( $data ); if ( ! is_wp_error( $response ) ) { $response = json_decode( $response['body'] ); // Cache for 10 minutes set_transient( $key, $response, 60 * 10 ); } } else { // Returns cached value return get_transient( $key ); } return $response; } /** * Fetches data from the Easy Post API via wp_remote_post */ function easy_post_make_request( $params ) { $to_address = array( 'zip' => $params['shipping_zip'], 'country' => 'US' ); $from_address = array( 'zip' => $params['zip'], 'country' => 'US' ); $parcel = array( 'length' => $params['length'], 'width' => $params['width'], 'height' => $params['height'], 'weight' => $params['weight'] ); $request = array( 'shipment' => array( 'to_address' => $to_address, 'from_address' => $from_address, 'parcel' => $parcel ) ); $args = array( 'method' => 'POST', 'blocking' => true, 'headers' => array( 'Content-Type' => 'application/json' ), 'timeout' => 15, 'sslverify' => false, 'body' => json_encode( $request ) ); // Replace the EASYPOST_TEST_API_KEY constant with your own API Key $api_url = 'https://' . EASYPOST_TEST_API_KEY . ':@api.easypost.com/v2/shipments'; $response = wp_remote_post( $api_url, $args ); return $response; } -
devinsays created this gist
Apr 3, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,129 @@ <?php /** * Sets up the Easy Post API Endpoints * * @package WP Theming */ /** * Sets up a JSON endpoint at /wp-json/easy-post/v1/rates/ */ function easy_post_api_init() { $namespace = 'easy-post/v1'; register_rest_route( $namespace, '/rates/', array( 'methods' => 'GET', 'callback' => 'easy_post_return_rates', ) ); } add_action( 'rest_api_init', 'easy_post_api_init' ); /** * Outputs Easy Post data on the JSON endpoint * * Defaults are used if query string data is missing, but you might want to return an error instead. * Valid URL: /wp-json/easy-post/v1/rates/?zip=78701&width=10&length=10&height=10&weight=10&shipping_zip=78751 */ function easy_post_return_rates( WP_REST_Request $request ) { // Default shipping settings $defaults = array( 'zip' => 78701, 'width' => 10, 'length' => 10, 'height' => 10, 'weight' => 16, 'shipping_zip' => 94105 ); // Get query strings params from request $params = $request->get_query_params(); // Ensures all param keys are available $params = array_replace( $defaults, $params ); // Sanitizes each of the keys $params = array_map( "absint", $params ); // Creates unique key from values to set or retrieve transient $key = 'easy_post_' . implode( "_", $params ); /** * To avoid making the same request multiple times in a short time period * we store the response as a transient in the WordPress database. * * This data will expire after 10 minutes. */ if ( false === ( $data = get_transient( $key ) ) ) { $data = easy_post_make_request( $params ); $response = new WP_REST_Response( $data ); if ( ! is_wp_error( $response ) ) { $response = json_decode( $response['body'] ); // Cache for 10 minutes set_transient( $key, $response, 60 * 10 ); } } else { // Returns cached value return get_transient( $key ); } return $response; } /** * Fetches data from the Easy Post API via wp_remote_post */ function easy_post_make_request( $params ) { $to_address = array( 'zip' => $params['shipping_zip'], 'country' => 'US' ); $from_address = array( 'zip' => $params['zip'], 'country' => 'US' ); $parcel = array( 'length' => $params['length'], 'width' => $params['width'], 'height' => $params['height'], 'weight' => $params['weight'] ); $request = array( 'shipment' => array( 'to_address' => $to_address, 'from_address' => $from_address, 'parcel' => $parcel ) ); $args = array( 'method' => 'POST', 'blocking' => true, 'headers' => array( 'Content-Type' => 'application/json' ), 'timeout' => 15, 'sslverify' => false, 'body' => json_encode( $request ) ); // Replace the EASYPOST_TEST_API_KEY constant with your own API Key $api_url = 'https://' . EASYPOST_TEST_API_KEY . ':@api.easypost.com/v2/shipments'; $response = wp_remote_post( $api_url, $args ); return $response; }