Skip to content

Instantly share code, notes, and snippets.

@ggdx
Last active March 26, 2024 21:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ggdx/3626a00a2f2d13b988378a698f1a3ab1 to your computer and use it in GitHub Desktop.
Save ggdx/3626a00a2f2d13b988378a698f1a3ab1 to your computer and use it in GitHub Desktop.
Makes creating custom post types more OOP in WordPress.
<?php
class PostType
{
private $post_type = false;
private $name = false;
private $singular_name = false;
private $add_new = false;
private $add_new_item = false;
private $edit_item = false;
private $new_item = false;
private $view_item = false;
private $search_items = false;
private $not_found = false;
private $not_found_in_trash = false;
private $parent_item_colon = false;
private $all_items = false;
private $enter_title_here = false;
private $public = true;
private $exclude_from_search = false;
private $publicly_queryable = true;
private $menu_icon = 'dashicons-admin-post';
private $hierarchical = false;
private $supports = [];
private $labels = [];
private $args = [];
/**
* Constructor
*
* Starts this party off
*
* @param string $post_type The slug for the post type
* @return void
*/
public function __construct($post_type)
{
$this->post_type = $post_type;
}
/**
* Setters
*/
/**
* Set Name
*
* @param string $data
* @return $this
*/
public function setNameLabel(string $data)
{
$this->name = $data;
return $this;
}
/**
* Set Singular Name
*
* @param string $data
* @return $this
*/
public function setSingularNameLabel(string $data)
{
$this->singular_name = $data;
return $this;
}
/**
* Set Add New Label
*
* @param string $data
* @return $this
*/
public function setAddNewLabel(string $data)
{
$this->add_new = $data;
return $this;
}
/**
* Set Add New Item label
*
* @param string $data
* @return $this
*/
public function setAddNewItemLabel(string $data)
{
$this->add_new_item = $data;
return $this;
}
/**
* Set Edit Item label
*
* @param string $data
* @return $this
*/
public function setEditItemLabel(string $data)
{
$this->edit_item = $data;
return $this;
}
/**
* Set New Item Label
*
* @param string $data
* @return $this
*/
public function setNewItemLabel(string $data)
{
$this->new_item = $data;
return $this;
}
/**
* Set View Item label
*
* @param string $data
* @return $this
*/
public function setViewItemLabel(string $data)
{
$this->view_item = $data;
return $this;
}
/**
* Set Search Label
*
* @param string $data
* @return $this
*/
public function setSearchItemsLabel(string $data)
{
$this->search_items = $data;
return $this;
}
/**
* Set Not Found Label
*
* @param string $data
* @return $this
*/
public function setNotFoundLabel(string $data)
{
$this->not_found = $data;
return $this;
}
/**
* Set Not Found in Trash label
*
* @param string $data
* @return $this
*/
public function setNotFoundInTrashLabel(string $data)
{
$this->not_found_in_trash = $data;
return $this;
}
/**
* Set Parent Item Colon label
*
* @param string $data
* @return $this
*/
public function setParentItemColonLabel(string $data)
{
$this->parent_item_colon = $data;
return $this;
}
/**
* Set All Items label
*
* @param string $data
* @return $this
*/
public function setAllItemsLabel(string $data)
{
$this->all_items = $data;
return $this;
}
/**
* Set Enter Title Here label
*
* @param string $data
* @return $this
*/
public function setEnterTitleHereLabel(string $data)
{
$this->enter_title_here = $data;
return $this;
}
/**
* Set Public
*
* @param bool $data
* @return $this
*/
public function setPublic(bool $data)
{
$this->public = $data;
return $this;
}
/**
* Set Exclude from Search
*
* @param bool $data
* @return $this
*/
public function setExcludeFromSearch(bool $data)
{
$this->exclude_from_search = $data;
return $this;
}
/**
* Set Publicly Queriable
*
* @param bool $data
* @return $this
*/
public function setPubliclyQueryable(bool $data)
{
$this->publicly_queryable = $data;
return $this;
}
/**
* Set Dashicon
*
* @param string $data
* @return $this
*/
public function setMenuIcon(string $data)
{
$this->menu_icon = $data;
return $this;
}
/**
* Set Hierarchical
*
* @param string $data
* @return $this
*/
public function setHierarchical(bool $data)
{
$this->hierarchical = $data;
return $this;
}
/**
* Set Post Type Supports
* https://codex.wordpress.org/Function_Reference/post_type_supports
*
* @param string $data
* @return $this
*/
public function setSupports(array $data)
{
$this->supports = $data;
return $this;
}
/**
* Set Labels
*
* Sets the "Label" array
*
* @return $this
*/
private function setLabels()
{
$labels = [
'name' => 'getNameLabel',
'singular_name' => 'getSingularNameLabel',
'add_new' => 'getAddNewLabel',
'add_new_item' => 'getAddNewItemLabel',
'edit_item' => 'getEditItemLabel',
'new_item' => 'getNewItemLabel',
'view_item' => 'getViewItemLabel',
'search_items' => 'getSearchItemsLabel',
'not_found' => 'getNotFoundLabel',
'not_found_in_trash' => 'getNotFoundInTrashLabel',
'parent_item_colon' => 'getParentItemColonLabel',
'all_items' => 'getAllItemsLabel',
'enter_title_here' => 'getEnterTitleHereLabel',
];
$data = [];
foreach ($labels as $key => $value) {
if($this->$value() != false){
$data[$key] = $this->$value();
}
}
$this->labels = $data;
return $this;
}
/**
* Getters
*/
/**
* Get Post Type
*/
private function getPostType()
{
return $this->post_type;
}
/**
* Get Name
*/
private function getNameLabel()
{
return $this->name;
}
/**
* Get Singular Name
*/
private function getSingularNameLabel()
{
return $this->singular_name;
}
/**
* Get Add New Label
*/
private function getAddNewLabel()
{
return $this->add_new;
}
/**
* Get Add New Item label
*/
private function getAddNewItemLabel(){
return $this->add_new_item;
}
/**
* Get Edit Item label
*/
private function getEditItemLabel(){
return $this->edit_item;
}
/**
* Get New Item Label
*/
private function getNewItemLabel()
{
return $this->new_item;
}
/**
* Get View Item label
*/
private function getViewItemLabel()
{
return $this->view_item;
}
/**
* Get Search Label
*/
private function getSearchItemsLabel()
{
return $this->search_items;
}
/**
* Get Not Found Label
*/
private function getNotFoundLabel()
{
return $this->not_found;
}
/**
* Get Not Found in Trash label
*/
private function getNotFoundInTrashLabel()
{
return $this->not_found_in_trash;
}
/**
* Get Parent Item Colon label
*/
private function getParentItemColonLabel()
{
return $this->parent_item_colon;
}
/**
* Get All Items label
*/
private function getAllItemsLabel()
{
return $this->all_items;
}
/**
* Get Enter Title Here label
*/
private function getEnterTitleHereLabel()
{
return $this->enter_title_here;
}
/**
* Get Public
*/
private function getPublic()
{
return $this->public;
}
/**
* Get Exclude from Search
*/
private function getExcludeFromSearch()
{
return $this->exclude_from_search;
}
/**
* Get Publicly Queriable
*/
private function getPubliclyQueryable()
{
return $this->publicly_queryable;
}
/**
* Get Dashicon
*/
private function getMenuIcon()
{
return $this->menu_icon;
}
/**
* Get Hierarchical
*/
private function getHierarchical()
{
return $this->hierarchical;
}
/**
* Get Post Type Supports
*/
private function getSupports()
{
return $this->supports;
}
/**
* Get Labels
*/
private function getLabels()
{
return $this->labels;
}
/**
* Register
*
* Registers the post type
*
* @return void
*/
public function register()
{
$this->setLabels()->setArgs();
register_post_type($this->post_type, $this->args);
flush_rewrite_rules();
}
/**
* Set Args
*
* Puts together the post type array
*
* @return obj
*/
private function setArgs()
{
$this->args = [
'labels' => $this->labels,
'supports' => $this->supports,
'public' => $this->public,
'exclude_from_search' => $this->exclude_from_search,
'publicly_queryable' => $this->publicly_queryable,
'menu_icon' => $this->menu_icon,
'hierarchical' => $this->hierarchical,
'rewrite' => [
'slug' => $this->post_type
]
];
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment