Beispiel #1
0
/**
 * Get post option value from the database
 *
 * @param null|int $post_id
 * @param string|null $option_id Specific option id (accepts multikey). null - all options
 * @param null|mixed $default_value If no option found in the database, this value will be returned
 * @param null|bool $get_original_value Original value is that with no translations and other changes
 *
 * @return mixed|null
 */
function fw_get_db_post_option($post_id = null, $option_id = null, $default_value = null, $get_original_value = null)
{
    if (!$post_id) {
        /** @var WP_Post $post */
        global $post;
        if (!$post) {
            return $default_value;
        } else {
            $post_id = $post->ID;
        }
        /**
         * Check if is Preview and use the preview post_id instead of real/current post id
         *
         * Note: WordPress changes the global $post content on preview:
         * 1. https://github.com/WordPress/WordPress/blob/2096b451c704715db3c4faf699a1184260deade9/wp-includes/query.php#L3573-L3583
         * 2. https://github.com/WordPress/WordPress/blob/4a31dd6fe8b774d56f901a29e72dcf9523e9ce85/wp-includes/revision.php#L485-L528
         */
        if (is_preview()) {
            $preview = wp_get_post_autosave($post->ID);
            if (is_object($preview)) {
                $post_id = $preview->ID;
            }
        }
    }
    $option_id = 'fw_options' . ($option_id !== null ? '/' . $option_id : '');
    return FW_WP_Meta::get('post', $post_id, $option_id, $default_value, $get_original_value);
}
Beispiel #2
0
function acf_filter_post_id($post_id)
{
    // set post_id to global
    if (!$post_id) {
        global $post;
        if ($post) {
            $post_id = $post->ID;
        }
    }
    // allow for option == options
    if ($post_id == "option") {
        $post_id = "options";
    }
    /*
     *  Override for preview
     *  
     *  If the $_GET['preview_id'] is set, then the user wants to see the preview data.
     *  There is also the case of previewing a page with post_id = 1, but using get_field
     *  to load data from another post_id.
     *  In this case, we need to make sure that the autosave revision is actually related
     *  to the $post_id variable. If they match, then the autosave data will be used, otherwise, 
     *  the user wants to load data from a completely different post_id
     */
    if (isset($_GET['preview_id'])) {
        $autosave = wp_get_post_autosave($_GET['preview_id']);
        if ($autosave->post_parent == $post_id) {
            $post_id = $autosave->ID;
        }
    }
    // return
    return $post_id;
}
 function post_revisions()
 {
     global $post, $current_user, $role;
     if ($this->WikiHelper->is_wiki('front_end_check')) {
         $wpw_options = get_option('wpw_options');
         $revisions = get_posts('post_status=any&post_type=revision&post_parent=' . $post->ID . '&numberposts=' . $wpw_options['number_of_revisions']);
         //Most recent revision
         $date = date(__('m/d/y g:i a'), mktime($post->post_modified));
         $author = $this->WikiHelper->get_author($post);
         $latest_revision = sprintf(__('Latest revision (@ %1s by %2s)'), $post->post_modified, $author);
         $output = '<a href="' . get_permalink($post->ID) . '">' . $latest_revision . '</a><br />';
         //If we have revisions...
         if ($revisions) {
             //Loop through them!
             $count = 0;
             foreach ($revisions as $revision) {
                 if (@wp_get_post_autosave($post->ID)->ID != $revision->ID) {
                     $author = $this->WikiHelper->get_author($revision);
                     $date = date(__('m/d/y g:i a'), mktime($revision->post_modified));
                     $revision_title = sprintf(__('Revision @ %1s by %2s'), $date, $author);
                     $output .= '<a href="' . get_permalink($post->ID) . '?revision=' . $revision->ID . '">' . $revision_title . '</a><br />';
                     $count++;
                 }
             }
         }
         return $output;
     }
 }
 /**
  * Request handler for XML.
  */
 public function request_handler()
 {
     if (get_query_var('xml')) {
         // Sanitize our article ID
         $id = get_the_ID();
         // If we don't have an Article, get out
         if (empty($id)) {
             wp_die(__('No article found.', $this->i18n));
         }
         // If we're not debugging, turn off errors
         if (!$this->debug) {
             $display_errors = ini_get('display_errors');
             ini_set('display_errors', 0);
         }
         // Default Article
         $article = null;
         // If we're published, grab the published article
         if (!is_preview()) {
             $article = get_post($id);
         } else {
             if (is_preview() && current_user_can('edit_post', $id)) {
                 $article = get_post($id);
                 /* Drafts and sometimes pending statuses append a preview_id on the 
                 			end of the preview URL.  While we're building the XML download link
                 			we do a check for that, and set this query arg if the preview_id is
                 			present. */
                 if (isset($_GET['autosave'])) {
                     $article = wp_get_post_autosave($id);
                 }
             }
         }
         // Ensure we have an article
         if (empty($article)) {
             wp_die(__('Required article first.', $this->i18n));
         }
         // Get our XML ready and stored
         $this->generate_xml($article);
         // Send our headers
         if (!$_GET['screen']) {
             $this->set_headers($article);
         }
         // Send the file
         echo $this->xml;
         exit;
     }
 }
 public function &get($postID = false, $type = "post")
 {
     if (!$postID) {
         global $post;
         if ($post) {
             $postID = $post->ID;
             $type = $post->post_type;
         } else {
             return $this->empty;
         }
     }
     if (isset($this->cache[$postID])) {
         return $this->cache[$postID];
     }
     $db = false;
     if ($postID) {
         $metaID = $postID;
         if (!empty($this->preview) && $postID == $this->preview) {
             // preview mode so we use meta stored in the autosave
             $preview = wp_get_post_autosave($postID);
             if (!empty($preview->ID)) {
                 $metaID = $preview->ID;
             }
         }
         $db = maybe_unserialize(get_post_meta($metaID, PE_THEME_META, true));
     }
     if (!isset($this->defaults[$type])) {
         $this->getDefaultValues($type);
     }
     if ($db) {
         $cache = new stdClass();
         $empty = array();
         foreach ($this->keys[$type] as $key) {
             if (!isset($db->{$key})) {
                 $db->{$key} = array();
             }
             $cache->{$key} = (object) array_merge((array) $this->defaults[$type]->{$key}, (array) $db->{$key});
         }
     } else {
         $cache =& $this->defaults[$type];
     }
     if ($postID) {
         $this->cache[$postID] =& $cache;
     }
     return $cache;
 }
