print_graph( $this->website_graph() ); return; } if ( ! is_singular() ) { return; } $post_id = get_queried_object_id(); if ( ! $post_id || ! self::is_enabled_for( $post_id ) ) { return; } $this->print_graph( $this->entry_graph( $post_id ) ); } /** * Whether JSON-LD is printed for an entry. * * Entries opt in one by one, or every post does when the site wide * setting is on. * * @param int $post_id Post ID. * @return bool */ public static function is_enabled_for( $post_id ) { if ( '1' === (string) get_post_meta( $post_id, '_acsseo_schema', true ) ) { return true; } return ACSSEO_Settings::get( 'schema_all_posts' ) && 'post' === get_post_type( $post_id ); } /** * Graph used on the site front page. * * @return array */ private function website_graph() { $graph = array( '@context' => 'https://schema.org', '@type' => 'WebSite', 'name' => get_bloginfo( 'name', 'display' ), 'url' => home_url( '/' ), ); $tagline = ACSSEO_Settings::home_description(); if ( '' !== $tagline ) { $graph['description'] = $tagline; } $graph['potentialAction'] = array( '@type' => 'SearchAction', 'target' => array( '@type' => 'EntryPoint', 'urlTemplate' => home_url( '/?s={search_term_string}' ), ), 'query-input' => 'required name=search_term_string', ); return $graph; } /** * Graph used on a single entry. * * @param int $post_id Post ID. * @return array */ private function entry_graph( $post_id ) { $is_post = ( 'post' === get_post_type( $post_id ) ); $graph = array( '@context' => 'https://schema.org', '@type' => $is_post ? 'BlogPosting' : 'WebPage', 'headline' => get_the_title( $post_id ), 'url' => get_permalink( $post_id ), 'datePublished' => get_the_date( DATE_W3C, $post_id ), 'dateModified' => get_the_modified_date( DATE_W3C, $post_id ), 'mainEntityOfPage' => array( '@type' => 'WebPage', '@id' => get_permalink( $post_id ), ), ); $description = ACSSEO_Meta::resolve_description( $post_id ); if ( '' !== $description ) { $graph['description'] = $description; } if ( $is_post ) { $author_id = (int) get_post_field( 'post_author', $post_id ); $author = get_the_author_meta( 'display_name', $author_id ); if ( $author ) { $graph['author'] = array( '@type' => 'Person', 'name' => $author, ); } } $image = ACSSEO_Social::resolve_image( $post_id ); if ( '' !== $image ) { $graph['image'] = $image; } $publisher = array( '@type' => 'Organization', 'name' => get_bloginfo( 'name', 'display' ), ); $logo = get_site_icon_url(); if ( $logo ) { $publisher['logo'] = array( '@type' => 'ImageObject', 'url' => $logo, ); } $graph['publisher'] = $publisher; return $graph; } /** * Print one JSON-LD block. * * @param array $graph Graph to encode. * @return void */ private function print_graph( array $graph ) { $json = wp_json_encode( $graph, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( false === $json ) { return; } // A closing tag can only appear inside a string value, so neutralising the // angle brackets keeps the block from being broken out of. $json = str_replace( array( '<', '>' ), array( '\u003c', '\u003e' ), $json ); wp_print_inline_script_tag( $json, array( 'type' => 'application/ld+json', ) ); } }