autoship_update_scheduled_order_h​andler (function)

(src/scheduled-orders.php) Main handler for updates to Scheduled Orders for the Native UI. It takes the (Scheduled) $order_id, the $action, and the (Scheduled Order) $data as its parameters. 

Function

 
/**
 * The main handler for form and endpoint Scheduled Order Updates.
 *
 * @param int $order_id       The Autoship Scheduled Order ID
 * @param string $action      A valid action to perform.
 * @param mixed $data         The Data necessary to perform that action.
 * @return int|bool|WP_Error  True if successful or WR_Error|false on failure.
 */
function autoship_update_scheduled_order_handler( $order_id, $action , $data = array() ) {

  $valid_actions = apply_filters( 'autoship_update_scheduled_order_handler_valid_actions', array(
    'autoship_add_order_coupon'             => true,
    'autoship_remove_order_coupon'          => true,
    'autoship_add_order_item'               => true,
    'autoship_remove_order_item'            => true,
    'autoship_update_order'                 => true,
    'autoship_delete_order'                 => true,
    'autoship_update_order_status'          => true,
    'autoship_update_order_schedule'        => true,
    'autoship_update_order_shipping_address'=> true,
    'autoship_update_schedule_items'        => true,
    'autoship_update_order_payment_method'  => true
  ), $order_id, $action , $data );

  // Rights for use to make the update.
  // Defaults to everyone can modify.
  if ( !autoship_rights_checker( 'autoship_update_scheduled_order_rights' , array() ) ||
       !autoship_rights_checker( "autoship_update_scheduled_order_{$action}_rights" , array() )  ){
    wc_add_notice( __( 'Insufficient rights to perform this action.', 'autoship' ), 'error' );
    return false;
  }

  // Check the supplied action is valid and enabled
  if ( !isset( $valid_actions[$action] ) || !$valid_actions[$action] ){
   wc_add_notice( __( 'The supplied Action is invalid.', 'autoship' ), 'error' );
   return false;
  }

  $scheduled_order_label = autoship_translate_text( 'Scheduled Order' );

  $default_msgs = apply_filters("default_{$action}_action_messages", array(
    'error' => __( "A problem was encountered while trying to update the {$scheduled_order_label}.", 'autoship' ),
    'success' => __("The {$scheduled_order_label} #%s was successfully updated.", 'autoship'),
  ) );

  // Allow adjustments to the data parameter to be made.
  $data = apply_filters( 'autoship_update_scheduled_order_handler_call_back_data', $data, $order_id, $action );

  do_action( 'autoship_before_update_scheduled_order_handler', $order_id, $action , $data );

  // run the actions specific handler
  $method = apply_filters( 'autoship_update_scheduled_order_' . $action . '_handler_call_back', $action . '_action_handler', $order_id, $action , $data );
  $result = function_exists( $method ) ? $method( $order_id, $data ) : new WP_Error( 'autoship_update_scheduled_order_handler_error', __("The {$scheduled_order_label} could not be updated since the supplied action could not be processed.", 'autoship' ) );

  // Check if error
  if ( is_wp_error( $result ) ){
    do_action( "autoship_after_{$action}_handler_failure", $order_id, $action , $data, $result );

    if ( 'User Message' == $result->get_error_code() ){
      wc_add_notice( __( $default_msgs['error'] . '<br/>' . $result->get_error_message(), 'autoship' ),  'error' );
      return false;
    }

    wc_add_notice( __( is_wp_error( $result ) ? $default_msgs['error'] . '<br/>Additional Details: ' . $result->get_error_message() : $default_msgs['error'], 'autoship' ),  'error' );
    return false;
  }

  if ( !$result ){
    do_action( "autoship_after_{$action}_handler_failure", $order_id, $action , $data, $result );
    return false;
  }

  do_action( "autoship_after_{$action}_handler_success", $order_id, $action , $data );

  wc_add_notice( __( sprintf( $default_msgs['success'], $order_id ) , 'autoship' ), 'success' );

  do_action( "autoship_after_handler_success", $order_id, $action , $data );

  return true;

}