Beispiel #6
0
 /**
  * @param array $posts
  * @return array
  */
 public static function maybe_set_preview($posts)
 {
     if (is_array($posts) && isset($_GET['preview']) && $_GET['preview'] && isset($_GET['preview_id']) && $_GET['preview_id'] && current_user_can('edit_post', $_GET['preview_id'])) {
         // No need to check the nonce, that already happened in _show_post_preview on init
         $preview_id = $_GET['preview_id'];
         foreach ($posts as &$post) {
             if (is_object($post) && $post->ID == $preview_id) {
                 // Based on _set_preview( $post ), but adds import_custom
                 $preview = wp_get_post_autosave($preview_id);
                 if (is_object($preview)) {
                     $preview = sanitize_post($preview);
                     $post->post_content = $preview->post_content;
                     $post->post_title = $preview->post_title;
                     $post->post_excerpt = $preview->post_excerpt;
                     $post->import_custom($preview_id);
                     add_filter('get_the_terms', '_wp_preview_terms_filter', 10, 3);
                 }
             }
         }
     }
     return $posts;
 }
 /**
  * Update an autosave for a post.
  *
  * @param WP_REST_Request $request Full details about the request.
  * @return WP_Error|WP_REST_Response
  */
 public function update_item($request)
 {
     $parent = get_post($request['id']);
     $autosave = wp_get_post_autosave($parent->ID, get_current_user_id());
     $post_data = (array) $this->prepare_item_for_database($request);
     if (!$autosave) {
         $autosave_id = _wp_put_post_revision($post_data, true);
     } else {
         $post_data['ID'] = $autosave->ID;
         /**
          * Fires before an autosave is stored.
          *
          * @since 4.1.0
          *
          * @param array $new_autosave Post array - the autosave that is about to be saved.
          */
         do_action('wp_creating_autosave', $post_data);
         wp_update_post($post_data);
         $autosave_id = $autosave->ID;
     }
     return $this->prepare_item_for_response(get_post($autosave_id), $request);
 }
 function callback($path = '', $blog_id = 0, $post_id = 0)
 {
     $blog_id = $this->api->switch_to_blog_and_validate_user($this->api->get_blog_id($blog_id));
     if (is_wp_error($blog_id)) {
         return $blog_id;
     }
     $post = get_post($post_id);
     if (!$post || is_wp_error($post)) {
         return new WP_Error('unknown_post', 'Unknown post', 404);
     }
     if (!current_user_can('edit_post', $post->ID)) {
         return new WP_Error('unauthorized', 'User cannot edit post', 403);
     }
     $autosave = wp_get_post_autosave($post->ID);
     if ($autosave) {
         $preview_url = add_query_arg('preview', 'true', get_permalink($post->ID));
         $nonce = wp_create_nonce('post_preview_' . $post->ID);
         $preview_url = add_query_arg(array('preview_id' => $auto_ID, 'preview_nonce' => $nonce), $preview_url);
         return array('ID' => $autosave->ID, 'author_ID' => $autosave->post_author, 'post_ID' => $autosave->post_parent, 'title' => $autosave->post_title, 'content' => $autosave->post_content, 'excerpt' => $autosave->post_excerpt, 'preview_URL' => $preview_url, 'modified' => $this->format_date($autosave->post_modified));
     } else {
         return new WP_Error('not_found', 'No autosaves exist for this post', 404);
     }
 }
Beispiel #9
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses _wp_translate_postdata()
 * @uses _wp_post_revision_fields()
 *
 * @return unknown
 */
function wp_create_post_autosave($post_id)
{
    $translated = _wp_translate_postdata(true);
    if (is_wp_error($translated)) {
        return $translated;
    }
    // Only store one autosave. If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id)) {
        $new_autosave = _wp_post_revision_fields($_POST, true);
        $new_autosave['ID'] = $old_autosave->ID;
        $new_autosave['post_author'] = get_current_user_id();
        return wp_update_post($new_autosave);
    }
    // _wp_put_post_revision() expects unescaped.
    $_POST = stripslashes_deep($_POST);
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($_POST, true);
}
Beispiel #10
0
/**
 * Get post option value from the database
 *
 * @param null|int $post_id
 * @param string|null $option_id Specific option id (accepts multikey). null - all options
 * @param null|mixed $default_value If no option found in the database, this value will be returned
 * @param null|bool $get_original_value Original value is that with no translations and other changes
 *
 * @return mixed|null
 */
function fw_get_db_post_option($post_id = null, $option_id = null, $default_value = null, $get_original_value = null)
{
    if (!$post_id) {
        /** @var WP_Post $post */
        global $post;
        if (!$post) {
            return $default_value;
        } else {
            $post_id = $post->ID;
        }
        /**
         * Check if is Preview and use the preview post_id instead of real/current post id
         *
         * Note: WordPress changes the global $post content on preview:
         * 1. https://github.com/WordPress/WordPress/blob/2096b451c704715db3c4faf699a1184260deade9/wp-includes/query.php#L3573-L3583
         * 2. https://github.com/WordPress/WordPress/blob/4a31dd6fe8b774d56f901a29e72dcf9523e9ce85/wp-includes/revision.php#L485-L528
         */
        if (is_preview() && is_object($preview = wp_get_post_autosave($post->ID))) {
            $post_id = $preview->ID;
        }
    }
    $post_type = get_post_type(($post_revision_id = wp_is_post_revision($post_id)) ? $post_revision_id : $post_id);
    /**
     * Before fw_db_option_storage_load() feature
     * there was possible to call fw_get_db_post_option() and it worked fine
     * but after v2.5.0 it's not possible anymore (it creates an infinite recursion)
     * but the Slider extension does that and maybe other extensions,
     * so the solution is to check if it is recursion, to not load the options array (disable the storage feature)
     */
    static $recursion = array();
    if (!isset($recursion[$post_type])) {
        $recursion[$post_type] = false;
    }
    if ($recursion[$post_type]) {
        /**
         * Allow known post types that sure don't have options with 'fw-storage' parameter
         */
        if (!in_array($post_type, array('fw-slider'))) {
            trigger_error('Infinite recursion detected in post type "' . $post_type . '" options caused by ' . __FUNCTION__ . '()', E_USER_WARNING);
        }
        $options = array();
    } else {
        $recursion[$post_type] = true;
        $options = fw_extract_only_options(fw()->theme->get_post_options($post_type));
        $recursion[$post_type] = false;
    }
    if ($option_id) {
        $option_id = explode('/', $option_id);
        // 'option_id/sub/keys'
        $_option_id = array_shift($option_id);
        // 'option_id'
        $sub_keys = implode('/', $option_id);
        // 'sub/keys'
        $option_id = $_option_id;
        unset($_option_id);
        $value = FW_WP_Meta::get('post', $post_id, 'fw_options/' . $option_id, null, $get_original_value);
        if (isset($options[$option_id])) {
            $value = fw()->backend->option_type($options[$option_id]['type'])->storage_load($option_id, $options[$option_id], $value, array('post-id' => $post_id));
        }
        if ($sub_keys) {
            return fw_akg($sub_keys, $value, $default_value);
        } else {
            return is_null($value) ? $default_value : $value;
        }
    } else {
        $value = FW_WP_Meta::get('post', $post_id, 'fw_options', $default_value, $get_original_value);
        if (!is_array($value)) {
            $value = array();
        }
        foreach ($options as $_option_id => $_option) {
            $value[$_option_id] = fw()->backend->option_type($_option['type'])->storage_load($_option_id, $_option, isset($value[$_option_id]) ? $value[$_option_id] : null, array('post-id' => $post_id));
        }
        return $value;
    }
}
Beispiel #11
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses _wp_translate_postdata()
 * @uses _wp_post_revision_fields()
 */
