ソースを参照

:sparkles: adding event and exhibition timestamps, endpoints, and custom fields

tags/0.9.0
j 5年前
コミット
122adda38c

+ 53
- 27
plugins/cia-endpoints/cia-end-points.php ファイルの表示

@@ -19,40 +19,66 @@ if ( ! defined( 'WPINC' ) ) { die; }
19 19
 require_once('includes/class.make-endpoint.php');
20 20
 require_once('includes/class.make-sortby.php');
21 21
 
22
-/**
23
- * The standard wordpress post_types
24
- */
25
-add_action( 'rest_api_init', function () {
26
-    $post_controller = new Make_Endpoint_For('post');
27
-    $post_controller->register_custom_route('posts');
28
-});
29 22
 add_action( 'rest_api_init', function () {
23
+    /**
24
+     * The standard wordpress post_types
25
+     */
30 26
     $page_controller = new Make_Endpoint_For('page');
31 27
     $page_controller->register_custom_route('pages');
32
-});
33
-add_action( 'rest_api_init', function () {
28
+
34 29
     $media_controller = new Make_Endpoint_For('media');
35 30
     $media_controller->register_custom_route('media');
36
-});
37 31
 
38
-/**
39
- * Craft in America custom post_types
40
- */
41
-add_action( 'rest_api_init', function () {
42
-    $media_controller = new Make_Endpoint_For('episode');
43
-    $media_controller->register_custom_route('episodes');
44
-});
45
-add_action( 'rest_api_init', function () {
46
-    $media_controller = new Make_Endpoint_For('artist');
47
-    $media_controller->register_custom_route('artists');
32
+    $post_controller = new Make_Endpoint_For('post');
33
+    $post_controller->register_custom_route('posts');
34
+
35
+    /**
36
+     * Craft in America custom post_types
37
+     */
38
+    $episode_controller = new Make_Endpoint_For('episode');
39
+    $episode_controller->register_custom_route('episodes');
40
+    
41
+    $artist_controller = new Make_Endpoint_For('artist');
42
+    $artist_controller->register_custom_route('artists');
43
+
44
+    $event_controller = new Make_Endpoint_For('event');
45
+    $event_controller->register_custom_route('events');
46
+    
47
+    $exhibition_controller = new Make_Endpoint_For('exhibition');
48
+    $exhibition_controller->register_custom_route('exhibitions');
49
+
50
+    /**
51
+     * Craft in America custom sort_types
52
+     */
53
+    $sort_controller = new Make_Sort_By('artist', 'by_alpha');
54
+    $sort_controller->register_custom_route('artists/by-alpha');
55
+    $sort_controller = new Make_Sort_By('artist', 'by_material');
56
+    $sort_controller->register_custom_route('artists/by-material');
48 57
 });
49 58
 
50 59
 /**
51
- * Craft in America custom sort_types
60
+ * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
61
+ * Depends on: WP REST Cache
62
+ * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
52 63
  */
53
-add_action( 'rest_api_init', function () {
54
-    $media_controller = new Make_Sort_By('artist', 'by_alpha');
55
-    $media_controller->register_custom_route('artists/by-alpha');
56
-    $media_controller = new Make_Sort_By('artist', 'by_material');
57
-    $media_controller->register_custom_route('artists/by-material');
58
-});
64
+add_filter('wp_rest_cache/allowed_endpoints', function () {
65
+    // The standard wordpress post_types
66
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('posts', $allowed_endpoints['craft/v2']) )
67
+        $allowed_endpoints['craft/v2'][] = 'posts';
68
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('pages', $allowed_endpoints['craft/v2']) )
69
+        $allowed_endpoints['craft/v2'][] = 'pages';
70
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
71
+        $allowed_endpoints['craft/v2'][] = 'media';
72
+
73
+    // Craft in America custom post_types
74
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episodes', $allowed_endpoints['craft/v2']) )
75
+        $allowed_endpoints['craft/v2'][] = 'episodes';
76
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('events', $allowed_endpoints['craft/v2']) )
77
+        $allowed_endpoints['craft/v2'][] = 'events';
78
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibitions', $allowed_endpoints['craft/v2']) )
79
+        $allowed_endpoints['craft/v2'][] = 'exhibitions';
80
+    if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artists', $allowed_endpoints['craft/v2']) )
81
+        $allowed_endpoints['craft/v2'][] = 'artists';
82
+
83
+    return $allowed_endpoints;
84
+}, 10, 1);

+ 3
- 1
plugins/cia-endpoints/includes/class.make-endpoint.php ファイルの表示

@@ -1,5 +1,5 @@
1 1
 <?php
2
-
2
+include('settings.php');
3 3
 include('reformat-blocks.php');
4 4
 include('related-items.php');
5 5
 include('formats.php');
