Przeglądaj źródła

:recycle: moving some functions around | capitalize custom type letter in menu

tags/0.9.0
j 5 lat temu
rodzic
commit
5e8fc95649

+ 22
- 14
plugins/cia-endpoints/includes/formats.php Wyświetl plik

@@ -1,4 +1,12 @@
1 1
 <?php
2
+function make_taxonomy_endpoint_for( $terms ) {
3
+    $type_slugs = [];
4
+    foreach ($terms as $type_slug):
5
+        array_push($type_slugs, $type_slug->slug);
6
+    endforeach;
7
+    return $type_slugs;
8
+}
9
+
2 10
 
3 11
 function default_post_format( $item ) {
4 12
     $filtered = array();
@@ -12,22 +20,22 @@ function default_post_format( $item ) {
12 20
 
13 21
     $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
14 22
 
15
-    if($item->post_type === 'artist') {
16
-        $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
17
-        $material_slugs = [];
18
-        foreach (get_the_terms( $item, 'material' ) as $material_slug):
19
-            array_push($material_slugs, $material_slug->slug);
20
-        endforeach;
21
-        $filtered[materials] = $material_slugs;
23
+    // Materials + type endpoints
24
+    // if($item->post_type === 'artist' || $item->post_type === 'event' || $item->post_type === 'exhibition') {
25
+    $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' ));
22 29
     }
23 30
     
24
-    // Parse category IDs and store just the slugs
25
-    $category_slugs = [];
26
-    foreach (wp_get_post_categories( $item->ID ) as $id):
27
-        $category = get_category($id);
28
-        array_push($category_slugs, $category->slug);
29
-    endforeach;
30
-    $filtered[categories] = $category_slugs;
31
+    // Custom fields endpoint
32
+    if($item->post_type === 'artist') $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
33
+
34
+    // Post categories and tags (store just the slugs)
35
+    if($item->post_type === 'post') {
36
+        $filtered[categories] = make_taxonomy_endpoint_for(get_the_terms( $item, 'category' ));
37
+        $filtered[tags] = make_taxonomy_endpoint_for(get_the_terms( $item, 'post_tag' ));
38
+    }
31 39
 
32 40
     return $filtered;
33 41
 }

+ 7
- 101
plugins/cia-post-types/cia-post-types.php Wyświetl plik

@@ -19,6 +19,8 @@ if ( ! defined( 'WPINC' ) ) { die; }
19 19
 require_once 'includes/cmb2/init.php';
20 20
 
21 21
 include('includes/custom-types.php');
22
+include('includes/custom-taxonomies.php');
23
+include('includes/custom-metaboxes.php');
22 24
 include('includes/p2p-mappings.php');
23 25
 
24 26
 /**
@@ -45,7 +47,7 @@ class PostType {
45 47
     /** Register custom post type call-back */
46 48
     public function register_post_type() {
47 49
         $args = [
48
-            'label'              => esc_html( $this->post_type, 'test-plugin' ),
50
+            'label'              => esc_html( ucfirst($this->post_type), 'test-plugin' ),
49 51
             'public'             => true,
50 52
             'menu_position'      => 47,
51 53
             'menu_icon'          => $this->icon,
@@ -74,109 +76,13 @@ endforeach;
74 76
 /**
75 77
  * Create taxonomies
76 78
  */
77
-
78 79
 add_action('init', 'create_materials_taxonomy'); 
79
-function create_materials_taxonomy() {
80
-    $post_types_that_show_materials = [ 'artist', 'exhibition', 'event' ];
81
-    register_taxonomy('material', $post_types_that_show_materials, [ "label" => "Materials"]);
82
-}
83
-
84
-add_action('init', 'create_artist_types_taxonomy'); 
85
-function create_artist_types_taxonomy() {
86
-    $post_types_that_show_artist_types = [ 'artist' ];
87
-    register_taxonomy('artist type', $post_types_that_show_artist_types, [ "label" => "Artist Type"]);
88
-}
89
-
90
-add_action('init', 'create_event_types_taxonomy'); 
91
-function create_event_types_taxonomy() {
92
-    $post_types_that_show_event_types = [ 'event' ];
93
-    register_taxonomy('event type', $post_types_that_show_event_types, [ "label" => "Event Type"]);
94
-}
95
-
96
-add_action('init', 'create_exhibit_types_taxonomy'); 
97
-function create_exhibit_types_taxonomy() {
98
-    $post_types_that_show_exhibit_types = [ 'exhibition' ];
99
-    register_taxonomy('Exhibition type', $post_types_that_show_exhibit_types, [ "label" => "Exhibition Type"]);
100
-}
101
-/* Plugin Logic -- END * /
80
+add_action('init', 'create_types_taxonomy'); 
102 81
 
103 82
 /**
104
- * Custom Fields
105
- * Defining our HERO, and Name override fields
83
+ * Create CMB Metaboxes
106 84
  */
107 85
 add_action( 'cmb2_admin_init', 'cmb2_hero_metaboxes' );
108
-
109
-function sanitize_hero_urls( $value, $field_args, $field ) {
110
-    $encoded = wp_json_encode( array( 'url' => $value ), true );
111
-    return $encoded;
112
-}
113
-
114
-function cmb_hero_render_row_cb( $field_args, $field ) {
115
-    $id          = $field->args( 'id' );
116
-    $label       = $field->args( 'name' );
117
-    $name        = $field->args( '_name' );
118
-    $value       = $field->escaped_value();
119
-    $description = $field->args( 'description' );
120
-    // !: Impossible to fine &quot; conversion
121
-    $decoded     = json_decode( str_replace('&quot;', '"', $value), true );
122
-    ?>
123
-    <div class="custom-field-row">
124
-        <p>
125
-            <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
126
-            <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" 
127
-                value="<?php echo $decoded['url']; ?>"/>
128
-        </p>
129
-        <p class="description"><?php echo $description; ?></p>
130
-    </div>
131
-    <?php
132
-}
133
-
134
-/**
135
- * Define the metabox and field configurations.
136
- */
137
-function cmb2_hero_metaboxes() {
138
-
139
-    /**
140
-     * Initiate the metabox
141
-     */
142
-    $cmb = new_cmb2_box( array(
143
-        'id'            => 'hero_metabox',
144
-        'title'         => __( 'Hero', 'cmb2' ),
145
-        'object_types'  => array( 'artist', ), // Post type
146
-        'context'       => 'normal',
147
-        'priority'      => 'high',
148
-        'show_names'    => true, // Show field names on the left
149
-        'show_in_rest'  => WP_REST_Server::READABLE
150
-        // 'cmb_styles' => false, // false to disable the CMB stylesheet
151
-        // 'closed'     => true, // Keep the metabox closed by default
152
-    ) );
153
-
154
-    // URL text field
155
-    $cmb->add_field( array(
156
-        'name'             => __( 'YouTube URL', 'cmb2' ),
157
-        'desc'             => __( 'Video for the hero section to display', 'cmb2' ),
158
-        'id'               => 'hero_header',
159
-        'type'             => 'text',
160
-        'sanitization_cb'  => 'sanitize_hero_urls',
161
-        'render_row_cb' => 'cmb_hero_render_row_cb'
162
-    ) );
163
-}
164
-
165 86
 add_action( 'cmb2_admin_init', 'cmb2_artist_sort_metaboxes' );
166
-function cmb2_artist_sort_metaboxes() {
167
-    $cmb = new_cmb2_box( array(
168
-        'id'            => 'artist_sort_metabox',
169
-        'title'         => __( 'Name Override', 'cmb2' ),
170
-        'object_types'  => array( 'artist' ),
171
-        'context'       => 'normal',
172
-        'priority'      => 'high',
173
-        'show_names'    => true,
174
-        'show_in_rest'  => WP_REST_Server::READABLE
175
-    ) );
176
-    $cmb->add_field( array(
177
-        'name'             => __( 'Alternate Name', 'cmb2' ),
178
-        'desc'             => __( 'Name to use for alphabetical sorting', 'cmb2' ),
179
-        'id'               => 'artist-sort-name',
180
-        'type'             => 'text'
181
-    ) );
182
-}
87
+
88
+/* Plugin Logic -- END * /

+ 71
- 0
plugins/cia-post-types/includes/custom-metaboxes.php Wyświetl plik

@@ -0,0 +1,71 @@
1
+<?php
2
+    /**
3
+     * Custom Fields
4
+     * Defining our HERO, and Name override fields
5
+     */
6
+    function sanitize_hero_urls( $value, $field_args, $field ) {
7
+        $encoded = wp_json_encode( array( 'url' => $value ), true );
8
+        return $encoded;
9
+    }
10
+
11
+    function cmb_hero_render_row_cb( $field_args, $field ) {
12
+        $id          = $field->args( 'id' );
13
+        $label       = $field->args( 'name' );
14
+        $name        = $field->args( '_name' );
15
+        $value       = $field->escaped_value();
16
+        $description = $field->args( 'description' );
17
+        // !: Impossible to fine &quot; conversion
18
+        $decoded     = json_decode( str_replace('&quot;', '"', $value), true );
19
+        ?>
20
+        <div class="custom-field-row">
21
+            <p>
22
+                <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
23
+                <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" 
24
+                    value="<?php echo $decoded['url']; ?>"/>
25
+            </p>
26
+            <p class="description"><?php echo $description; ?></p>
27
+        </div>
28
+        <?php
29
+    }
30
+
31
+    /**
32
+     * Define the metabox and field configurations.
33
+     */
34
+    function cmb2_hero_metaboxes() {
35
+
36
+        /**
37
+         * Initiate the metabox
38
+         */
39
+        $cmb = new_cmb2_box( array(
40
+            'id'            => 'hero_metabox',
41
+            'title'         => __( 'Hero', 'cmb2' ),
42
+            'object_types'  => array( 'artist', ), // Post type
43
+            'show_in_rest'  => WP_REST_Server::READABLE
44
+        ) );
45
+
46
+        // URL text field
47
+        $cmb->add_field( array(
48
+            'name'             => __( 'YouTube URL', 'cmb2' ),
49
+            'desc'             => __( 'Video for the hero section to display', 'cmb2' ),
50
+            'id'               => 'hero_header',
51
+            'type'             => 'text',
52
+            'sanitization_cb'  => 'sanitize_hero_urls',
53
+            'render_row_cb' => 'cmb_hero_render_row_cb'
54
+        ) );
55
+    }
56
+
57
+    function cmb2_artist_sort_metaboxes() {
58
+        $cmb = new_cmb2_box( array(
59
+            'id'            => 'artist_sort_metabox',
60
+            'title'         => __( 'Name Override', 'cmb2' ),
61
+            'object_types'  => array( 'artist' ),
62
+            'show_in_rest'  => WP_REST_Server::READABLE
63
+        ) );
64
+        $cmb->add_field( array(
65
+            'name'             => __( 'Alternate Name', 'cmb2' ),
66
+            'desc'             => __( 'Name to use for alphabetical sorting', 'cmb2' ),
67
+            'id'               => 'artist-sort-name',
68
+            'type'             => 'text'
69
+        ) );
70
+    }
71
+?>

+ 12
- 0
plugins/cia-post-types/includes/custom-taxonomies.php Wyświetl plik

@@ -0,0 +1,12 @@
1
+<?php
2
+    function create_materials_taxonomy() {
3
+        $post_types_that_show_materials = [ 'artist', 'exhibition', 'event' ];
4
+        register_taxonomy('material', $post_types_that_show_materials, [ "label" => "Materials"]);
5
+    }
6
+    function create_types_taxonomy() {
7
+        $post_types = [ 'artist', 'exhibition', 'event' ];
8
+        foreach ( $post_types as $post_type ):
9
+            register_taxonomy($post_type . '_type', $post_type, [ "label" => ucfirst($post_type) . " Type"]);
10
+        endforeach;
11
+    }
12
+?>

Ładowanie…
Anuluj
Zapisz