function wp_create_post_autosave($post_id)
{
    $translated = _wp_translate_postdata(true);
    if (is_wp_error($translated)) {
        return $translated;
    }
    // Only store one autosave.  If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id)) {
        $new_autosave = _wp_post_revision_fields($_POST, true);
        $new_autosave['ID'] = $old_autosave->ID;
        return wp_update_post($new_autosave);
    }
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($_POST, true);
}
Beispiel #12
0
 function get_post_id($post_id)
 {
     // if not $post_id, load queried object
     if (!$post_id) {
         // try for global post (needed for setup_postdata)
         $post_id = (int) get_the_ID();
         // try for current screen
         if (!$post_id) {
             $post_id = get_queried_object();
         }
     }
     // $post_id may be an object
     if (is_object($post_id)) {
         // user
         if (isset($post_id->roles, $post_id->ID)) {
             $post_id = 'user_' . $post_id->ID;
             // term
         } elseif (isset($post_id->taxonomy, $post_id->term_id)) {
             $post_id = $post_id->taxonomy . '_' . $post_id->term_id;
             // comment
         } elseif (isset($post_id->comment_ID)) {
             $post_id = 'comment_' . $post_id->comment_ID;
             // post
         } elseif (isset($post_id->ID)) {
             $post_id = $post_id->ID;
             // default
         } else {
             $post_id = 0;
         }
     }
     // allow for option == options
     if ($post_id === 'option') {
         $post_id = 'options';
     }
     /*
      *  Override for preview
      *  
      *  If the $_GET['preview_id'] is set, then the user wants to see the preview data.
      *  There is also the case of previewing a page with post_id = 1, but using get_field
      *  to load data from another post_id.
      *  In this case, we need to make sure that the autosave revision is actually related
      *  to the $post_id variable. If they match, then the autosave data will be used, otherwise, 
      *  the user wants to load data from a completely different post_id
      */
     if (isset($_GET['preview_id'])) {
         $autosave = wp_get_post_autosave($_GET['preview_id']);
         if ($autosave && $autosave->post_parent == $post_id) {
             $post_id = (int) $autosave->ID;
         }
     }
     // return
     return $post_id;
 }
 /**
  * Get a post by a specified field and value
  *
  * @param string $field
  * @param string $field_value
  * @param string $context Post use context (e.g. 'display')
  * @return array Post
  **/
 function get_post_by($field, $field_value, $context = 'display')
 {
     global $blog_id;
     /** This filter is documented in class.json-api-endpoints.php */
     $is_jetpack = true === apply_filters('is_jetpack_site', false, $blog_id);
     if (defined('GEO_LOCATION__CLASS') && class_exists(GEO_LOCATION__CLASS)) {
         $geo = call_user_func(array(GEO_LOCATION__CLASS, 'init'));
     } else {
         $geo = false;
     }
     if ('display' === $context) {
         $args = $this->query_args();
         if (isset($args['content_width']) && $args['content_width']) {
             $GLOBALS['content_width'] = (int) $args['content_width'];
         }
     }
     if (strpos($_SERVER['HTTP_USER_AGENT'], 'wp-windows8')) {
         remove_shortcode('gallery', 'gallery_shortcode');
         add_shortcode('gallery', array(&$this, 'win8_gallery_shortcode'));
     }
     switch ($field) {
         case 'name':
             $post_id = $this->get_post_id_by_name($field_value);
             if (is_wp_error($post_id)) {
                 return $post_id;
             }
             break;
         default:
             $post_id = (int) $field_value;
             break;
     }
     $post = get_post($post_id, OBJECT, $context);
     if (!$post || is_wp_error($post)) {
         return new WP_Error('unknown_post', 'Unknown post', 404);
     }
     if (!$this->is_post_type_allowed($post->post_type) && (!function_exists('is_post_freshly_pressed') || !is_post_freshly_pressed($post->ID))) {
         return new WP_Error('unknown_post', 'Unknown post', 404);
     }
     // Permissions
     $capabilities = $this->get_current_user_capabilities($post);
     switch ($context) {
         case 'edit':
             if (!$capabilities['edit_post']) {
                 return new WP_Error('unauthorized', 'User cannot edit post', 403);
             }
             break;
         case 'display':
             break;
         default:
             return new WP_Error('invalid_context', 'Invalid API CONTEXT', 400);
     }
     $can_view = $this->user_can_view_post($post->ID);
     if (!$can_view || is_wp_error($can_view)) {
         return $can_view;
     }
     $GLOBALS['post'] = $post;
     if ('display' === $context) {
         setup_postdata($post);
     }
     $response = array();
     foreach (array_keys($this->post_object_format) as $key) {
         switch ($key) {
             case 'ID':
                 // explicitly cast all output
                 $response[$key] = (int) $post->ID;
                 break;
             case 'site_ID':
                 $response[$key] = (int) $this->api->get_blog_id_for_output();
                 break;
             case 'author':
                 $response[$key] = (object) $this->get_author($post, 'edit' === $context && $capabilities['edit_post']);
                 break;
             case 'date':
                 $response[$key] = (string) $this->format_date($post->post_date_gmt, $post->post_date);
                 break;
             case 'modified':
                 $response[$key] = (string) $this->format_date($post->post_modified_gmt, $post->post_modified);
                 break;
             case 'title':
                 if ('display' === $context) {
                     $response[$key] = (string) get_the_title($post->ID);
                 } else {
                     $response[$key] = (string) htmlspecialchars_decode($post->post_title, ENT_QUOTES);
                 }
                 break;
             case 'URL':
                 if ('revision' === $post->post_type) {
                     $response[$key] = (string) esc_url_raw(get_permalink($post->post_parent));
                 } else {
                     $response[$key] = (string) esc_url_raw(get_permalink($post->ID));
                 }
                 break;
             case 'short_URL':
                 $response[$key] = (string) esc_url_raw(wp_get_shortlink($post->ID));
                 break;
             case 'content':
                 if ('display' === $context) {
                     add_filter('the_password_form', array($this, 'the_password_form'));
                     $response[$key] = (string) $this->get_the_post_content_for_display();
                     remove_filter('the_password_form', array($this, 'the_password_form'));
                 } else {
                     $response[$key] = (string) $post->post_content;
                 }
                 break;
             case 'excerpt':
                 if ('display' === $context) {
                     add_filter('the_password_form', array($this, 'the_password_form'));
                     ob_start();
                     the_excerpt();
                     $response[$key] = (string) ob_get_clean();
                     remove_filter('the_password_form', array($this, 'the_password_form'));
                 } else {
                     $response[$key] = htmlspecialchars_decode((string) $post->post_excerpt, ENT_QUOTES);
                 }
                 break;
             case 'status':
                 $response[$key] = (string) get_post_status($post->ID);
                 break;
             case 'sticky':
                 $response[$key] = (bool) is_sticky($post->ID);
                 break;
             case 'slug':
                 $response[$key] = (string) $post->post_name;
                 break;
             case 'guid':
                 $response[$key] = (string) $post->guid;
                 break;
             case 'password':
                 $response[$key] = (string) $post->post_password;
                 if ('edit' === $context) {
                     $response[$key] = htmlspecialchars_decode((string) $response[$key], ENT_QUOTES);
                 }
                 break;
             case 'parent':
                 // (object|false)
                 if ($post->post_parent) {
                     $parent = get_post($post->post_parent);
                     if ('display' === $context) {
                         $parent_title = (string) get_the_title($parent->ID);
                     } else {
                         $parent_title = (string) htmlspecialchars_decode($post->post_title, ENT_QUOTES);
                     }
                     $response[$key] = (object) array('ID' => (int) $parent->ID, 'type' => (string) $parent->post_type, 'link' => (string) $this->get_post_link($this->api->get_blog_id_for_output(), $parent->ID), 'title' => $parent_title);
                 } else {
                     $response[$key] = false;
                 }
                 break;
             case 'type':
                 $response[$key] = (string) $post->post_type;
                 break;
             case 'discussion':
                 $response[$key] = array('comments_open' => (bool) comments_open($post->ID), 'comment_status' => (string) $post->comment_status, 'pings_open' => (bool) pings_open($post->ID), 'ping_status' => (string) $post->ping_status, 'comment_count' => (int) $post->comment_count);
                 break;
             case 'likes_enabled':
                 /** This filter is documented in modules/likes.php */
                 $sitewide_likes_enabled = (bool) apply_filters('wpl_is_enabled_sitewide', !get_option('disabled_likes'));
                 $post_likes_switched = (bool) get_post_meta($post->ID, 'switch_like_status', true);
                 $post_likes_enabled = $sitewide_likes_enabled;
                 if ($post_likes_switched) {
                     $post_likes_enabled = !$post_likes_enabled;
                 }
                 $response[$key] = (bool) $post_likes_enabled;
                 break;
             case 'sharing_enabled':
                 $show = true;
                 /** This filter is documented in modules/sharedaddy/sharing-service.php */
                 $show = apply_filters('sharing_show', $show, $post);
                 $switched_status = get_post_meta($post->ID, 'sharing_disabled', false);
                 if (!empty($switched_status)) {
                     $show = false;
                 }
                 $response[$key] = (bool) $show;
                 break;
             case 'like_count':
                 $response[$key] = (int) $this->api->post_like_count($blog_id, $post->ID);
                 break;
             case 'i_like':
                 $response[$key] = (int) $this->api->is_liked($blog_id, $post->ID);
                 break;
             case 'is_reblogged':
                 $response[$key] = (int) $this->api->is_reblogged($blog_id, $post->ID);
                 break;
             case 'is_following':
                 $response[$key] = (int) $this->api->is_following($blog_id);
                 break;
             case 'global_ID':
                 $response[$key] = (string) $this->api->add_global_ID($blog_id, $post->ID);
                 break;
             case 'featured_image':
                 if ($is_jetpack && (defined('IS_WPCOM') && IS_WPCOM)) {
                     $response[$key] = get_post_meta($post->ID, '_jetpack_featured_image', true);
                 } else {
                     $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
                     if (is_array($image_attributes) && isset($image_attributes[0])) {
                         $response[$key] = (string) $image_attributes[0];
                     } else {
                         $response[$key] = '';
                     }
                 }
                 break;
             case 'post_thumbnail':
                 $response[$key] = null;
                 $thumb_id = get_post_thumbnail_id($post->ID);
                 if (!empty($thumb_id)) {
                     $attachment = get_post($thumb_id);
                     if (!empty($attachment)) {
                         $featured_image_object = $this->get_attachment($attachment);
                     }
                     if (!empty($featured_image_object)) {
                         $response[$key] = (object) $featured_image_object;
                     }
                 }
                 break;
             case 'format':
                 $response[$key] = (string) get_post_format($post->ID);
                 if (!$response[$key]) {
                     $response[$key] = 'standard';
                 }
                 break;
             case 'geo':
                 // (object|false)
                 if (!$geo) {
                     $response[$key] = false;
                 } else {
                     $geo_data = $geo->get_geo('post', $post->ID);
                     $response[$key] = false;
                     if ($geo_data) {
                         $geo_data = array_intersect_key($geo_data, array('latitude' => true, 'longitude' => true, 'address' => true, 'public' => true));
                         if ($geo_data) {
                             $response[$key] = (object) array('latitude' => isset($geo_data['latitude']) ? (double) $geo_data['latitude'] : 0, 'longitude' => isset($geo_data['longitude']) ? (double) $geo_data['longitude'] : 0, 'address' => isset($geo_data['address']) ? (string) $geo_data['address'] : '');
                         } else {
                             $response[$key] = false;
                         }
                         // Private
                         if (!isset($geo_data['public']) || !$geo_data['public']) {
                             if ('edit' !== $context || !$capabilities['edit_post']) {
                                 // user can't access
                                 $response[$key] = false;
                             }
                         }
                     }
                 }
                 break;
             case 'menu_order':
                 $response[$key] = (int) $post->menu_order;
                 break;
             case 'page_template':
                 $response[$key] = (string) get_post_meta($post->ID, '_wp_page_template', true);
                 break;
             case 'publicize_URLs':
                 $publicize_URLs = array();
                 $publicize = get_post_meta($post->ID, 'publicize_results', true);
                 if ($publicize) {
                     foreach ($publicize as $service => $data) {
                         switch ($service) {
                             case 'twitter':
                                 foreach ($data as $datum) {
                                     $publicize_URLs[] = esc_url_raw("https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}");
                                 }
                                 break;
                             case 'fb':
                                 foreach ($data as $datum) {
                                     $publicize_URLs[] = esc_url_raw("https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}");
                                 }
                                 break;
                         }
                     }
                 }
                 $response[$key] = (array) $publicize_URLs;
                 break;
             case 'tags':
                 $response[$key] = array();
                 $terms = wp_get_post_tags($post->ID);
                 foreach ($terms as $term) {
                     if (!empty($term->name)) {
                         $response[$key][$term->name] = $this->format_taxonomy($term, 'post_tag', 'display');
                     }
                 }
                 $response[$key] = (object) $response[$key];
                 break;
             case 'categories':
                 $response[$key] = array();
                 $terms = wp_get_object_terms($post->ID, 'category', array('fields' => 'all'));
                 foreach ($terms as $term) {
                     if (!empty($term->name)) {
                         $response[$key][$term->name] = $this->format_taxonomy($term, 'category', 'display');
                     }
                 }
                 $response[$key] = (object) $response[$key];
                 break;
             case 'attachments':
                 $response[$key] = array();
                 $_attachments = new WP_Query(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => '20'));
                 foreach ($_attachments->posts as $attachment) {
                     $response[$key][$attachment->ID] = $this->get_media_item_v1_1($attachment->ID);
                 }
                 $response['attachment_count'] = $_attachments->found_posts;
                 $response[$key] = (object) $response[$key];
                 break;
             case 'metadata':
                 // (array|false)
                 $metadata = array();
                 foreach ((array) has_meta($post_id) as $meta) {
                     // Don't expose protected fields.
                     $show = false;
                     if ($this->is_metadata_public($meta['meta_key'])) {
                         $show = true;
                     }
                     if (current_user_can('edit_post_meta', $post_id, $meta['meta_key'])) {
                         $show = true;
                     }
                     if (!$show) {
                         continue;
                     }
                     $metadata[] = array('id' => $meta['meta_id'], 'key' => $meta['meta_key'], 'value' => maybe_unserialize($meta['meta_value']));
                 }
                 if (!empty($metadata)) {
                     $response[$key] = $metadata;
                 } else {
                     $response[$key] = false;
                 }
                 break;
             case 'meta':
                 $response[$key] = (object) array('links' => (object) array('self' => (string) $this->get_post_link($this->api->get_blog_id_for_output(), $post->ID), 'help' => (string) $this->get_post_link($this->api->get_blog_id_for_output(), $post->ID, 'help'), 'site' => (string) $this->get_site_link($this->api->get_blog_id_for_output()), 'replies' => (string) $this->get_post_link($this->api->get_blog_id_for_output(), $post->ID, 'replies/'), 'likes' => (string) $this->get_post_link($this->api->get_blog_id_for_output(), $post->ID, 'likes/')));
                 // add autosave link if a more recent autosave exists
                 if ('edit' === $context) {
                     $autosave = wp_get_post_autosave($post_id);
                     if ($autosave && $autosave->post_modified > $post->post_modified) {
                         $response[$key]->links->autosave = (string) $this->get_post_link($this->api->get_blog_id_for_output(), $post->ID) . '/autosave';
                     }
                 }
                 break;
             case 'capabilities':
                 $response[$key] = $capabilities;
                 break;
             case 'other_URLs':
                 $other_urls = array();
                 if ('publish' !== $post->post_status) {
                     $other_urls = $this->get_post_permalink_suggestions($post->ID, $post->post_title);
                 }
                 $response[$key] = (object) $other_urls;
                 break;
         }
     }
     // WPCOM_JSON_API_Post_Endpoint::find_featured_worthy_media( $post );
     // $response['featured_media'] = self::find_featured_media( $response );
     unset($GLOBALS['post']);
     return $response;
 }
