Skip to content

Instantly share code, notes, and snippets.

@wkirby
Last active March 25, 2018 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkirby/6ad67c0feb58ba7171cb2fe9a091985e to your computer and use it in GitHub Desktop.
Save wkirby/6ad67c0feb58ba7171cb2fe9a091985e to your computer and use it in GitHub Desktop.
1-to-1 Relationship Fields for Carbon Fields
<?php
namespace Carbon_Fields\Field;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* Taxonomy field class.
*/
class Related_Post_Field extends Select_Field
{
protected $post_type = array();
/**
* Stores the post type as an array
*
* @var array|string
**/
public function set_post_type( $post_type ) {
if ( ! is_array( $post_type ) ) {
$post_type = array( $post_type );
}
$this->post_type = $post_type;
return $this;
}
/**
* Get posts for each post type passed to field type
*
* @return array $posts The selectable options of the relationship field.
*/
private function options_for_post_type($post_type)
{
$filter_name = 'carbon_relationship_options_' . $this->get_name() . '_post_' . $post_type;
$args = apply_filters( $filter_name, array(
'post_type' => $post_type,
'posts_per_page' => -1,
'fields' => 'ids',
'suppress_filters' => false,
));
return array_reduce(get_posts($args), function(&$result, $id) {
$result[$id] = $this->get_title_by_type( $id, 'post', $post_type );
return $result;
});
}
/**
* Generate the item options for the select field.
*
* @return array $options The selectable options.
*/
protected function load_options() {
$options = array();
foreach ( $this->post_type as $post_type ) {
$options = array_merge( $options, $this->options_for_post_type($post_type) );
}
$this->options = $options;
}
/**
* Used to get the title of an item.
*
* Can be overriden or extended by the `carbon_relationship_title` filter.
*
* @param int $id The database ID of the item.
* @param string $type Item type (post, term, user, comment, or a custom one).
* @param string $subtype The subtype - "page", "post", "category", etc.
* @return string $title The title of the item.
*/
public function get_title_by_type( $id, $type, $subtype = '' ) {
$title = get_the_title( $id );
if ( ! $title ) {
$title = '(no title) - ID: ' . $id;
}
/**
* Filter the title of the relationship item.
*
* @param string $title The unfiltered item title.
* @param string $name Name of the relationship field.
* @param int $id The database ID of the item.
* @param string $type Item type (post, term, user, comment, or a custom one).
* @param string $subtype Subtype - "page", "post", "category", etc.
*/
return apply_filters( 'carbon_relationship_title', $title, $this->get_name(), $id, $type, $subtype );
}
}
<?php
namespace Carbon_Fields\Field;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* Taxonomy field class.
*/
class Taxonomy_Field extends Select_Field
{
protected $taxonomy;
/**
* Stores the taxonomy for the dropdown
*
* @var string
**/
public function set_taxonomy($taxonomy)
{
if ( taxonomy_exists($taxonomy) ) {
$this->min = $this->add_options($this->values_for_taxonomy($taxonomy));
} else {
Incorrect_Syntax_Exception::raise('Only valid taxonomies are allowed in the <code>set_taxonomy()</code> method.');
}
return $this;
}
private function values_for_taxonomy($taxonomy)
{
$terms = get_terms(array('taxonomy' => $taxonomy, 'hide_empty' => false));
return array_reduce($terms, function(&$result, $item) {
$result[$item->term_id] = $item->name;
return $result;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment