Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Synchronize People

Jared Cobb edited this page Feb 26, 2018 · 2 revisions

Overview

You may find it useful to have a list of the Individuals that exist in CCB to be available in WordPress. This is an example implementation of a new CCB_Core_CPT extended class called CCB_Core_People.

Important Note: Please take care when synchronizing the personal information of individuals from CCB. This example will mark the post type as "not public" & "not publicly queryable", and the data is protected from the front end of the site. If this data needs to be made available on the front end, it's a good idea to secure that information in some other way (password protection, for example).

Recipe

This code can exist in your theme or your own plugin. Be sure your API user is authorized to use the individual_profiles service in CCB.

This snippet assumes that you will create a class filename of class-ccb-core-people.php and include it in your theme or plugin. So for example, in your theme's functions.php file you can do something like:

// Where /includes/ is the location you've placed this class file.
include_once ( get_template_directory() . '/includes/class-ccb-core-people.php' );
<?php
/**
 * An example implementation of a custom post type
 * that will synchronize individual profiles.
 *
 * @package    CCB_Core
 */

/**
 * An example implementation of a custom post type
 * that will synchronize individual profiles.
 *
 * @package    CCB_Core
 */
class CCB_Core_People extends CCB_Core_CPT {

	/**
	 * Name of the post type
	 *
	 * @var   string
	 */
	public $name = 'ccb_core_people';

	/**
	 * Use the constructor to add any actions / filters.
	 */
	public function __construct() {
		// Since this example will not implement any menu settings,
		// go ahead and force it to be enabled by default.
		$this->enabled = true;
		parent::__construct();
	}

	/**
	 * Setup the custom post type args
	 *
	 * @since    1.0.0
	 * @return   array $args for register_post_type
	 */
	public function get_post_args() {

		return [
			'labels' => [
				'name' => 'People',
				'singular_name' => 'Person',
				'all_items' => 'All People',
				'add_new' => 'Add New',
				'add_new_item' => 'Add New Person',
				'edit' => 'Edit',
				'edit_item' => 'Edit Person',
				'new_item' => 'New Person',
				'view_item' => 'View Person',
				'search_items' => 'Search People',
				'not_found' => 'Nothing found in database',
				'not_found_in_trash' => 'Nothing found in trash',
			],
			'description' => 'People synchronized from CCB',
			'public' => false,
			'publicly_queryable' => false,
			'exclude_from_search' => true,
			'show_ui' => true,
			'show_in_nav_menus' => false,
			'query_var' => false,
			'menu_position' => 8,
			'menu_icon' => 'dashicons-id',
			'rewrite' => [ 'slug' => 'people' ],
			'has_archive' => 'people',
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports' => [ 'title', 'author', 'custom-fields' ],
		];

	}

	/**
	 * Configure the options that users are allowed to set
	 *
	 * @since    1.0.0
	 * @param    array $settings The settings definitions.
	 * @return   array
	 */
	public function get_post_settings_definitions( $settings ) {
		// This method is required, but you do not need to actually create
		// a settings page or settings fields if you don't need them. Just
		// return the settings in that case.
		return $settings;
	}

	/**
	 * Define the mapping of CCB API fields to the Post fields
	 *
	 * @since    1.0.0
	 * @param    array $maps A collection of mappings from the API to WordPress.
	 * @return   array
	 */
	public function get_post_api_map( $maps ) {

		$maps[ $this->name ] = [
			'service' => 'individual_profiles', // This becomes the `srv` URL parameter in the API request.
			'data' => [
				'include_inactive' => false, // These are any additional URL parameters that need to be sent with the API request.
			],
			'nodes' => [ 'individuals', 'individual' ], // The path from <response> all the way to (and including) the CCB Entity.
			'fields' => [
				'full_name' => 'post_title', // Map to a Post Title from an entity's property.
				'giving_number' => 'post_meta', // Map to `post_meta` to have this saved as post meta.
				'family_position' => 'post_meta', // Any other properties can be mapped to post meta.
				'email' => 'post_meta',
				'birthday' => 'post_meta',
			],
		];

		return $maps;
	}

}

new CCB_Core_People();