Beispiel #14
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @param mixed $post_data Associative array containing the post data or int post ID.
 * @return mixed The autosave revision ID. WP_Error or 0 on error.
 */
function wp_create_post_autosave($post_data)
{
    if (is_numeric($post_data)) {
        $post_id = $post_data;
        $post_data = $_POST;
    } else {
        $post_id = (int) $post_data['post_ID'];
    }
    $post_data = _wp_translate_postdata(true, $post_data);
    if (is_wp_error($post_data)) {
        return $post_data;
    }
    $post_author = get_current_user_id();
    // Store one autosave per author. If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id, $post_author)) {
        $new_autosave = _wp_post_revision_data($post_data, true);
        $new_autosave['ID'] = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;
        // If the new autosave has the same content as the post, delete the autosave.
        $post = get_post($post_id);
        $autosave_is_different = false;
        foreach (array_intersect(array_keys($new_autosave), array_keys(_wp_post_revision_fields($post))) as $field) {
            if (normalize_whitespace($new_autosave[$field]) != normalize_whitespace($post->{$field})) {
                $autosave_is_different = true;
                break;
            }
        }
        if (!$autosave_is_different) {
            wp_delete_post_revision($old_autosave->ID);
            return 0;
        }
        /**
         * Fires before an autosave is stored.
         *
         * @since 4.1.0
         *
         * @param array $new_autosave Post array - the autosave that is about to be saved.
         */
        do_action('wp_creating_autosave', $new_autosave);
        return wp_update_post($new_autosave);
    }
    // _wp_put_post_revision() expects unescaped.
    $post_data = wp_unslash($post_data);
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($post_data, true);
}
/**
 * Display list of a post's revisions.
 *
 * Can output either a UL with edit links or a TABLE with diff interface, and
 * restore action links.
 *
 * Second argument controls parameters:
 *   (bool)   parent : include the parent (the "Current Revision") in the list.
 *   (string) format : 'list' or 'form-table'. 'list' outputs UL, 'form-table'
 *                     outputs TABLE with UI.
 *   (int)    right  : what revision is currently being viewed - used in
 *                     form-table format.
 *   (int)    left   : what revision is currently being diffed against right -
 *                     used in form-table format.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses wp_get_post_revisions()
 * @uses wp_post_revision_title()
 * @uses get_edit_post_link()
 * @uses get_the_author_meta()
 *
 * @todo split into two functions (list, form-table) ?
 *
 * @param int|object $post_id Post ID or post object.
 * @param string|array $args See description {@link wp_parse_args()}.
 * @return null
 */
