Skip to content

Commit 265df95

Browse files
Hugo SolarHugo Solar
Hugo Solar
authored and
Hugo Solar
committed
New Chapter section backend && queulat toolset mu-plugin
1 parent 31ed589 commit 265df95

9 files changed

+966
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Plugin Name: Chapters Custom Post Type Plugin
4+
* Plugin URI:
5+
* Description: Global Network chapters Wordpress Custom Post Type
6+
* Version: 0.1.0
7+
* Author:
8+
* Author URI:
9+
* License: GPL-3.0-or-later
10+
*/
11+
12+
register_activation_hook( __FILE__, function(){
13+
require_once __DIR__ .'/class-cc-chapters-post-type.php';
14+
Cc_Chapters_Post_Type::activate_plugin();
15+
});
16+
17+
add_action('plugins_loaded', function(){
18+
require_once __DIR__ .'/class-cc-chapters-post-type.php';
19+
require_once __DIR__ .'/class-cc-chapters-post-query.php';
20+
require_once __DIR__ .'/class-cc-chapters-post-object.php';
21+
require_once __DIR__ . '/class-cc-chapters-metabox.php';
22+
});
23+
24+
add_action('init', ['Cc_Chapters_Post_Type', 'register_post_type']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
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']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Queulat\Post_Object;
4+
5+
class Cc_Chapters_Post_Object extends Post_Object {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Queulat\Post_Query;
4+
5+
class Cc_Chapters_Post_Query extends Post_Query {
6+
public function get_post_type() : string {
7+
return 'cc_chapters';
8+
}
9+
public function get_decorator() : string {
10+
return Cc_Chapters_Post_Object::class;
11+
}
12+
public function get_default_args() : array {
13+
return [];
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
use Queulat\Post_Type;
4+
5+
class Cc_Chapters_Post_Type extends Post_Type {
6+
public function get_post_type() : string {
7+
return 'cc_chapters';
8+
}
9+
public function get_post_type_args() : array {
10+
return [
11+
'label' => __('Chapters', 'cpt_cc_chapters'),
12+
'labels' => [
13+
'name' => __('Chapters', 'cpt_cc_chapters'),
14+
'singular_name' => __('Chapters', 'cpt_cc_chapters'),
15+
'add_new' => __('Add New', 'cpt_cc_chapters'),
16+
'add_new_item' => __('Add New Page', 'cpt_cc_chapters'),
17+
'edit_item' => __('Edit Page', 'cpt_cc_chapters'),
18+
'new_item' => __('New Page', 'cpt_cc_chapters'),
19+
'view_item' => __('View Page', 'cpt_cc_chapters'),
20+
'view_items' => __('View Pages', 'cpt_cc_chapters'),
21+
'search_items' => __('Search Pages', 'cpt_cc_chapters'),
22+
'not_found' => __('No pages found.', 'cpt_cc_chapters'),
23+
'not_found_in_trash' => __('No pages found in Trash.', 'cpt_cc_chapters'),
24+
'parent_item_colon' => __('Parent Page:', 'cpt_cc_chapters'),
25+
'all_items' => __('Chapters', 'cpt_cc_chapters'),
26+
'archives' => __('Chapters', 'cpt_cc_chapters'),
27+
'attributes' => __('Page Attributes', 'cpt_cc_chapters'),
28+
'insert_into_item' => __('Insert into page', 'cpt_cc_chapters'),
29+
'uploaded_to_this_item' => __('Uploaded to this page', 'cpt_cc_chapters'),
30+
'featured_image' => __('Featured Image', 'cpt_cc_chapters'),
31+
'set_featured_image' => __('Set featured image', 'cpt_cc_chapters'),
32+
'remove_featured_image' => __('Remove featured image', 'cpt_cc_chapters'),
33+
'use_featured_image' => __('Use as featured image', 'cpt_cc_chapters'),
34+
'filter_items_list' => __('Filter pages list', 'cpt_cc_chapters'),
35+
'items_list_navigation' => __('Pages list navigation', 'cpt_cc_chapters'),
36+
'items_list' => __('Pages list', 'cpt_cc_chapters'),
37+
'item_published' => __('Page published.', 'cpt_cc_chapters'),
38+
'item_published_privately' => __('Page published privately.', 'cpt_cc_chapters'),
39+
'item_reverted_to_draft' => __('Page reverted to draft.', 'cpt_cc_chapters'),
40+
'item_scheduled' => __('Page scheduled.', 'cpt_cc_chapters'),
41+
'item_updated' => __('Page updated.', 'cpt_cc_chapters'),
42+
'menu_name' => __('Chapters', 'cpt_cc_chapters'),
43+
'name_admin_bar' => __('Chapters', 'cpt_cc_chapters'),
44+
],
45+
'description' => __('Global Network chapters Wordpress Custom Post Type', 'cpt_cc_chapters'),
46+
'public' => true,
47+
'hierarchical' => true,
48+
'exclude_from_search' => false,
49+
'publicly_queryable' => true,
50+
'show_ui' => true,
51+
'show_in_menu' => true,
52+
'show_in_nav_menus' => true,
53+
'show_in_admin_bar' => true,
54+
'menu_position' => 20,
55+
'menu_icon' => 'dashicons-location',
56+
'capability_type' => [
57+
0 => 'cc_chapter',
58+
1 => 'cc_chapters',
59+
],
60+
'map_meta_cap' => true,
61+
'register_meta_box_cb' => null,
62+
'taxonomies' => [],
63+
'has_archive' => true,
64+
'query_var' => 'cc_chapters',
65+
'can_export' => true,
66+
'delete_with_user' => true,
67+
'rewrite' => [
68+
'with_front' => true,
69+
'feeds' => true,
70+
'pages' => true,
71+
'slug' => 'cc_chapters',
72+
'ep_mask' => 1,
73+
],
74+
'supports' => [
75+
0 => 'title',
76+
1 => 'editor',
77+
2 => 'thumbnail',
78+
3 => 'excerpt',
79+
],
80+
'show_in_rest' => true,
81+
'rest_base' => false,
82+
'rest_controller_class' => false
83+
];
84+
}
85+
}

queulat/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

0 commit comments

Comments
 (0)