Making WordPress.org

Changeset 9604


Ignore:
Timestamp:
03/18/2020 11:44:45 PM (5 years ago)
Author:
coffee2code
Message:

Support Forums: Output meta description tags on topic pages.

Props jonoaldersonwp, coffee2code.
Fixes #4798.

File:
1 edited

Legend:

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

    r9603 r9604  
    3030        // Output robots headers.
    3131        add_action( 'wp_head', array( $this, 'robots_noindex_header' ));
     32
     33        // Output meta description.
     34        add_action( 'wp_head', array( $this, 'meta_description' ) );
    3235
    3336        // Link to create new topics atop topic list.
     
    526529            $prev_page_url = $canonical_url . ( $prev_page > 1 ? 'page/' . absint( $prev_page ) . '/' : '' );
    527530            echo '<link rel="prev" href="' . esc_url( $prev_page_url ) . '" />' . "\n";
     531        }
     532    }
     533
     534    /**
     535     * Outputs meta description tags.
     536     */
     537    public function meta_description() {
     538        $description = '';
     539        $max_length  = 150;
     540
     541        // Single topic.
     542        if ( bbp_is_single_topic() ) {
     543            $topic_id = bbp_get_topic_id();
     544   
     545            // Prepend label if thread is closed.
     546            if ( bbp_is_topic_closed( $topic_id ) ) {
     547                /* translators: %s: Excerpt of the topic's first post. */
     548                $description = __( '[This thread is closed.] %s', 'wporg-support' );
     549            } else {
     550                $description = '%s '; // trailing space is intentional
     551            }
     552
     553            // Determine remaining available description length.
     554            $length = $max_length - mb_strlen( $description ) + 3; // 3 === strlen( ' %s' )
     555
     556            // Get the excerpt for the topic's first post (similar to
     557            // `bbp_get_topic_excerpt()`).
     558            $excerpt = get_post_field( 'post_excerpt', $topic_id );
     559            if ( ! $excerpt  ) {
     560                $excerpt = bbp_get_topic_content( $topic_id );
     561            }
     562            // Remove tags, condense whitespace, then trim.
     563            $excerpt = trim( preg_replace( '/\s+/', ' ', strip_tags( $excerpt ) ) );
     564
     565            // If excerpt length exceeds description limit, truncate it to end of nearest
     566            // word or sentence.
     567            if ( mb_strlen( $excerpt ) > $length ) {
     568                // Truncate string at max length.
     569                $excerpt = substr( $excerpt, 0, $length );
     570                // Reverse string for easier handling.
     571                $rev = strrev( $excerpt );
     572                // Find first reasonable and natural break.
     573                preg_match( '/[\s\.\?!\)\]\}]/', $rev, $match, PREG_OFFSET_CAPTURE );
     574                $pos = ! empty( $match[0][1] ) ? $match[0][1] : 0;
     575                // Get the string up to that natural break and reverse it.
     576                $excerpt = trim( strrev( substr( $rev, $pos ) ) );
     577                // Append ellipsis.
     578                $excerpt .= '&hellip;';
     579            }
     580
     581            $description = sprintf( $description, $excerpt );
     582        }
     583
     584        // Output description meta tags if a description has been set.
     585        if ( $description ) {
     586            $description = trim( $description );
     587
     588            printf( '<meta name="og:description" content="%s" />' . "\n", esc_attr( $description ) );
     589            printf( '<meta name="description" content="%s" />' . "\n", esc_attr( $description ) );
    528590        }
    529591    }
Note: See TracChangeset for help on using the changeset viewer.