@@ -100,7 +100,9 @@ class Make_Endpoint_For extends WP_REST_Controller {
100 100
             // Find all image info
101 101
             $filtered[attached] = $this->_getAttachments($item, $filtered[galleries]);
102 102
 
103
+            // For your hero URL
103 104
             $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
105
+            
104 106
             $filtered[relatedto] = p2p_related_to($item->ID, $item->post_type);
105 107
 
106 108
             $collection[$item->ID] = $filtered;

+ 12
- 4
plugins/cia-endpoints/includes/formats.php ファイルの表示

@@ -18,18 +18,26 @@ function default_post_format( $item ) {
18 18
     $filtered[date] = $item->post_date;
19 19
     $filtered[content] = $item->post_content;
20 20
 
21
-    $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
22 21
 
23 22
     // Materials + type endpoints
24 23
     // if($item->post_type === 'artist' || $item->post_type === 'event' || $item->post_type === 'exhibition') {
25 24
     $posts_with_type = ['artist', 'exhbition', 'event'];
26
-    if(in_array($item->post_type, $posts_with_type)) {
27
-        $filtered[materials] = make_taxonomy_endpoint_for(get_the_terms( $item, 'material' ));
28
-        $filtered[type] = make_taxonomy_endpoint_for(get_the_terms( $item, $item->post_type . '_type' ));
25
+    if( in_array($item->post_type, $posts_with_type) ) {
26
+        $filtered[materials] = make_taxonomy_endpoint_for( get_the_terms( $item, 'material' ) );
27
+        $filtered[type] = make_taxonomy_endpoint_for( get_the_terms( $item, $item->post_type . '_type' ) );
29 28
     }
30 29
     
31 30
     // Custom fields endpoint
32 31
     if($item->post_type === 'artist') $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
32
+    
33
+    if($item->post_type === 'event') {
34
+        $filtered[start] = get_post_meta( $item->ID, 'event-start-time', true );
35
+        $filtered[end] = get_post_meta( $item->ID, 'event-end-time', true );
36
+    }
37
+    if($item->post_type === 'exhibition') {
38
+        $filtered[start] = get_post_meta( $item->ID, 'exhibit-start-date', true );
39
+        $filtered[end] = get_post_meta( $item->ID, 'exhibit-end-date', true );
40
+    }
33 41
 
34 42
     // Post categories and tags (store just the slugs)
35 43
     if($item->post_type === 'post') {

+ 2
- 0
plugins/cia-post-types/cia-post-types.php ファイルの表示

@@ -84,5 +84,7 @@ add_action('init', 'create_types_taxonomy');
84 84
  */
85 85
 add_action( 'cmb2_admin_init', 'cmb2_hero_metaboxes' );
86 86
 add_action( 'cmb2_admin_init', 'cmb2_artist_sort_metaboxes' );
87
+add_action( 'cmb2_admin_init', 'cmb2_event_date_metaboxes' );
88
+add_action( 'cmb2_admin_init', 'cmb2_exhibition_date_metaboxes' );
87 89
 
88 90
 /* Plugin Logic -- END * /

+ 43
- 1
plugins/cia-post-types/includes/custom-metaboxes.php ファイルの表示

@@ -39,7 +39,7 @@
39 39
         $cmb = new_cmb2_box( array(
40 40
             'id'            => 'hero_metabox',
41 41
             'title'         => __( 'Hero', 'cmb2' ),
42
-            'object_types'  => array( 'artist', ), // Post type
42
+            'object_types'  => array( 'artist', 'event', 'exhibition' ), // Post type
43 43
             'show_in_rest'  => WP_REST_Server::READABLE
44 44
         ) );
45 45
 
@@ -68,4 +68,46 @@
68 68
             'type'             => 'text'
69 69
         ) );
70 70
     }
71
+
72
+    function cmb2_event_date_metaboxes() {
73
+        $cmb = new_cmb2_box( array(
74
+            'id'            => 'event-date',
75
+            'title'         => __( 'Date and Time', 'cmb2' ),
76
+            'object_types'  => array( 'event' ), // Post type
77
+            'show_in_rest'  => WP_REST_Server::READABLE
78
+        ) );
79
+        $cmb->add_field( array(
80
+            'name'             => __( 'Start', 'cmb2' ),
81
+            'desc'             => __( 'The date and time of your event', 'cmb2' ),
82
+            'id'               => 'event-start-time',
83
+            'type'             => 'text_datetime_timestamp'
84
+        ) );
85
+        $cmb->add_field( array(
86
+            'name'             => __( 'End', 'cmb2' ),
87
+            'desc'             => __( 'The date and time your event ends', 'cmb2' ),
88
+            'id'               => 'event-end-time',
89
+            'type'             => 'text_datetime_timestamp'
90
+        ) );
91
+    }
92
+
93
+    function cmb2_exhibition_date_metaboxes() {
94
+        $cmb = new_cmb2_box( array(
95
+            'id'            => 'exhibit-date',
96
+            'title'         => __( 'Date', 'cmb2' ),
97
+            'object_types'  => array( 'exhibition' ), // Post type
98
+            'show_in_rest'  => WP_REST_Server::READABLE
99
+        ) );
100
+        $cmb->add_field( array(
101
+            'name'             => __( 'Start Date', 'cmb2' ),
102
+            'desc'             => __( 'The date of your exhibition', 'cmb2' ),
103
+            'id'               => 'exhibit-start-date',
104
+            'type'             => 'text_date_timestamp'
105
+        ) );
106
+        $cmb->add_field( array(
107
+            'name'             => __( 'End Date', 'cmb2' ),
108
+            'desc'             => __( 'The date your exhibition ends', 'cmb2' ),
109
+            'id'               => 'exhibit-end-date',
110
+            'type'             => 'text_date_timestamp'
111
+        ) );
112
+    }
71 113
 ?>

読み込み中…
キャンセル
保存