Code Duplication    Length = 33-35 lines in 2 locations

src/includes/class-wordlift-entity-service.php 1 location

@@ 150-182 (lines=33) @@
147
	 *
148
	 * @return WP_Post|null A WP_Post instance or null if not found.
149
	 */
150
	public function get_entity_post_by_uri( $uri ) {
151
152
		$query = new WP_Query( array(
153
				'posts_per_page' => 1,
154
				'post_status'    => 'any',
155
				'post_type'      => self::TYPE_NAME,
156
				'meta_query'     => array(
157
					'relation' => 'OR',
158
					array(
159
						'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
160
						'value'   => $uri,
161
						'compare' => '='
162
					),
163
					array(
164
						'key'     => WL_ENTITY_URL_META_NAME,
165
						'value'   => $uri,
166
						'compare' => '='
167
					)
168
				)
169
			)
170
		);
171
172
		// Get the matching entity posts.
173
		$posts = $query->get_posts();
174
175
		// Return null if no post is found.
176
		if ( 0 === count( $posts ) ) {
177
			return null;
178
		}
179
180
		// Return the found post.
181
		return $posts[0];
182
	}
183
184
	/**
185
	 * Fires once a post has been saved.

src/wordlift_entity_functions.php 1 location

@@ 13-47 (lines=35) @@
10
 *
11
 * @return WP_Post|null A WP_Post instance or null if not found.
12
 */
13
function wl_get_entity_post_by_uri( $uri ) {
14
15
	$query = new WP_Query( array(
16
			'posts_per_page' => 1,
17
			'post_status'    => 'any',
18
			'post_type'      => Wordlift_Entity_Service::TYPE_NAME,
19
			'meta_query'     => array(
20
				'relation' => 'OR',
21
				array(
22
					'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
23
					'value'   => $uri,
24
					'compare' => '='
25
				),
26
				array(
27
					'key'     => WL_ENTITY_URL_META_NAME,
28
					'value'   => $uri,
29
					'compare' => '='
30
				)
31
			)
32
		)
33
	);
34
35
	// Get the matching entity posts.
36
	$posts = $query->get_posts();
37
38
	// wl_write_log( "wl_get_entity_post_by_uri [ uri :: $uri ][ count :: " . count( $posts ) . " ]\n" );
39
40
	// Return null if no post is found.
41
	if ( 0 === count( $posts ) ) {
42
		return null;
43
	}
44
45
	// Return the found post.
46
	return $posts[0];
47
}
48
49
/**
50
 * Find entity posts by the entity URIs. Entity as searched by their entity URI or same as.