NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cia-post-types.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Craft in America Custom Types Plugin
  4. *
  5. * @since 1.0.0
  6. * @package cia_post_types
  7. *
  8. * @wordpress-plugin
  9. * Plugin Name: Craft in America - Content Types
  10. * Plugin URI:
  11. * Description: Adds custom custom types, and p2p relationships
  12. * Version: 1.0.0
  13. * Author: TOJ <john@yvvas.com>
  14. */
  15. // If this file is called directly, abort.
  16. if ( ! defined( 'WPINC' ) ) { die; }
  17. require_once 'includes/cmb2/init.php';
  18. include('includes/custom-types.php');
  19. include('includes/p2p-mappings.php');
  20. /**
  21. * Class that holds all the necessary
  22. * functionality to build custom post types
  23. */
  24. class PostType {
  25. /**
  26. * The custom post type slug
  27. * @var string
  28. */
  29. private $post_type;
  30. /**
  31. * The custom post type icon
  32. * @var string
  33. */
  34. private $icon;
  35. function __construct($post_type, $icon) {
  36. $this->post_type = $post_type;
  37. $this->icon = $icon;
  38. }
  39. /** Register custom post type call-back */
  40. public function register_post_type() {
  41. $args = [
  42. 'label' => esc_html( $this->post_type, 'test-plugin' ),
  43. 'public' => true,
  44. 'menu_position' => 47,
  45. 'menu_icon' => $this->icon,
  46. 'supports' => ['title', 'editor', 'revisions', 'thumbnail'],
  47. 'has_archive' => false,
  48. 'show_in_rest' => true,
  49. 'publicly_queryable' => false
  50. ];
  51. register_post_type( $this->post_type, $args );
  52. }
  53. }
  54. /**
  55. * Plugin Logic
  56. * Where the magic actually happens
  57. */
  58. $custom_types = get_all_custom_types();
  59. foreach ($custom_types as $type):
  60. $icon = get_icon($type);
  61. $custom_type_instance = new PostType($type, $icon);
  62. add_action( 'init', [ $custom_type_instance, 'register_post_type' ], 10 );
  63. endforeach;
  64. /**
  65. * Custom Fields
  66. * Defining our HERO, and Name override fields
  67. */
  68. add_action( 'cmb2_admin_init', 'cmb2_hero_metaboxes' );
  69. function sanitize_hero_urls( $value, $field_args, $field ) {
  70. $encoded = wp_json_encode( array( 'url' => $value ), true );
  71. return $encoded;
  72. }
  73. function cmb_hero_render_row_cb( $field_args, $field ) {
  74. $id = $field->args( 'id' );
  75. $label = $field->args( 'name' );
  76. $name = $field->args( '_name' );
  77. $value = $field->escaped_value();
  78. $description = $field->args( 'description' );
  79. // !: Impossible to fine &quot; conversion
  80. $decoded = json_decode( str_replace('&quot;', '"', $value), true );
  81. ?>
  82. <div class="custom-field-row">
  83. <p>
  84. <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
  85. <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>"
  86. value="<?php echo $decoded['url']; ?>"/>
  87. </p>
  88. <p class="description"><?php echo $description; ?></p>
  89. </div>
  90. <?php
  91. }
  92. /**
  93. * Define the metabox and field configurations.
  94. */
  95. function cmb2_hero_metaboxes() {
  96. /**
  97. * Initiate the metabox
  98. */
  99. $cmb = new_cmb2_box( array(
  100. 'id' => 'hero_metabox',
  101. 'title' => __( 'Hero', 'cmb2' ),
  102. 'object_types' => array( 'artist', ), // Post type
  103. 'context' => 'normal',
  104. 'priority' => 'high',
  105. 'show_names' => true, // Show field names on the left
  106. 'show_in_rest' => WP_REST_Server::READABLE
  107. // 'cmb_styles' => false, // false to disable the CMB stylesheet
  108. // 'closed' => true, // Keep the metabox closed by default
  109. ) );
  110. // URL text field
  111. $cmb->add_field( array(
  112. 'name' => __( 'YouTube URL', 'cmb2' ),
  113. 'desc' => __( 'Video for the hero section to display', 'cmb2' ),
  114. 'id' => 'hero_header',
  115. 'type' => 'text',
  116. 'sanitization_cb' => 'sanitize_hero_urls',
  117. 'render_row_cb' => 'cmb_hero_render_row_cb'
  118. ) );
  119. }
  120. add_action( 'cmb2_admin_init', 'cmb2_artist_sort_metaboxes' );
  121. function cmb2_artist_sort_metaboxes() {
  122. $cmb = new_cmb2_box( array(
  123. 'id' => 'artist_sort_metabox',
  124. 'title' => __( 'Name Override', 'cmb2' ),
  125. 'object_types' => array( 'artist' ),
  126. 'context' => 'normal',
  127. 'priority' => 'high',
  128. 'show_names' => true,
  129. 'show_in_rest' => WP_REST_Server::READABLE
  130. ) );
  131. $cmb->add_field( array(
  132. 'name' => __( 'Alternate Name', 'cmb2' ),
  133. 'desc' => __( 'Name to use for alphabetical sorting', 'cmb2' ),
  134. 'id' => 'artist-sort-name',
  135. 'type' => 'text'
  136. ) );
  137. }
  138. /**
  139. * Create taxonomies
  140. */
  141. add_action('init', 'create_materials_taxonomy');
  142. function create_materials_taxonomy() {
  143. $post_types_that_show_materials = [ 'artist' ];
  144. register_taxonomy("material", $post_types_that_show_materials, [ "label" => "Materials"]);
  145. }