autoship_update_scheduled_order_shipping_address_post_wrapper (function)

( src/scheduled-orders.php) Updates a Scheduled Order Shipping Address via POST to QPilot.

Function

/**
 * Updates a Scheduled Order Shipping Address via POST
 * @see autoship_update_scheduled_order_handler
 */
function autoship_update_scheduled_order_shipping_address_post_wrapper (){

  // Verify action
  if ( ! isset( $_REQUEST['autoship_update_order_shipping_address'] ) ) {
    return;
  }

	wc_nocache_headers();

  // Validate Nonce and get order id.
  $nonce = $_REQUEST['autoship-update-scheduled-order-shipping-address-nonce'];
  $autoship_order_id = autoship_update_scheduled_order_post_valid_nonce_id( $nonce, 'autoship-update-scheduled-order-shipping-address' );

  if ( !$autoship_order_id )
  return;

  $original_autoship_order = autoship_pull_scheduled_order_session( $nonce, $autoship_order_id );

  // Check for the country
  $country = isset( $_POST['country'] ) ? sanitize_text_field( $_POST['country'] ) : NULL;
  $base = !empty( $country ) ? array( 'country' => $country ) : array();

  // If the current country isn't valid add an error and bail.
  $validation = $success = true;
	if ( ! array_key_exists( $country, WC()->countries->get_countries() ) ){

    $validation = new WP_Error( 'invalid_shipping_address_values', __( "The selected Shipping Address country field is invalid.", "autoship" ) );

    $messages = apply_filters( 'autoship_update_scheduled_order_shipping_address_invalid_country_notice', $validation->get_error_messages(), array( 'country' => $country ) );

    // Add our general error notice
    foreach ( $messages as $code => $message)
    wc_add_notice( $message, 'error' );

    // Allow others to add on additional invalid notices
    do_action( 'autoship_update_scheduled_order_shipping_address_invalid_country_field', array( 'country' => $country ) );

    $success = false;

  }

  if ( $success ){

    // Retrieve the Form Fields to Check
    // Country must be used from the submitted address
    // Else Base Country will be used.
    $fields = autoship_shipping_address_form_fields( $base );
    $values = $valids = $invalids = array();

    // Get posted values
    foreach ( $fields as $key => $data ) {
      if ( isset( $_POST[$key] ) )
      $values[$key] = $_POST[$key];
    }

    // Allow posted values to be sanitized & formatted
    $values = apply_filters( 'autoship_scheduled_order_sanitize_shipping_address_post_fields', $values, $fields );

    // Validate posted field values
    $valids = apply_filters( 'autoship_scheduled_order_validate_shipping_address_post_field', $values, $fields );

    //Get Invalids
    foreach ($values as $key => $value){
      if ( !isset( $valids[$key] ) )
      $invalids[$key] = $value;
    }

    // Check for Invalid
    $validation = true;
    if ( !empty( $invalids ) )
    $validation = new WP_Error( 'invalid_shipping_address_values', __( "One or more of the entered Shipping Address fields are invalid.", "autoship" ) );

    $validation = apply_filters( 'autoship_scheduled_order_validate_shipping_address_required_posted_fields', $validation, $valids, $fields );

    if ( is_wp_error( $validation ) ){

      $messages = apply_filters( 'autoship_update_scheduled_order_shipping_address_invalid_notices', $validation->get_error_messages(), $invalids, $fields );

      // Add our general error notice
      foreach ( $messages as $code => $message)
      wc_add_notice( $message, 'error' );

      // Allow others to add on additional invalid notices
      do_action( 'autoship_update_scheduled_order_shipping_address_invalid_fields', $invalids, $fields );

      $success = false;

    } else {

      $success = autoship_update_scheduled_order_handler(
        $autoship_order_id,
        'autoship_update_order_shipping_address',
        array(  'shipping_address' => $valids,
                'original_autoship_order' => $original_autoship_order )
      );

    }

  }

  // If update failed include url action so form can be displayed.
  $url = is_wp_error( $success ) || !$success ?
  add_query_arg( 'action', 'edit-shipping-address', autoship_get_scheduled_order_url( $autoship_order_id ) ) : autoship_get_scheduled_order_url( $autoship_order_id );

  wp_redirect( $url );
  exit();

}