Making WordPress.org


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

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

Fixes #5037.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.