/**
  * Get pre-flight status for a batch.
  *
  * Production will evaluate the batch and look for any issues that might
  * cause trouble when user later on deploys the batch.
  *
  * Display any pre-flight messages that is returned by production.
  *
  * Runs on content stage.
  */
 public function preflight_status()
 {
     // Get batch ID.
     $batch_id = isset($_POST['batch_id']) ? intval($_POST['batch_id']) : null;
     // Get batch GUID.
     $batch_guid = isset($_POST['batch_guid']) ? $_POST['batch_guid'] : null;
     $response = array('status' => 0, 'messages' => array());
     $response = apply_filters('sme_preflight_status', $response, $batch_id);
     // Get status from production.
     $prod_status = isset($response['status']) ? $response['status'] : 2;
     // Get production messages.
     $prod_messages = isset($response['messages']) ? $response['messages'] : array();
     // Get status from content stage.
     $stage_status = $this->api->get_preflight_status($batch_id);
     // Ensure no pre-flight status is not set to failed.
     $status = $prod_status != 2 && $stage_status != 2 ? $prod_status : 2;
     // Get content stage messages.
     $stage_messages = $this->api->get_preflight_messages($batch_id);
     // All pre-flight messages.
     $messages = array_merge($prod_messages, $stage_messages);
     // Set success message.
     if ($status == 3) {
         $message = new Message();
         $message->set_level('success');
         $message->set_message('Pre-flight successful!');
         $message->set_code(201);
         array_push($messages, $message);
     }
     // Prepare response.
     $response = array('status' => $status, 'messages' => $this->api->convert_messages($messages));
     header('Content-Type: application/json');
     echo json_encode($response);
     die;
     // Required to return a proper result.
 }
 /**
  * Convert message objects into arrays or from arrays into objects.
  *
  * @param array $messages
  * @param bool  $to_array
  *
  * @return array
  */
 public function convert_messages($messages, $to_array = true)
 {
     $result = array();
     if (!is_array($messages)) {
         return $result;
     }
     // Convert message objects into arrays.
     if ($to_array) {
         foreach ($messages as $message) {
             if ($message instanceof Message) {
                 array_push($result, $message->to_array());
             }
         }
         return $result;
     }
     // Convert messages into objects.
     foreach ($messages as $message) {
         $msg = new Message();
         $msg->set_id(isset($message['id']) ? $message['id'] : null);
         $msg->set_message(isset($message['message']) ? $message['message'] : '');
         $msg->set_level(isset($message['level']) ? $message['level'] : 'info');
         $msg->set_code(isset($message['code']) ? $message['code'] : 0);
         array_push($result, $msg);
     }
     return $result;
 }
 /**
  * @param array $raw
  *
  * @return Message
  */
 protected function do_create_object(array $raw)
 {
     if (!isset($raw['meta_id'])) {
         return null;
     }
     $obj = new Message($raw['meta_id']);
     if (isset($raw['post_id'])) {
         $obj->set_post_id($raw['post_id']);
     }
     if (!isset($raw['meta_value'])) {
         return $obj;
     }
     if (isset($raw['meta_value']['message'])) {
         $obj->set_message($raw['meta_value']['message']);
     }
     if (isset($raw['meta_value']['code'])) {
         $obj->set_code($raw['meta_value']['code']);
     }
     if (isset($raw['meta_value']['level'])) {
         $obj->set_level($raw['meta_value']['level']);
     }
     return $obj;
 }