function wp_list_post_revisions($post_id = 0, $args = null)
{
    if (!($post = get_post($post_id))) {
        return;
    }
    $defaults = array('parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all');
    extract(wp_parse_args($args, $defaults), EXTR_SKIP);
    switch ($type) {
        case 'autosave':
            if (!($autosave = wp_get_post_autosave($post->ID))) {
                return;
            }
            $revisions = array($autosave);
            break;
        case 'revision':
            // just revisions - remove autosave later
        // just revisions - remove autosave later
        case 'all':
        default:
            if (!($revisions = wp_get_post_revisions($post->ID))) {
                return;
            }
            break;
    }
    /* translators: post revision: 1: when, 2: author name */
    $titlef = _x('%1$s by %2$s', 'post revision');
    if ($parent) {
        array_unshift($revisions, $post);
    }
    $rows = $right_checked = '';
    $class = false;
    $can_edit_post = current_user_can('edit_post', $post->ID);
    foreach ($revisions as $revision) {
        if (!current_user_can('read_post', $revision->ID)) {
            continue;
        }
        if ('revision' === $type && wp_is_post_autosave($revision)) {
            continue;
        }
        $date = wp_post_revision_title($revision);
        $name = get_the_author_meta('display_name', $revision->post_author);
        if ('form-table' == $format) {
            if ($left) {
                $left_checked = $left == $revision->ID ? ' checked="checked"' : '';
            } else {
                $left_checked = $right_checked ? ' checked="checked"' : '';
            }
            // [sic] (the next one)
            $right_checked = $right == $revision->ID ? ' checked="checked"' : '';
            $class = $class ? '' : " class='alternate'";
            if ($post->ID != $revision->ID && $can_edit_post) {
                $actions = '<a href="' . wp_nonce_url(add_query_arg(array('revision' => $revision->ID, 'action' => 'restore')), "restore-post_{$post->ID}|{$revision->ID}") . '">' . __('Restore') . '</a>';
            } else {
                $actions = '';
            }
            $rows .= "<tr{$class}>\n";
            $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='{$revision->ID}'{$left_checked} /></th>\n";
            $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='right' value='{$revision->ID}'{$right_checked} /></th>\n";
            $rows .= "\t<td>{$date}</td>\n";
            $rows .= "\t<td>{$name}</td>\n";
            $rows .= "\t<td class='action-links'>{$actions}</td>\n";
            $rows .= "</tr>\n";
        } else {
            $title = sprintf($titlef, $date, $name);
            $rows .= "\t<li>{$title}</li>\n";
        }
    }
    if ('form-table' == $format) {
        ?>

<form action="revision.php" method="get">

<div class="tablenav">
	<div class="alignleft">
		<input type="submit" class="button-secondary" value="<?php 
        esc_attr_e('Compare Revisions');
        ?>
" />
		<input type="hidden" name="action" value="diff" />
		<input type="hidden" name="post_type" value="<?php 
        echo esc_attr($post->post_type);
        ?>
" />
	</div>
</div>

<br class="clear" />

<table class="widefat post-revisions" cellspacing="0" id="post-revisions">
	<col />
	<col />
	<col style="width: 33%" />
	<col style="width: 33%" />
	<col style="width: 33%" />
<thead>
<tr>
	<th scope="col"><?php 
        /* translators: column name in revisons */
        _ex('Old', 'revisions column name');
        ?>
</th>
	<th scope="col"><?php 
        /* translators: column name in revisons */
        _ex('New', 'revisions column name');
        ?>
</th>
	<th scope="col"><?php 
        /* translators: column name in revisons */
        _ex('Date Created', 'revisions column name');
        ?>
</th>
	<th scope="col"><?php 
        _e('Author');
        ?>
</th>
	<th scope="col" class="action-links"><?php 
        _e('Actions');
        ?>
</th>
</tr>
</thead>
<tbody>

<?php 
        echo $rows;
        ?>

</tbody>
</table>

</form>

<?php 
    } else {
        echo "<ul class='post-revisions'>\n";
        echo $rows;
        echo "</ul>";
    }
}
/**
 * Sets up the post object for preview based on the post autosave.
 *
 * @since 2.7.0
 * @access private
 *
 * @param WP_Post $post
 * @return WP_Post|false
 */
