소스 검색

: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 년 전
부모
커밋
07e19cfcf0

+ 2
- 4
plugins/cia-endpoints/includes/class.make-endpoint.php 파일 보기

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

+ 7
- 7
plugins/cia-endpoints/includes/class.make-sortby.php 파일 보기

19
             ),
19
             ),
20
         ]);
20
         ]);
21
     }
21
     }
22
-    
22
+
23
     public function by_alpha( $request ) {
23
     public function by_alpha( $request ) {
24
         global $wpdb;
24
         global $wpdb;
25
         $res = $wpdb->get_results($wpdb->prepare(
25
         $res = $wpdb->get_results($wpdb->prepare(
26
-            "SELECT DISTINCT wp_posts.*, 
26
+            "SELECT DISTINCT wp_posts.*,
27
             IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
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
             WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
30
             WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
31
             ORDER BY sort_name",
31
             ORDER BY sort_name",
32
             $this->post_type
32
             $this->post_type
33
         ));
33
         ));
34
         wp_reset_postdata();
34
         wp_reset_postdata();
35
-        
35
+
36
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
36
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
37
     }
37
     }
38
 
38
 
41
         // !: Make this a real query
41
         // !: Make this a real query
42
         $res = $wpdb->get_results($wpdb->prepare(
42
         $res = $wpdb->get_results($wpdb->prepare(
43
             "SELECT * FROM wp_posts
43
             "SELECT * FROM wp_posts
44
-            WHERE post_type = %s 
44
+            WHERE post_type = %s
45
             AND post_status='publish'",
45
             AND post_status='publish'",
46
             $this->post_type
46
             $this->post_type
47
         ));
47
         ));
52
 
52
 
53
     public function prepare_items_for_reponse( $items ) {
53
     public function prepare_items_for_reponse( $items ) {
54
         $collection = array();
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
         return $collection;
56
         return $collection;
57
     }
57
     }
58
 }
58
 }

+ 8
- 4
plugins/cia-endpoints/includes/formats.php 파일 보기

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

+ 7
- 4
plugins/cia-endpoints/includes/related-items.php 파일 보기

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

+ 4
- 1
vue-theme/src/components/sidebars/sidebar.vue 파일 보기

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

+ 43
- 24
vue-theme/src/pages/single.vue 파일 보기

8
             h1 {{ type }}:{{ $route.params.slug }} {{ post.title }}
8
             h1 {{ type }}:{{ $route.params.slug }} {{ post.title }}
9
             p(v-if="post.categories") categories: {{ post.categories }}
9
             p(v-if="post.categories") categories: {{ post.categories }}
10
             p(v-if="post.type") type: {{ post.type }}
10
             p(v-if="post.type") type: {{ post.type }}
11
+            p(v-if="post.subtypes") subtypes: {{ post.subtypes }}
11
 
12
 
12
             .date-info(v-if="type === 'events' || post.type === 'exhibitions'")
13
             .date-info(v-if="type === 'events' || post.type === 'exhibitions'")
13
                 p start: {{ dateFrom(post.start) }}
14
                 p start: {{ dateFrom(post.start) }}
17
 
18
 
18
         credits(v-if="type === 'episodes'" :post="post")
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
 </template>
28
 </template>
26
 
29
 
27
 <script>
30
 <script>
90
         },
93
         },
91
         activeImageIndex() {
94
         activeImageIndex() {
92
             return Object.keys(this.imagesInGallery).indexOf(this.activeImageID.toString())
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
     methods: {
105
     methods: {
129
         dateFrom(unix) {
139
         dateFrom(unix) {
130
             return new Date( parseInt(unix) * 1000 )
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
     watch: {
166
     watch: {
134
         post(newVal, oldVal) {
167
         post(newVal, oldVal) {
135
             this.checkAndSetHero(newVal)
168
             this.checkAndSetHero(newVal)
169
+        },
170
+        $route(newVal, oldVal) {
171
+            console.log('route changed...')
172
+            this.loadPostData()
136
         }
173
         }
137
     },
174
     },
138
     async created() {
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
 </script>
179
 </script>

Loading…
취소
저장