| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- /**
- * Craft in America Custom Types Plugin
- *
- * @since 1.0.0
- * @package cia_post_types
- *
- * @wordpress-plugin
- * Plugin Name: Craft in America - Content Types
- * Plugin URI:
- * Description: Adds custom custom types, and p2p relationships
- * Version: 1.0.0
- * Author: TOJ <john@yvvas.com>
- */
-
- // If this file is called directly, abort.
- if ( ! defined( 'WPINC' ) ) { die; }
-
- require_once 'includes/cmb2/init.php';
-
- include('includes/custom-types.php');
- include('includes/p2p-mappings.php');
-
- /**
- * Class that holds all the necessary
- * functionality to build custom post types
- */
- class PostType {
- /**
- * The custom post type slug
- * @var string
- */
- private $post_type;
- /**
- * The custom post type icon
- * @var string
- */
- private $icon;
-
- function __construct($post_type, $icon) {
- $this->post_type = $post_type;
- $this->icon = $icon;
- }
-
- /** Register custom post type call-back */
- public function register_post_type() {
- $args = [
- 'label' => esc_html( $this->post_type, 'test-plugin' ),
- 'public' => true,
- 'menu_position' => 47,
- 'menu_icon' => $this->icon,
- 'supports' => ['title', 'editor', 'revisions', 'thumbnail'],
- 'has_archive' => false,
- 'show_in_rest' => true,
- 'publicly_queryable' => false
- ];
-
- register_post_type( $this->post_type, $args );
- }
- }
-
- /**
- * Plugin Logic
- * Where the magic actually happens
- */
- $custom_types = get_all_custom_types();
- foreach ($custom_types as $type):
- $icon = get_icon($type);
- $custom_type_instance = new PostType($type, $icon);
-
- add_action( 'init', [ $custom_type_instance, 'register_post_type' ], 10 );
- endforeach;
-
- /**
- * Create taxonomies
- */
-
- add_action('init', 'create_materials_taxonomy');
- function create_materials_taxonomy() {
- $post_types_that_show_materials = [ 'artist', 'exhibition', 'event' ];
- register_taxonomy('material', $post_types_that_show_materials, [ "label" => "Materials"]);
- }
-
- add_action('init', 'create_artist_types_taxonomy');
- function create_artist_types_taxonomy() {
- $post_types_that_show_artist_types = [ 'artist' ];
- register_taxonomy('artist type', $post_types_that_show_artist_types, [ "label" => "Artist Type"]);
- }
-
- add_action('init', 'create_event_types_taxonomy');
- function create_event_types_taxonomy() {
- $post_types_that_show_event_types = [ 'event' ];
- register_taxonomy('event type', $post_types_that_show_event_types, [ "label" => "Event Type"]);
- }
-
- add_action('init', 'create_exhibit_types_taxonomy');
- function create_exhibit_types_taxonomy() {
- $post_types_that_show_exhibit_types = [ 'exhibition' ];
- register_taxonomy('Exhibition type', $post_types_that_show_exhibit_types, [ "label" => "Exhibition Type"]);
- }
- /* Plugin Logic -- END * /
-
- /**
- * Custom Fields
- * Defining our HERO, and Name override fields
- */
- add_action( 'cmb2_admin_init', 'cmb2_hero_metaboxes' );
-
- function sanitize_hero_urls( $value, $field_args, $field ) {
- $encoded = wp_json_encode( array( 'url' => $value ), true );
- return $encoded;
- }
-
- function cmb_hero_render_row_cb( $field_args, $field ) {
- $id = $field->args( 'id' );
- $label = $field->args( 'name' );
- $name = $field->args( '_name' );
- $value = $field->escaped_value();
- $description = $field->args( 'description' );
- // !: Impossible to fine " conversion
- $decoded = json_decode( str_replace('"', '"', $value), true );
- ?>
- <div class="custom-field-row">
- <p>
- <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
- <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>"
- value="<?php echo $decoded['url']; ?>"/>
- </p>
- <p class="description"><?php echo $description; ?></p>
- </div>
- <?php
- }
-
- /**
- * Define the metabox and field configurations.
- */
- function cmb2_hero_metaboxes() {
-
- /**
- * Initiate the metabox
- */
- $cmb = new_cmb2_box( array(
- 'id' => 'hero_metabox',
- 'title' => __( 'Hero', 'cmb2' ),
- 'object_types' => array( 'artist', ), // Post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => true, // Show field names on the left
- 'show_in_rest' => WP_REST_Server::READABLE
- // 'cmb_styles' => false, // false to disable the CMB stylesheet
- // 'closed' => true, // Keep the metabox closed by default
- ) );
-
- // URL text field
- $cmb->add_field( array(
- 'name' => __( 'YouTube URL', 'cmb2' ),
- 'desc' => __( 'Video for the hero section to display', 'cmb2' ),
- 'id' => 'hero_header',
- 'type' => 'text',
- 'sanitization_cb' => 'sanitize_hero_urls',
- 'render_row_cb' => 'cmb_hero_render_row_cb'
- ) );
- }
-
- add_action( 'cmb2_admin_init', 'cmb2_artist_sort_metaboxes' );
- function cmb2_artist_sort_metaboxes() {
- $cmb = new_cmb2_box( array(
- 'id' => 'artist_sort_metabox',
- 'title' => __( 'Name Override', 'cmb2' ),
- 'object_types' => array( 'artist' ),
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => true,
- 'show_in_rest' => WP_REST_Server::READABLE
- ) );
- $cmb->add_field( array(
- 'name' => __( 'Alternate Name', 'cmb2' ),
- 'desc' => __( 'Name to use for alphabetical sorting', 'cmb2' ),
- 'id' => 'artist-sort-name',
- 'type' => 'text'
- ) );
- }
|