NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cia-post-types.php 4.8KB

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