Making WordPress.org


Ignore:
Timestamp:
02/18/2019 12:54:32 AM (8 years ago)
Author:
SergeyBiryukov
Message:

Support Forums: Output rel="next", rel="prev" meta tags for paginated archives.

Fixes #4142.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php

    r8229 r8263  
    2323                // Output rel="canonical" meta tag. Runs before WP's rel_canonical to unhook that if needed.
    2424                add_action( 'wp_head', array( $this, 'rel_canonical' ), 9 );
     25
     26                // Output rel="next", rel="prev" meta tags.
     27                add_action( 'wp_head', array( $this, 'rel_next_prev' ), 9 );
    2528
    2629                // Link to create new topics atop topic list.
    … …  
    299302
    300303        /**
    301          * Outputs <link rel="canonical"> tags for various pages.
    302          */
    303         public function rel_canonical() {
    304                 $canonical_url = false;
     304         * Returns the canonical URL for various bbPress pages.
     305         *
     306         * @return string The canonical URL.
     307         */
     308        public function get_canonical_url() {
     309                $canonical_url = '';
     310
    305311                if ( bbp_is_topic_tag() ) {
    306312                        $canonical_url = bbp_get_topic_tag_link();
    … …  
    310316                        $canonical_url = get_post_type_archive_link( 'forum' );
    311317                } elseif ( bbp_is_single_topic() ) {
    312                         remove_action( 'wp_head', 'rel_canonical' ); // Doesn't handle pagination.
    313318                        $canonical_url = bbp_get_topic_permalink();
    314319                } elseif ( bbp_is_single_forum() ) {
    315                         remove_action( 'wp_head', 'rel_canonical' ); // Doesn't handle pagination.
    316320                        $canonical_url = bbp_get_forum_permalink();
    317321                } elseif ( bbpress()->displayed_user && bbpress()->displayed_user->exists() ) {
    … …  
    320324                }
    321325
     326                return $canonical_url;
     327        }
     328
     329        /**
     330         * Outputs <link rel="canonical"> tags for various bbPress pages.
     331         */
     332        public function rel_canonical() {
     333                $canonical_url = $this->get_canonical_url();
     334
     335                if ( bbp_is_single_topic() || bbp_is_single_forum() ) {
     336                        remove_action( 'wp_head', 'rel_canonical' ); // Doesn't handle pagination.
     337                }
     338
    322339                // Make sure canonical has pagination if needed.
    323340                $page = get_query_var( 'paged', 0 );
    … …  
    328345                if ( $canonical_url ) {
    329346                        echo '<link rel="canonical" href="' . esc_url( $canonical_url ) . '" />' . "\n";
     347                }
     348        }
     349
     350        /**
     351         * Outputs rel="next", rel="prev" for paginated archives.
     352         */
     353        public function rel_next_prev() {
     354                global $wp_query;
     355
     356                $canonical_url = $this->get_canonical_url();
     357                $max_pages     = $wp_query->max_num_pages;
     358
     359                if ( bbp_is_single_view() ) {
     360                        bbp_view_query();  // Populate bbpress()->topic_query.
     361                        $max_pages = bbpress()->topic_query->max_num_pages;
     362                } elseif ( bbp_is_single_topic() ) {
     363                        bbp_has_replies(); // Populate bbpress()->reply_query.
     364                        $max_pages = bbpress()->reply_query->max_num_pages;
     365                } elseif ( bbp_is_single_forum() ) {
     366                        $topic_count = get_post_meta( get_queried_object_id(), '_bbp_topic_count', true );
     367                        if ( $topic_count ) {
     368                                $max_pages = $topic_count / bbp_get_topics_per_page();
     369                        }
     370                }
     371
     372                if ( ! $canonical_url || ! $max_pages ) {
     373                        return;
     374                }
     375
     376                $page      = max( 1, get_query_var( 'paged', 0 ) );
     377                $next_page = min( $page + 1, $max_pages );
     378                $prev_page = max( $page - 1, 1 );
     379
     380                if ( $page < $max_pages ) {
     381                        $next_page_url = $canonical_url . 'page/' . absint( $next_page ) . '/';
     382                        echo '<link rel="next" href="' . esc_url( $next_page_url ) . '" />' . "\n";
     383                }
     384
     385                if ( $page > 1 ) {
     386                        $prev_page_url = $canonical_url . ( $prev_page > 1 ? 'page/' . absint( $prev_page ) . '/' : '' );
     387                        echo '<link rel="prev" href="' . esc_url( $prev_page_url ) . '" />' . "\n";
    330388                }
    331389        }
Note: See TracChangeset for help on using the changeset viewer.