function _set_preview($post)
{
    if (!is_object($post)) {
        return $post;
    }
    $preview = wp_get_post_autosave($post->ID);
    if (!is_object($preview)) {
        return $post;
    }
    $preview = sanitize_post($preview);
    $post->post_content = $preview->post_content;
    $post->post_title = $preview->post_title;
    $post->post_excerpt = $preview->post_excerpt;
    add_filter('get_the_terms', '_wp_preview_terms_filter', 10, 3);
    return $post;
}
 function get_autosave_notice()
 {
     global $post;
     if ('auto-draft' == $post->post_status) {
         $autosave = false;
     } else {
         $autosave = wp_get_post_autosave($post->ID);
     }
     // Detect if there exists an autosave newer than the post and if that autosave is different than the post
     if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) {
         foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) {
             if (normalize_whitespace($autosave->{$autosave_field}) !== normalize_whitespace($post->{$autosave_field})) {
                 return sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID));
             }
         }
         // If this autosave isn't different from the current post, begone.
         wp_delete_post_revision($autosave->ID);
     }
     return false;
 }
 public function get_meta()
 {
     $meta = (object) array('links' => (object) array('self' => (string) $this->get_post_link(), 'help' => (string) $this->get_post_link('help'), 'site' => (string) $this->get_site_link(), 'replies' => (string) $this->get_post_link('replies/'), 'likes' => (string) $this->get_post_link('likes/')));
     // add autosave link if a more recent autosave exists
     if ('edit' === $this->context) {
         $autosave = wp_get_post_autosave($this->post->ID);
         if ($autosave && $autosave->post_modified > $this->post->post_modified) {
             $meta->links->autosave = (string) $this->get_post_link() . '/autosave';
         }
     }
     return $meta;
 }
