Passed
Pull Request — master (#472)
by Virginia
09:32 queued 05:14
created
includes/classes/class-lsx-schema-utils.php 1 patch
Indentation   +208 added lines, -208 removed lines patch added patch discarded remove patch
@@ -10,212 +10,212 @@
 block discarded – undo
10 10
  * @since 11.6
11 11
  */
12 12
 class LSX_Schema_Utils {
13
-	/**
14
-	 * Determines whether a given post type should have Review schema.
15
-	 *
16
-	 * @param string $post_type       Post type to check.
17
-	 * @param string $comparison_type Post type to check against.
18
-	 *
19
-	 * @return bool True if it has schema, false if not.
20
-	 */
21
-	public static function is_type( $post_type = null, $comparison_type = null ) {
22
-		if ( is_null( $comparison_type ) ) {
23
-			return false;
24
-		}
25
-		if ( is_null( $post_type ) ) {
26
-			$post_type = get_post_type();
27
-		}
28
-		/**
29
-		 * Filter: 'wpseo_schema_$this->post_type_post_types' - Allow changing for which post types we output Review schema.
30
-		 *
31
-		 * @api string[] $post_types The post types for which we output Review.
32
-		 */
33
-		$post_types = apply_filters( 'wpseo_schema_' . $comparison_type . '_post_types', array( $comparison_type ) );
34
-		return in_array( $post_type, $post_types );
35
-	}
36
-	/**
37
-	 * Retrieve a users Schema ID.
38
-	 *
39
-	 * @param string               $place_id The Name of the Reviewer you need a for.
40
-	 * @param string               $type the type of the place.
41
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
42
-	 *
43
-	 * @return string The user's schema ID.
44
-	 */
45
-	public static function get_places_schema_id( $place_id, $type, $context ) {
46
-		$url = $context->site_url . '#/schema/' . strtolower( $type ) . '/' . wp_hash( $place_id . get_the_title( $place_id ) );
47
-		return trailingslashit( $url );
48
-	}
49
-	/**
50
-	 * Retrieve a users Schema ID.
51
-	 *
52
-	 * @param string               $name The Name of the Reviewer you need a for.
53
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
54
-	 *
55
-	 * @return string The user's schema ID.
56
-	 */
57
-	public static function get_subtrip_schema_id( $name, $context ) {
58
-		$url = $context->site_url . '#/subtrip/' . wp_hash( $name . $context->id );
59
-		return trailingslashit( $url );
60
-	}
61
-	/**
62
-	 * Retrieve an offer Schema ID.
63
-	 *
64
-	 * @param string               $id      post ID of the place being added.
65
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
66
-	 * @param string               $local   if the Schema is local true / false.
67
-	 *
68
-	 * @return string The user's schema ID.
69
-	 */
70
-	public static function get_offer_schema_id( $id, $context, $local = false ) {
71
-		if ( false === $local ) {
72
-			$url = $context->site_url;
73
-		} else {
74
-			$url = get_permalink( $context->id );
75
-		}
76
-		$url .= '#/schema/offer/';
77
-		$url .= wp_hash( $id . get_the_title( $id ) );
78
-		return trailingslashit( $url );
79
-	}
80
-	/**
81
-	 * Retrieve an review Schema ID.
82
-	 *
83
-	 * @param string               $id      post ID of the place being added.
84
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
85
-	 * @param string               $local   if the Schema is local true / false.
86
-	 *
87
-	 * @return string The user's schema ID.
88
-	 */
89
-	public static function get_review_schema_id( $id, $context, $local = false ) {
90
-		if ( false === $local ) {
91
-			$url = $context->site_url;
92
-		} else {
93
-			$url = get_permalink( $context->id );
94
-		}
95
-		$url .= '#/schema/review/';
96
-		$url .= wp_hash( $id . get_the_title( $id ) );
97
-		return trailingslashit( $url );
98
-	}
99
-	/**
100
-	 * Retrieve an Article Schema ID.
101
-	 *
102
-	 * @param string               $id      post ID of the place being added.
103
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
104
-	 * @param string               $local   if the Schema is local true / false.
105
-	 *
106
-	 * @return string The user's schema ID.
107
-	 */
108
-	public static function get_article_schema_id( $id, $context, $local = false ) {
109
-		if ( false === $local ) {
110
-			$url = get_permalink( $id ) . \Schema_IDs::ARTICLE_HASH;
111
-		} else {
112
-			$url = get_permalink( $context->id ) . '#/schema/article/' . wp_hash( $id . get_the_title( $id ) );
113
-		}
114
-		return trailingslashit( $url );
115
-	}
116
-	/**
117
-	 * Retrieve a users Schema ID.
118
-	 *
119
-	 * @param string               $name The Name of the Reviewer you need a for.
120
-	 * @param WPSEO_Schema_Context $context A value object with context variables.
121
-	 *
122
-	 * @return string The user's schema ID.
123
-	 */
124
-	public static function get_author_schema_id( $name, $email, $context ) {
125
-		return $context->site_url . \Schema_IDs::PERSON_HASH . wp_hash( $name . $email );
126
-	}
127
-	/**
128
-	 * Generates the place graph piece for the subtrip / Itinerary arrays.
129
-	 *
130
-	 * @param array                $data         subTrip / itinerary data.
131
-	 * @param string               $type         The type in data to save the terms in.
132
-	 * @param string               $post_id      The post ID of the current Place to add.
133
-	 * @param WPSEO_Schema_Context $context      The post ID of the current Place to add.
134
-	 * @param string               $contained_in The @id of the containedIn place.
135
-	 *
136
-	 * @return mixed array $data Place data.
137
-	 */
138
-	public static function add_place( $data, $type, $post_id, $context, $contained_in = false ) {
139
-		$at_id = self::get_places_schema_id( $post_id, $type, $context );
140
-		$place = array(
141
-			'@type'       => $type,
142
-			'@id'         => $at_id,
143
-			'name'        => get_the_title( $post_id ),
144
-			'description' => get_the_excerpt( $post_id ),
145
-			'url'         => get_permalink( $post_id ),
146
-		);
147
-		if ( false !== $contained_in ) {
148
-			$place['containedInPlace'] = array(
149
-				'@type' => 'Country',
150
-				'@id'   => $contained_in,
151
-			);
152
-		}
153
-		$data[] = $place;
154
-		return $data;
155
-	}
156
-	/**
157
-	 * Adds an image node if the post has a featured image.
158
-	 *
159
-	 * @param array                $data         The Review data.
160
-	 * @param WPSEO_Schema_Context $context      The post ID of the current Place to add.
161
-	 *
162
-	 * @return array $data The Review data.
163
-	 */
164
-	public static function add_image( $data, $context ) {
165
-		if ( $context->has_image ) {
166
-			$data['image'] = array(
167
-				'@id' => $context->canonical . \Schema_IDs::PRIMARY_IMAGE_HASH,
168
-			);
169
-		}
170
-		return $data;
171
-	}
172
-	/**
173
-	 * Generates the itemReviewed schema
174
-	 *
175
-	 * @param  array  $items The array of IDS.
176
-	 * @param  string $type The schema type.
177
-	 * @return array $schema An array of the schema markup.
178
-	 */
179
-	public static function get_item_reviewed( $items = array(), $type = '' ) {
180
-		$schema = array();
181
-		if ( false !== $items && ! empty( $items ) && '' !== $type ) {
182
-			array_unique( $items );
183
-			foreach ( $items as $item ) {
184
-				$title = get_the_title( $item );
185
-				if ( '' !== $title ) {
186
-					$item_schema = array(
187
-						'@type' => $type,
188
-						'name'  => $title,
189
-					);
190
-					$schema[]    = $item_schema;
191
-				}
192
-			}
193
-		}
194
-		return $schema;
195
-	}
196
-	/**
197
-	 * Adds a term or multiple terms, comma separated, to a field.
198
-	 *
199
-	 * @param array  $data     Review data.
200
-	 * @param string $post_id  The ID of the item to fetch terms.
201
-	 * @param string $key      The key in data to save the terms in.
202
-	 * @param string $taxonomy The taxonomy to retrieve the terms from.
203
-	 *
204
-	 * @return mixed array $data Review data.
205
-	 */
206
-	public static function add_terms( $data, $post_id, $key, $taxonomy ) {
207
-		$terms = get_the_terms( $post_id, $taxonomy );
208
-		if ( is_array( $terms ) ) {
209
-			$keywords = array();
210
-			foreach ( $terms as $term ) {
211
-				// We are checking against the WordPress internal translation.
212
-				// @codingStandardsIgnoreLine
213
-				if ( __( 'Uncategorized', 'lsx' ) !== $term->name ) {
214
-					$keywords[] = $term->name;
215
-				}
216
-			}
217
-			$data[ $key ] = implode( ',', $keywords );
218
-		}
219
-		return $data;
220
-	}
13
+     /**
14
+      * Determines whether a given post type should have Review schema.
15
+      *
16
+      * @param string $post_type       Post type to check.
17
+      * @param string $comparison_type Post type to check against.
18
+      *
19
+      * @return bool True if it has schema, false if not.
20
+      */
21
+     public static function is_type( $post_type = null, $comparison_type = null ) {
22
+          if ( is_null( $comparison_type ) ) {
23
+               return false;
24
+          }
25
+          if ( is_null( $post_type ) ) {
26
+               $post_type = get_post_type();
27
+          }
28
+          /**
29
+           * Filter: 'wpseo_schema_$this->post_type_post_types' - Allow changing for which post types we output Review schema.
30
+           *
31
+           * @api string[] $post_types The post types for which we output Review.
32
+           */
33
+          $post_types = apply_filters( 'wpseo_schema_' . $comparison_type . '_post_types', array( $comparison_type ) );
34
+          return in_array( $post_type, $post_types );
35
+     }
36
+     /**
37
+      * Retrieve a users Schema ID.
38
+      *
39
+      * @param string               $place_id The Name of the Reviewer you need a for.
40
+      * @param string               $type the type of the place.
41
+      * @param WPSEO_Schema_Context $context A value object with context variables.
42
+      *
43
+      * @return string The user's schema ID.
44
+      */
45
+     public static function get_places_schema_id( $place_id, $type, $context ) {
46
+          $url = $context->site_url . '#/schema/' . strtolower( $type ) . '/' . wp_hash( $place_id . get_the_title( $place_id ) );
47
+          return trailingslashit( $url );
48
+     }
49
+     /**
50
+      * Retrieve a users Schema ID.
51
+      *
52
+      * @param string               $name The Name of the Reviewer you need a for.
53
+      * @param WPSEO_Schema_Context $context A value object with context variables.
54
+      *
55
+      * @return string The user's schema ID.
56
+      */
57
+     public static function get_subtrip_schema_id( $name, $context ) {
58
+          $url = $context->site_url . '#/subtrip/' . wp_hash( $name . $context->id );
59
+          return trailingslashit( $url );
60
+     }
61
+     /**
62
+      * Retrieve an offer Schema ID.
63
+      *
64
+      * @param string               $id      post ID of the place being added.
65
+      * @param WPSEO_Schema_Context $context A value object with context variables.
66
+      * @param string               $local   if the Schema is local true / false.
67
+      *
68
+      * @return string The user's schema ID.
69
+      */
70
+     public static function get_offer_schema_id( $id, $context, $local = false ) {
71
+          if ( false === $local ) {
72
+               $url = $context->site_url;
73
+          } else {
74
+               $url = get_permalink( $context->id );
75
+          }
76
+          $url .= '#/schema/offer/';
77
+          $url .= wp_hash( $id . get_the_title( $id ) );
78
+          return trailingslashit( $url );
79
+     }
80
+     /**
81
+      * Retrieve an review Schema ID.
82
+      *
83
+      * @param string               $id      post ID of the place being added.
84
+      * @param WPSEO_Schema_Context $context A value object with context variables.
85
+      * @param string               $local   if the Schema is local true / false.
86
+      *
87
+      * @return string The user's schema ID.
88
+      */
89
+     public static function get_review_schema_id( $id, $context, $local = false ) {
90
+          if ( false === $local ) {
91
+               $url = $context->site_url;
92
+          } else {
93
+               $url = get_permalink( $context->id );
94
+          }
95
+          $url .= '#/schema/review/';
96
+          $url .= wp_hash( $id . get_the_title( $id ) );
97
+          return trailingslashit( $url );
98
+     }
99
+     /**
100
+      * Retrieve an Article Schema ID.
101
+      *
102
+      * @param string               $id      post ID of the place being added.
103
+      * @param WPSEO_Schema_Context $context A value object with context variables.
104
+      * @param string               $local   if the Schema is local true / false.
105
+      *
106
+      * @return string The user's schema ID.
107
+      */
108
+     public static function get_article_schema_id( $id, $context, $local = false ) {
109
+          if ( false === $local ) {
110
+               $url = get_permalink( $id ) . \Schema_IDs::ARTICLE_HASH;
111
+          } else {
112
+               $url = get_permalink( $context->id ) . '#/schema/article/' . wp_hash( $id . get_the_title( $id ) );
113
+          }
114
+          return trailingslashit( $url );
115
+     }
116
+     /**
117
+      * Retrieve a users Schema ID.
118
+      *
119
+      * @param string               $name The Name of the Reviewer you need a for.
120
+      * @param WPSEO_Schema_Context $context A value object with context variables.
121
+      *
122
+      * @return string The user's schema ID.
123
+      */
124
+     public static function get_author_schema_id( $name, $email, $context ) {
125
+          return $context->site_url . \Schema_IDs::PERSON_HASH . wp_hash( $name . $email );
126
+     }
127
+     /**
128
+      * Generates the place graph piece for the subtrip / Itinerary arrays.
129
+      *
130
+      * @param array                $data         subTrip / itinerary data.
131
+      * @param string               $type         The type in data to save the terms in.
132
+      * @param string               $post_id      The post ID of the current Place to add.
133
+      * @param WPSEO_Schema_Context $context      The post ID of the current Place to add.
134
+      * @param string               $contained_in The @id of the containedIn place.
135
+      *
136
+      * @return mixed array $data Place data.
137
+      */
138
+     public static function add_place( $data, $type, $post_id, $context, $contained_in = false ) {
139
+          $at_id = self::get_places_schema_id( $post_id, $type, $context );
140
+          $place = array(
141
+               '@type'       => $type,
142
+               '@id'         => $at_id,
143
+               'name'        => get_the_title( $post_id ),
144
+               'description' => get_the_excerpt( $post_id ),
145
+               'url'         => get_permalink( $post_id ),
146
+          );
147
+          if ( false !== $contained_in ) {
148
+               $place['containedInPlace'] = array(
149
+                    '@type' => 'Country',
150
+                    '@id'   => $contained_in,
151
+               );
152
+          }
153
+          $data[] = $place;
154
+          return $data;
155
+     }
156
+     /**
157
+      * Adds an image node if the post has a featured image.
158
+      *
159
+      * @param array                $data         The Review data.
160
+      * @param WPSEO_Schema_Context $context      The post ID of the current Place to add.
161
+      *
162
+      * @return array $data The Review data.
163
+      */
164
+     public static function add_image( $data, $context ) {
165
+          if ( $context->has_image ) {
166
+               $data['image'] = array(
167
+                    '@id' => $context->canonical . \Schema_IDs::PRIMARY_IMAGE_HASH,
168
+               );
169
+          }
170
+          return $data;
171
+     }
172
+     /**
173
+      * Generates the itemReviewed schema
174
+      *
175
+      * @param  array  $items The array of IDS.
176
+      * @param  string $type The schema type.
177
+      * @return array $schema An array of the schema markup.
178
+      */
179
+     public static function get_item_reviewed( $items = array(), $type = '' ) {
180
+          $schema = array();
181
+          if ( false !== $items && ! empty( $items ) && '' !== $type ) {
182
+               array_unique( $items );
183
+               foreach ( $items as $item ) {
184
+                    $title = get_the_title( $item );
185
+                    if ( '' !== $title ) {
186
+                         $item_schema = array(
187
+                              '@type' => $type,
188
+                              'name'  => $title,
189
+                         );
190
+                         $schema[]    = $item_schema;
191
+                    }
192
+               }
193
+          }
194
+          return $schema;
195
+     }
196
+     /**
197
+      * Adds a term or multiple terms, comma separated, to a field.
198
+      *
199
+      * @param array  $data     Review data.
200
+      * @param string $post_id  The ID of the item to fetch terms.
201
+      * @param string $key      The key in data to save the terms in.
202
+      * @param string $taxonomy The taxonomy to retrieve the terms from.
203
+      *
204
+      * @return mixed array $data Review data.
205
+      */
206
+     public static function add_terms( $data, $post_id, $key, $taxonomy ) {
207
+          $terms = get_the_terms( $post_id, $taxonomy );
208
+          if ( is_array( $terms ) ) {
209
+               $keywords = array();
210
+               foreach ( $terms as $term ) {
211
+                    // We are checking against the WordPress internal translation.
212
+                    // @codingStandardsIgnoreLine
213
+                    if ( __( 'Uncategorized', 'lsx' ) !== $term->name ) {
214
+                         $keywords[] = $term->name;
215
+                    }
216
+               }
217
+               $data[ $key ] = implode( ',', $keywords );
218
+          }
219
+          return $data;
220
+     }
221 221
 }
Please login to merge, or discard this patch.
includes/classes/class-lsx-schema-graph-piece.php 1 patch
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -12,221 +12,221 @@
 block discarded – undo
12 12
 use \Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece;
13 13
 
14 14
 if ( class_exists( 'Abstract_Schema_Piece' ) ) {
15
-	class LSX_Schema_Graph_Piece extends Abstract_Schema_Piece {
16
-		/**
17
-		 * A value object with context variables.
18
-		 *
19
-		 * @var \WPSEO_Schema_Context
20
-		 */
21
-		public $context;
22
-		/**
23
-		 * This is the post type that you want the piece to output for.
24
-		 *
25
-		 * @var string;
26
-		 */
27
-		public $post_type;
28
-		/**
29
-		 * If this is a top level parent
30
-		 *
31
-		 * @var boolean
32
-		 */
33
-		public $is_top_level;
34
-		/**
35
-		 * This holds the meta_key => scehma_type of the fields you want to add to your subtrip.
36
-		 *
37
-		 * @var array()
38
-		 */
39
-		public $place_ids;
40
-		/**
41
-		 * This holds an object or the current trip post.
42
-		 *
43
-		 * @var WP_Post();
44
-		 */
45
-		public $post;
46
-		/**
47
-		 * This holds URL for the trip
48
-		 *
49
-		 * @var string
50
-		 */
51
-		public $post_url;
52
-		/**
53
-		 * Constructor.
54
-		 *
55
-		 * @param \WPSEO_Schema_Context $context A value object with context variables.
56
-		 */
57
-		public function __construct( WPSEO_Schema_Context $context ) {
58
-			$this->context      = $context;
59
-			$this->place_ids    = array();
60
-			$this->post         = get_post( $this->context->id );
61
-			$this->post_url     = get_permalink( $this->context->id );
62
-			$this->is_top_level = false;
63
-			if ( is_object( $this->post ) && isset( $this->post->post_parent ) && ( false === $this->post->post_parent || 0 === $this->post->post_parent || '' === $this->post->post_parent ) ) {
64
-				$this->is_top_level = true;
65
-			}
66
-		}
67
-		/**
68
-		 * Determines whether or not a piece should be added to the graph.
69
-		 *
70
-		 * @return bool
71
-		 */
72
-		public function is_needed() {
73
-			if ( ! is_singular() ) {
74
-				return false;
75
-			}
76
-			if ( false === $this->context->site_represents ) {
77
-				return false;
78
-			}
79
-			return LSX_Schema_Utils::is_type( get_post_type(), $this->post_type );
80
-		}
81
-		/**
82
-		 * Returns Review data.
83
-		 *
84
-		 * @return array $data Review data.
85
-		 */
86
-		public function generate() {
87
-			$data = array();
88
-			return $data;
89
-		}
90
-		/**
91
-		 * Gets the connected reviews post type and set it as the "Review" schema
92
-		 *
93
-		 * @param  array    $data An array of offers already added.
94
-		 * @param  string   $data_key
95
-		 * @param  boolean  $include_aggregate
96
-		 * @return array    $data
97
-		 */
98
-		public function add_reviews( $data, $data_key = 'reviews', $include_aggregate = true ) {
99
-			$reviews       = get_post_meta( $this->context->id, 'review_to_' . $this->post_type, false );
100
-			$reviews_array = array();
101
-			if ( ! empty( $reviews ) ) {
102
-				$aggregate_value = 1;
103
-				$review_count    = 0;
104
-				foreach ( $reviews as $review_id ) {
105
-					$rating      = get_post_meta( $review_id, 'rating', true );
106
-					$author      = get_post_meta( $review_id, 'reviewer_name', true );
107
-					$description = wp_strip_all_tags( get_the_excerpt( $review_id ) );
108
-					$review_args = array(
109
-						'author'     => $author,
110
-						'reviewBody' => $description,
111
-					);
112
-					// Add in the review rating.
113
-					if ( false !== $rating && '' !== $rating && '0' !== $rating && 0 !== $rating ) {
114
-						$review_args['reviewRating'] = array(
115
-							'@type'       => 'Rating',
116
-							'ratingValue' => $rating,
117
-						);
118
-					}
119
-					$reviews_array = LSX_Schema_Utils::add_review( $reviews_array, $review_id, $this->context, $review_args );
120
-					$review_count++;
121
-				}
122
-				if ( ! empty( $reviews_array ) ) {
123
-					if ( true === $include_aggregate ) {
124
-						$data['aggregateRating'] = array(
125
-							'@type'       => 'AggregateRating',
126
-							'ratingValue' => (string) $aggregate_value,
127
-							'reviewCount' => (string) $review_count,
128
-							'bestRating'  => '5',
129
-							'worstRating' => '1',
130
-						);
131
-					}
132
-					$data[ $data_key ] = $reviews_array;
133
-				}
134
-			}
135
-			return $data;
136
-		}
137
-		/**
138
-		 * Gets the connected posts and set it as the "Article" schema
139
-		 *
140
-		 * @param  array  $data An array of offers already added.
141
-		 * @param  string $data_key
142
-		 * @return array  $data
143
-		 */
144
-		public function add_articles( $data, $data_key = 'subjectOf' ) {
145
-			$posts       = get_post_meta( $this->context->id, 'post_to_' . $this->post_type, false );
146
-			$posts_array = array();
147
-			if ( ! empty( $posts ) ) {
148
-				foreach ( $posts as $post_id ) {
149
-					$post_args = array(
150
-						'articleBody' => wp_strip_all_tags( get_the_excerpt( $post_id ) ),
151
-						'headline'    => get_the_title( $post_id ),
152
-					);
153
-					$section   = get_the_term_list( $post_id, 'category' );
154
-					if ( ! is_wp_error( $section ) && '' !== $section && false !== $section ) {
155
-						$post_args['articleSection'] = wp_strip_all_tags( $section );
156
-					}
157
-					if ( $this->context->site_represents_reference ) {
158
-						$post_args['publisher'] = $this->context->site_represents_reference;
159
-					}
160
-					$image_url = get_the_post_thumbnail_url( $post_id, 'lsx-thumbnail-wide' );
161
-					if ( false !== $image_url ) {
162
-						$post_args['image'] = $image_url;
163
-					}
164
-					$posts_array = LSX_Schema_Utils::add_article( $posts_array, $post_id, $this->context, $post_args );
165
-				}
166
-				if ( ! empty( $posts_array ) ) {
167
-					$data[ $data_key ] = $posts_array;
168
-				}
169
-			}
170
-			return $data;
171
-		}
172
-		/**
173
-		 * Adds the Project and Testimonials attached to the Team Member
174
-		 *
175
-		 * @param array $data
176
-		 *
177
-		 * @return array $data
178
-		 */
179
-		public function add_connections( $data ) {
180
-			$connections_array = array();
181
-			if ( $this->is_top_level ) {
182
-				$connections_array = $this->add_regions( $connections_array );
183
-				$connections_array = $this->add_accommodation( $connections_array );
184
-				if ( ! empty( $connections_array ) ) {
185
-					$data['containsPlace'] = $connections_array;
186
-				}
187
-			} else {
188
-				$connections_array             = $this->add_countries( $connections_array );
189
-				$data['containedInPlace'] = $connections_array;
190
-				$connections_array          = array();
191
-				$connections_array          = $this->add_accommodation( $connections_array );
192
-				$data['containsPlace'] = $connections_array;
193
-			}
194
-			return $data;
195
-		}
196
-		/**
197
-		 * Adds the terms for the taxonomy
198
-		 *
199
-		 * @param array $data     Review data.
200
-		 * @param array $data_key the parameter name you wish to assign it to.
201
-		 * @param array $taxonomy the taxonomy to grab terms for.
202
-		 *
203
-		 * @return array $data Review data.
204
-		 */
205
-		public function add_taxonomy_terms( $data, $data_key, $taxonomy ) {
206
-			/**
207
-			 * Filter: 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Review data.
208
-			 *
209
-			 * @api string $taxonomy The chosen taxonomy.
210
-			 */
211
-			$taxonomy = apply_filters( 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy', $taxonomy );
212
-			return LSX_Schema_Utils::add_terms( $data, $this->context->id, $data_key, $taxonomy );
213
-		}
214
-		/**
215
-		 * Adds the custom field value for the supplied key
216
-		 *
217
-		 * @param array   $data     Schema data.
218
-		 * @param string  $data_key the parameter name you wish to assign it to.
219
-		 * @param string  $meta_key the taxonomy to grab terms for.
220
-		 * @param boolean $single   A single custom field or an array
221
-		 *
222
-		 * @return array $data Review data.
223
-		 */
224
-		public function add_custom_field( $data, $data_key, $meta_key, $single = true ) {
225
-			$value = get_post_meta( $this->context->id, $meta_key, $single );
226
-			if ( '' !== $value && false !== $value ) {
227
-				$data[ $data_key ] = $value;
228
-			}
229
-			return $data;
230
-		}
231
-	}
15
+     class LSX_Schema_Graph_Piece extends Abstract_Schema_Piece {
16
+          /**
17
+           * A value object with context variables.
18
+           *
19
+           * @var \WPSEO_Schema_Context
20
+           */
21
+          public $context;
22
+          /**
23
+           * This is the post type that you want the piece to output for.
24
+           *
25
+           * @var string;
26
+           */
27
+          public $post_type;
28
+          /**
29
+           * If this is a top level parent
30
+           *
31
+           * @var boolean
32
+           */
33
+          public $is_top_level;
34
+          /**
35
+           * This holds the meta_key => scehma_type of the fields you want to add to your subtrip.
36
+           *
37
+           * @var array()
38
+           */
39
+          public $place_ids;
40
+          /**
41
+           * This holds an object or the current trip post.
42
+           *
43
+           * @var WP_Post();
44
+           */
45
+          public $post;
46
+          /**
47
+           * This holds URL for the trip
48
+           *
49
+           * @var string
50
+           */
51
+          public $post_url;
52
+          /**
53
+           * Constructor.
54
+           *
55
+           * @param \WPSEO_Schema_Context $context A value object with context variables.
56
+           */
57
+          public function __construct( WPSEO_Schema_Context $context ) {
58
+               $this->context      = $context;
59
+               $this->place_ids    = array();
60
+               $this->post         = get_post( $this->context->id );
61
+               $this->post_url     = get_permalink( $this->context->id );
62
+               $this->is_top_level = false;
63
+               if ( is_object( $this->post ) && isset( $this->post->post_parent ) && ( false === $this->post->post_parent || 0 === $this->post->post_parent || '' === $this->post->post_parent ) ) {
64
+                    $this->is_top_level = true;
65
+               }
66
+          }
67
+          /**
68
+           * Determines whether or not a piece should be added to the graph.
69
+           *
70
+           * @return bool
71
+           */
72
+          public function is_needed() {
73
+               if ( ! is_singular() ) {
74
+                    return false;
75
+               }
76
+               if ( false === $this->context->site_represents ) {
77
+                    return false;
78
+               }
79
+               return LSX_Schema_Utils::is_type( get_post_type(), $this->post_type );
80
+          }
81
+          /**
82
+           * Returns Review data.
83
+           *
84
+           * @return array $data Review data.
85
+           */
86
+          public function generate() {
87
+               $data = array();
88
+               return $data;
89
+          }
90
+          /**
91
+           * Gets the connected reviews post type and set it as the "Review" schema
92
+           *
93
+           * @param  array    $data An array of offers already added.
94
+           * @param  string   $data_key
95
+           * @param  boolean  $include_aggregate
96
+           * @return array    $data
97
+           */
98
+          public function add_reviews( $data, $data_key = 'reviews', $include_aggregate = true ) {
99
+               $reviews       = get_post_meta( $this->context->id, 'review_to_' . $this->post_type, false );
100
+               $reviews_array = array();
101
+               if ( ! empty( $reviews ) ) {
102
+                    $aggregate_value = 1;
103
+                    $review_count    = 0;
104
+                    foreach ( $reviews as $review_id ) {
105
+                         $rating      = get_post_meta( $review_id, 'rating', true );
106
+                         $author      = get_post_meta( $review_id, 'reviewer_name', true );
107
+                         $description = wp_strip_all_tags( get_the_excerpt( $review_id ) );
108
+                         $review_args = array(
109
+                              'author'     => $author,
110
+                              'reviewBody' => $description,
111
+                         );
112
+                         // Add in the review rating.
113
+                         if ( false !== $rating && '' !== $rating && '0' !== $rating && 0 !== $rating ) {
114
+                              $review_args['reviewRating'] = array(
115
+                                   '@type'       => 'Rating',
116
+                                   'ratingValue' => $rating,
117
+                              );
118
+                         }
119
+                         $reviews_array = LSX_Schema_Utils::add_review( $reviews_array, $review_id, $this->context, $review_args );
120
+                         $review_count++;
121
+                    }
122
+                    if ( ! empty( $reviews_array ) ) {
123
+                         if ( true === $include_aggregate ) {
124
+                              $data['aggregateRating'] = array(
125
+                                   '@type'       => 'AggregateRating',
126
+                                   'ratingValue' => (string) $aggregate_value,
127
+                                   'reviewCount' => (string) $review_count,
128
+                                   'bestRating'  => '5',
129
+                                   'worstRating' => '1',
130
+                              );
131
+                         }
132
+                         $data[ $data_key ] = $reviews_array;
133
+                    }
134
+               }
135
+               return $data;
136
+          }
137
+          /**
138
+           * Gets the connected posts and set it as the "Article" schema
139
+           *
140
+           * @param  array  $data An array of offers already added.
141
+           * @param  string $data_key
142
+           * @return array  $data
143
+           */
144
+          public function add_articles( $data, $data_key = 'subjectOf' ) {
145
+               $posts       = get_post_meta( $this->context->id, 'post_to_' . $this->post_type, false );
146
+               $posts_array = array();
147
+               if ( ! empty( $posts ) ) {
148
+                    foreach ( $posts as $post_id ) {
149
+                         $post_args = array(
150
+                              'articleBody' => wp_strip_all_tags( get_the_excerpt( $post_id ) ),
151
+                              'headline'    => get_the_title( $post_id ),
152
+                         );
153
+                         $section   = get_the_term_list( $post_id, 'category' );
154
+                         if ( ! is_wp_error( $section ) && '' !== $section && false !== $section ) {
155
+                              $post_args['articleSection'] = wp_strip_all_tags( $section );
156
+                         }
157
+                         if ( $this->context->site_represents_reference ) {
158
+                              $post_args['publisher'] = $this->context->site_represents_reference;
159
+                         }
160
+                         $image_url = get_the_post_thumbnail_url( $post_id, 'lsx-thumbnail-wide' );
161
+                         if ( false !== $image_url ) {
162
+                              $post_args['image'] = $image_url;
163
+                         }
164
+                         $posts_array = LSX_Schema_Utils::add_article( $posts_array, $post_id, $this->context, $post_args );
165
+                    }
166
+                    if ( ! empty( $posts_array ) ) {
167
+                         $data[ $data_key ] = $posts_array;
168
+                    }
169
+               }
170
+               return $data;
171
+          }
172
+          /**
173
+           * Adds the Project and Testimonials attached to the Team Member
174
+           *
175
+           * @param array $data
176
+           *
177
+           * @return array $data
178
+           */
179
+          public function add_connections( $data ) {
180
+               $connections_array = array();
181
+               if ( $this->is_top_level ) {
182
+                    $connections_array = $this->add_regions( $connections_array );
183
+                    $connections_array = $this->add_accommodation( $connections_array );
184
+                    if ( ! empty( $connections_array ) ) {
185
+                         $data['containsPlace'] = $connections_array;
186
+                    }
187
+               } else {
188
+                    $connections_array             = $this->add_countries( $connections_array );
189
+                    $data['containedInPlace'] = $connections_array;
190
+                    $connections_array          = array();
191
+                    $connections_array          = $this->add_accommodation( $connections_array );
192
+                    $data['containsPlace'] = $connections_array;
193
+               }
194
+               return $data;
195
+          }
196
+          /**
197
+           * Adds the terms for the taxonomy
198
+           *
199
+           * @param array $data     Review data.
200
+           * @param array $data_key the parameter name you wish to assign it to.
201
+           * @param array $taxonomy the taxonomy to grab terms for.
202
+           *
203
+           * @return array $data Review data.
204
+           */
205
+          public function add_taxonomy_terms( $data, $data_key, $taxonomy ) {
206
+               /**
207
+                * Filter: 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Review data.
208
+                *
209
+                * @api string $taxonomy The chosen taxonomy.
210
+                */
211
+               $taxonomy = apply_filters( 'lsx_schema_' . $this->post_type . '_' . $data_key . '_taxonomy', $taxonomy );
212
+               return LSX_Schema_Utils::add_terms( $data, $this->context->id, $data_key, $taxonomy );
213
+          }
214
+          /**
215
+           * Adds the custom field value for the supplied key
216
+           *
217
+           * @param array   $data     Schema data.
218
+           * @param string  $data_key the parameter name you wish to assign it to.
219
+           * @param string  $meta_key the taxonomy to grab terms for.
220
+           * @param boolean $single   A single custom field or an array
221
+           *
222
+           * @return array $data Review data.
223
+           */
224
+          public function add_custom_field( $data, $data_key, $meta_key, $single = true ) {
225
+               $value = get_post_meta( $this->context->id, $meta_key, $single );
226
+               if ( '' !== $value && false !== $value ) {
227
+                    $data[ $data_key ] = $value;
228
+               }
229
+               return $data;
230
+          }
231
+     }
232 232
 }
Please login to merge, or discard this patch.