WordPress.org

Make WordPress Core

Ticket #12976: save_restore_postdata_get_post_content_excerpt.diff

File save_restore_postdata_get_post_content_excerpt.diff, 3.1 KB (added by mikeschinkel, 8 years ago)

Adds get_post_content(), get_post_excerpt() as well as save_postdata() and restore_postdata()

  • post.php

    diff -rupN wp-includes/post.php new/post.php
    old new function _show_post_preview() { 
    46344634                add_filter('the_preview', '_set_preview');
    46354635        }
    46364636}
     4637
     4638/**
     4639 * Retrieve content from the specified post.
     4640 *
     4641 * get_the_content() assumes setup_postdata() has previously been called.
     4642 * In order to call get_the_content() without side-effects you must first
     4643 * collect up the values of the global variables assigned in setup_postdata()
     4644 * and afterwards restore those global variables.
     4645 *
     4646 * @since 3.1.0
     4647 *
     4648 * @uses save_postdata(), setup_postdata(), restore_postdata(), get_the_content()
     4649 *
     4650 * @param object $post Post data.
     4651 * @param string $more_link_text Optional. Content for when there is more text.
     4652 * @param string $stripteaser Optional. Teaser content before the more text.
     4653 * @return string The Post's content
     4654 */
     4655function get_post_content($post, $more_link_text = null, $stripteaser = 0) {
     4656        $save = save_postdata();
     4657        setup_postdata($post);
     4658        $the_content = get_the_content($more_link_text,$stripteaser);
     4659        restore_postdata($save);
     4660        return $the_content;
     4661}
     4662/**
     4663 * Retrieve the excerpt from the specified post.
     4664 *
     4665 * get_the_excerpt() assumes setup_postdata() has previously been called.
     4666 * In order to call get_the_excerpt() without side-effects you must first
     4667 * collect up the values of the global variables assigned in setup_postdata()
     4668 * and afterwards restore those global variables.
     4669 *
     4670 * @since 3.1.0
     4671 *
     4672 * @uses save_postdata(), setup_postdata(), restore_postdata(), get_the_excerpt()
     4673 *
     4674 * @param object $post Post data.
     4675 * @return string The Post's excerpt
     4676 */
     4677function get_post_excerpt($post) {
     4678        $save = save_postdata();
     4679        setup_postdata($post);
     4680        $the_excerpt = get_the_excerpt();
     4681        restore_postdata($save);
     4682        return $the_excerpt;
     4683}
  • query.php

    diff -rupN wp-includes/query.php new/query.php
    old new function wp_old_slug_redirect() { 
    28082808}
    28092809
    28102810/**
     2811 * Save global post data into an array for later restore.
     2812 *
     2813 * @since 3.1.0
     2814 *
     2815 * @return array Values of selected global variables to be restored by restore_postdata().
     2816 */
     2817function save_postdata() {
     2818        global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
     2819        $postdata = array($id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages);
     2820        return $postdata;
     2821}
     2822/**
     2823 * Save global post data into an array for later restore.
     2824 *
     2825 * @since 3.1.0
     2826 *
     2827 * @param array Values of selected global variables collected by save_postdata().
     2828 */
     2829function restore_postdata($postdata) {
     2830        global $id, $day, $more, $post, $page, $pages, $numpages, $multipage, $authordata, $currentmonth;
     2831        list($id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages) = $postdata;
     2832}
     2833
     2834/**
    28112835 * Set up global post data.
    28122836 *
    28132837 * @since 1.5.0
    function setup_postdata($post) { 
    28502874
    28512875        return true;
    28522876}
     2877
    28532878?>
     2879 No newline at end of file