Making WordPress.org

Changeset 9503


Ignore:
Timestamp:
02/13/2020 11:40:22 PM (5 years ago)
Author:
coffee2code
Message:

Developer theme: Allow preview link for explanations to preview the explanation being edited within the context of its associated function/hook/method/class.

Props valentinbora.
Fixes #5018.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/explanations.php

    r8662 r9503  
    6060        // Output checkmark in explanations column if post has an explanation.
    6161        add_action( 'manage_posts_custom_column', array( $this, 'handle_column_data'  ), 10, 2 );
     62
     63        add_filter( 'preview_post_link',       array ( $this, 'preview_post_link'     ), 10, 2 );
    6264
    6365        // Permissions.
     
    9395            ),
    9496            'public'            => false,
     97            'publicly_queryable'=> true,
    9598            'hierarchical'      => false,
    9699            'show_ui'           => true,
     
    116119            remove_post_type_support( $type, 'editor' );
    117120        }
     121    }
     122
     123    /**
     124     * Override preview post links for explanations to preview the explanation
     125     * within the context of its associated function/hook/method/class.
     126     *
     127     * The associated post's preview link is amended with query parameters used
     128     * by `get_explanation_content()` to use the explanation being previewed
     129     * instead of the published explanation currently associated with the post.
     130     *
     131     * @access public
     132     * @see 'preview_post_link' filter
     133     *
     134     * @param string  $preview_link URL used for the post preview.
     135     * @param WP_Post $post         Post object.
     136     * @return string
     137     **/
     138    public function preview_post_link( $preview_link, $post ) {
     139        if ( $this->exp_post_type !== $post->post_type ) {
     140            return $preview_link;
     141        }
     142
     143        if ( false !== strpos( $preview_link, 'preview_nonce=' ) ) {
     144            $url = parse_url( $preview_link );
     145            $url_query = array();
     146            parse_str ( $url['query'], $url_query );
     147
     148            $preview_link = get_preview_post_link(
     149                $post->post_parent,
     150                array(
     151                    'wporg_explanations_preview_id'    => $url_query['preview_id'],
     152                    'wporg_explanations_preview_nonce' => $url_query['preview_nonce'],
     153                )
     154            );
     155        }
     156
     157        return $preview_link;
    118158    }
    119159
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r9325 r9503  
    16821682
    16831683        // Get explanation's raw post content.
    1684         $content = get_explanation_field( 'post_content', $_post );
     1684        $content = '';
     1685        if (
     1686            ! empty( $_GET['wporg_explanations_preview_nonce'] )
     1687        &&
     1688            false !== wp_verify_nonce( $_GET['wporg_explanations_preview_nonce'], 'post_preview_' . $post->ID )
     1689        ) {
     1690            $preview = wp_get_post_autosave( $post->ID );
     1691
     1692            if ( is_object( $preview ) ) {
     1693                $post = $preview;
     1694                $content = get_post_field( 'post_content', $preview, 'display' );
     1695            }
     1696        } else {
     1697            $content = get_explanation_field( 'post_content', $_post );
     1698        }
    16851699
    16861700        // Pass the content through expected content filters.
Note: See TracChangeset for help on using the changeset viewer.