Code Duplication    Length = 22-23 lines in 2 locations

src/wordlift/jsonld/class-jsonld-endpoint.php 2 locations

@@ 142-163 (lines=22) @@
139
		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
140
	}
141
142
	public function jsonld_using_get_page_by_path( $request ) {
143
144
		$post_name = $request['post_name'];
145
		$post_type = $request['post_type'];
146
147
		global $wpdb;
148
149
		$sql = "
150
			SELECT ID
151
			FROM $wpdb->posts
152
			WHERE post_name = %s
153
			 AND post_type = %s
154
		";
155
156
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
157
158
		if ( is_null( $post_id ) ) {
159
			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
160
		}
161
162
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
163
	}
164
165
	/**
166
	 * @param WP_REST_Request $request
@@ 171-193 (lines=23) @@
168
	 * @return WP_REST_Response
169
	 * @throws \Exception
170
	 */
171
	public function jsonld_using_post_meta( $request ) {
172
173
		$meta_key   = $request['meta_key'];
174
		$meta_value = current( $request->get_query_params( 'meta_value' ) );
175
176
		global $wpdb;
177
178
		$sql = "
179
			SELECT post_id AS ID
180
			FROM $wpdb->postmeta
181
			WHERE meta_key = %s
182
			 AND meta_value = %s
183
			LIMIT 1
184
		";
185
186
		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
187
188
		if ( is_null( $post_id ) ) {
189
			return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) );
190
		}
191
192
		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
193
	}
194
195
}
196