generated from 10up/plugin-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIntegration.php
170 lines (147 loc) · 4.14 KB
/
Integration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Site Automation Integration: Hook into WP_Query or block to replace WordPress content with Sophi curated.
*
* @package SophiWP
*/
namespace SophiWP\SiteAutomation;
use function SophiWP\Settings\get_sophi_settings;
/**
* Class: Integration.
*/
class Integration {
/**
* Request object.
*
* @var Request $request
*/
private $request;
/**
* Class constructor.
*
* @param Request $request Request object.
*/
public function __construct( $request ) {
$this->request = $request;
add_filter( 'posts_pre_query', [ $this, 'get_curated_posts' ], 10, 2 );
add_filter( 'found_posts', array( $this, 'found_posts' ), 10, 2 );
}
/**
* Change the found_posts variable on WP_Query.
*
* @param int $found_posts Number of found posts
* @param WP_Query $query Query object
* @return int Found posts.
*/
public function found_posts( $found_posts, $query ) {
if ( isset( $query->sophi_curated_post_list_success ) && $query->sophi_curated_post_list_success ) {
return $query->num_posts;
}
return $found_posts;
}
/**
* Inject Sophi data to WP_Query.
*
* @param array|null $posts Return an array of post data to short-circuit WP's query,
* or null to allow WP to run its normal queries.
* @param \WP_Query $query The WP_Query instance (passed by reference).
*/
public function get_curated_posts( $posts, $query ) {
$query_integration = get_sophi_settings( 'query_integration' );
if ( 1 !== intval( $query_integration ) ) {
return $posts;
}
$query_vars = $query->query_vars;
if ( empty( $query_vars['sophi_curated_page'] ) || empty( $query_vars['sophi_curated_widget'] ) ) {
return $posts;
}
$curated_response = $this->request->get( $query_vars['sophi_curated_page'], $query_vars['sophi_curated_widget'] );
$request_status = $this->request->get_status();
if ( ! empty ( $request_status['success'] ) ) {
$query->sophi_curated_post_list_success = true;
$query->num_posts = count( $curated_response );
// Determine how we should format the results based on the fields parameter.
$fields = $query->get( 'fields', '' );
switch ( $fields ) {
case 'ids':
$new_posts = $this->format_hits_as_ids( $curated_response );
break;
case 'id=>parent':
$new_posts = $this->format_hits_as_id_parents( $curated_response );
break;
default:
$new_posts = $this->format_hits_as_posts( $curated_response );
break;
}
}
if ( ! empty( $new_posts ) ) {
$posts = array_filter( $new_posts );
}
/**
* The curated post list result that is injected to WP_Query.
*
* @since 1.0.9
* @hook sophi_curated_post_list
*
* @param {array} $posts Post list.
* @param {string} $sophi_curated_page Sophi curated page param.
* @param {string} $sophi_curated_widget Sophi curated widget param.
* @param {array} $request_status The request status, whether it was successful or not.
* @param {WP_Query} $query Original query.
*
* @return {array} Post list.
*/
return apply_filters( 'sophi_curated_post_list', $posts, $query_vars['sophi_curated_page'], $query_vars['sophi_curated_widget'], $request_status, $query );
}
/**
* Format results as an array of ID.
*
* @param array $data Response from Sophi.
*
* @return array
*/
private function format_hits_as_ids( $data ) {
return array_map(
function( $id ) {
return intval( $id );
},
$data
);
}
/**
* Format the results as objects containing id and parent id.
*
* @param array $data Response from Sophi.
*
* @return array
*/
private function format_hits_as_id_parents( $data ) {
return array_map(
function( $id ) {
$post = new \stdClass();
$post->ID = intval( $id );
$post->post_parent = wp_get_post_parent_id( intval( $id ) );
return $post;
},
$data
);
}
/**
* Format the results as post objects.
*
* @param array $data Response from Sophi.
*
* @return array
*/
private function format_hits_as_posts( $data ) {
return array_map(
function( $id ) {
$post = get_post( intval( $id ) );
if ( is_a( $post, 'WP_Post' ) ) {
return $post;
}
},
$data
);
}
}