/**
  * Initial loader.
  *
  * @access public
  *
  * @throws Exception If the UUID is invalid.
  *
  * @param Customize_Snapshot_Manager $snapshot_manager     Customize snapshot bootstrap instance.
  * @param string                     $uuid                 Snapshot unique identifier.
  */
 public function __construct(Customize_Snapshot_Manager $snapshot_manager, $uuid)
 {
     $this->snapshot_manager = $snapshot_manager;
     $this->data = array();
     if (!Customize_Snapshot_Manager::is_valid_uuid($uuid)) {
         throw new Exception(__('You\'ve entered an invalid snapshot UUID.', 'customize-snapshots'));
     }
     $this->uuid = $uuid;
     $post = $this->post();
     if ($post) {
         $this->data = $this->snapshot_manager->post_type->get_post_content($post);
     }
 }
 /**
  * Testing passing Customize save for a user who has customize_publish capability.
  */
 function test_ajax_customize_save_passing_customize_publish()
 {
     $this->set_current_user('administrator');
     $this->plugin->customize_snapshot_manager->customize_manager->setup_theme();
     $this->add_setting();
     $snapshot_uuid = $this->plugin->customize_snapshot_manager->current_snapshot_uuid;
     $snapshot_post_id = $this->plugin->customize_snapshot_manager->post_type->find_post($snapshot_uuid);
     $this->assertNull($snapshot_post_id);
     // Get the results.
     $this->make_ajax_call('customize_save');
     $response = json_decode($this->_last_response, true);
     $this->assertTrue($response['success']);
     if (method_exists('WP_Customize_Manager', 'prepare_setting_validity_for_js')) {
         $this->assertArrayHasKey('setting_validities', $response['data']);
         $this->assertArrayHasKey('anyonecanedit', $response['data']['setting_validities']);
         $this->assertTrue($response['data']['setting_validities']['anyonecanedit']);
     }
     $this->assertArrayHasKey('new_customize_snapshot_uuid', $response['data']);
     $this->assertTrue(Customize_Snapshot_Manager::is_valid_uuid($response['data']['new_customize_snapshot_uuid']));
     $snapshot_post_id = $this->plugin->customize_snapshot_manager->post_type->find_post($snapshot_uuid);
     $this->assertNotNull($snapshot_post_id);
     $snapshot_post = get_post($snapshot_post_id);
     $this->assertSame($this->plugin->customize_snapshot_manager->customize_manager->unsanitized_post_values(), wp_list_pluck(json_decode($snapshot_post->post_content, true), 'value'));
 }
 /**
  * Persist the data in the snapshot post content.
  *
  * @param array $args Args.
  * @return int|\WP_Error Post ID for snapshot or WP_Error instance.
  */
 public function save(array $args)
 {
     // @todo Add support for $args['post_id'].
     if (empty($args['uuid']) || !Customize_Snapshot_Manager::is_valid_uuid($args['uuid'])) {
         return new \WP_Error('missing_valid_uuid');
     }
     $post_arr = array('post_name' => $args['uuid'], 'post_title' => $args['uuid'], 'post_type' => static::SLUG, 'meta_input' => array('_snapshot_version' => $this->snapshot_manager->plugin->version));
     if (!empty($args['status'])) {
         if (isset($args['post_date'], $args['edit_date'], $args['post_date_gmt'])) {
             $post_arr['post_date'] = $args['post_date'];
             $post_arr['edit_date'] = $args['edit_date'];
             $post_arr['post_date_gmt'] = $args['post_date_gmt'];
         }
         if (!get_post_status_object($args['status'])) {
             return new \WP_Error('bad_status');
         }
         $post_arr['post_status'] = $args['status'];
     }
     $post_id = $this->find_post($args['uuid']);
     $is_update = !empty($post_id);
     if ($post_id) {
         $post_arr['ID'] = $post_id;
     }
     if (isset($args['data'])) {
         if (!is_array($args['data'])) {
             return new \WP_Error('missing_data');
         }
         foreach ($args['data'] as $setting_id => $setting_params) {
             if (!is_array($setting_params)) {
                 return new \WP_Error('bad_setting_params');
             }
             if (!array_key_exists('value', $setting_params)) {
                 return new \WP_Error('missing_value_param');
             }
         }
         $post_arr['post_content'] = Customize_Snapshot_Manager::encode_json($args['data']);
     } elseif (!$is_update) {
         $post_arr['post_content'] = Customize_Snapshot_Manager::encode_json(array());
     }
     if (!empty($args['theme'])) {
         $post_arr['meta_input']['_snapshot_theme'] = $args['theme'];
     }
     if (!empty($args['author'])) {
         $post_arr['post_author'] = $args['author'];
     }
     if (!empty($args['date_gmt'])) {
         $post_arr['post_date_gmt'] = $args['date_gmt'];
     }
     $this->suspend_kses();
     if ($is_update) {
         $r = wp_update_post(wp_slash($post_arr), true);
     } else {
         $r = wp_insert_post(wp_slash($post_arr), true);
     }
     $this->restore_kses();
     return $r;
 }