foreach ($authors as $user) {
'name' => $user->display_name,
$author_options[] = $item;
'author' => $author_options,
'template' => $template_options,
'date' => $this->get_date_options($type),
$categories = get_categories(array('hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC'));
$tags = get_tags(array('hide_empty' => false));
$category_options = array();
foreach ($categories as $category) {
'id' => $category->term_id,
'name' => $category->name,
'parent' => $category->parent
$category_options[] = $item;
foreach ($tags as $tag) {
$response['category'] = $category_options;
$response['tag'] = $tag_options;
* Changes the state/status of the given post based from the submitted action/request
* @param int $id The ID of the current page to work on
* @param string $action The type of change that the current request is going to apply
* @param string $type The type of the module that the current request is processing
protected function apply_state($id, $action, $type = 'post') {
if (empty($id)) return false;
$previous_status = $post->post_status;
$args = array('ID' => $id, 'post_status' => 'draft');
$args = array('ID' => $id, 'post_status' => 'publish');
$args = array('ID' => $id, 'post_status' => 'pending');
$result = wp_delete_post($id, true);
if (!empty($result)) $deleted = true;
$postdata = $this->get_postdata($post);
if (!empty($postdata) || $deleted) {
$data = $deleted ? $id : $postdata;
'previous_status' => $previous_status
* Imports image from UpdraftCentral's page/post editor
* @param string $image_url The URL of the image to import
* @param string $image_data The image data to save. If empty, image_url will be used to download the image
* @param int $post_id The ID of the page where this image is to be attached
protected function attach_remote_image($image_url, $image_data, $post_id) {
if (empty($image_url) || empty($post_id)) return;
$image = pathinfo($image_url);
$image_name = $image['basename'];
$upload_dir = wp_upload_dir();
if (empty($image_data)) {
$response = wp_remote_get($image_url);
if (!is_wp_error($response)) {
$image_data = wp_remote_retrieve_body($response);
$image_data = base64_decode($image_data);
if (!empty($image_data)) {
$unique_file_name = wp_unique_filename($upload_dir['path'], $image_name);
$filename = basename($unique_file_name);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_status' => 'inherit'
$media_id = wp_insert_attachment($attachment, $file, $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($media_id, $file);
wp_update_attachment_metadata($media_id, $attach_data);
set_post_thumbnail($post_id, $media_id);
* Checks whether we have the required fields submitted and the user has
* the capabilities to execute the requested action
* @param array $capabilities The capabilities to check and validate
protected function _validate_capabilities($capabilities) {
foreach ($capabilities as $capability) {
if (!current_user_can($capability)) return $this->_generic_error_response('insufficient_permission');