1
+ <?php
2
+ use Queulat \Metabox ;
3
+ use Queulat \Forms \Node_Factory ;
4
+ use Queulat \Forms \Element \WP_Media ;
5
+ use Queulat \Forms \Element \Input_Url ;
6
+ use Queulat \Forms \Element \WP_Editor ;
7
+ use Queulat \Forms \Element \Input_Text ;
8
+ use Queulat \Forms \Element \Input ;
9
+ use Queulat \Forms \Element \Select ;
10
+ use Queulat \Forms \Element \UI_Select2 ;
11
+ use Queulat \Forms \Element \Input_Number ;
12
+ use Queulat \Forms \Element \Input_Email ;
13
+
14
+
15
+ class Chapters_Metabox extends Metabox
16
+ {
17
+ public function __construct ($ id = '' , $ title = '' , $ post_type = '' , array $ args = array ()) {
18
+ parent ::__construct ($ id , $ title , $ post_type , $ args );
19
+ add_action ("wp_ajax_event-metabox__search-members " , [$ this , 'search_members ' ]);
20
+ add_action ("wp_ajax_event-metabox__get_countries " , [$ this , 'get_countries ' ]);
21
+ }
22
+ public function get_countries () {
23
+ $ search = filter_input (INPUT_GET , 'q ' , FILTER_SANITIZE_STRING );
24
+ $ country_id = (array )get_post_meta (get_the_ID (), 'cc_chapter_chapter_country ' , false );
25
+ $ countries = $ countries = GF_Field_Address::get_countries ();
26
+ $ results = [];
27
+ foreach ($ countries as $ key =>$ country ) {
28
+ if (!empty ($ search )) {
29
+ if (stripos ($ country , $ search ) !== false ) {
30
+ $ results [] = [
31
+ 'id ' => $ key ,
32
+ 'text ' => $ country ,
33
+ 'selected ' => in_array ($ key , $ country_id , true )
34
+ ];
35
+ }
36
+ } else {
37
+ $ results [] = [
38
+ 'id ' => $ key ,
39
+ 'text ' => $ country ,
40
+ 'selected ' => in_array ($ key , $ country_id , true )
41
+ ];
42
+ }
43
+ }
44
+ return wp_send_json ([
45
+ 'results ' => $ results
46
+ ]);
47
+ }
48
+ public function search_members ()
49
+ {
50
+ $ search = filter_input (INPUT_GET , 'q ' , FILTER_SANITIZE_STRING );
51
+ $ existing = (array )get_post_meta (filter_input (INPUT_GET , 'chapter_id ' , FILTER_SANITIZE_NUMBER_INT ), 'cc_chapters_chapter_lead ' , false );
52
+ //$existing = array_map('absint', $existing);
53
+ $ members_query = new WP_User_Query (array (
54
+ 'search ' => $ search .'* ' ,
55
+ 'meta_key ' => 'ccgn-application-state ' ,
56
+ 'meta_value ' => 'accepted '
57
+ ));
58
+ $ people = $ members_query ->get_results ();
59
+ $ results = [];
60
+ if (!empty ($ people )) {
61
+ foreach ($ people as $ person ) {
62
+ $ results [] = [
63
+ 'id ' => $ person ->ID ,
64
+ 'text ' => $ person ->data ->display_name ,
65
+ 'selected ' => in_array ($ person ->ID , $ existing , true )
66
+ ];
67
+ }
68
+ }
69
+ return wp_send_json ([
70
+ 'results ' => $ results
71
+ ]);
72
+ }
73
+ public function get_fields () : array
74
+ {
75
+ return [
76
+ Node_Factory::make (
77
+ Input::class,
78
+ [
79
+ 'name ' => 'date ' ,
80
+ 'label ' => 'Date founded ' ,
81
+ 'attributes ' => [
82
+ 'type ' => 'date '
83
+ ],
84
+ 'properties ' => [
85
+ 'description ' => 'Date when chapter was founded '
86
+ ]
87
+ ]
88
+ ),
89
+ Node_Factory::make (
90
+ Input_Email::class,
91
+ [
92
+ 'name ' => 'email ' ,
93
+ 'label ' => 'Email ' ,
94
+ 'properties ' => [
95
+ 'description ' => 'Chapter contact email '
96
+ ]
97
+ ]
98
+ ),
99
+ Node_Factory::make (
100
+ UI_Select2::class,
101
+ [
102
+ 'name ' => 'chapter_country ' ,
103
+ 'label ' => 'Chapter Country ' ,
104
+ 'attributes ' => [
105
+ 'class ' => 'widefat '
106
+ ],
107
+ 'properties ' => [
108
+ 'instance ' => [
109
+ 'multiple ' => false ,
110
+ 'minimumInputLength ' => 3
111
+ // 'ajax' => [
112
+ // 'url' => admin_url('admin-ajax.php?action=event-metabox__get_countries'),
113
+ // ]
114
+ ],
115
+ 'description ' => 'Choose the Chapter Country ' ,
116
+ ],
117
+ 'options ' => (function () {
118
+ // $country_id = (array)get_post_meta(get_the_ID(), 'cc_chapter_chapter_country', false);
119
+ // if (!$country_id) {
120
+ // return [];
121
+ // }
122
+ $ countries = GF_Field_Address::get_countries ();
123
+ if (!empty ($ countries )) {
124
+ return $ countries ;
125
+ }
126
+ return [];
127
+ })()
128
+ ]
129
+ ),
130
+ Node_Factory::make (
131
+ UI_Select2::class,
132
+ [
133
+ 'name ' => 'chapter_lead ' ,
134
+ 'label ' => 'Chapter Lead ' ,
135
+ 'attributes ' => [
136
+ 'class ' => 'widefat '
137
+ ],
138
+ 'properties ' => [
139
+ 'instance ' => [
140
+ 'multiple ' => false ,
141
+ 'minimumInputLength ' => 3 ,
142
+ 'ajax ' => [
143
+ 'url ' => admin_url ('admin-ajax.php?action=event-metabox__search-members&chapter_id= ' . get_the_ID ()),
144
+ ]
145
+ ],
146
+ 'description ' => 'Choose the Chapter Lead from the list '
147
+ ],
148
+ 'options ' => (function () {
149
+ $ author_ids = get_post_meta (get_the_ID (), 'cc_chapters_chapter_lead ' , false );
150
+ if (!$ author_ids ) {
151
+ return [];
152
+ }
153
+ $ people = get_users ([
154
+ 'meta_key ' => 'ccgn-application-state ' ,
155
+ 'meta_value ' => 'accepted ' ,
156
+ 'include ' => $ author_ids ,
157
+ 'search ' => $ search
158
+ ]);
159
+ if (!empty ($ people )) {
160
+ return wp_list_pluck ($ people , 'display_name ' , 'ID ' );
161
+ }
162
+ return [];
163
+ })()
164
+ ]
165
+ ),
166
+ Node_Factory::make (
167
+ UI_Select2::class,
168
+ [
169
+ 'name ' => 'member_gnc ' ,
170
+ 'label ' => 'Global Network Council representative ' ,
171
+ 'attributes ' => [
172
+ 'class ' => 'widefat '
173
+ ],
174
+ 'properties ' => [
175
+ 'instance ' => [
176
+ 'multiple ' => false ,
177
+ 'minimumInputLength ' => 3 ,
178
+ 'ajax ' => [
179
+ 'url ' => admin_url ('admin-ajax.php?action=event-metabox__search-members&chapter_id= ' . get_the_ID ()),
180
+ ]
181
+ ],
182
+ 'description ' => 'Choose the Member of the representative to the network council '
183
+ ],
184
+ 'options ' => (function () {
185
+ $ author_ids = (array )get_post_meta (get_the_ID (), 'cc_chapters_member_gnc ' , false );
186
+ if (!$ author_ids ) {
187
+ return [];
188
+ }
189
+ $ people = get_users ([
190
+ 'meta_key ' => 'ccgn-application-state ' ,
191
+ 'meta_value ' => 'accepted ' ,
192
+ 'include ' => $ author_ids ,
193
+ 'search ' => $ search
194
+ ]);
195
+ if (!empty ($ people )) {
196
+ return wp_list_pluck ($ people , 'display_name ' , 'ID ' );
197
+ }
198
+ return [];
199
+ })()
200
+ ]
201
+ ),
202
+ Node_Factory::make (
203
+ Input_Url::class,
204
+ [
205
+ 'name ' => 'url ' ,
206
+ 'label ' => 'URL ' ,
207
+ 'attributes ' => [
208
+ 'class ' => 'widefat ' ,
209
+ 'placeholder ' => 'Chapter URL ' ,
210
+ 'type ' => 'url '
211
+ ]
212
+ ]
213
+ ),
214
+ Node_Factory::make (
215
+ Input_Url::class,
216
+ [
217
+ 'name ' => 'meeting_url ' ,
218
+ 'label ' => 'Meeting URL ' ,
219
+ 'attributes ' => [
220
+ 'class ' => 'widefat ' ,
221
+ 'placeholder ' => 'Chapter URL ' ,
222
+ 'type ' => 'url '
223
+ ],
224
+ 'properties ' => [
225
+ 'description ' => 'Chapter first meeting URL '
226
+ ]
227
+ ]
228
+ )
229
+ ];
230
+ }
231
+ public function sanitize_data (array $ data ) : array
232
+ {
233
+ $ sanitized = [];
234
+ foreach ($ data as $ key => $ val ) {
235
+ switch ($ key ) {
236
+ case 'date ' :
237
+ $ sanitized [$ key ] = $ val ;
238
+ break ;
239
+ case 'email ' :
240
+ $ sanitized [$ key ] = $ val ;
241
+ break ;
242
+ case 'chapter_country ' :
243
+ $ sanitized [$ key ] = $ val ;
244
+ case 'chapter_lead ' :
245
+ $ sanitized [$ key ] = $ val ;
246
+ case 'member_gnc ' :
247
+ $ sanitized [$ key ] = $ val ;
248
+ break ;
249
+ case 'url ' :
250
+ $ sanitized [$ key ] = esc_url_raw ($ val );
251
+ break ;
252
+ case 'meeting_url ' :
253
+ $ sanitized [$ key ] = esc_url_raw ($ val );
254
+ break ;
255
+ }
256
+ }
257
+ return $ sanitized ;
258
+ }
259
+ }
260
+
261
+ new Chapters_Metabox ('cc_chapters ' , 'Chapter information ' , 'cc_chapters ' , ['context ' => 'normal ' ]);
0 commit comments