浏览代码

:recycle: adding alts in the image parser

tags/1.0.2
J 3 年前
父节点
当前提交
9b423efd9a

+ 7
- 0
plugins/cia-endpoints/cia-end-points.php 查看文件

17
 if ( ! defined( 'WPINC' ) ) { die; }
17
 if ( ! defined( 'WPINC' ) ) { die; }
18
 
18
 
19
 require_once('includes/all-types.php');
19
 require_once('includes/all-types.php');
20
+require_once('includes/class.make-oneofeach.php');
20
 require_once('includes/class.make-endpoint.php');
21
 require_once('includes/class.make-endpoint.php');
21
 require_once('includes/class.make-terms.php');
22
 require_once('includes/class.make-terms.php');
22
 require_once('includes/class.make-sticky.php');
23
 require_once('includes/class.make-sticky.php');
53
     $sticky_controller = new Make_Sticky_Endpoint();
54
     $sticky_controller = new Make_Sticky_Endpoint();
54
     $sticky_controller->register_custom_route('sticky');
55
     $sticky_controller->register_custom_route('sticky');
55
     
56
     
57
+    /**
58
+     * OneEach endpoint
59
+     */
60
+    $oneeach_controller = new Make_One_Each_Endpoint();
61
+    $oneeach_controller->register_custom_route('oneofeach');
62
+    
56
     /**
63
     /**
57
      * Terms endpoint
64
      * Terms endpoint
58
      */
65
      */

+ 63
- 0
plugins/cia-endpoints/includes/class.make-oneofeach.php 查看文件

1
+<?php
2
+// include('formats.php');
3
+
4
+class Make_One_Each_Endpoint extends WP_REST_Controller {
5
+    /**
6
+     * Register the routes for the objects of the controller.
7
+     */
8
+    public function register_custom_route($route) {
9
+        $version = '2';
10
+        $namespace = 'craft/v' . $version;
11
+
12
+        register_rest_route( $namespace, '/' . $route, [
13
+            array(
14
+                'methods'  => WP_REST_Server::READABLE,
15
+                'callback' => array( $this, 'get_all_items' )
16
+            ),
17
+        ]);
18
+    }
19
+    public function get_all_items( $request ) {
20
+        $args = array(
21
+            'numberposts' => 1,
22
+        );
23
+
24
+        return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
25
+    }
26
+    public function prepare_all_items_for_response( $args ) {
27
+        $collection = array();
28
+        $types = [
29
+            'artist',
30
+            'episode',
31
+            'event',
32
+            'exhibition',
33
+            'guide',
34
+            'object',
35
+            'publication',
36
+            'technique',
37
+            'short',
38
+            'post',
39
+        ];
40
+
41
+        $q = [];
42
+        foreach($types as $post_type) {
43
+            array_push($q, new WP_Query(array(
44
+                'numberposts' => 1,
45
+                'post_type' => $post_type,
46
+                'post_status' => ['publish'],
47
+            )));
48
+            array_push($q, $post_type);
49
+            
50
+            $found_post = $q[0]->get_posts();
51
+
52
+            $formatted = default_post_format( $found_post, false );
53
+            $formatted['tried'] = $q[1];
54
+            array_push($collection, $formatted);
55
+        }
56
+        wp_reset_postdata();
57
+
58
+        return $collection;
59
+        // return $types;
60
+    }
61
+}
62
+
63
+?>

+ 8
- 3
plugins/cia-endpoints/includes/reformat-blocks.php 查看文件

3
         $parsed_blocks = array();
3
         $parsed_blocks = array();
