/**
  * Receive the XML-RPC request, authenticate and return a response.
  * Response messages is collected from observing objects and returned as
  * the XML-RPC response data.
  *
  * @param array $args
  *
  * @return array
  */
 public function handle_request($args)
 {
     if (!isset($args[0])) {
         $message = new Message();
         $message->set_level('error');
         $message->set_message('No access token has been provided. Request failed.');
         $response = array('status' => 2, 'messages' => array($message));
         return $this->prepare_response($response);
     }
     if (!isset($args[1])) {
         $message = new Message();
         $message->set_level('error');
         $message->set_message('No data has been provided. Request failed.');
         $response = array('status' => 2, 'messages' => array($message));
         return $this->prepare_response($response);
     }
     $access_token = $args[0];
     $data = $args[1];
     /*
      * Check that a valid access token has been provided together with the
      * request.
      */
     if ($access_token !== $this->generate_access_token($data)) {
         // Invalid access token, construct an error message.
         $msg = 'Authentication failed. ';
         $msg .= sprintf('<strong>%s</strong> did not accept the provided access token. <br/>', $_SERVER['HTTP_HOST']);
         $msg .= 'Check that your content staging environment and your production environment is using the same secret key.';
         // Respond with error message.
         $message = new Message();
         $message->set_level('error');
         $message->set_message($msg);
         $response = array('status' => 2, 'messages' => array($message));
         return $this->prepare_response($response);
     }
     // Get the request data.
     $this->filtered_request = unserialize($this->decode($data));
 }
 /**
  * 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;
 }
 /**
  * Look for message indicating that a post has been deleted on production
  * and remove these posts from log of deleted posts.
  *
  * @param Message $message
  */
 private function handle_deleted_posts(Message $message)
 {
     $regex = '#<span class="hidden" data-blog-id="(.*?)">(.*?)</span>#';
     preg_match($regex, $message->get_message(), $groups);
     // Make sure groups has been set.
     if (empty($groups[1]) || empty($groups[2])) {
         return;
     }
     $blog_id = intval($groups[1]);
     $post_ids = array_map('intval', explode(',', $groups[2]));
     // Make sure blog ID and post IDs has been set.
     if (!$blog_id || empty($post_ids)) {
         return;
     }
     /**
      * @var Custom_DAO $custom_dao
      */
     $custom_dao = Helper_Factory::get_instance()->get_dao('Custom');
     $current_blog_id = get_current_blog_id();
     // Switch blog if we are not on the correct one.
     if ($current_blog_id !== $blog_id) {
         switch_to_blog($blog_id);
     }
     // Cleanup WP options.
     $custom_dao->remove_from_deleted_posts_log($post_ids);
     // Restore blog if we switched before.
     if ($current_blog_id !== $blog_id) {
         restore_current_blog();
     }
 }
 /**
  * Runs on production when an import request is received.
  *
  * @param array $args
  *
  * @return string
  */
 public function import(array $args)
 {
     $this->xmlrpc_client->handle_request($args);
     $result = $this->xmlrpc_client->get_request_data();
     // Get batch.
     $batch = isset($result['batch']) ? $result['batch'] : null;
     // Check if a batch has been provided.
     if (!$batch instanceof Batch) {
         $message = new Message();
         $message->set_level('error');
         $message->set_message('No batch has been sent from content stage to production.');
         $response = array('status' => 2, 'messages' => array($message));
         return $this->xmlrpc_client->prepare_response($response);
     }
     // Get production batch ID (if this is an existing batch).
     $batch_id = $this->batch_dao->get_id_by_guid($batch->get_guid());
     $batch->set_id($batch_id);
     if ($batch_id !== null) {
         $this->batch_dao->update_batch($batch);
     } else {
         $this->batch_dao->insert($batch);
     }
     // If auto import is set to true, then start the batch import immediately.
     if (!isset($result['auto_import']) || $result['auto_import']) {
         $this->api->import($batch);
     }
     $response = array('status' => $this->api->get_deploy_status($batch->get_id()), 'messages' => $this->api->get_deploy_messages($batch->get_id()));
     // Prepare and return the XML-RPC response data.
     return $this->xmlrpc_client->prepare_response($response);
 }
 /**
  * @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;
 }