Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Registrar classes #83

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 49 additions & 25 deletions src/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ class Columns
public $sortable = [];

/**
* Set the all columns
* @param array $columns an array of all the columns to replace
* Set the all columns.
*
* @param array $columns an array of all the columns to replace.
*/
public function set($columns)
{
$this->items = $columns;
}

/**
* Add a new column
* @param string $column the slug of the column
* @param string $label the label for the column
* Add a new column.
*
* @param string $column the slug of the column.
* @param string $label the label for the column.
*
* @return PostType\Columns
*/
public function add($columns, $label = null)
{
Expand All @@ -90,8 +94,11 @@ public function add($columns, $label = null)
}

/**
* Add a column to hide
* @param string $column the slug of the column to hdie
* Add a column to hide.
*
* @param string $column the slug of the column to hide.
*
* @return PostType\Columns
*/
public function hide($columns)
{
Expand All @@ -107,9 +114,12 @@ public function hide($columns)
}

/**
* Set a custom callback to populate a column
* @param string $column the column slug
* @param mixed $callback callback function
* Set a custom callback to populate a column.
*
* @param string $column the column slug.
* @param mixed $callback callback function.
*
* @return PostType\Columns
*/
public function populate($column, $callback)
{
Expand All @@ -119,8 +129,11 @@ public function populate($column, $callback)
}

/**
* Define the postion for a columns
* Define the postion for a columns.
*
* @param string $columns an array of columns
*
* @return PostType\Columns
*/
public function order($columns)
{
Expand All @@ -132,10 +145,13 @@ public function order($columns)
}

/**
* Set columns that are sortable
* Set columns that are sortable.
*
* @param string $column the slug of the column
* @param string $meta_value the meta_value to orderby
* @param boolean $is_num whether to order by string/number
*
* @return PostType\Columns
*/
public function sortable($sortable)
{
Expand All @@ -148,7 +164,10 @@ public function sortable($sortable)

/**
* Check if an orderby field is a custom sort option.
* @param string $orderby the orderby value from query params
*
* @param string $orderby the orderby value from query params.
*
* @return boolean
*/
public function isSortable($orderby)
{
Expand All @@ -170,7 +189,10 @@ public function isSortable($orderby)

/**
* Get meta key for an orderby.
* @param string $orderby the orderby value from query params
*
* @param string $orderby the orderby value from query params
*
* @return mixed
*/
public function sortableMeta($orderby)
{
Expand All @@ -191,46 +213,48 @@ public function sortableMeta($orderby)
}

/**
* Modify the columns for the object
* Modify the columns for the object.
*
* @param array $columns WordPress default columns
* @return array The modified columns
*
* @return array
*/
public function modifyColumns($columns)
{
// if user defined set columns, return those
// If user defined set columns, return those.
if (!empty($this->items)) {
return $this->items;
}

// add additional columns
// Add additional columns.
if (!empty($this->add)) {
foreach ($this->add as $key => $label) {
$columns[$key] = $label;
}
}

// unset hidden columns
// Unset hidden columns.
if (!empty($this->hide)) {
foreach ($this->hide as $key) {
unset($columns[$key]);
}
}

// if user has made added custom columns
// If user has made added custom columns.
if (!empty($this->positions)) {
foreach ($this->positions as $key => $position) {
// find index of the element in the array
// Find index of the element in the array.
$index = array_search($key, array_keys($columns));
// retrieve the element in the array of columns
// Retrieve the element in the array of columns.
$item = array_slice($columns, $index, 1);
// remove item from the array
// Remove item from the array.
unset($columns[$key]);

// split columns array into two at the desired position
// Split columns array into two at the desired position.
$start = array_slice($columns, 0, $position, true);
$end = array_slice($columns, $position, count($columns) - 1, true);

// insert column into position
// Insert column into position.
$columns = $start + $item + $end;
}
}
Expand Down
74 changes: 74 additions & 0 deletions src/Contracts/PostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace PostTypes\Contracts;

interface PostType
{
/**
* Set the Post Type names.
*
* @param array $names An array of post type names.
*
* @return PostTypes\PostType
*/
public function names(array $names);

/**
* Set the Post Type options.
*
* @param array $options An array of post type options.
*
* @return PostTypes\PostType
*/
public function options(array $options);

/**
* Set the Post Type labels.
*
* @param array $labels An array of post type labels.
*
* @return PostTypes\PostType
*/
public function labels(array $labels);

/**
* Set the Post Type Taxonomies.
*
* @param array $taxonomies An array of taxonomies registered to the post type.
*
* @return PostType\PostType
*/
public function taxonomy(array $taxonomies);

/**
* Set the admin filters for the Post Type.
*
* @param array $filters An array of Taxonomy names to use as filters.
*
* @return PostType\PostType
*/
public function filters(array $filters);

/**
* Set the Post Type icon.
*
* @param string $icon The dashicon icon name.
*
* @return PostType\PostType
*/
public function icon(string $icon);

/**
* Returns the Column Manager.
*
* @return PostTypes\Columns
*/
public function columns();

/**
* Registers the Post Type to WordPress.
*
* @return void
*/
public function register();
}
56 changes: 56 additions & 0 deletions src/Contracts/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace PostTypes\Contracts;

interface Taxonomy
{
/**
* Set the Taxonomy names.
*
* @param array $names An array of taxonomy names.
*
* @return PostTypes\Taxonomy
*/
public function names(array $names);

/**
* Set the Taxonomy options.
*
* @param array $options An array of taxonomy options.
*
* @return PostTypes\Taxonomy
*/
public function options(array $options);

/**
* Set the Taxonomy labels.
*
* @param array $labels An array of taxonomy labels.
*
* @return PostTypes\Taxonomy
*/
public function labels(array $labels);

/**
* Set the post types to register to the taxonomy.
*
* @param array $posttypes An array of post types to register to the taxonomy.
*
* @return PostTypes\Taxonomy
*/
public function posttype(array $posttypes);

/**
* Returns the Column Manager.
*
* @return PostTypes\Columns
*/
public function columns();

/**
* Registers the Taxonomy to WordPress.
*
* @return void
*/
public function register();
}
Loading