Completed
Push — develop ( d47cfb...7b174f )
by David
06:38
created

Wordlift_Post_To_Jsonld_Converter::convert()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 71
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 6
nop 2
dl 0
loc 71
rs 6.7968
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Converters: Post to JSON-LD Converter.
4
 *
5
 * This file defines a converter from an entity {@link WP_Post} to a JSON-LD array.
6
 *
7
 * @since   3.10.0
8
 * @package Wordlift
9
 */
10
11
/**
12
 * Define the {@link Wordlift_Post_To_Jsonld_Converter} class.
13
 *
14
 * @since 3.10.0
15
 */
16
class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
17
18
	/**
19
	 * A {@link Wordlift_Configuration_Service} instance.
20
	 *
21
	 * @since  3.10.0
22
	 * @access private
23
	 * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
24
	 */
25
	private $configuration_service;
26
27
	/**
28
	 * A {@link Wordlift_Log_Service} instance.
29
	 *
30
	 * @since  3.10.0
31
	 * @access private
32
	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
	 */
34
	private $log;
35
36
	/**
37
	 * Wordlift_Post_To_Jsonld_Converter constructor.
38
	 *
39
	 * @since 3.10.0
40
	 *
41
	 * @param \Wordlift_Entity_Type_Service   $entity_type_service   A {@link Wordlift_Entity_Type_Service} instance.
42
	 * @param \Wordlift_Entity_Service        $entity_service        A {@link Wordlift_Entity_Service} instance.
43
	 * @param \Wordlift_User_Service          $user_service          A {@link Wordlift_User_Service} instance.
44
	 * @param \Wordlift_Attachment_Service    $attachment_service    A {@link Wordlift_Attachment_Service} instance.
45
	 * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
46
	 */
47
	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
48
		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
49
50
		$this->configuration_service = $configuration_service;
51
52
		// Set a reference to the logger.
53
		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
	}
55
56
	/**
57
	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
58
	 * found while processing the post is set in the $references array.
59
	 *
60
	 * @since 3.10.0
61
	 *
62
	 *
63
	 * @param int   $post_id    The post id.
64
	 * @param array $references An array of entity references.
65
	 *
66
	 * @return array A JSON-LD array.
67
	 */
68
	public function convert( $post_id, &$references = array() ) {
69
70
		// Get the post instance.
71
		if ( null === $post = get_post( $post_id ) ) {
72
			// Post not found.
73
			return null;
74
		}
75
76
		// Get the base JSON-LD and the list of entities referenced by this entity.
77
		$jsonld = parent::convert( $post_id, $references );
78
79
		// Get the entity name.
80
		$jsonld['headline'] = $post->post_title;
81
82
		// Get the author.
83
		$author    = get_the_author_meta( 'display_name', $post->post_author );
84
		$author_id = $this->user_service->get_uri( $post->post_author );
85
86
		$jsonld['author'] = array(
87
			'@type' => 'Person',
88
			'@id'   => $author_id,
89
			'name'  => $author,
90
		);
91
92
		// Set the published and modified dates.
93
		$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
94
		$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
95
96
		// Set the publisher.
97
		$this->set_publisher( $jsonld );
98
99
		// Process the references if any.
100
		if ( 0 < sizeof( $references ) ) {
101
102
			// Prepare the `about` and `mentions` array.
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
			$about = $mentions = array();
104
105
			// If the entity is in the title, then it should be an `about`.
106
			foreach ( $references as $reference ) {
107
108
				// Get the entity labels.
109
				$labels = $this->entity_service->get_labels( $reference );
110
111
				// Get the entity URI.
112
				$item = array( '@id' => $this->entity_service->get_uri( $reference ) );
113
114
				// Check if the labels match any part of the title.
115
				$matches = 1 === preg_match( '/' . implode( '|', $labels ) . '/', $post->post_title );
116
117
				// If the title matches, assign the entity to the about, otherwise to the mentions.
118
				if ( $matches ) {
119
					$about[] = $item;
120
				} else {
121
					$mentions[] = $item;
122
				}
123
			}
124
125
			// If we have abouts, assign them to the JSON-LD.
126
			if ( 0 < sizeof( $about ) ) {
127
				$jsonld['about'] = $about;
128
			}
129
130
			// If we have mentions, assign them to the JSON-LD.
131
			if ( 0 < sizeof( $mentions ) ) {
132
				$jsonld['mentions'] = $mentions;
133
			}
134
135
		}
136
137
		return $jsonld;
138
	}
139
140
	/**
141
	 * Enrich the provided params array with publisher data, if available.
142
	 *
143
	 * @since 3.10.0
144
	 *
145
	 * @param array $params The parameters array.
146
	 */
147
	private function set_publisher( &$params ) {
148
149
		// If the publisher id isn't set don't do anything.
150
		if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
151
			return;
152
		}
153
154
		// Get the post instance.
155
		if ( null === $post = get_post( $publisher_id ) ) {
156
			// Publisher not found.
157
			return;
158
		}
159
160
		// Get the item id
161
		$id = $this->entity_service->get_uri( $publisher_id );
162
163
		// Get the type.
164
		$type = $this->entity_type_service->get( $publisher_id );
165
166
		// Get the name.
167
		$name = $post->post_title;
168
169
		// Set the publisher data.
170
		$params['publisher'] = array(
171
			'@type' => $this->relative_to_context( $type['uri'] ),
172
			'@id'   => $id,
173
			'name'  => $name,
174
		);
175
176
		// Set the logo, only for http://schema.org/Organization as Person doesn't
177
		// support the logo property.
178
		//
179
		// See http://schema.org/logo
180
		if ( 'http://schema.org/Organization' !== $type['uri'] ) {
181
			return;
182
		}
183
184
		// Get the logo, WP < 4.4 way: only post ID accepted here.
185
		if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
186
			return;
187
		}
188
189
		// Get the image URL.
190
		if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) {
191
			return;
192
		}
193
194
		// Copy over some useful properties.
195
		//
196
		// See https://developers.google.com/search/docs/data-types/articles
197
		$params['publisher']['logo']['@type']  = 'ImageObject';
198
		$params['publisher']['logo']['url']    = $attachment[0];
199
		$params['publisher']['logo']['width']  = $attachment[1] . 'px';
200
		$params['publisher']['logo']['height'] = $attachment[2] . 'px';
201
202
	}
203
204
}
205