Beispiel #19
0
 private function _get_post_revisions_meta($post_id, $key, $type = 'revision')
 {
     if (!($post = get_post($post_id))) {
         return array();
     }
     $revisions = array();
     switch ($type) {
         case 'autosave':
             if (function_exists('wp_get_post_autosave')) {
                 if ($autosave = wp_get_post_autosave($post->ID)) {
                     $revisions = array($autosave);
                 }
             }
             break;
         case 'revision':
             // just revisions - remove autosave later
         // just revisions - remove autosave later
         case 'all':
         default:
             if (function_exists('wp_get_post_revisions')) {
                 if (!($revisions = wp_get_post_revisions($post->ID))) {
                     $revisions = array();
                 }
             }
             break;
     }
     $meta_vals = array();
     foreach ($revisions as $revision) {
         $meta_vals[] = $this->_get_post_meta($revision->ID, $key);
     }
     return $meta_vals;
 }
 /**
  * calc section params by keyword
  * @param string $keyword
  * @return array
  */
 protected function calcParams($keyword)
 {
     $params = array('title' => 0, 'h1' => 0, 'h2' => 0, 'h3' => 0, 'bold' => 0, 'italic' => 0, 'underline' => 0, 'alt' => 0);
     $title = wtb_seo_helper::strtolower($this->post['title']);
     $content = $this->post['content'];
     $html = str_get_html($content);
     // h1
     if (strpos($title, $keyword) !== false) {
         $params['h1'] = 1;
     }
     if ($html) {
         foreach ($html->find('h1') as $h1) {
             if (strpos($h1->plaintext, $keyword) !== false) {
                 $params['h1'] = 1;
                 break;
             }
         }
         // h2
         foreach ($html->find('h2') as $h2) {
             if (strpos($h2->plaintext, $keyword) !== false) {
                 $params['h2'] = 1;
                 break;
             }
         }
         // h3
         foreach ($html->find('h3') as $h3) {
             if (strpos($h3->plaintext, $keyword) !== false) {
                 $params['h3'] = 1;
                 break;
             }
         }
         // bold
         foreach ($html->find('strong') as $strong) {
             if (strpos($strong->plaintext, $keyword) !== false) {
                 $params['bold'] = 1;
                 break;
             }
         }
         foreach ($html->find('b') as $strong) {
             if (strpos($strong->plaintext, $keyword) !== false) {
                 $params['bold'] = 1;
                 break;
             }
         }
         // italic
         foreach ($html->find('i') as $strong) {
             if (strpos($strong->plaintext, $keyword) !== false) {
                 $params['italic'] = 1;
                 break;
             }
         }
         foreach ($html->find('em') as $strong) {
             if (strpos($strong->plaintext, $keyword) !== false) {
                 $params['italic'] = 1;
                 break;
             }
         }
         // underline
         foreach ($html->find('u') as $strong) {
             if (strpos($strong->plaintext, $keyword) !== false) {
                 $params['underline'] = 1;
                 break;
             }
         }
         foreach ($html->find('span') as $strong) {
             if (strpos($strong->__toString(), 'text-decoration: underline') !== false and strpos($strong->plaintext, $keyword) !== false) {
                 $params['underline'] = 1;
                 break;
             }
         }
         // img alt
         foreach ($html->find('img') as $img) {
             // because bug on vendor lib
             $imgAsString = $img->__toString();
             if (strpos($imgAsString, $keyword) !== false and strpos($imgAsString, $keyword, (int) strpos($imgAsString, ' alt=')) > (int) strpos($imgAsString, ' alt=')) {
                 $params['alt'] = 1;
                 break;
             }
         }
     }
     // title
     $post = wp_get_post_autosave($this->post['id']);
     if (!$post) {
         $post = get_post($this->post['id']);
     }
     // try to simulate wp_title()
     if (isset($post->post_title)) {
         $metaTitle = apply_filters('single_post_title', $this->post['title'], $post);
     } else {
         $metaTitle = apply_filters('single_post_title', $this->post['title']);
     }
     $t_sep = '%WP_TITILE_SEP%';
     $prefix = '';
     if (!empty($metaTitle)) {
         $prefix = " | ";
     }
     $title_array = explode($t_sep, $metaTitle);
     $title_array = array_reverse($title_array);
     $metaTitle = implode(" | ", $title_array) . $prefix;
     $metaTitle = apply_filters('wp_title', $metaTitle, '|', 'right');
     if (strpos(wtb_seo_helper::strtolower($metaTitle), $keyword) !== false) {
         $params['title'] = 1;
     }
     return $params;
 }
    if (isset($messages[$post_type][$_GET['message']])) {
        $message = $messages[$post_type][$_GET['message']];
    } elseif (!isset($messages[$post_type]) && isset($messages['post'][$_GET['message']])) {
        $message = $messages['post'][$_GET['message']];
    }
}
$notice = false;
$form_extra = '';
if ('auto-draft' == $post->post_status) {
    if ('edit' == $action) {
        $post->post_title = '';
    }
    $autosave = false;
    $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
} else {
    $autosave = wp_get_post_autosave($post_ID);
}
$form_action = 'editpost';
$nonce_action = 'update-post_' . $post_ID;
$form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($post_ID) . "' />";
// Detect if there exists an autosave newer than the post and if that autosave is different than the post
if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) {
    foreach (_wp_post_revision_fields($post) as $autosave_field => $_autosave_field) {
        if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post->{$autosave_field})) {
            $notice = sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID));
            break;
        }
    }
    // If this autosave isn't different from the current post, begone.
    if (!$notice) {
        wp_delete_post_revision($autosave->ID);
Beispiel #22
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses _wp_translate_postdata()
 * @uses _wp_post_revision_fields()
 *
 * @return unknown
 */
function wp_create_post_autosave($post_id)
{
    $translated = _wp_translate_postdata(true);
    if (is_wp_error($translated)) {
        return $translated;
    }
    $post_author = get_current_user_id();
    // Store one autosave per author. If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id, $post_author)) {
        $new_autosave = _wp_post_revision_fields($_POST, true);
        $new_autosave['ID'] = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;
        // If the new autosave is the same content as the post, delete the old autosave.
        $post = get_post($post_id);
        $autosave_is_different = false;
        foreach (array_keys(_wp_post_revision_fields()) as $field) {
            if (normalize_whitespace($new_autosave[$field]) != normalize_whitespace($post->{$field})) {
                $autosave_is_different = true;
                break;
            }
        }
        if (!$autosave_is_different) {
            wp_delete_post_revision($old_autosave->ID);
            return;
        }
        return wp_update_post($new_autosave);
    }
    // _wp_put_post_revision() expects unescaped.
    $post_data = wp_unslash($_POST);
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($post_data, true);
}
 /**
  * If we're in the midst of an autosave then we're going to do things a bit differently than the usual get_all_related (commenting within).  For description of params see the get_all_related() comments
  *
  * @access protected
  * @return EE_Base_Class[]
  */
 protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false)
 {
     //first we check if the post_id for the incoming query is for an autosave.  If it isn't that's what we want!
     $model_object_id = $this->_get_model_object_id($model_object_or_id);
     $autosave = wp_get_post_autosave($model_object_id);
     $id_to_use = $autosave ? $autosave->ID : $model_object_id;
     $autosave_relations = parent::get_all_related($id_to_use, $query_params);
     $parent_ids = $parents = array();
     $return_objs = array();
     //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects);
     foreach ($autosave_relations as $a_r) {
         $pid = $a_r->parent();
         if (!empty($pid)) {
             $parent_ids[] = $pid;
         } else {
             $return_objs[] = $a_r;
         }
     }
     //we have to make sure we also include the ORIGINAL values
     $originals = parent::get_all_related($model_object_or_id, $query_params);
     //merge $originals with $return_objs
     if ($originals) {
         $return_objs = array_merge($originals, $return_objs);
     }
     //now we setup the query to get all the parents
     if (!empty($parent_ids)) {
         $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name();
         $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids);
         $parents = $this->get_other_model()->get_all($query_params);
     }
     //var_dump($parents);
     //now merge parents with our current $return_objs and send back
     return array_merge($parents, $return_objs);
 }
