Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active February 7, 2021 15:11
Show Gist options
  • Select an option

  • Save devinsays/dd15de71e3c563a3e6577acea7fa2d9d to your computer and use it in GitHub Desktop.

Select an option

Save devinsays/dd15de71e3c563a3e6577acea7fa2d9d to your computer and use it in GitHub Desktop.

Revisions

  1. devinsays revised this gist Apr 3, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wordpress-easy-post.php
    Original 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'] );
    }
    $response = json_decode( $response['body'] );
    }

    return $response;
    }
  2. devinsays revised this gist Apr 3, 2017. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions wordpress-easy-post.php
    Original 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 );

    if ( ! is_wp_error( $response ) ) {
    $response = json_decode( $response['body'] );

    // Cache for 10 minutes
    set_transient( $key, $response, 60 * 10 );
    }
    // 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;

    }
  3. devinsays revised this gist Apr 3, 2017. 1 changed file with 8 additions and 13 deletions.
    21 changes: 8 additions & 13 deletions wordpress-easy-post.php
    Original 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 );

    $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 );
    }
    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
  4. devinsays revised this gist Apr 3, 2017. 1 changed file with 74 additions and 74 deletions.
    148 changes: 74 additions & 74 deletions wordpress-easy-post.php
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    */
    function easy_post_api_init() {

    $namespace = 'easy-post/v1';
    $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
    );
    // 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();
    // Get query strings params from request
    $params = $request->get_query_params();

    // Ensures all param keys are available
    $params = array_replace( $defaults, $params );
    // Ensures all param keys are available
    $params = array_replace( $defaults, $params );

    // Sanitizes each of the keys
    $params = array_map( "absint", $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 );
    // 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 ) ) ) {
    /**
    * 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 );
    $data = easy_post_make_request( $params );
    $response = new WP_REST_Response( $data );

    if ( ! is_wp_error( $response ) ) {
    $response = json_decode( $response['body'] );
    if ( ! is_wp_error( $response ) ) {
    $response = json_decode( $response['body'] );

    // Cache for 10 minutes
    set_transient( $key, $response, 60 * 10 );
    }
    // Cache for 10 minutes
    set_transient( $key, $response, 60 * 10 );
    }

    } else {
    } else {

    // Returns cached value
    return get_transient( $key );
    // Returns cached value
    return get_transient( $key );

    }
    }

    return $response;

    }
    }

    /**
    * Fetches data from the Easy Post API via wp_remote_post
    */
    * 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'
    );
    $to_address = array(
    'zip' => $params['shipping_zip'],
    'country' => 'US'
    );

    $from_address = array(
    $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;
    '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;

    }
  5. devinsays created this gist Apr 3, 2017.
    129 changes: 129 additions & 0 deletions wordpress-easy-post.php
    Original 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;

    }