Bladeren bron

:sparkles: fixing up related posts in endpoint plugin | making content optional in endpoint plugin | displaying related posts in frontend | moved data loading in single template

tags/0.9.0
TOJ 5 jaren geleden
bovenliggende
commit
07e19cfcf0

+ 2
- 4
plugins/cia-endpoints/includes/class.make-endpoint.php Bestand weergeven

@@ -91,7 +91,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
91 91
 
92 92
         // https://developer.wordpress.org/reference/functions/get_posts/
93 93
         foreach( get_posts($args) as $item ) {
94
-            $filtered = default_post_format( $item );
94
+            $filtered = default_post_format( $item, false );
95 95
 
96 96
             // Get those Block!
97 97
             $filtered[blocks] = get_rearrange_blocks(
@@ -114,8 +114,6 @@ class Make_Endpoint_For extends WP_REST_Controller {
114 114
             // For your hero URL
115 115
             $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
116 116
 
117
-            $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
118
-
119 117
             $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
120 118
 
121 119
             $collection[$item->ID] = $filtered;
@@ -129,7 +127,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
129 127
 
130 128
         // https://developer.wordpress.org/reference/functions/get_posts/
131 129
         foreach( get_posts($args) as $item ) {
132
-            $filtered = default_post_format( $item );
130
+            $filtered = default_post_format( $item, true );
133 131
 
134 132
             // Find all image info
135 133
             $filtered[attached] = $this->_getAttachments( $item, $filtered[galleries] );

+ 7
- 7
plugins/cia-endpoints/includes/class.make-sortby.php Bestand weergeven

@@ -19,20 +19,20 @@ class Make_Sort_By extends WP_REST_Controller {
19 19
             ),
20 20
         ]);
21 21
     }
22
-    
22
+
23 23
     public function by_alpha( $request ) {
24 24
         global $wpdb;
25 25
         $res = $wpdb->get_results($wpdb->prepare(
26
-            "SELECT DISTINCT wp_posts.*, 
26
+            "SELECT DISTINCT wp_posts.*,
27 27
             IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
28
-            FROM wp_posts LEFT JOIN wp_postmeta 
29
-            ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name') 
28
+            FROM wp_posts LEFT JOIN wp_postmeta
29
+            ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
30 30
             WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
31 31
             ORDER BY sort_name",
32 32
             $this->post_type
33 33
         ));
34 34
         wp_reset_postdata();
35
-        
35
+
36 36
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
37 37
     }
38 38
 
@@ -41,7 +41,7 @@ class Make_Sort_By extends WP_REST_Controller {
41 41
         // !: Make this a real query
42 42
         $res = $wpdb->get_results($wpdb->prepare(
43 43
             "SELECT * FROM wp_posts
44
-            WHERE post_type = %s 
44
+            WHERE post_type = %s
45 45
             AND post_status='publish'",
46 46
             $this->post_type
47 47
         ));
@@ -52,7 +52,7 @@ class Make_Sort_By extends WP_REST_Controller {
52 52
 
53 53
     public function prepare_items_for_reponse( $items ) {
54 54
         $collection = array();
55
-        forEach( $items as $key => $item ) $collection[$key] = default_post_format($item);
55
+        forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, true);
56 56
         return $collection;
57 57
     }
58 58
 }

+ 8
- 4
plugins/cia-endpoints/includes/formats.php Bestand weergeven

@@ -8,15 +8,17 @@ function make_taxonomy_endpoint_for( $terms ) {
8 8
 }
9 9
 
10 10
 
11
-function default_post_format( $item ) {
12
-    $filtered = array();
11
+function default_post_format( $item, $omit_content ) {
12
+    $filtered = [];
13 13
     $filtered[id] = $item->ID;
14 14
     $filtered[slug] = $item->post_name;
15 15
     $filtered[type] = $item->post_type;
16 16
     $filtered[title] = $item->post_title;
17 17
     $filtered[excerpt] = $item->post_excerpt;
18 18
     $filtered[date] = $item->post_date;
19
-    $filtered[content] = $item->post_content;
19
+
20
+    if($include_content) $filtered[content] = $item->post_content;
21
+
20 22
     $filtered[featured] = get_the_post_thumbnail_url($item);
21 23
 
22 24
 
@@ -25,10 +27,12 @@ function default_post_format( $item ) {
25 27
     $posts_with_type = ['artist', 'exhbition', 'event'];
26 28
     if( in_array($item->post_type, $posts_with_type) ) {
27 29
         $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' ) );
30
+        $filtered[subtypes] = make_taxonomy_endpoint_for( get_the_terms( $item, $item->post_type . '_type' ) );
29 31
     }
30 32
 
31 33
     // Custom fields endpoint
34
+    if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
35
+
32 36
     if($item->post_type === 'artist') $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
33 37
 
34 38
     if($item->post_type === 'event') {

+ 7
- 4
plugins/cia-endpoints/includes/related-items.php Bestand weergeven

@@ -3,10 +3,10 @@
3 3
 include(formats.php);
4 4
 
5 5
 function grab_ids_related_to_and_from($id_to_remove, $p2p_res) {
6
-    $related_to_ids = array_map(function($post) { 
6
+    $related_to_ids = array_map(function($post) {
7 7
         return (int)$post->p2p_to;
8 8
     }, $p2p_res);
9
-    $related_from_ids = array_map(function($post) { 
9
+    $related_from_ids = array_map(function($post) {
10 10
         return (int)$post->p2p_from;
11 11
     }, $p2p_res);
12 12
 
@@ -19,7 +19,9 @@ function grab_ids_related_to_and_from($id_to_remove, $p2p_res) {
19 19
 function prepare_related_items($args) {
20 20
     // Rearrange what fields get shown
21 21
     $collection = array();
22
-    forEach( get_posts($args) as $item ) $collection[$item->ID] = default_post_format($item);
22
+    forEach( get_posts($args) as $item ) {
23
+        $collection[$item->ID] = default_post_format($item, true);
24
+    }
23 25
     return $collection;
24 26
 }
25 27
 
@@ -34,7 +36,7 @@ function p2p_related_to($id, $type) {
34 36
         OR p2p_to = %d",
35 37
         $id, $id
36 38
     ));
37
-    
39
+
38 40
     $related_posts_ids = grab_ids_related_to_and_from($id, $res);
39 41
 
40 42
     // Use IDs to get posts from the wpdb
@@ -44,6 +46,7 @@ function p2p_related_to($id, $type) {
44 46
         'include' => $related_posts_ids
45 47
     );
46 48
     wp_reset_postdata();
49
+
47 50
     return prepare_related_items($args);
48 51
 }
49 52
 

+ 4
- 1
vue-theme/src/components/sidebars/sidebar.vue Bestand weergeven

@@ -1,7 +1,7 @@
1 1
 <template lang="pug">
2 2
 aside.sidebar
3 3
     section
4
-        .shadow(v-if="type === 'artists'")
4
+        .shadow(v-if="type === 'artists' && layout !== 'single'")
5 5
             h1.t-up sort {{ type }} by
6 6
             ul
7 7
                 li(v-for="option in sortOptions")
@@ -19,6 +19,9 @@ export default {
19 19
     props: {
20 20
         type: {
21 21
             type: String
22
+        },
23
+        layout: {
24
+            type: String
22 25
         }
23 26
     },
24 27
     data() {

+ 43
- 24
vue-theme/src/pages/single.vue Bestand weergeven

@@ -8,6 +8,7 @@
8 8
             h1 {{ type }}:{{ $route.params.slug }} {{ post.title }}
9 9
             p(v-if="post.categories") categories: {{ post.categories }}
10 10
             p(v-if="post.type") type: {{ post.type }}
11
+            p(v-if="post.subtypes") subtypes: {{ post.subtypes }}
11 12
 
12 13
             .date-info(v-if="type === 'events' || post.type === 'exhibitions'")
13 14
                 p start: {{ dateFrom(post.start) }}
@@ -17,11 +18,13 @@
17 18
 
18 19
         credits(v-if="type === 'episodes'" :post="post")
19 20
 
20
-    sidebar(v-if="sidebar" :type="`${type}`")
21
-        .shadow
22
-            h1.t-up single slot
23
-            div
24
-                p body whatever
21
+    sidebar(v-if="sidebar" :type="`${type}`" layout="single")
22
+        .shadow(v-if="Object.keys(p2pPostsByType).length" v-for="p2pPostType in Object.keys(p2pPostsByType)")
23
+            h3.t-up related {{ p2pPostType }}s
24
+            ul
25
+                li(v-for="relatedPost in p2pPostsByType[p2pPostType]")
26
+                    router-link(v-if="relatedPost" :to="`/${relatedPost.type}s/${relatedPost.slug}`")
27
+                        p {{ relatedPost.title }}
25 28
 </template>
26 29
 
27 30
 <script>
@@ -90,6 +93,13 @@ export default {
90 93
         },
91 94
         activeImageIndex() {
92 95
             return Object.keys(this.imagesInGallery).indexOf(this.activeImageID.toString())
96
+        },
97
+        p2pPostsByType() {
98
+            return this.post ? Object.values(this.post.relatedto).reduce((byType, relatedPost) => {
99
+                if(!byType[relatedPost.type]) byType[relatedPost.type] = []
100
+                byType[relatedPost.type].push(relatedPost)
101
+                return byType
102
+            }, {}) : {}
93 103
         }
94 104
     },
95 105
     methods: {
@@ -129,32 +139,41 @@ export default {
129 139
         dateFrom(unix) {
130 140
             return new Date( parseInt(unix) * 1000 )
131 141
         },
142
+
143
+        async loadPostData() {
144
+            /**
145
+             * Conditionally load based on post type
146
+             * which is derived from the route
147
+             * !: posts watcher fires when this finishes
148
+             */
149
+            let type = convertTitleCase(this.type)
150
+            let allPostsOfType = this.$store.state[this.type].all
151
+
152
+            /**
153
+             * Load posts if they're not already in state
154
+             */
155
+            if(!this[`all${type}Loaded`] && allPostsOfType.length < 1) {
156
+                const res = await this.$store.dispatch(`getAll${type}`)
157
+                allPostsOfType = res
158
+                console.log(`reloaded ${type}...`)
159
+            }
160
+            const singlePostData = Object.values(allPostsOfType).filter(post => post.slug == this.$route.params.slug)[0]
161
+
162
+            if(!singlePostData) return
163
+            await this.$store.dispatch(`getSingle${dePluralize(type)}`, singlePostData.id)
164
+        }
132 165
     },
133 166
     watch: {
134 167
         post(newVal, oldVal) {
135 168
             this.checkAndSetHero(newVal)
169
+        },
170
+        $route(newVal, oldVal) {
171
+            console.log('route changed...')
172
+            this.loadPostData()
136 173
         }
137 174
     },
138 175
     async created() {
139
-        /**
140
-         * Conditionally load based on post type
141
-         * which is derived from the route
142
-         * !: posts watcher fires when this finishes
143
-         */
144
-        let type = convertTitleCase(this.type)
145
-        let allPostsOfType = this.$store.state[this.type].all
146
-
147
-        /**
148
-         * Load posts if they're not already in state
149
-         */
150
-        if(!this[`all${type}Loaded`] && allPostsOfType.length < 1) {
151
-            const res = await this.$store.dispatch(`getAll${type}`)
152
-            allPostsOfType = res
153
-            console.log(`reloaded ${type}...`)
154
-        }
155
-        const singlePostData = Object.values(allPostsOfType).filter(post => post.slug == this.$route.params.slug)[0]
156
-
157
-        await this.$store.dispatch(`getSingle${dePluralize(type)}`, singlePostData.id)
176
+        this.loadPostData()
158 177
     }
159 178
 }
160 179
 </script>

Laden…
Annuleren
Opslaan