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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * Create taxonomies
  66. */
  67. add_action('init', 'create_materials_taxonomy');
  68. function create_materials_taxonomy() {
  69. $post_types_that_show_materials = [ 'artist' ];
  70. register_taxonomy('material', $post_types_that_show_materials, [ "label" => "Materials"]);
  71. }
  72. /* Plugin Logic -- END * /
  73. /**
  74. * Custom Fields
  75. * Defining our HERO, and Name override fields
  76. */
  77. add_action( 'cmb2_admin_init', 'cmb2_hero_metaboxes' );
  78. function sanitize_hero_urls( $value, $field_args, $field ) {
  79. $encoded = wp_json_encode( array( 'url' => $value ), true );
  80. return $encoded;
  81. }
  82. function cmb_hero_render_row_cb( $field_args, $field ) {
  83. $id = $field->args( 'id' );
  84. $label = $field->args( 'name' );
  85. $name = $field->args( '_name' );
  86. $value = $field->escaped_value();
  87. $description = $field->args( 'description' );
  88. // !: Impossible to fine &quot; conversion
  89. $decoded = json_decode( str_replace('&quot;', '"', $value), true );
  90. ?>
  91. <div class="custom-field-row">
  92. <p>
  93. <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
  94. <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>"
  95. value="<?php echo $decoded['url']; ?>"/>
  96. </p>
  97. <p class="description"><?php echo $description; ?></p>
  98. </div>
  99. <?php
  100. }
  101. /**
  102. * Define the metabox and field configurations.
  103. */
  104. function cmb2_hero_metaboxes() {
  105. /**
  106. * Initiate the metabox
  107. */
  108. $cmb = new_cmb2_box( array(
  109. 'id' => 'hero_metabox',
  110. 'title' => __( 'Hero', 'cmb2' ),
  111. 'object_types' => array( 'artist', ), // Post type
  112. 'context' => 'normal',
  113. 'priority' => 'high',
  114. 'show_names' => true, // Show field names on the left
  115. 'show_in_rest' => WP_REST_Server::READABLE
  116. // 'cmb_styles' => false, // false to disable the CMB stylesheet
  117. // 'closed' => true, // Keep the metabox closed by default
  118. ) );
  119. // URL text field
  120. $cmb->add_field( array(
  121. 'name' => __( 'YouTube URL', 'cmb2' ),
  122. 'desc' => __( 'Video for the hero section to display', 'cmb2' ),
  123. 'id' => 'hero_header',
  124. 'type' => 'text',
  125. 'sanitization_cb' => 'sanitize_hero_urls',
  126. 'render_row_cb' => 'cmb_hero_render_row_cb'
  127. ) );
  128. }
  129. add_action( 'cmb2_admin_init', 'cmb2_artist_sort_metaboxes' );
  130. function cmb2_artist_sort_metaboxes() {
  131. $cmb = new_cmb2_box( array(
  132. 'id' => 'artist_sort_metabox',
  133. 'title' => __( 'Name Override', 'cmb2' ),
  134. 'object_types' => array( 'artist' ),
  135. 'context' => 'normal',
  136. 'priority' => 'high',
  137. 'show_names' => true,
  138. 'show_in_rest' => WP_REST_Server::READABLE
  139. ) );
  140. $cmb->add_field( array(
  141. 'name' => __( 'Alternate Name', 'cmb2' ),
  142. 'desc' => __( 'Name to use for alphabetical sorting', 'cmb2' ),
  143. 'id' => 'artist-sort-name',
  144. 'type' => 'text'
  145. ) );
  146. }