Completed
Pull Request — master (#1095)
by Naveen
02:32
created
src/public/class-wordlift-term-jsonld-adapter.php 2 patches
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -16,214 +16,214 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Term_JsonLd_Adapter {
18 18
 
19
-	/**
20
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
21
-	 *
22
-	 * @since 3.20.0
23
-	 * @access private
24
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
25
-	 */
26
-	private $entity_uri_service;
27
-
28
-	/**
29
-	 * The {@link Wordlift_Jsonld_Service} instance.
30
-	 *
31
-	 * @since 3.20.0
32
-	 * @access private
33
-	 * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance.
34
-	 */
35
-	private $jsonld_service;
36
-
37
-	private static $instance;
38
-
39
-	/**
40
-	 * Wordlift_Term_JsonLd_Adapter constructor.
41
-	 *
42
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
43
-	 * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance.
44
-	 *
45
-	 * @since 3.20.0
46
-	 *
47
-	 */
48
-	public function __construct( $entity_uri_service, $jsonld_service ) {
49
-
50
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
51
-
52
-		$this->entity_uri_service = $entity_uri_service;
53
-		$this->jsonld_service     = $jsonld_service;
54
-
55
-		self::$instance = $this;
56
-	}
57
-
58
-	public static function get_instance() {
59
-
60
-		return self::$instance;
61
-	}
62
-
63
-	/**
64
-	 * Adds carousel json ld data to term page if the conditions match
65
-	 *
66
-	 * @param $jsonld array JsonLd Array.
67
-	 *
68
-	 * @return array
69
-	 */
70
-	public function get_carousel_jsonld( $jsonld, $id = null ) {
71
-		$posts = $this->get_posts( $id );
72
-
73
-		if ( ! is_array( $posts ) ) {
74
-			// Bail out if no posts are present.
75
-			return $jsonld;
76
-		}
77
-
78
-		$entities = array();
79
-
80
-		if ( count( $posts ) < 2 ) {
81
-			return $jsonld;
82
-		}
83
-
84
-		if ( ! is_null( $id ) ) {
85
-			$term                  = get_term( $id );
86
-			$jsonld['description'] = $term->description;
87
-
88
-			$thumbnail_id = get_term_meta( $id, 'thumbnail_id', true );
89
-			if ( ! empty( $thumbnail_id ) ) {
90
-				$jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
91
-			}
92
-		}
93
-
94
-		// More than 2 items are present, so construct the jsonld data
95
-		$jsonld['@context']        = 'https://schema.org';
96
-		$jsonld['@type']           = 'ItemList';
97
-		$jsonld['url']             = $this->get_term_url( $id );
98
-		$jsonld['itemListElement'] = array();
99
-		$position                  = 1;
100
-
101
-		foreach ( $posts as $post_id ) {
102
-			$post_jsonld = $this->jsonld_service->get_jsonld( false, $post_id );
103
-			$result      = array(
104
-				'@type'    => 'ListItem',
105
-				'position' => $position,
106
-				/**
107
-				 * We can't use `item` here unless we change the URL for the item to point to the current page.
108
-				 *
109
-				 * See https://developers.google.com/search/docs/data-types/carousel
110
-				 */
111
-				'url' => get_permalink($post_id)
112
-			);
113
-			array_push( $jsonld['itemListElement'], $result );
114
-
115
-			$entities = array_merge( $entities, $post_jsonld );
116
-			$position += 1;
117
-		}
118
-
119
-		return array(
120
-			'post_jsonld' => $jsonld,
121
-			'entities'    => array()
122
-		);
123
-	}
124
-
125
-	private function get_posts( $id ) {
126
-		global $wp_query;
127
-
128
-		if ( ! is_null( $wp_query->posts ) ) {
129
-			return array_map( function ( $post ) {
130
-				return $post->ID;
131
-			}, $wp_query->posts );
132
-		}
133
-
134
-		if ( is_null( $id ) ) {
135
-			return null;
136
-		}
137
-
138
-		$term = get_term( $id );
139
-
140
-		return get_objects_in_term( $id, $term->taxonomy );
141
-	}
142
-
143
-	/**
144
-	 * Hook to `wp_head` to print the JSON-LD.
145
-	 *
146
-	 * @since 3.20.0
147
-	 */
148
-	public function wp_head() {
149
-		// Bail out if it's not a category page.
150
-		if ( ! is_tax() && ! is_category() ) {
151
-			return;
152
-		}
153
-
154
-		$query_object = get_queried_object();
155
-		$term_id      = $query_object->term_id;
156
-		$jsonld       = $this->get( $term_id );
157
-
158
-		// Bail out if the JSON-LD is empty.
159
-		if ( empty( $jsonld ) ) {
160
-			return;
161
-		}
162
-
163
-		$jsonld_string = wp_json_encode( $jsonld );
164
-
165
-		echo "<script type=\"application/ld+json\">$jsonld_string</script>";
166
-
167
-	}
168
-
169
-	public function get( $id ) {
170
-
171
-		/**
172
-		 * Support for carousel rich snippet, get jsonld data present
173
-		 * for all the posts shown in the term page, and add the jsonld data
174
-		 * to list
175
-		 *
176
-		 * see here: https://developers.google.com/search/docs/data-types/carousel
177
-		 *
178
-		 * @since 3.26.0
179
-		 */
180
-		$carousel_data = $this->get_carousel_jsonld( array(), $id );
181
-
182
-		// If the carousel jsonld returns empty array, then fallback to previous jsonld generation.
183
-		if ( isset( $carousel_data['entities'] )
184
-		     && isset( $carousel_data['post_jsonld'] )
185
-		     && $carousel_data['post_jsonld'] !== array() ) {
186
-
187
-			$entities    = $carousel_data['entities'];
188
-			$post_jsonld = $carousel_data['post_jsonld'];
189
-
190
-			$jsonld = array( $post_jsonld );
191
-			$jsonld = array_merge( $jsonld, $entities );
192
-		} else {
193
-			// The `_wl_entity_id` are URIs.
194
-			$entity_ids         = get_term_meta( $id, '_wl_entity_id' );
195
-			$entity_uri_service = $this->entity_uri_service;
196
-
197
-			$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
198
-				return $entity_uri_service->is_internal( $uri );
199
-			} );
200
-
201
-			// Bail out if there are no entities.
202
-			if ( empty( $local_entity_ids ) ) {
203
-				return array();
204
-			}
205
-
206
-			$post   = $this->entity_uri_service->get_entity( $local_entity_ids[0] );
207
-			$jsonld = $this->jsonld_service->get_jsonld( false, $post->ID );
208
-			// Reset the `url` to the term page.
209
-			$jsonld[0]['url'] = get_term_link( $id );
210
-		}
211
-
212
-		return $jsonld;
213
-	}
214
-
215
-	private function get_term_url( $id ) {
216
-
217
-		if ( is_null( $id ) ) {
218
-			return $_SERVER['REQUEST_URI'];
219
-		}
220
-
221
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
222
-		if ( ! empty( $maybe_url ) ) {
223
-			return $maybe_url;
224
-		}
225
-
226
-		return get_term_link( $id );
227
-	}
19
+    /**
20
+     * The {@link Wordlift_Entity_Uri_Service} instance.
21
+     *
22
+     * @since 3.20.0
23
+     * @access private
24
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
25
+     */
26
+    private $entity_uri_service;
27
+
28
+    /**
29
+     * The {@link Wordlift_Jsonld_Service} instance.
30
+     *
31
+     * @since 3.20.0
32
+     * @access private
33
+     * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance.
34
+     */
35
+    private $jsonld_service;
36
+
37
+    private static $instance;
38
+
39
+    /**
40
+     * Wordlift_Term_JsonLd_Adapter constructor.
41
+     *
42
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
43
+     * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance.
44
+     *
45
+     * @since 3.20.0
46
+     *
47
+     */
48
+    public function __construct( $entity_uri_service, $jsonld_service ) {
49
+
50
+        add_action( 'wp_head', array( $this, 'wp_head' ) );
51
+
52
+        $this->entity_uri_service = $entity_uri_service;
53
+        $this->jsonld_service     = $jsonld_service;
54
+
55
+        self::$instance = $this;
56
+    }
57
+
58
+    public static function get_instance() {
59
+
60
+        return self::$instance;
61
+    }
62
+
63
+    /**
64
+     * Adds carousel json ld data to term page if the conditions match
65
+     *
66
+     * @param $jsonld array JsonLd Array.
67
+     *
68
+     * @return array
69
+     */
70
+    public function get_carousel_jsonld( $jsonld, $id = null ) {
71
+        $posts = $this->get_posts( $id );
72
+
73
+        if ( ! is_array( $posts ) ) {
74
+            // Bail out if no posts are present.
75
+            return $jsonld;
76
+        }
77
+
78
+        $entities = array();
79
+
80
+        if ( count( $posts ) < 2 ) {
81
+            return $jsonld;
82
+        }
83
+
84
+        if ( ! is_null( $id ) ) {
85
+            $term                  = get_term( $id );
86
+            $jsonld['description'] = $term->description;
87
+
88
+            $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true );
89
+            if ( ! empty( $thumbnail_id ) ) {
90
+                $jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
91
+            }
92
+        }
93
+
94
+        // More than 2 items are present, so construct the jsonld data
95
+        $jsonld['@context']        = 'https://schema.org';
96
+        $jsonld['@type']           = 'ItemList';
97
+        $jsonld['url']             = $this->get_term_url( $id );
98
+        $jsonld['itemListElement'] = array();
99
+        $position                  = 1;
100
+
101
+        foreach ( $posts as $post_id ) {
102
+            $post_jsonld = $this->jsonld_service->get_jsonld( false, $post_id );
103
+            $result      = array(
104
+                '@type'    => 'ListItem',
105
+                'position' => $position,
106
+                /**
107
+                 * We can't use `item` here unless we change the URL for the item to point to the current page.
108
+                 *
109
+                 * See https://developers.google.com/search/docs/data-types/carousel
110
+                 */
111
+                'url' => get_permalink($post_id)
112
+            );
113
+            array_push( $jsonld['itemListElement'], $result );
114
+
115
+            $entities = array_merge( $entities, $post_jsonld );
116
+            $position += 1;
117
+        }
118
+
119
+        return array(
120
+            'post_jsonld' => $jsonld,
121
+            'entities'    => array()
122
+        );
123
+    }
124
+
125
+    private function get_posts( $id ) {
126
+        global $wp_query;
127
+
128
+        if ( ! is_null( $wp_query->posts ) ) {
129
+            return array_map( function ( $post ) {
130
+                return $post->ID;
131
+            }, $wp_query->posts );
132
+        }
133
+
134
+        if ( is_null( $id ) ) {
135
+            return null;
136
+        }
137
+
138
+        $term = get_term( $id );
139
+
140
+        return get_objects_in_term( $id, $term->taxonomy );
141
+    }
142
+
143
+    /**
144
+     * Hook to `wp_head` to print the JSON-LD.
145
+     *
146
+     * @since 3.20.0
147
+     */
148
+    public function wp_head() {
149
+        // Bail out if it's not a category page.
150
+        if ( ! is_tax() && ! is_category() ) {
151
+            return;
152
+        }
153
+
154
+        $query_object = get_queried_object();
155
+        $term_id      = $query_object->term_id;
156
+        $jsonld       = $this->get( $term_id );
157
+
158
+        // Bail out if the JSON-LD is empty.
159
+        if ( empty( $jsonld ) ) {
160
+            return;
161
+        }
162
+
163
+        $jsonld_string = wp_json_encode( $jsonld );
164
+
165
+        echo "<script type=\"application/ld+json\">$jsonld_string</script>";
166
+
167
+    }
168
+
169
+    public function get( $id ) {
170
+
171
+        /**
172
+         * Support for carousel rich snippet, get jsonld data present
173
+         * for all the posts shown in the term page, and add the jsonld data
174
+         * to list
175
+         *
176
+         * see here: https://developers.google.com/search/docs/data-types/carousel
177
+         *
178
+         * @since 3.26.0
179
+         */
180
+        $carousel_data = $this->get_carousel_jsonld( array(), $id );
181
+
182
+        // If the carousel jsonld returns empty array, then fallback to previous jsonld generation.
183
+        if ( isset( $carousel_data['entities'] )
184
+             && isset( $carousel_data['post_jsonld'] )
185
+             && $carousel_data['post_jsonld'] !== array() ) {
186
+
187
+            $entities    = $carousel_data['entities'];
188
+            $post_jsonld = $carousel_data['post_jsonld'];
189
+
190
+            $jsonld = array( $post_jsonld );
191
+            $jsonld = array_merge( $jsonld, $entities );
192
+        } else {
193
+            // The `_wl_entity_id` are URIs.
194
+            $entity_ids         = get_term_meta( $id, '_wl_entity_id' );
195
+            $entity_uri_service = $this->entity_uri_service;
196
+
197
+            $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
198
+                return $entity_uri_service->is_internal( $uri );
199
+            } );
200
+
201
+            // Bail out if there are no entities.
202
+            if ( empty( $local_entity_ids ) ) {
203
+                return array();
204
+            }
205
+
206
+            $post   = $this->entity_uri_service->get_entity( $local_entity_ids[0] );
207
+            $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID );
208
+            // Reset the `url` to the term page.
209
+            $jsonld[0]['url'] = get_term_link( $id );
210
+        }
211
+
212
+        return $jsonld;
213
+    }
214
+
215
+    private function get_term_url( $id ) {
216
+
217
+        if ( is_null( $id ) ) {
218
+            return $_SERVER['REQUEST_URI'];
219
+        }
220
+
221
+        $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
222
+        if ( ! empty( $maybe_url ) ) {
223
+            return $maybe_url;
224
+        }
225
+
226
+        return get_term_link( $id );
227
+    }
228 228
 
