Making WordPress.org


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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.