Making WordPress.org

Changeset 9533


Ignore:
Timestamp:
02/24/2020 07:29:25 AM (6 years ago)
Author:
dd32
Message:

Theme Directory: Swap out inline metadata for a JSON+LD object.

Fixes #5037.

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
4 edited

Legend:

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

    r9522 r9533  
    804804                        'tested' => true,
    805805                        'requires' => true,
    806                         'downloaded' => false,
     806                        'downloaded' => true,
    807807                        'downloadlink' => true,
    808808                        'last_updated' => true,
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/functions.php

    r9523 r9533  
    452452        new Jetpack_SEO;
    453453}
     454
     455/**
     456 * Prints markup information in the head of a page.
     457 *
     458 * @link http://schema.org/SoftwareApplication
     459 * @link https://developers.google.com/search/docs/data-types/software-apps
     460 */
     461function wporg_themes_json_ld_schema() {
     462        $schema = false;
     463
     464        // Schema for the front page.
     465        if ( is_front_page() ) {
     466                $schema = [
     467                        "@context" => "http://schema.org",
     468                        "@type"    => "WebSite",
     469                        "name"     => __( 'WordPress Themes', 'wporg-themes' ),
     470                        "url"      => home_url( '/' ),
     471                        "potentialAction" => [
     472                                [
     473                                        "@type"       => "SearchAction",
     474                                        "target"      => home_url( '/search/{search_term_string}' ),
     475                                        "query-input" => "required name=search_term_string"
     476                                ]
     477                        ]
     478                ];
     479
     480        // Schema for theme pages.
     481        } elseif ( is_singular( 'repopackage' ) && 'publish' === get_post_status( get_queried_object_id() ) ) {
     482                $schema = wporg_themes_json_jd_schema( get_queried_object() );
     483        }
     484
     485        // Print the schema.
     486        if ( $schema ) {
     487                echo PHP_EOL, '<script type="application/ld+json">', PHP_EOL;
     488                // Output URLs without escaping the slashes, and print it human readable.
     489                echo wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
     490                echo PHP_EOL, '</script>', PHP_EOL;
     491        }
     492}
     493add_action( 'wp_head', 'wporg_themes_json_ld_schema' );
     494
     495/**
     496 * Fetches JSON LD schema for a specific theme.
     497 *
     498 * @static
     499 *
     500 * @param \WP_Post $post Plugin to output JSON LD Schema for.
     501 * @return array Schema object.
     502 */
     503function wporg_themes_json_jd_schema( $post ) {
     504
     505        $theme = wporg_themes_theme_information( $post->post_name );
     506
     507        $schema = [];
     508
     509        // Add the theme 'SoftwareApplication' node.
     510        $software_application = [
     511                "@context"            => "http://schema.org",
     512                "@type"               => [
     513                        "SoftwareApplication",
     514                        "Product"
     515                ],
     516                "applicationCategory" => "Theme",
     517                "operatingSystem"     => "WordPress",
     518                "name"                => $theme->name,
     519                "url"                 => get_permalink( $post ),
     520                "description"         => $theme->description,
     521                "softwareVersion"     => $theme->version,
     522                "fileFormat"          => "application/zip",
     523                "downloadUrl"         => $theme->download_link,
     524                "dateModified"        => get_post_modified_time( 'c', false, $post ),
     525                "aggregateRating"     => [
     526                        "@type"       => "AggregateRating",
     527                        "worstRating" => 1,
     528                        "bestRating"  => 5,
     529                        "ratingValue" => round( $theme->rating / 20 / 0.5 )*0.5,
     530                        "ratingCount" => (int) $theme->num_ratings,
     531                        "reviewCount" => (int) $theme->num_ratings,
     532                ],
     533                "interactionStatistic" => [
     534                        "@type"                => "InteractionCounter",
     535                        "interactionType"      => "http://schema.org/DownloadAction",
     536                        "userInteractionCount" => $theme->downloaded,
     537                ],
     538                "image" => $theme->screenshot_url,
     539                "offers" => [
     540                        "@type"         => "Offer",
     541                        "url"           => get_permalink( $post ),
     542                        "price"         => "0.00",
     543                        "priceCurrency" => "USD",
     544                        "seller"        => [
     545                                "@type" => "Organization",
     546                                "name"  => "WordPress.org",
     547                                "url"   => "https://wordpress.org"
     548                        ]
     549                ]
     550        ];
     551
     552        // Remove the aggregateRating node if there's no reviews.
     553        if ( ! $software_application['aggregateRating']['ratingCount'] ) {
     554                unset( $software_application['aggregateRating'] );
     555        }
     556
     557        $schema[] = $software_application;
     558
     559        return $schema;
     560}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/theme-single.php

    r9346 r9533  
    88        </div>
    99        <div class="theme-wrap">
    10                 <div class="theme-about hentry" itemscope itemtype="http://schema.org/CreativeWork">
     10                <div class="theme-about hentry">
    1111                        <?php if ( time() - strtotime( $theme->last_updated ) > 2 * YEAR_IN_SECONDS ) { ?>
    1212                        <div class="theme-notice notice notice-warning notice-alt">
     
    1616
    1717                        <div>
    18                                 <h3 class="theme-name entry-title" itemprop="name"><?php echo esc_html( $theme->name ); ?></h3>
     18                                <h3 class="theme-name entry-title"><?php echo esc_html( $theme->name ); ?></h3>
    1919                                <?php if ( $theme->author->display_name ) { ?>
    20                                 <h4 class="theme-author"><?php printf( _x( 'By %s', 'theme author', 'wporg-themes' ), '<a href="https://wordpress.org/themes/author/' . $theme->author->user_nicename . '/"><span class="author" itemprop="author">' . esc_html( $theme->author->display_name ) . '</span></a>' ); ?></h4>
     20                                <h4 class="theme-author"><?php printf( _x( 'By %s', 'theme author', 'wporg-themes' ), '<a href="https://wordpress.org/themes/author/' . $theme->author->user_nicename . '/"><span class="author">' . esc_html( $theme->author->display_name ) . '</span></a>' ); ?></h4>
    2121                                <?php } ?>
    2222
     
    8484                                <?php } ?>
    8585
    86                                 <div class="theme-description entry-summary" itemprop="description"><p><?php echo esc_html( $theme->description ); ?></p></div>
     86                                <div class="theme-description entry-summary"><p><?php echo esc_html( $theme->description ); ?></p></div>
    8787
    8888                                <?php if ( $theme->tags ) { ?>
     
    108108
    109109                        <div class="theme-meta">
    110                                 <div class="theme-ratings" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
    111                                         <meta itemprop="ratingCount" content="<?php echo $theme->num_ratings; ?>"/>
     110                                <div class="theme-ratings">
    112111                                        <h4><?php _e( 'Ratings', 'wporg-themes' ); ?></h4>
    113112
     
    121120                                                <span class="four"></span>
    122121                                                <span class="five"></span>
    123                                                 <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-themes' ), '<span itemprop="ratingValue">' . round( $theme->rating / 20 / 0.5 )*0.5 . '</span>' ); ?></p>
     122                                                <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-themes' ), '<span>' . round( $theme->rating / 20 / 0.5 )*0.5 . '</span>' ); ?></p>
    124123                                        </div>
    125124                                        <?php } else { ?>
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/view-templates/theme-single.php

    r9346 r9533  
    88        </div>
    99        <div class="theme-wrap">
    10                 <div class="theme-about hentry" itemscope itemtype="http://schema.org/CreativeWork">
     10                <div class="theme-about hentry">
    1111                        <# if ( data.is_outdated ) { #>
    1212                        <div class="theme-notice notice notice-warning notice-alt">
     
    1616
    1717                        <div>
    18                                 <h3 class="theme-name entry-title" itemprop="name">{{{ data.name }}}</h3>
     18                                <h3 class="theme-name entry-title">{{{ data.name }}}</h3>
    1919                                <# if ( data.author.display_name ) { #>
    20                                 <h4 class="theme-author"><?php printf( _x( 'By %s', 'theme author', 'wporg-themes' ), '<a href="{{{ data.path }}}author/{{ data.author.user_nicename }}/"><span class="author" itemprop="author">{{{ data.author.display_name }}}</span></a>' ); ?></h4>
     20                                <h4 class="theme-author"><?php printf( _x( 'By %s', 'theme author', 'wporg-themes' ), '<a href="{{{ data.path }}}author/{{ data.author.user_nicename }}/"><span class="author">{{{ data.author.display_name }}}</span></a>' ); ?></h4>
    2121                                <# } #>
    2222
     
    6363                                <# } #>
    6464
    65                                 <div class="theme-description entry-summary" itemprop="description"><p>{{{ data.description }}}</p></div>
     65                                <div class="theme-description entry-summary"><p>{{{ data.description }}}</p></div>
    6666
    6767                                <# if ( data.tags ) { #>
     
    7979
    8080                        <div class="theme-meta">
    81                                 <div class="theme-ratings" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
    82                                         <meta itemprop="ratingCount" content="{{ data.num_ratings }}"/>
     81                                <div class="theme-ratings">
    8382                                        <h4><?php _e( 'Ratings', 'wporg-themes' ); ?></h4>
    8483
     
    9291                                                <span class="four"></span>
    9392                                                <span class="five"></span>
    94                                                 <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-themes' ), '<span itemprop="ratingValue">{{ Math.round( data.rating / 20 / 0.5 )*0.5 }}</span>' ); ?></p>
     93                                                <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-themes' ), '<span>{{ Math.round( data.rating / 20 / 0.5 )*0.5 }}</span>' ); ?></p>
    9594                                        </div>
    9695                                        <# } else { #>
Note: See TracChangeset for help on using the changeset viewer.