Last active
August 29, 2015 14:01
-
-
Save matthewhaworth/bf89572c07b7e546e1eb to your computer and use it in GitHub Desktop.
Adanced Custom Fields Object Oriented
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 | |
$fieldset = new CodepoolFieldset('two_column_fieldset', 'Two Column', array('the_content')); | |
$fieldset->addField(new CodepoolField('left_content_title', 'Left Content Title', 'text')); | |
$fieldset->addField(new CodepoolField('left_content_subtitle', 'Left Content Subtitle', 'text')); | |
$fieldset->addField(new CodepoolField('left_content_image', 'Left Image', 'image')); | |
$fieldset->addField(new CodepoolField('left_content', 'Left Content', 'wp_wysiwyg')); | |
$fieldset->addField(new CodepoolField('right_content_title', 'Right Content Title', 'text')); | |
$fieldset->addField(new CodepoolField('right_content_subtitle', 'Right Content Subtitle', 'text')); | |
$fieldset->addField(new CodepoolField('right_content_image', 'Right Image', 'image')); | |
$fieldset->addField(new CodepoolField('right_content', 'Right Content', 'wp_wysiwyg')); | |
$fieldset->addLocation(new CodepoolPage('test-page-1')); | |
$fieldset->addLocation(new CodepoolPage('test-page-2')); | |
$fieldset->register(); | |
// Vague implementation | |
abstract class CodepoolLocationAbstract | |
{ | |
public function map() | |
{ | |
return array(array( | |
'param' => $this->getType(), | |
'operator' => '==', | |
'value' => $this->getId(), | |
'order_no' => 0, | |
'group_no' => 0, | |
)); | |
} | |
} | |
class CodepoolPage extends CodepoolLocationAbstract | |
{ | |
protected $_type = 'page'; | |
protected $_id; | |
public function __construct($pageSlug) | |
{ | |
$this->_id = codepool_get_page_id_by_slug($pageSlug); | |
} | |
public function getType() | |
{ | |
return $this->_type; | |
} | |
public function getId() | |
{ | |
return $this->_id; | |
} | |
} | |
class CodepoolPostType extends CodepoolLocationAbstract | |
{ | |
protected $_type = 'post_type'; | |
protected $_id; | |
public function __construct($pageType) | |
{ | |
$this->_id = $pageType; | |
} | |
public function getType() | |
{ | |
return $this->_type; | |
} | |
public function getId() | |
{ | |
return $this->_id; | |
} | |
} | |
class CodepoolField | |
{ | |
protected $_fieldsetId; | |
protected $_key; | |
protected $_name; | |
protected $_label; | |
protected $_type; | |
public function __construct($name, $label, $type) | |
{ | |
$this->_name = $name; | |
$this->_label = $label; | |
$this->_type = $type; | |
} | |
public function setFieldsetId($fieldsetId) | |
{ | |
$this->_fieldsetId = $fieldsetId; | |
} | |
public function getKey() | |
{ | |
return $this->_key; | |
} | |
public function getName() | |
{ | |
return $this->_name; | |
} | |
public function getLabel() | |
{ | |
return $this->_label; | |
} | |
public function getType() | |
{ | |
return $this->_type; | |
} | |
public function map() | |
{ | |
$key = "{$this->_fieldsetId}_{$this->_name}_{$this->_type}"; | |
return array( | |
'key' => $key, | |
'label' => $this->_label, | |
'name' => $this->_name, | |
'type' => $this->_type, | |
'default_value' => '', | |
'toolbar' => 'full', | |
'media_upload' => 'no', | |
'formatting' => 'none', | |
); | |
} | |
} | |
class CodepoolFieldset | |
{ | |
protected $_fieldsetId; | |
protected $_fieldsetLabel; | |
protected $_fields = array(); | |
protected $_locations = array(); | |
protected $_sectionsToHide = array(); | |
public function __construct($fieldsetId, $fieldsetLabel, $sectionsToHide = array()) | |
{ | |
$this->_fieldsetId = $fieldsetId; | |
$this->_fieldsetLabel = $fieldsetLabel; | |
$this->_sectionsToHide = $sectionsToHide; | |
} | |
public function addField(CodepoolField $field) | |
{ | |
$this->_fields[] = $field; | |
} | |
public function addLocation(CodepoolLocationAbstract $location) | |
{ | |
$this->_locations[] = $location; | |
} | |
public function register() | |
{ | |
$fields = array(); | |
foreach ($this->_fields as $_field) { | |
$_field->setFieldsetId($this->_fieldsetId); | |
$fields[] = $_field->map(); | |
} | |
$locations = array(); | |
foreach ($this->_locations as $_location) { | |
$locations[] = $_location->map(); | |
} | |
$data = array( | |
'id' => $this->_fieldsetId, | |
'title' => $this->_fieldsetLabel, | |
'fields' => $fields, | |
'location' => $locations, | |
'options' => array( | |
'position' => 'normal', | |
'layout' => 'default', | |
'hide_on_screen' => $this->_sectionsToHide, | |
), | |
'menu_order' => 0, | |
); | |
register_field_group($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment