Pārlūkot izejas kodu

:recycle: trimming p2p responses

tags/0.9.0
J 4 gadus atpakaļ
vecāks
revīzija
603322cedb

+ 1
- 1
plugins/cia-endpoints/includes/class.make-endpoint.php Parādīt failu

@@ -83,7 +83,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
83 83
                 parse_blocks($item->post_content)
84 84
             );
85 85
             $filtered[attached] = get_images_from_content($item->post_content);
86
-            $filtered[relatedto] = p2p_related_to( $item->ID );
86
+            $filtered[relatedto] = p2p_related_to( $item->ID, false );
87 87
             array_push($collection, $filtered);
88 88
         }
89 89
         wp_reset_postdata();

+ 6
- 3
plugins/cia-endpoints/includes/class.make-sortby.php Parādīt failu

@@ -58,7 +58,8 @@ class Make_Sort_By extends WP_REST_Controller {
58 58
         }
59 59
         
60 60
         if($params['episode']) {
61
-            $args['p2p'] = $params['episode'];
61
+            $args['p2p_query'] = $params['episode'];
62
+            $args['tax_query'] = [];
62 63
         }
63 64
 
64 65
         return $args;
@@ -138,10 +139,11 @@ class Make_Sort_By extends WP_REST_Controller {
138 139
 
139 140
     public function by_episode( $request ) {
140 141
         $q = new WP_Query($this->make_args($request, null));
141
-        $found_posts = $q->get_posts();
142
+        $episode_name = $q->query->p2p_query;
143
+        $items = $this->prepare_items_for_reponse($q->get_posts());
142 144
         wp_reset_postdata();
143 145
 
144
-        return new WP_REST_Response([$q], 200 );
146
+        return new WP_REST_Response( $items, 200 );
145 147
     }
146 148
 
147 149
     public function prepare_items_for_reponse( $items ) {
@@ -150,6 +152,7 @@ class Make_Sort_By extends WP_REST_Controller {
150 152
         forEach( $items as $item ) {
151 153
             $formatted = default_post_format( $item, false );
152 154
             $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
155
+            $formatted[relatedto] = p2p_related_to( $item->ID, false );
153 156
             array_push($collection, $formatted);
154 157
         }
155 158
 

+ 30
- 0
plugins/cia-endpoints/includes/formats.php Parādīt failu

@@ -70,4 +70,34 @@ function default_post_format( $item, $include_content ) {
70 70
     return $filtered;
71 71
 }
72 72
 
73
+function minimal_post_format( $item ) {
74
+    $filtered = [];
75
+    $filtered[id] = $item->ID;
76
+    $filtered[slug] = $item->post_name;
77
+    $filtered[type] = $item->post_type;
78
+    $filtered[title] = $item->post_title;
79
+    $filtered[date] = $item->post_date;
80
+    
81
+    $filtered[thumb] = get_the_post_thumbnail_url($item, 'medium');
82
+    if(!$filtered[thumb]) {
83
+        $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
84
+        $filtered[thumb] = get_post($thumbnailId)->guid;
85
+    }
86
+
87
+    $posts_with_date = ['exhbition', 'event'];
88
+    if( in_array($item->post_type, $posts_with_date) ) {
89
+        $filtered[start] = get_post_meta($item->ID, $item->post_type . '-start-time', true);
90
+        $filtered[end] = get_post_meta($item->ID, $item->post_type . '-end-time', true);
91
+        $filtered[current] = time() <= (int)$filtered[end] && time() >= (int)$filtered[start];
92
+    }
93
+
94
+    // Post categories and tags (store just the slugs)
95
+    if($item->post_type === 'post') {
96
+        $filtered[categories] = make_taxonomy_endpoint_for(get_the_terms( $item, 'category' ));
97
+        $filtered[tags] = make_taxonomy_endpoint_for(get_the_terms( $item, 'post_tag' ));
98
+    }
99
+
100
+    return $filtered;
101
+}
102
+
73 103
 ?>

+ 9
- 4
plugins/cia-endpoints/includes/related-items.php Parādīt failu

@@ -14,7 +14,7 @@ function grab_ids_related_to_and_from($id_to_remove, $p2p_res) {
14 14
     return array_diff( $deduped, array($id_to_remove) );
15 15
 }
16 16
 
17
-function prepare_related_items($post_ids) {
17
+function prepare_related_items($post_ids, $include_post) {
18 18
     // Rearrange what fields get shown
19 19
     $collection = array();
20 20
 
@@ -27,12 +27,16 @@ function prepare_related_items($post_ids) {
27 27
     
28 28
     $related_items = get_posts($args);
29 29
     forEach( $related_items as $item ) {
30
-        $collection[$item->ID] = default_post_format($item, false);
30
+        if($include_post) {
31
+            $collection[$item->ID] = default_post_format($item, false);
32
+        } else {
33
+            $collection[$item->ID] = minimal_post_format($item);
34
+        }
31 35
     }
32 36
     return $collection;
33 37
 }
34 38
 
35
-function p2p_related_to($id, $type) {
39
+function p2p_related_to($id, $include_post) {
36 40
     global $wpdb;
37 41
     $sql = $wpdb->prepare(
38 42
         "SELECT * FROM wp_p2p
@@ -43,7 +47,8 @@ function p2p_related_to($id, $type) {
43 47
     $res = $wpdb->get_results($sql);
44 48
     
45 49
     $related_posts_ids = grab_ids_related_to_and_from($id, $res);
46
-    $related_posts = prepare_related_items($related_posts_ids);
50
+    
51
+    $related_posts = prepare_related_items($related_posts_ids, $include_post);
47 52
     
48 53
     if(empty($related_posts_ids)) {
49 54
         return [];

+ 1
- 1
vue-theme/src/pages/single.vue Parādīt failu

@@ -109,7 +109,7 @@ export default {
109 109
             )
110 110
         },
111 111
         p2pPostsByType() {
112
-            return this.singlePost
112
+            return this.singlePost && this.singlePost.relatedto
113 113
                 ? Object.values(this.singlePost.relatedto).reduce(
114 114
                       (byType, relatedPost) => {
115 115
                           if (!byType[relatedPost.type])

Notiek ielāde…
Atcelt
Saglabāt