Completed
Push — develop ( ce465f...dc3813 )
by David
02:29 queued 11s
created

Local_Autocomplete_Service   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A query() 0 34 1
1
<?php
2
/**
3
 * This file provides an autocomplete service using the local data.
4
 *
5
 * @author David Riccitelli <[email protected]>
6
 * @since 3.24.2
7
 * @package Wordlift\Autocomplete
8
 */
9
10
namespace Wordlift\Autocomplete;
11
12
use Wordlift_Entity_Service;
13
use Wordlift_Post_Excerpt_Helper;
14
15
class Local_Autocomplete_Service extends Abstract_Autocomplete_Service {
16
17
	/**
18
	 * @inheritDoc
19
	 */
20
	public function query( $query, $scope, $excludes ) {
21
22
		$args = Wordlift_Entity_Service::add_criterias( array(
23
			'numberposts'         => 50,
24
			'post_status'         => 'any',
25
			's'                   => $query,
26
			'ignore_sticky_posts' => true,
27
			'suppress_filters'    => true,
28
		) );
29
30
		$posts = get_posts( $args );
31
32
		$results = array_map( function ( $item ) {
33
34
			$entity_service = Wordlift_Entity_Service::get_instance();
35
			$uri            = $entity_service->get_uri( $item->ID );
36
37
			return array(
38
				'id'           => $uri,
39
				'label'        => array( $item->post_title ),
40
				'labels'       => $entity_service->get_alternative_labels( $item->ID ),
41
				'descriptions' => array( Wordlift_Post_Excerpt_Helper::get_text_excerpt( $item ) ),
42
				'scope'        => 'local',
43
				'sameAss'      => get_post_meta( $item->ID, \Wordlift_Schema_Service::FIELD_SAME_AS ),
44
				// The following properties are less relevant because we're linking entities that exist already in the
45
				// vocabulary. That's why we don't make an effort to load the real data.
46
				'types'        => array( 'http://schema.org/Thing' ),
47
				'urls'         => array(),
48
				'images'       => array(),
49
			);
50
		}, $posts );
51
52
		return $this->filter( $results, $excludes );
53
	}
54
55
}