NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cia-post-types.php 5.7KB

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