瀏覽代碼

:bug: fixing api bug to get images by id

tags/0.9.0
j 5 年之前
父節點
當前提交
75f4c527bb
共有 1 個文件被更改,包括 22 次插入15 次删除
  1. 22
    15
      plugins/cia-endpoints/includes/class.make-endpoint.php

+ 22
- 15
plugins/cia-endpoints/includes/class.make-endpoint.php 查看文件

@@ -53,21 +53,27 @@ class Make_Endpoint_For extends WP_REST_Controller {
53 53
 
54 54
     private function _getAttachments($item) {
55 55
         // Get media
56
-        $attached = array();
57
-            
58
-        // Image sizes you want
59 56
         $desiredSizes = ['thumbnail', 'medium', 'large', 'full'];
60
-        
61
-        foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
62
-            $imageSizes = array();
63
-            foreach ( $desiredSizes as $size ):
64
-                $imageSizes[$size] = wp_get_attachment_image_url($attached_media->ID, $size);
65
-            endforeach;
57
+		$all_image_ids_in_gallery = array();
58
+		$all_image_ids = array();
59
+		foreach ($galleries as $gallery) {
60
+			array_push($all_image_ids_in_gallery, $gallery[ids]);
61
+        }
62
+		foreach ($all_image_ids_in_gallery as $ids) {
63
+			foreach ($ids as $id) {
64
+				array_push($all_image_ids, $id);
65
+			}
66
+		}
67
+		$attachment_map = array();
68
+		foreach ($all_image_ids as $id) {
69
+			$imageSizes = array();
70
+            foreach ($desiredSizes as $size) {	
71
+                $imageSizes[$size] = wp_get_attachment_image_url($id, $size);
72
+			}
66 73
             // store size:url map under image id
67
-            $attached[$attached_media->ID] = $imageSizes;
68
-        endforeach;
69
-
70
-        return $attached;
74
+			$attachment_map[$id] = $imageSizes;
75
+        }
76
+    	return $attachment_map;
71 77
     }
72 78
 
73 79
     public function prepare_item_for_response( $args ) {
@@ -81,8 +87,6 @@ class Make_Endpoint_For extends WP_REST_Controller {
81 87
             $filtered[blocks] = get_rearrange_blocks(
82 88
                 parse_blocks( $item->post_content )
83 89
             );
84
-
85
-            $filtered[attached] = $this->_getAttachments($item);
86 90
             
87 91
             // Galleries From blocks
88 92
             $media_ids = array();
@@ -93,6 +97,9 @@ class Make_Endpoint_For extends WP_REST_Controller {
93 97
                 parse_blocks( $item->post_content )
94 98
             );
95 99
 
100
+            // Find all image info
101
+            $filtered[attached] = $this->_getAttachments($item, $filtered[galleries]);
102
+
96 103
             $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
97 104
             $filtered[relatedto] = p2p_related_to($item->ID, $item->post_type);
98 105
 

Loading…
取消
儲存