-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAjaxRequestCustomFieldKeys.php
70 lines (52 loc) · 1.61 KB
/
AjaxRequestCustomFieldKeys.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
<?php
namespace AC\Controller;
use AC\Ajax;
use AC\Helper\Select;
use AC\Helper\Select\Generic\GroupFormatter\BlogSite;
use AC\Helper\Select\Generic\GroupFormatter\VisibilityType;
use AC\Helper\Select\Generic\Groups;
use AC\Meta\Query;
use AC\Registerable;
use AC\Request;
use AC\Response;
class AjaxRequestCustomFieldKeys implements Registerable
{
public function register(): void
{
$this->get_ajax_handler()->register();
}
private function get_ajax_handler(): Ajax\Handler
{
$handler = new Ajax\Handler();
$handler
->set_action('ac_custom_field_options')
->set_callback([$this, 'ajax_get_custom_fields']);
return $handler;
}
public function ajax_get_custom_fields(): void
{
$this->get_ajax_handler()->verify_request();
$request = new Request();
$response = new Response\Json();
$post_type = $request->get('post_type');
$query = new Query($request->get('meta_type'));
$query->select('meta_key')
->distinct()
->order_by('meta_key');
if ($post_type) {
$query->where_post_type($post_type);
}
$formatter = is_multisite()
? new BlogSite()
: new VisibilityType();
$meta_keys = $query->get();
$options = Select\Options::create_from_array(
array_combine($meta_keys, $meta_keys)
);
$options = new Groups($options, $formatter);
$select = new Select\Response($options);
$response
->set_parameters($select())
->success();
}
}