Passed
Pull Request — master (#225)
by Jacques
02:08
created

LSX_Schema_Graph_Piece::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Schema for LSX
4
 *
5
 * @package lsx
6
 */
7
/**
8
 * Returns schema Review data.
9
 *
10
 * @since 10.2
11
 */
12
class LSX_Schema_Graph_Piece implements WPSEO_Graph_Piece {
13
	/**
14
	 * A value object with context variables.
15
	 *
16
	 * @var \WPSEO_Schema_Context
17
	 */
18
	public $context;
19
	/**
20
	 * This is the post type that you want the piece to output for.
21
	 *
22
	 * @var string;
23
	 */
24
	public $post_type;
25
	/**
26
	 * If this is a top level parent
27
	 *
28
	 * @var boolean
29
	 */
30
	public $is_top_level;
31
	/**
32
	 * This holds the meta_key => scehma_type of the fields you want to add to your subtrip.
33
	 *
34
	 * @var array()
35
	 */
36
	public $place_ids;
37
	/**
38
	 * This holds an object or the current trip post.
39
	 *
40
	 * @var WP_Post();
41
	 */
42
	public $post;
43
	/**
44
	 * This holds URL for the trip
45
	 *
46
	 * @var string
47
	 */
48
	public $post_url;
49
	/**
50
	 * Constructor.
51
	 *
52
	 * @param \WPSEO_Schema_Context $context A value object with context variables.
53
	 */
54
	public function __construct( WPSEO_Schema_Context $context ) {
55
		$this->context      = $context;
56
		$this->place_ids    = array();
57
		$this->post         = get_post( $this->context->id );
58
		$this->post_url     = get_permalink( $this->context->id );
59
		$this->is_top_level = false;
60
		if ( is_object( $this->post ) && isset( $this->post->post_parent ) && ( false === $this->post->post_parent || 0 === $this->post->post_parent || '' === $this->post->post_parent ) ) {
61
			$this->is_top_level = true;
62
		}
63
	}
64
	/**
65
	 * Determines whether or not a piece should be added to the graph.
66
	 *
67
	 * @return bool
68
	 */
69
	public function is_needed() {
70
		if ( ! is_singular() ) {
71
			return false;
72
		}
73
		if ( false === $this->context->site_represents ) {
74
			return false;
75
		}
76
		return LSX_Schema_Utils::is_type( get_post_type(), $this->post_type );
77
	}
78
	/**
79
	 * Returns Review data.
80
	 *
81
	 * @return array $data Review data.
82
	 */
83
	public function generate() {
84
		$data = array();
85
		return $data;
86
	}
87
	/**
88
	 * Gets the connected reviews post type and set it as the "Review" schema
89
	 *
90
	 * @param  array    $data An array of offers already added.
91
	 * @param  string   $data_key
92
	 * @param  boolean  $include_aggregate
93
	 * @return array    $data
94
	 */
95
	public function add_reviews( $data, $data_key = 'reviews', $include_aggregate = true ) {
96
		$reviews       = get_post_meta( $this->context->id, 'review_to_' . $this->post_type, false );
97
		$reviews_array = array();
98
		if ( ! empty( $reviews ) ) {
99
			$aggregate_value = 1;
100
			$review_count    = 0;
101
			foreach ( $reviews as $review_id ) {
102
				$rating      = get_post_meta( $review_id, 'rating', true );
103
				$author      = get_post_meta( $review_id, 'reviewer_name', true );
104
				$description = wp_strip_all_tags( get_the_excerpt( $review_id ) );
105
				$review_args = array(
106
					'author'     => $author,
107
					'reviewBody' => $description,
108
				);
109
				// Add in the review rating.
110
				if ( false !== $rating && '' !== $rating && '0' !== $rating && 0 !== $rating ) {
111
					$review_args['reviewRating'] = array(
112
						'@type'       => 'Rating',
113
						'ratingValue' => $rating,
114
					);
115
				}
116
				$reviews_array = LSX_Schema_Utils::add_review( $reviews_array, $review_id, $this->context, $review_args );
0 ignored issues
show
Bug introduced by
The method add_review() does not seem to exist on object<LSX_Schema_Utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
				$review_count++;
118
			}
119
			if ( ! empty( $reviews_array ) ) {
120
				if ( true === $include_aggregate ) {
121
					$data['aggregateRating'] = array(
122
						'@type'       => 'AggregateRating',
123
						'ratingValue' => (string) $aggregate_value,
124
						'reviewCount' => (string) $review_count,
125
						'bestRating'  => '5',
126
						'worstRating' => '1',
127
					);
128
				}
129
				$data[ $data_key ] = $reviews_array;
130
			}
131
		}
132
		return $data;
133
	}
134
	/**
135
	 * Gets the connected posts and set it as the "Article" schema
136
	 *
137
	 * @param  array  $data An array of offers already added.
138
	 * @param  string $data_key
139
	 * @return array  $data
140
	 */
141
	public function add_articles( $data, $data_key = 'subjectOf' ) {
142
		$posts       = get_post_meta( $this->context->id, 'post_to_' . $this->post_type, false );
143
		$posts_array = array();
144
		if ( ! empty( $posts ) ) {
145
			foreach ( $posts as $post_id ) {
146
				$post_args = array(
147
					'articleBody' => wp_strip_all_tags( get_the_excerpt( $post_id ) ),
148
					'headline'    => get_the_title( $post_id ),
149
				);
150
				$section   = get_the_term_list( $post_id, 'category' );
151
				if ( ! is_wp_error( $section ) && '' !== $section && false !== $section ) {
152
					$post_args['articleSection'] = wp_strip_all_tags( $section );
153
				}
154
				if ( $this->context->site_represents_reference ) {
155
					$post_args['publisher'] = $this->context->site_represents_reference;
156
				}
157
				$image_url = get_the_post_thumbnail_url( $post_id, 'lsx-thumbnail-wide' );
158
				if ( false !== $image_url ) {
159
					$post_args['image'] = $image_url;
160
				}
161
				$posts_array = LSX_Schema_Utils::add_article( $posts_array, $post_id, $this->context, $post_args );
0 ignored issues
show
Bug introduced by
The method add_article() does not seem to exist on object<LSX_Schema_Utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
			}
163
			if ( ! empty( $posts_array ) ) {
164
				$data[ $data_key ] = $posts_array;
165
			}
166
		}
167
		return $data;
168
	}
169
	/**
170
	 * Adds the Project and Testimonials attached to the Team Member
171
	 *
172
	 * @param array $data
173
	 *
174
	 * @return array $data
175
	 */
176
	public function add_connections( $data ) {
177
		$connections_array = array();
178
		if ( $this->is_top_level ) {
179
			$connections_array = $this->add_regions( $connections_array );
180
			$connections_array = $this->add_accommodation( $connections_array );
181
			if ( ! empty( $connections_array ) ) {
182
				$data['containsPlace'] = $connections_array;
183
			}
184
		} else {
185
			$connections_array             = $this->add_countries( $connections_array );
186
			$data['containedInPlace'] = $connections_array;
187
			$connections_array          = array();
188
			$connections_array          = $this->add_accommodation( $connections_array );
189
			$data['containsPlace'] = $connections_array;
190
		}
191
		return $data;
192
	}
193
	/**
194
	 * Adds the terms for the taxonomy
195
	 *
196
	 * @param array $data     Review data.
197
	 * @param array $data_key the parameter name you wish to assign it to.
198
	 * @param array $taxonomy the taxonomy to grab terms for.
199
	 *
200
	 * @return array $data Review data.
201
	 */
202
	public function add_taxonomy_terms( $data, $data_key, $taxonomy ) {
203
		/**
204
		 * Filter: 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Review data.
205
		 *
206
		 * @api string $taxonomy The chosen taxonomy.
207
		 */
208
		$taxonomy = apply_filters( 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy', $taxonomy );
209
		return LSX_Schema_Utils::add_terms( $data, $this->context->id, $data_key, $taxonomy );
0 ignored issues
show
Documentation introduced by
$data_key is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
210
	}
211
	/**
212
	 * Adds the custom field value for the supplied key
213
	 *
214
	 * @param array   $data     Schema data.
215
	 * @param string  $data_key the parameter name you wish to assign it to.
216
	 * @param string  $meta_key the taxonomy to grab terms for.
217
	 * @param boolean $single   A single custom field or an array
218
	 *
219
	 * @return array $data Review data.
220
	 */
221
	public function add_custom_field( $data, $data_key, $meta_key, $single = true ) {
222
		$value = get_post_meta( $this->context->id, $meta_key, $single );
223
		if ( '' !== $value && false !== $value ) {
224
			$data[ $data_key ] = $value;
225
		}
226
		return $data;
227
	}
228
}
229