Beispiel #24
0
 /**
  * プレビューの Post ID を返す
  * 
  * @param int $post_id
  * @return int $preview_id
  */
 protected function get_preview_id($post_id)
 {
     global $post;
     $preview_id = 0;
     if (isset($post->ID) && intval($post->ID) === intval($post_id) && is_preview() && ($preview = wp_get_post_autosave($post->ID))) {
         $preview_id = $preview->ID;
     }
     return $preview_id;
 }
Beispiel #25
0
 /**
  * Test autosaving a locked post
  * @return void
  */
 public function test_autosave_locked_post()
 {
     // Lock the post to another user
     $another_user_id = $this->factory->user->create(array('role' => 'editor'));
     wp_set_current_user($another_user_id);
     wp_set_post_lock($this->_post->ID);
     wp_set_current_user($this->user_id);
     // Ensure post is locked
     $this->assertEquals($another_user_id, wp_check_post_lock($this->_post->ID));
     // Set up the $_POST request
     $md5 = md5(uniqid());
     $_POST = array('action' => 'heartbeat', '_nonce' => wp_create_nonce('heartbeat-nonce'), 'data' => array('wp_autosave' => array('post_id' => $this->_post->ID, '_wpnonce' => wp_create_nonce('update-post_' . $this->_post->ID), 'post_content' => $this->_post->post_content . PHP_EOL . $md5, 'post_type' => 'post')));
     // Make the request
     try {
         $this->_handleAjax('heartbeat');
     } catch (WPAjaxDieContinueException $e) {
         unset($e);
     }
     $response = json_decode($this->_last_response, true);
     // Ensure everything is correct
     $this->assertNotEmpty($response['wp_autosave']);
     $this->assertTrue($response['wp_autosave']['success']);
     // Check that the original post was NOT edited
     $post = get_post($this->_post->ID);
     $this->assertFalse(strpos($post->post_content, $md5));
     // Check if the autosave post was created
     $autosave = wp_get_post_autosave($this->_post->ID, get_current_user_id());
     $this->assertNotEmpty($autosave);
     $this->assertGreaterThanOrEqual(0, strpos($autosave->post_content, $md5));
 }
Beispiel #26
0
function acf_get_valid_post_id($post_id = 0)
{
    // set post_id to global
    if (!$post_id) {
        $post_id = (int) get_the_ID();
    }
    // allow for option == options
    if ($post_id == 'option') {
        $post_id = 'options';
    }
    // $post_id may be an object
    if (is_object($post_id)) {
        if (isset($post_id->roles, $post_id->ID)) {
            $post_id = 'user_' . $post_id->ID;
        } elseif (isset($post_id->taxonomy, $post_id->term_id)) {
            $post_id = $post_id->taxonomy . '_' . $post_id->term_id;
        } elseif (isset($post_id->comment_ID)) {
            $post_id = 'comment_' . $post_id->comment_ID;
        } elseif (isset($post_id->ID)) {
            $post_id = $post_id->ID;
        }
    }
    // append language code
    if ($post_id == 'options') {
        $dl = acf_get_setting('default_language');
        $cl = acf_get_setting('current_language');
        if ($cl && $cl !== $dl) {
            $post_id .= '_' . $cl;
        }
    }
    /*
     *  Override for preview
     *  
     *  If the $_GET['preview_id'] is set, then the user wants to see the preview data.
     *  There is also the case of previewing a page with post_id = 1, but using get_field
     *  to load data from another post_id.
     *  In this case, we need to make sure that the autosave revision is actually related
     *  to the $post_id variable. If they match, then the autosave data will be used, otherwise, 
     *  the user wants to load data from a completely different post_id
     */
    if (isset($_GET['preview_id'])) {
        $autosave = wp_get_post_autosave($_GET['preview_id']);
        if ($autosave && $autosave->post_parent == $post_id) {
            $post_id = (int) $autosave->ID;
        }
    }
    // return
    return $post_id;
}
 /**
  * for the current post remove its auto save
  * we don't use the default drafts / autosaves in livepress mode so remove any to stop messages etc.
  */
 function remove_auto_save()
 {
     global $post;
     if (null !== $post) {
         $autosave = wp_get_post_autosave($post->ID);
         if (false !== $autosave) {
             wp_delete_post_revision($autosave->ID);
         }
     }
 }
function _set_preview($post)
{
    if (!is_object($post)) {
        return $post;
    }
    $preview = wp_get_post_autosave($post->ID);
    if (!is_object($preview)) {
        return $post;
    }
    $preview = sanitize_post($preview);
    $post->post_content = $preview->post_content;
    $post->post_title = $preview->post_title;
    $post->post_excerpt = $preview->post_excerpt;
    return $post;
}
Beispiel #29
0
 /**
  * プレビューのときはそのプレビューの Post ID を返す
  *
  * @param int $post_id
  * @return int
  */
 protected static function get_real_post_id($post_id)
 {
     if (is_preview()) {
         $preview_post = wp_get_post_autosave($post_id);
         if (isset($preview_post->ID)) {
             $post_id = $preview_post->ID;
         }
     }
     return $post_id;
 }