-
Notifications
You must be signed in to change notification settings - Fork 4
Custom Post Types
Creating a Custom Post Type Class is how you map a CCB Entity to a Custom Post Type in WordPress. CCB Core includes two Custom Post Type Classes by default: CCB_Core_Group and CCB_Core_Calendar which map groups and public calendar events.
| Class | API Service | Entity | Custom Post Type |
|---|---|---|---|
CCB_Core_Group |
group_profiles |
<group> |
ccb_core_group |
CCB_Core_Calendar |
public_calendar_listing |
<item> |
ccb_core_calendar |
There is an example class in /includes/post-types/class-example-post-type.php. You can also look at the CCB_Core_Group and CCB_Core_Calendar classes.
You can create your own classes to implement your own mappings of CCB API services to Custom Post Types.
All Custom Post Type Classes inhert from (extend) a parent class called CCB_Core_CPT. Your class is required to implement three methods which help to define the mappings and functionality.
This method should return an array of $args that define this new WordPress Custom Post Type. These are the same $args that are used by register_post_type.
| Parameter | Description |
|---|---|
$maps |
An array of maps that define the API service name, nodes, and mapping of Entity properties to post properties and post meta. |
The $maps array is a collection of all API mappings that have been registered, so the goal is to insert your own additional mapping to be included with the others.
A $map consists of four array elements:
| Element | Data Type | Description |
|---|---|---|
service |
string | The CCB API service name (i.e. group_profiles, public_calendar_listing, etc) |
data |
array | This is a collection of key / value pairs. Any additional parameters that need to be sent along with the CCB API request are included here. For example, the public_calendar_listing requires that we include date_start, so we would include 'data' => [ 'date_start' => '2018-01-01' ]. |
nodes |
array | This is a path from the <response> node all the way to (and including) the node that defines the CCB Entity. For example, the nodes value for group_profiles would be 'nodes' => [ 'groups', 'group' ]. |
fields |
array | This is a collection of key / value pairs. Each item is a mapping from a CCB Entity property to a WP_Post property name or use post_meta to have it saved as post meta. |
Each $map in the $maps array is inserted with its custom post type slug as the key. For example:
[Array]
(
['ccb_core_groups'] => {all mapping info about groups}
['ccb_core_calendar'] => {all mapping info about events}
[{your_new_post_type_slug}] => {all your mapping info}
)public function get_post_api_map( $maps ) {
$maps['ccb_core_person'] = [
'service' => 'individual_profiles',
'data' => [
'include_inactive' => true,
],
'nodes' => [ 'individuals', 'individual' ],
'fields' => [
'full_name' => 'post_title',
'family_position' => 'post_content',
'email' => 'post_meta',
'addresses' => 'post_meta',
],
];
return $maps;
}| Parameter | Description |
|---|---|
$settings |
An array that contains a settings page, settings section, and settings fields, that get used by add_submenu_page() and settings_fields(). |
This method is used to build any settings pages & fields that should be exposed to the user. It gives you the ability to let users customize the integration. The settings get stored in the global ccb_core_settings array.
Creating a settings page & fields is completely optional, however you still need to implement the method. If you do not want to create any settings for this map you should do:
public function get_post_settings_definitions( $settings ) {
return $settings;
}