Forked from lucasstark/acf-post-type-model-example.php
Created
February 19, 2016 12:33
-
-
Save joedooley/f24a8253a09bbef9723b to your computer and use it in GitHub Desktop.
Example of wrapping a custom post type inside of a model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AWP_CTP_Model_Contact { | |
/** | |
* Gets an array of data for the object. Use this to get data to pass to the constructor. | |
* @param int $id | |
* @return array | |
*/ | |
public static function get_data( $id ) { | |
$post = get_post( $id ); | |
setup_postdata( $post ); | |
/** For the example gist I've just hardcoded some things | |
*/ | |
if ( has_post_thumbnail( $post->ID ) ) { | |
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); | |
} else { | |
//No featured image, use default. | |
$image = array( | |
'' | |
); | |
} | |
$general_name_format = get_option( 'awp_ctp_contact_general_name_format', '{first} {last}, {suffix}' ); | |
$search_name_format = get_option( 'awp_ctp_contact_search_name_format', '{last}, {first}, {suffix}' ); | |
$f = get_field( 'first_name', $post->ID ); | |
$l = get_field( 'last_name', $post->ID ); | |
$s = get_field( 'suffix', $post->ID ); | |
$data = array( | |
'ID' => $post->ID, | |
'title' => trim( str_replace( array('{first}', '{last}', '{suffix}'), array($f, $l, $s), $general_name_format ), ", " ), | |
'search_title' => trim( str_replace( array('{first}', '{last}', '{suffix}'), array($f, $l, $s), $search_name_format ), ", " ), | |
'excerpt' => apply_filters( 'get_the_excerpt', $post->post_excerpt ), | |
'content' => apply_filters( 'the_content', $post->post_content ), | |
'first_name' => $f, | |
'last_name' => $l, | |
'suffix' => $s, | |
'phone_primary' => get_field( 'phone_primary', $post->ID ), | |
'email_primary' => get_field( 'email_primary', $post->ID ), | |
'permalink' => get_permalink( $post->ID ), | |
'featured_image_url' => $image[0], | |
'office_location' => get_field( 'office_location', $post->ID ), | |
'address_one' => get_field( 'address_one', $post->ID ), | |
'address_two' => get_field( 'address_two', $post->ID ), | |
'address_city' => get_field( 'address_city', $post->ID ), | |
'address_region' => get_field( 'address_region', $post->ID ), | |
'address_postal_code' => get_field( 'address_postal_code', $post->ID ), | |
'office_hours' => get_field( 'office_hours', $post->ID ), | |
'repeater_sample' => get_field( 'repeater_sample', $post->ID ) | |
); | |
wp_reset_postdata(); | |
return apply_filters( 'awp_ctp_model_contact_data', $data, $id ); | |
} | |
/** | |
* Get's an instance of a AWP_CTP_Model_Contact object. | |
* @param int $id | |
* @return \AWP_CTP_Model_Contact | |
*/ | |
public static function get_instance( $id ) { | |
$data = self::get_data( $id ); | |
return new AWP_CTP_Model_Contact( $data ); | |
} | |
private $__data; | |
private $_id; | |
private $_content; | |
private $_excerpt; | |
private $_permalink; | |
private $_featured_image_url; | |
private $_display_title; | |
private $_search_title; | |
private $_first_name; | |
private $_last_name; | |
private $_suffix; | |
private $_phone_primary; | |
private $_email_primary; | |
private $_office_location; | |
private $_address_one; | |
private $_address_two; | |
private $_address_city; | |
private $_address_region; | |
private $_address_postal_code; | |
private $_office_hours; | |
/** | |
* | |
* @var AWP_CTP_Model_Repeater_Sample[] | |
*/ | |
private $_repeater_samples; | |
public function __construct( $data ) { | |
$this->__data = $data; | |
/* | |
* There is a lot of different ways to setup the data for the object. | |
* I like to be explicit about it, however you could reference WP_Post class to see how to do it with php __get function. | |
*/ | |
$this->set_value( '_id', 'ID' ); | |
$this->set_value( '_display_title', 'title' ); | |
$this->set_value( '_search_title', 'search_title' ); | |
$this->set_value( '_content', 'content' ); | |
$this->set_value( '_excerpt', 'excerpt' ); | |
$this->set_value( '_permalink', 'permalink' ); | |
$this->set_value( '_featured_image_url', 'featured_image_url' ); | |
$this->set_value( '_first_name', 'first_name' ); | |
$this->set_value( '_last_name', 'last_name' ); | |
$this->set_value( '_suffix', 'suffix' ); | |
$this->set_value( '_office_location', 'office_location' ); | |
$this->set_value( '_address_one', 'address_one' ); | |
$this->set_value( '_address_two', 'address_two' ); | |
$this->set_value( '_address_city', 'address_city' ); | |
$this->set_value( '_address_region', 'address_region' ); | |
$this->set_value( '_address_postal_code', 'address_postal_code' ); | |
$this->set_value( '_phone_primary', 'phone_primary' ); | |
$this->set_value( '_email_primary', 'email_primary' ); | |
$this->set_value( '_departments', 'departments' ); | |
$this->set_value( '_office_hours', 'office_hours' ); | |
if ( isset( $this->__data['repeater_sample'] ) ) { | |
foreach ( $this->__data['repeater_sample'] as $rd ) { | |
//Pass in the repeater row data to the model. | |
$this->_repeater_samples[] = new AWP_CTP_Model_Repeater_Sample( $rd ); | |
} | |
} | |
} | |
/** | |
* Helper method to set model values in a safe way | |
* @param string $property | |
* @param string $key | |
*/ | |
protected function set_value( $property, $key ) { | |
if ( isset( $this->__data[$key] ) ) { | |
if ( property_exists( $this, $property ) ) { | |
$this->{$property} = $this->__data[$key]; | |
} | |
} | |
} | |
/** | |
* The ID ( Remote ) of the Contact | |
* @return int | |
*/ | |
public function get_ID() { | |
return $this->_id; | |
} | |
/** | |
* Contact Title for Single Results | |
* @return string | |
*/ | |
public function get_display_title() { | |
return apply_filters( 'awp_ctp_contact_the_display_title', $this->_display_title ); | |
} | |
/** | |
* Contact Title for Search Results | |
* @return string | |
*/ | |
public function get_search_title() { | |
return apply_filters( 'awp_ctp_contact_the_search_title', $this->_search_title ); | |
} | |
/** | |
* Contact Excerpt ( Typically a very short description of the person, typically not used. ) | |
* @return string | |
*/ | |
public function get_the_excerpt() { | |
return $this->_excerpt; | |
} | |
/** | |
* Contact Content ( Typically a description or other information about the person ) | |
* @return string | |
*/ | |
public function get_the_content() { | |
return $this->_content; | |
} | |
/** | |
* Contact Link ( Remote URL ) | |
* @return string | |
*/ | |
public function get_permalink() { | |
return $this->_permalink; | |
} | |
/** | |
* Contact Featured Image URL | |
* @return string | |
*/ | |
public function get_featured_image_url() { | |
return $this->_featured_image_url; | |
} | |
/** | |
* Contact First Name | |
* @return string | |
*/ | |
public function get_first_name() { | |
return $this->_first_name; | |
} | |
/** | |
* Contact Last Name | |
* @return string | |
*/ | |
public function get_last_name() { | |
return $this->_last_name; | |
} | |
/** | |
* Contact Suffix ( Dr. PhD. Sr. Jr, etc.. ) | |
* @return string | |
*/ | |
public function get_suffix() { | |
return $this->_suffix; | |
} | |
/** | |
* Contact Phone Primary | |
* @return string | |
*/ | |
public function get_phone_primary() { | |
return apply_filters( 'awp_ctp_contact_format_phone_number', $this->_phone_primary, $this ); | |
} | |
/** | |
* Contact Primary Email | |
* @return string | |
*/ | |
public function get_email_primary() { | |
return $this->_email_primary; | |
} | |
/** | |
* Contact Office Location | |
* @return string | |
*/ | |
public function get_office_location() { | |
return $this->_office_location; | |
} | |
/** | |
* Contact Address Line One | |
* @return string | |
*/ | |
public function get_address_one() { | |
return $this->_address_one; | |
} | |
/** | |
* Contact Address Line Two | |
* @return string | |
*/ | |
public function get_address_two() { | |
return $this->_address_two; | |
} | |
/** | |
* Contact City | |
* @return string | |
*/ | |
public function get_address_city() { | |
return $this->_address_city; | |
} | |
/** | |
* Contact Region ( State in US ) | |
* @return string | |
*/ | |
public function get_address_region() { | |
return $this->_address_region; | |
} | |
/** | |
* Contact Postal Code | |
* @return string | |
*/ | |
public function get_address_postal_code() { | |
return $this->_address_postal_code; | |
} | |
/** | |
* Contact Office Hours | |
* @return string | |
*/ | |
public function get_office_hours() { | |
return $this->_office_hours; | |
} | |
/** | |
* An example repeater. | |
* @return AWP_CTP_Model_Repeater_Sample[] | |
*/ | |
public function get_repeater_samples() { | |
return $this->_repeater_samples ? $this->_repeater_samples : array(); | |
} | |
} | |
/** | |
* Just a simple example of how you might include a repeater as its own entity. This would normally be in it's own file, just placed here for the gist | |
*/ | |
class AWP_CTP_Model_Repeater_Sample { | |
private $_data; | |
public function __construct( $data ) { | |
$this->_data = $data; | |
} | |
public function get_field_one() { | |
return $this->_data['custom_field_one']; | |
} | |
public function get_field_two() { | |
return $this->_data['custom_field_two']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment