Skip to content

Commit 201eddb

Browse files
committed
add parameters to sites endpoint
1 parent 1488303 commit 201eddb

3 files changed

+129
-18
lines changed

class-multisite-api.php

+47-10
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
1111
* Author URI: http://github.com/thomasnavarro.
1212
*/
13+
14+
namespace IciOnDrive;
15+
1316
if (!defined('ABSPATH')) {
1417
exit;
1518
}
1619

17-
if (!class_exists('IOD_Multisite_API')) {
18-
class IOD_Multisite_API
20+
if (!class_exists('Multisite_API')) {
21+
class Multisite_API
1922
{
2023
/**
2124
* @var string namespace
@@ -32,8 +35,8 @@ public function __construct()
3235
// WP hooks
3336
add_action('init', [$this, 'init_hook']);
3437

35-
$this->get_sites = new IOD_Multisite_API_Get_Sites();
36-
$this->get_site = new IOD_Multisite_API_Get_Site();
38+
$this->get_sites = new Get_Sites();
39+
$this->get_site = new Get_Site();
3740
}
3841

3942
public function init_hook()
@@ -47,11 +50,34 @@ public function register_rest_route()
4750
register_rest_route(
4851
self::NAMESPACE, '/sites',
4952
[
50-
'methods' => WP_REST_Server::READABLE,
53+
'methods' => \WP_REST_Server::READABLE,
5154
'callback' => [$this->get_sites, 'callback'],
5255
// 'permission_callback' => [$this, 'get_item_permissions_check'],
53-
'args' => [],
54-
]);
56+
'args' => [
57+
'page' => [
58+
'description' => __('Current page of the result.'),
59+
'type' => 'integer',
60+
'default' => 1,
61+
],
62+
'per_page' => [
63+
'description' => __('Maximum number of items to be returned in result set.'),
64+
'type' => 'integer',
65+
'default' => 10,
66+
],
67+
'fields' => [
68+
'default' => true,
69+
'type' => 'boolean',
70+
'description' => __('Retrieves the ACF fields from the site.'),
71+
],
72+
'gps' => [
73+
'default' => false,
74+
'type' => 'boolean',
75+
'description' => __('Retrieves only the GPS coordinates of the site.'),
76+
],
77+
],
78+
// 'schema' => [$this, 'get_public_item_schema'],
79+
]
80+
);
5581

5682
// Get site by blog ID
5783
register_rest_route(
@@ -64,10 +90,21 @@ public function register_rest_route()
6490
],
6591
],
6692
[
67-
'methods' => WP_REST_Server::READABLE,
93+
'methods' => \WP_REST_Server::READABLE,
6894
'callback' => [$this->get_site, 'callback'],
6995
// 'permission_callback' => [$this, 'get_item_permissions_check'],
70-
'args' => [],
96+
'args' => [
97+
'fields' => [
98+
'default' => true,
99+
'type' => 'boolean',
100+
'description' => __('Retrieves the ACF fields from the site.'),
101+
],
102+
'gps' => [
103+
'default' => false,
104+
'type' => 'boolean',
105+
'description' => __('Retrieves only the GPS coordinates of the site.'),
106+
],
107+
],
71108
],
72109
'schema' => [$this, 'get_public_item_schema'],
73110
]
@@ -91,5 +128,5 @@ private function get_item_permissions_check()
91128
require_once dirname(__FILE__).'/class-multisite-get-site.php';
92129

93130
// Instantiate
94-
new IOD_Multisite_API();
131+
new Multisite_API();
95132
}

class-multisite-get-site.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
class IOD_Multisite_API_Get_Site extends WP_REST_Controller
3+
namespace IciOnDrive;
4+
5+
class Get_Site extends \WP_REST_Controller
46
{
57
/**
68
* Get a collection of items.
79
*
8-
* @param WP_REST_Request $request full data about the request
10+
* @param \WP_REST_Request $request full data about the request
911
*
10-
* @return WP_Error|WP_REST_Response
12+
* @return \WP_Error|\WP_REST_Response
1113
*/
1214
public function callback($request)
1315
{
@@ -22,7 +24,7 @@ public function callback($request)
2224

2325
protected function get_site($id)
2426
{
25-
$error = new WP_Error(
27+
$error = new \WP_Error(
2628
'rest_site_invalid_id',
2729
__('Invalid site ID.'),
2830
['status' => 404]
@@ -34,10 +36,41 @@ protected function get_site($id)
3436

3537
$site = get_site((int) $id);
3638

39+
// Get ACF Fields
40+
if ($request['fields']) {
41+
$site = $this->get_fields($site);
42+
}
43+
44+
// Get GPS Coordinates
45+
if ($request['gps']) {
46+
$site = $this->get_coordinates($site);
47+
}
48+
3749
if (empty($site) || empty($site->blog_id)) {
3850
return $error;
3951
}
4052

4153
return $site;
4254
}
55+
56+
protected function get_fields($site)
57+
{
58+
$home_id = get_option('page_on_front');
59+
$fields = get_fields($home_id);
60+
$site->fields = $fields;
61+
62+
return apply_filters('multisite_api/get_site/fields', $site);
63+
}
64+
65+
protected function get_coordinates($site)
66+
{
67+
$coordinates = $site->fields['business_details']['address'];
68+
69+
$site->gps = [
70+
'lat' => $coordinates['lat'],
71+
'lng' => $coordinates['lng'],
72+
];
73+
74+
return apply_filters('multisite_api/get_site/gps', $site);
75+
}
4376
}

class-multisite-get-sites.php

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
class IOD_Multisite_API_Get_Sites extends WP_REST_Controller
3+
namespace IciOnDrive;
4+
5+
class Get_Sites extends \WP_REST_Controller
46
{
57
/**
68
* Get a collection of items.
79
*
8-
* @param WP_REST_Request $request full data about the request
10+
* @param \WP_REST_Request $request full data about the request
911
*
10-
* @return WP_Error|WP_REST_Response
12+
* @return \WP_Error|\WP_REST_Response
1113
*/
1214
public function callback($request)
1315
{
@@ -17,12 +19,51 @@ public function callback($request)
1719
'mature' => 0,
1820
'spam' => 0,
1921
'deleted' => 0,
20-
'site__not_in' => [1],
22+
'site__not_in' => [1, 91],
23+
// 'count' => true,
24+
'number' => $request['per_page'],
2125
];
2226

2327
$args = apply_filters('multisite_api/get_sites/args', $args);
2428
$sites = apply_filters('multisite_api/get_sites', get_sites($args));
2529

30+
foreach ($sites as $key => $site) {
31+
switch_to_blog($site->blog_id);
32+
// Get ACF Fields
33+
if ($request['fields']) {
34+
$site = $this->get_fields($site);
35+
}
36+
37+
// Get GPS Coordinates
38+
if ($request['gps']) {
39+
$site = $this->get_coordinates($site);
40+
}
41+
42+
restore_current_blog();
43+
}
44+
2645
return rest_ensure_response($sites);
2746
}
47+
48+
protected function get_fields($site)
49+
{
50+
$home_id = get_option('page_on_front');
51+
$fields = get_fields($home_id);
52+
$site->fields = $fields;
53+
54+
return apply_filters('multisite_api/get_sites/fields', $site);
55+
}
56+
57+
protected function get_coordinates($site)
58+
{
59+
$home_id = get_option('page_on_front');
60+
$coordinates = get_field('business_details', $home_id)['address'];
61+
62+
$site->gps = [
63+
'lat' => $coordinates['lat'],
64+
'lng' => $coordinates['lng'],
65+
];
66+
67+
return apply_filters('multisite_api/get_sites/coordinates', $site);
68+
}
2869
}

0 commit comments

Comments
 (0)