Making WordPress.org

Changeset 8186


Ignore:
Timestamp:
02/04/2019 04:31:48 AM (7 years ago)
Author:
dd32
Message:

Plugin Directory: Output a <link rel="canonical"> on most plugin directory pages.

See #4067.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r8185 r8186  
    5151        add_action( 'wp_head', array( Template::class, 'hreflang_link_attributes' ), 2 );
    5252        add_action( 'wp_head', array( Template::class, 'archive_link_rel_prev_next' ), 3 );
     53        add_action( 'wp_head', array( Template::class, 'archive_rel_canonical_link' ), 3 );
    5354
    5455        // Cron tasks.
     
    525526        remove_action( 'wp_head', 'feed_links', 2 );
    526527        remove_action( 'wp_head', 'feed_links_extra', 3 );
     528
     529        // Remove the core <link rel="canonical"> as we've got a plugin-directory-specific version
     530        remove_action( 'wp_head', 'rel_canonical' );
    527531
    528532        add_filter( 'get_term', array( __NAMESPACE__ . '\I18n', 'translate_term' ) );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r8171 r8186  
    911911        }
    912912    }
     913
     914    /**
     915     * Outputs a <link rel="canonical"> on archive pages.
     916     */
     917    public function archive_rel_canonical_link() {
     918        if ( $url = self::get_current_url() ) {
     919            printf(
     920                '<link rel="canonical" href="%s">' . "\n",
     921                esc_url( $url )
     922            );
     923        }
     924    }
     925
     926    /**
     927     * Get the current front-end requested URL.
     928     */
     929    public function get_current_url() {
     930        $queried_object = get_queried_object();
     931        $link = false;
     932
     933        if ( is_tax() ) {
     934            $link = get_term_link( $queried_object );
     935        } elseif ( is_author() ) {
     936            $link = get_author_link( $queried_object );
     937        } elseif ( is_singular() ) {
     938            $link = get_permalink( $queried_object );
     939
     940            if ( is_singular( 'plugin' ) && get_query_var( 'plugin_advanced' ) ) {
     941                $link .= 'advanced/';
     942            }
     943        } elseif ( is_search() ) {
     944            $link = home_url( 'search/' . urlencode( get_query_var( 's' ) ) . '/' );
     945        }
     946
     947        if ( $link && is_paged() ) {
     948            if ( false !== stripos( $link, '?' ) ) {
     949                $link = add_query_arg( 'paged', (int) get_query_var( 'paged' ), $link );
     950            } else {
     951                $link = rtrim( $link, '/' ) . '/page/' . (int) get_query_var( 'paged' ) . '/';
     952            }
     953        }
     954
     955        return $link;
     956    }
    913957}
Note: See TracChangeset for help on using the changeset viewer.