229 229
 }
Please login to merge, or discard this patch.
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -45,9 +45,9 @@  discard block
 block discarded – undo
45 45
 	 * @since 3.20.0
46 46
 	 *
47 47
 	 */
48
-	public function __construct( $entity_uri_service, $jsonld_service ) {
48
+	public function __construct($entity_uri_service, $jsonld_service) {
49 49
 
50
-		add_action( 'wp_head', array( $this, 'wp_head' ) );
50
+		add_action('wp_head', array($this, 'wp_head'));
51 51
 
52 52
 		$this->entity_uri_service = $entity_uri_service;
53 53
 		$this->jsonld_service     = $jsonld_service;
@@ -67,39 +67,39 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @return array
69 69
 	 */
70
-	public function get_carousel_jsonld( $jsonld, $id = null ) {
71
-		$posts = $this->get_posts( $id );
70
+	public function get_carousel_jsonld($jsonld, $id = null) {
71
+		$posts = $this->get_posts($id);
72 72
 
73
-		if ( ! is_array( $posts ) ) {
73
+		if ( ! is_array($posts)) {
74 74
 			// Bail out if no posts are present.
75 75
 			return $jsonld;
76 76
 		}
77 77
 
78 78
 		$entities = array();
79 79
 
80
-		if ( count( $posts ) < 2 ) {
80
+		if (count($posts) < 2) {
81 81
 			return $jsonld;
82 82
 		}
83 83
 
84
-		if ( ! is_null( $id ) ) {
85
-			$term                  = get_term( $id );
84
+		if ( ! is_null($id)) {
85
+			$term                  = get_term($id);
86 86
 			$jsonld['description'] = $term->description;
87 87
 
88
-			$thumbnail_id = get_term_meta( $id, 'thumbnail_id', true );
89
-			if ( ! empty( $thumbnail_id ) ) {
90
-				$jsonld['image'] = wp_get_attachment_url( $thumbnail_id );
88
+			$thumbnail_id = get_term_meta($id, 'thumbnail_id', true);
89
+			if ( ! empty($thumbnail_id)) {
90
+				$jsonld['image'] = wp_get_attachment_url($thumbnail_id);
91 91
 			}
92 92
 		}
93 93
 
94 94
 		// More than 2 items are present, so construct the jsonld data
95 95
 		$jsonld['@context']        = 'https://schema.org';
96 96
 		$jsonld['@type']           = 'ItemList';
97
-		$jsonld['url']             = $this->get_term_url( $id );
97
+		$jsonld['url']             = $this->get_term_url($id);
98 98
 		$jsonld['itemListElement'] = array();
99 99
 		$position                  = 1;
100 100
 
101
-		foreach ( $posts as $post_id ) {
102
-			$post_jsonld = $this->jsonld_service->get_jsonld( false, $post_id );
101
+		foreach ($posts as $post_id) {
102
+			$post_jsonld = $this->jsonld_service->get_jsonld(false, $post_id);
103 103
 			$result      = array(
104 104
 				'@type'    => 'ListItem',
105 105
 				'position' => $position,
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
 				 */
111 111
 				'url' => get_permalink($post_id)
112 112
 			);
113
-			array_push( $jsonld['itemListElement'], $result );
113
+			array_push($jsonld['itemListElement'], $result);
114 114
 
115
-			$entities = array_merge( $entities, $post_jsonld );
115
+			$entities = array_merge($entities, $post_jsonld);
116 116
 			$position += 1;
117 117
 		}
118 118
 
@@ -122,22 +122,22 @@  discard block
 block discarded – undo
122 122
 		);
123 123
 	}
124 124
 
125
-	private function get_posts( $id ) {
125
+	private function get_posts($id) {
126 126
 		global $wp_query;
127 127
 
128
-		if ( ! is_null( $wp_query->posts ) ) {
129
-			return array_map( function ( $post ) {
128
+		if ( ! is_null($wp_query->posts)) {
129
+			return array_map(function($post) {
130 130
 				return $post->ID;
131
-			}, $wp_query->posts );
131
+			}, $wp_query->posts);
132 132
 		}
133 133
 
134
-		if ( is_null( $id ) ) {
134
+		if (is_null($id)) {
135 135
 			return null;
136 136
 		}
137 137
 
138
-		$term = get_term( $id );
138
+		$term = get_term($id);
139 139
 
140
-		return get_objects_in_term( $id, $term->taxonomy );
140
+		return get_objects_in_term($id, $term->taxonomy);
141 141
 	}
142 142
 
143 143
 	/**
@@ -147,26 +147,26 @@  discard block
 block discarded – undo
147 147
 	 */
148 148
 	public function wp_head() {
149 149
 		// Bail out if it's not a category page.
150
-		if ( ! is_tax() && ! is_category() ) {
150
+		if ( ! is_tax() && ! is_category()) {
151 151
 			return;
152 152
 		}
153 153
 
154 154
 		$query_object = get_queried_object();
155 155
 		$term_id      = $query_object->term_id;
156
-		$jsonld       = $this->get( $term_id );
156
+		$jsonld       = $this->get($term_id);
157 157
 
158 158
 		// Bail out if the JSON-LD is empty.
159
-		if ( empty( $jsonld ) ) {
159
+		if (empty($jsonld)) {
160 160
 			return;
161 161
 		}
162 162
 
163
-		$jsonld_string = wp_json_encode( $jsonld );
163
+		$jsonld_string = wp_json_encode($jsonld);
164 164
 
165 165
 		echo "<script type=\"application/ld+json\">$jsonld_string</script>";
166 166
 
167 167
 	}
168 168
 
169
-	public function get( $id ) {
169
+	public function get($id) {
170 170
 
171 171
 		/**
172 172
 		 * Support for carousel rich snippet, get jsonld data present
@@ -177,53 +177,53 @@  discard block
 block discarded – undo
177 177
 		 *
178 178
 		 * @since 3.26.0
179 179
 		 */
180
-		$carousel_data = $this->get_carousel_jsonld( array(), $id );
180
+		$carousel_data = $this->get_carousel_jsonld(array(), $id);
181 181
 
182 182
 		// If the carousel jsonld returns empty array, then fallback to previous jsonld generation.
183
-		if ( isset( $carousel_data['entities'] )
184
-		     && isset( $carousel_data['post_jsonld'] )
185
-		     && $carousel_data['post_jsonld'] !== array() ) {
183
+		if (isset($carousel_data['entities'])
184
+		     && isset($carousel_data['post_jsonld'])
185
+		     && $carousel_data['post_jsonld'] !== array()) {
186 186
 
187 187
 			$entities    = $carousel_data['entities'];
188 188
 			$post_jsonld = $carousel_data['post_jsonld'];
189 189
 
190
-			$jsonld = array( $post_jsonld );
191
-			$jsonld = array_merge( $jsonld, $entities );
190
+			$jsonld = array($post_jsonld);
191
+			$jsonld = array_merge($jsonld, $entities);
192 192
 		} else {
193 193
 			// The `_wl_entity_id` are URIs.
194
-			$entity_ids         = get_term_meta( $id, '_wl_entity_id' );
194
+			$entity_ids         = get_term_meta($id, '_wl_entity_id');
195 195
 			$entity_uri_service = $this->entity_uri_service;
196 196
 
197
-			$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) {
198
-				return $entity_uri_service->is_internal( $uri );
197
+			$local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) {
198
+				return $entity_uri_service->is_internal($uri);
199 199
 			} );
200 200
 
201 201
 			// Bail out if there are no entities.
202
-			if ( empty( $local_entity_ids ) ) {
202
+			if (empty($local_entity_ids)) {
203 203
 				return array();
204 204
 			}
205 205
 
206
-			$post   = $this->entity_uri_service->get_entity( $local_entity_ids[0] );
207
-			$jsonld = $this->jsonld_service->get_jsonld( false, $post->ID );
206
+			$post   = $this->entity_uri_service->get_entity($local_entity_ids[0]);
207
+			$jsonld = $this->jsonld_service->get_jsonld(false, $post->ID);
208 208
 			// Reset the `url` to the term page.
209
-			$jsonld[0]['url'] = get_term_link( $id );
209
+			$jsonld[0]['url'] = get_term_link($id);
210 210
 		}
211 211
 
212 212
 		return $jsonld;
213 213
 	}
214 214
 
215
-	private function get_term_url( $id ) {
215
+	private function get_term_url($id) {
216 216
 
217
-		if ( is_null( $id ) ) {
217
+		if (is_null($id)) {
218 218
 			return $_SERVER['REQUEST_URI'];
219 219
 		}
220 220
 
221
-		$maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true );
222
-		if ( ! empty( $maybe_url ) ) {
221
+		$maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true);
222
+		if ( ! empty($maybe_url)) {
223 223
 			return $maybe_url;
224 224
 		}
225 225
 
226
-		return get_term_link( $id );
226
+		return get_term_link($id);
227 227
 	}
228 228
 
229 229
 }
Please login to merge, or discard this patch.