Skip to:
Content

bbPress.org

Changeset 7348


Ignore:
Timestamp:
08/01/2025 04:12:03 PM (12 months ago)
Author:
johnjamesjacoby
Message:

Core - Canonical: Convert bbp_redirect_canonical() into a sub-action.

This change abstracts the existing internal canonical redirect logic into new dedicated functions so that they can be individually improved or customized in the future.

Pagination and Editing theme-side require intercepting and preventing the core redirection when requesting certain URLs, and there are likely to be new conditions in the future.

In trunk, for 2.7.

Fixes #3648.

Location:
trunk/src/includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/filters.php

    r7335 r7348  
    4545add_filter( 'map_meta_cap',            'bbp_map_meta_caps',      10, 4 );
    4646add_filter( 'allowed_themes',          'bbp_allowed_themes',     10    );
    47 add_filter( 'redirect_canonical',      'bbp_redirect_canonical', 10    );
     47add_filter( 'redirect_canonical',      'bbp_redirect_canonical', 10, 2 );
    4848add_filter( 'login_redirect',          'bbp_redirect_login',     2,  3 );
    4949add_filter( 'logout_url',              'bbp_logout_url',         2,  2 );
     
    105105add_filter( 'bbp_template_include', 'bbp_template_include_theme_compat',   4, 2 );
    106106
     107/**
     108 * Canonical Redirection
     109 *
     110 * bbPress needs to tweak the way that the default canonical redirect behavior
     111 * works for a number of reasons, and they are individually hooked to a
     112 * dedicated sub-action to make them easier to customize.
     113 */
     114add_filter( 'bbp_redirect_canonical', 'bbp_do_not_redirect_edits',       10, 2 );
     115add_filter( 'bbp_redirect_canonical', 'bbp_do_not_redirect_paginations', 10, 2 );
     116       
    107117// Filter bbPress template locations
    108118add_filter( 'bbp_get_template_stack', 'bbp_add_template_stack_locations' );
  • trunk/src/includes/core/sub-actions.php

    r7080 r7348  
    552552        return (array) apply_filters( 'bbp_mail', $args );
    553553}
     554
     555/**
     556 * Redirects incoming links to the proper URL based on the site URL.
     557 *
     558 * @see redirect_canonical()
     559 *
     560 * @since 2.0.0 bbPress (r2628)
     561 * @since 2.7.0 bbPress {r7345) Converted to a sub-action.
     562 *
     563 * @param string $redirect_url  The redirect URL.
     564 * @param string $requested_url The requested URL.
     565 *
     566 * @return string Empty string if a topic/forum and their first page,
     567 *                 otherwise the redirect URL.
     568 */
     569function bbp_redirect_canonical( $redirect_url = '', $requested_url = '' ) {
     570
     571        // Filter & return
     572        return (string) apply_filters( 'bbp_redirect_canonical', $redirect_url, $requested_url );
     573}
  • trunk/src/includes/core/theme-compat.php

    r7329 r7348  
    851851}
    852852
    853 /** Helpers *******************************************************************/
    854 
    855 /**
    856  * Remove the canonical redirect to allow pretty pagination
    857  *
    858  * @since 2.0.0 bbPress (r2628)
    859  *
    860  * @param string $redirect_url Redirect url
    861  *
    862  * @return bool|string False if it's a topic/forum and their first page,
    863  *                      otherwise the redirect url
    864  */
    865 function bbp_redirect_canonical( $redirect_url ) {
    866 
    867         // Canonical is for the beautiful
    868         if ( bbp_use_pretty_urls() ) {
    869 
    870                 // If viewing beyond page 1 of several
    871                 if ( 1 < bbp_get_paged() ) {
    872 
    873                         // Only on single topics...
    874                         if ( bbp_is_single_topic() ) {
    875                                 $redirect_url = false;
    876 
    877                         // ...and single forums...
    878                         } elseif ( bbp_is_single_forum() ) {
    879                                 $redirect_url = false;
    880 
    881                         // ...and single replies...
    882                         } elseif ( bbp_is_single_reply() ) {
    883                                 $redirect_url = false;
    884 
    885                         // ...and any single anything else...
    886                         //
    887                         // @todo - Find a more accurate way to disable paged canonicals for
    888                         //          paged shortcode usage within other posts.
    889                         } elseif ( is_page() || is_singular() ) {
    890                                 $redirect_url = false;
    891                         }
    892 
    893                 // If editing a topic
    894                 } elseif ( bbp_is_topic_edit() ) {
    895                         $redirect_url = false;
    896 
    897                 // If editing a reply
    898                 } elseif ( bbp_is_reply_edit() ) {
    899                         $redirect_url = false;
     853/** Redirection ***************************************************************/
     854
     855/**
     856 * Prevent canonical redirection when editing forums, topics, topic-tags,
     857 * replies, and users.
     858 *
     859 * @since 2.7.0 bbPress (r7345)
     860 *
     861 * @param string $redirect_url The redirect URL.
     862 *
     863 * @return string Empty string if cancelling redirection.
     864 */
     865function bbp_do_not_redirect_edits( $redirect_url = '' ) {
     866
     867        // Default return value
     868        $retval = $redirect_url;
     869
     870        // Pretty URLs only
     871        if ( ! bbp_use_pretty_urls() ) {
     872                return $retval;
     873        }
     874
     875        // If editing a forum, topic, topic-tag, reply, or user
     876        if ( bbp_is_edit() ) {
     877                $retval = '';
     878        }
     879
     880        // Return
     881        return $retval;
     882}
     883
     884/**
     885 * Prevent canonical redirection to allow pretty pagination of forums & topics.
     886 *
     887 * @since 2.7.0 bbPress (r7345)
     888 *
     889 * @param string $redirect_url The redirect URL.
     890 *
     891 * @return string Empty string if cancelling redirection.
     892 */
     893function bbp_do_not_redirect_paginations( $redirect_url = '' ) {
     894
     895        // Default return value
     896        $retval = $redirect_url;
     897
     898        // Pretty URLs only
     899        if ( ! bbp_use_pretty_urls() ) {
     900                return $retval;
     901        }
     902
     903        // If viewing beyond page 1 of several
     904        if ( 1 < bbp_get_paged() ) {
     905
     906                // Only on single topics...
     907                if ( bbp_is_single_topic() ) {
     908                        $retval = '';
     909
     910                // ...and single forums...
     911                } elseif ( bbp_is_single_forum() ) {
     912                        $retval = '';
     913
     914                // ...and single replies...
     915                } elseif ( bbp_is_single_reply() ) {
     916                        $retval = '';
     917
     918                // ...and any single anything else...
     919                //
     920                // @todo - Find a more accurate way to disable paged canonicals for
     921                //          paged shortcode usage within other posts.
     922                } elseif ( is_page() || is_singular() ) {
     923                        $retval = '';
    900924                }
    901925        }
    902926
    903         return $redirect_url;
     927        // Return
     928        return $retval;
    904929}
    905930
  • trunk/src/includes/forums/functions.php

    r7341 r7348  
    24862486        }
    24872487
    2488         // If forum is explicitly hidden and user not capable, set 404
     2488        // If forum is explicitly hidden and user not capable...
    24892489        if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     2490
     2491                // Set 404 status
    24902492                bbp_set_404( $wp_query );
    24912493        }
     
    25312533        }
    25322534
    2533         // If forum is explicitly hidden and user not capable, set 404
     2535        // If forum is explicitly private and user not capable
    25342536        if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     2537
     2538                // Set 404 status
    25352539                bbp_set_404( $wp_query );
    25362540        }
Note: See TracChangeset for help on using the changeset viewer.