4
         foreach ($blocks as $block) {
4
         foreach ($blocks as $block) {
5
             if($block[blockName] === "core/gallery") {
5
             if($block[blockName] === "core/gallery") {
6
+                // Fix for new style gallery blocks
6
                 if(count($block[attrs][ids]) < 1) {
7
                 if(count($block[attrs][ids]) < 1) {
7
                     $gallery_imgs_ids = [];
8
                     $gallery_imgs_ids = [];
8
                     foreach($block[innerBlocks] as $inner) {
9
                     foreach($block[innerBlocks] as $inner) {
9
                         $inner_id = $inner[attrs][id];
10
                         $inner_id = $inner[attrs][id];
10
                         array_push($gallery_imgs_ids, $inner_id);
11
                         array_push($gallery_imgs_ids, $inner_id);
11
                     }
12
                     }
12
-                    $block[attrs]['zzz'] = $gallery_imgs_ids;
13
                     $block[attrs][ids] = $gallery_imgs_ids;
13
                     $block[attrs][ids] = $gallery_imgs_ids;
14
                 } 
14
                 } 
15
                 array_push($parsed_blocks, $block[attrs]);
15
                 array_push($parsed_blocks, $block[attrs]);
34
 
34
 
35
         foreach ($images as $image) {
35
         foreach ($images as $image) {
36
             if($image->getAttribute('data-id')) {
36
             if($image->getAttribute('data-id')) {
37
-                $parse_images[$image->getAttribute('data-id')] = $image->getAttribute('src');
37
+                $id = $image->getAttribute('data-id');
38
             } else {
38
             } else {
39
                 $class_pieces = explode("-", $image->getAttribute('class'));
39
                 $class_pieces = explode("-", $image->getAttribute('class'));
40
-                $parse_images[end($class_pieces)] = $image->getAttribute('src');
40
+                $id = end($class_pieces);
41
             }
41
             }
42
+            // Format for lightbox wants an object
43
+            $parse_images[$id] = [
44
+                'src' => $image->getAttribute('src'),
45
+                'title' => $image->getAttribute('alt'),
46
+            ];
42
         }
47
         }
43
         return $parse_images;
48
         return $parse_images;
44
     }
49
     }

+ 25
- 18
vue-theme/src/pages/index.vue 查看文件

59
             // Limit the amount of posts because we
59
             // Limit the amount of posts because we
60
             // only need the most recent
60
             // only need the most recent
61
             const params = { limit: 1 }
61
             const params = { limit: 1 }
62
-            
62
+
63
             // We try and fetch EVERYTHING except
63
             // We try and fetch EVERYTHING except
64
             // for EVENTS and EXHIBITIONS
64
             // for EVENTS and EXHIBITIONS
65
             if (['event', 'exhibition'].includes(type)) {
65
             if (['event', 'exhibition'].includes(type)) {
66
                 // Only grab the current or upcoming on load
66
                 // Only grab the current or upcoming on load
67
-                const eventsOrExhibitions = await this.$store.dispatch(
68
-                    action,
69
-                    { 
70
-                        sortType: sortTypes.currentAndUpcoming,
71
-                        params
72
-                    }
73
-                )
67
+                const eventsOrExhibitions = await this.$store.dispatch(action, {
68
+                    sortType: sortTypes.currentAndUpcoming,
69
+                    params,
70
+                })
74
                 // If no current or upcoming, get past events
71
                 // If no current or upcoming, get past events
75
                 // to fill in the blanks
72
                 // to fill in the blanks
76
                 if (eventsOrExhibitions.length < 1) {
73
                 if (eventsOrExhibitions.length < 1) {
77
-                    this.$store.dispatch(action, { sortType: sortTypes.past, params })
74
+                    this.$store.dispatch(action, {
75
+                        sortType: sortTypes.past,
76
+                        params,
77
+                    })
78
                 }
78
                 }
79
-            }
80
-            else if (!omit.includes(type)){
79
+            } else if (!omit.includes(type)) {
81
                 this.$store.dispatch(action, { sortType: null, params })
80
                 this.$store.dispatch(action, { sortType: null, params })
82
             }
81
             }
83
         }
82
         }
