PluginProbe
Parse.ly / 3.14.3
Parse.ly v3.14.3
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-metadata-renderer.php

class-metadata-renderer.php in Parse.ly 3.14.3, at src/UI/class-metadata-renderer.php

163 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UI: Metadata renderer class.
4 *
5 * @package Parsely
6 * @since 3.3.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\UI;
12
13 use Parsely\Metadata;
14 use Parsely\Parsely;
15 use WP_Post;
16
17 use const Parsely\PARSELY_FILE;
18
19 /**
20 * Renders metadata in the WordPress front-end header.
21 *
22 * @since 3.3.0
23 */
24 final class Metadata_Renderer {
25 /**
26 * Instance of Parsely class.
27 *
28 * @var Parsely
29 */
30 private $parsely;
31
32 /**
33 * Constructor.
34 *
35 * @param Parsely $parsely Instance of Parsely class.
36 */
37 public function __construct( Parsely $parsely ) {
38 $this->parsely = $parsely;
39 }
40
41 /**
42 * Registers metadata actions.
43 *
44 * @since 3.3.0
45 */
46 public function run(): void {
47 /**
48 * Filter whether the Parse.ly meta tags should be inserted in the page.
49 *
50 * By default, the tags are inserted.
51 *
52 * @since 3.0.0
53 *
54 * @param bool $insert_metadata True to insert the metadata, false otherwise.
55 */
56 if ( apply_filters( 'wp_parsely_should_insert_metadata', true ) ) {
57 add_action( 'wp_head', array( $this, 'render_metadata_on_head' ) );
58 }
59 }
60
61 /**
62 * Renders metadata on site's head using the format from the site's options.
63 *
64 * @since 3.3.0
65 */
66 public function render_metadata_on_head(): void {
67 $parsely_options = $this->parsely->get_options();
68 $this->render_metadata( $parsely_options['meta_type'] );
69 }
70
71 /**
72 * Inserts the code for the meta parameter within the head tag.
73 *
74 * @since 3.2.0
75 * @since 3.3.0 Moved from `Parsely` class to `Metadata_Header`.
76 *
77 * @param string $meta_type `json_ld` or `repeated_metas`.
78 */
79 public function render_metadata( string $meta_type ): void {
80 $parsely_options = $this->parsely->get_options();
81
82 if (
83 $this->parsely->site_id_is_missing() ||
84
85 // Chosen not to track logged-in users.
86 ( ! $parsely_options['track_authenticated_users'] && $this->parsely->is_blog_member_logged_in() ) ||
87
88 // 404 pages are not tracked.
89 is_404() ||
90
91 // Search pages are not tracked.
92 is_search()
93 ) {
94 return;
95 }
96
97 global $post;
98
99 // We can't construct the metadata without a valid post object.
100 $parsed_post = get_post( $post );
101 if ( ! $parsed_post instanceof WP_Post ) {
102 return;
103 }
104
105 $metadata = ( new Metadata( $this->parsely ) )->construct_metadata( $parsed_post );
106
107 // Something went wrong - abort.
108 if ( ! isset( $metadata['headline'] ) ) {
109 return;
110 }
111
112 // Insert JSON-LD or repeated metas.
113 if ( 'json_ld' === $meta_type ) {
114 echo '<script type="application/ld+json">' . wp_json_encode( $metadata ) . '</script>';
115 } else {
116 // Assume `meta_type` is `repeated_metas`.
117 $parsely_post_type = $this->parsely->convert_jsonld_to_parsely_type( $metadata['@type'] ?? '' );
118
119 // @phpstan-ignore-next-line
120 if ( isset( $metadata['keywords'] ) && is_array( $metadata['keywords'] ) ) {
121 $metadata['keywords'] = implode( ',', $metadata['keywords'] );
122 }
123
124 $parsely_metas = array(
125 'title' => $metadata['headline'],
126 'link' => $metadata['url'] ?? null,
127 'type' => $parsely_post_type,
128 'image-url' => $metadata['thumbnailUrl'] ?? null,
129 'pub-date' => $metadata['datePublished'] ?? null,
130 'section' => $metadata['articleSection'] ?? null,
131 'tags' => $metadata['keywords'] ?? null,
132 'author' => isset( $metadata['author'] ),
133 );
134 $parsely_metas = array_filter( $parsely_metas, array( $this, 'filter_empty_and_not_string_from_array' ) );
135
136 if ( isset( $metadata['author'] ) ) {
137 $parsely_page_authors = wp_list_pluck( $metadata['author'], 'name' );
138 $parsely_page_authors = array_filter( $parsely_page_authors, array( $this, 'filter_empty_and_not_string_from_array' ) );
139 }
140
141 include plugin_dir_path( PARSELY_FILE ) . 'views/repeated-metas.php';
142 }
143
144 // Add any custom metadata.
145 if ( isset( $metadata['custom_metadata'] ) ) {
146 echo '<meta name="parsely-metadata" content="' . esc_attr( $metadata['custom_metadata'] ) . '" />';
147 }
148 }
149
150 /**
151 * Function to be used in `array_filter` to clean up repeated metas.
152 *
153 * @since 2.6.0
154 * @since 3.3.0 Moved from `Parsely` class to `Metadata_Header`.
155 *
156 * @param mixed $value Value to filter from the array.
157 * @return bool True if the variable is not empty, and it's a string.
158 */
159 private function filter_empty_and_not_string_from_array( $value ): bool {
160 return is_string( $value ) && '' !== $value;
161 }
162 }
163