84
         if (!this['allPagesLoaded']) {
83
         if (!this['allPagesLoaded']) {
85
-            await this.$store.dispatch('getAllPages', { sortType: null, params: null })
84
+            await this.$store.dispatch('getAllPages', {
85
+                sortType: null,
86
+                params: null,
87
+            })
86
         }
88
         }
87
         await this.checkAndSetHero('welcome')
89
         await this.checkAndSetHero('welcome')
88
-        await this.$store.dispatch('getRandomPosts', ['artist', 'guide', 'object', 'technique', 'publication', 'post'])
90
+        await this.$store.dispatch('getRandomPosts', [
91
+            'artist',
92
+            'guide',
93
+            'object',
94
+            'technique',
95
+            'publication',
96
+            'post',
97
+        ])
89
     },
98
     },
90
     methods: {
99
     methods: {
91
         firstPostOfType(type) {
100
         firstPostOfType(type) {
97
             // We always set a hero no matter what
106
             // We always set a hero no matter what
98
             // Because the hero component will deal
107
             // Because the hero component will deal
99
             // with how to render based on hero.url
108
             // with how to render based on hero.url
100
-            if(!this.allPages) return console.warn('no pages in state', this)
101
-            const page = this.allPages.filter(
102
-                page => page.slug == type,
103
-            )[0]
104
-            if(!page) return console.warn(`no page for ${type} found`)
109
+            if (!this.allPages) return console.warn('no pages in state', this)
110
+            const page = this.allPages.filter(page => page.slug == type)[0]
111
+            if (!page) return console.warn(`no page for ${type} found`)
105
 
112
 
106
             this.$store.commit('SET_HERO', this._setHeroInfo(page))
113
             this.$store.commit('SET_HERO', this._setHeroInfo(page))
107
         },
114
         },

+ 0
- 8
vue-theme/src/pages/single.vue 查看文件

202
             }
202
             }
203
         },
203
         },
204
         async singlePost(post) {
204
         async singlePost(post) {
205
-            console.log('---')
206
             if (!this.$el) return
205
             if (!this.$el) return
207
             const section = this.$el.children[0].querySelector('section')
206
             const section = this.$el.children[0].querySelector('section')
208
             await nextTick()
207
             await nextTick()
215
                         const activeGallery =
214
                         const activeGallery =
216
                             post.galleries[this.activeGalleryIndex]
215
                             post.galleries[this.activeGalleryIndex]
217
 
216
 
218
-                        console.log(post.galleries)
219
-                        console.log(activeGallery)
220
-
221
                         if (!activeGallery.ids) return
217
                         if (!activeGallery.ids) return
222
 
218
 
223
                         this.activeImageIndex = activeGallery.ids.indexOf(
219
                         this.activeImageIndex = activeGallery.ids.indexOf(
224
                             parseInt(e.target.dataset.id),
220
                             parseInt(e.target.dataset.id),
225
                         )
221
                         )
226
-                        console.log(
227
-                            `opening gallery: ${this.activeGalleryIndex}.${this.activeImageIndex}`,
228
-                        )
229
-                        console.log(e.target.dataset)
230
                     })
222
                     })
231
                 })
223
                 })
232
             })
224
             })

+ 11
- 0
vue-theme/src/utils/api.js 查看文件

71
                 cb(e)
71
                 cb(e)
72
             })
72
             })
73
     },
73
     },
74
+    async getOneOfEach(types, cb, random = false) {
75
+        let posts = []
76
+        let get = `oneofeach?types=${types.split(',')}`
77
+        if (random) {
78
+            get = get + '&orderby=rand'
79
+        }
80
+        await axios.get(SETTINGS.API_BASE_PATH + get).then(response => {
81
+            posts = [...response.data]
82
+        })
83
+        cb(posts)
84
+    },
74
     async getRandom(types, cb) {
85
     async getRandom(types, cb) {
75
         let randomPosts = []
86
         let randomPosts = []
76
         for (let type of types) {
87
         for (let type of types) {

正在加载...
取消
保存