Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 2
Jetpack_Tiled_Gallery_Item
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 6
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
56
 fuzzy_image_meta
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 meta_width
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 meta_height
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 medium_file
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 large_file
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
Jetpack_Tiled_Gallery_Rectangular_Item
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
Jetpack_Tiled_Gallery_Square_Item
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
Jetpack_Tiled_Gallery_Circle_Item
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
4
5if ( ! defined( 'ABSPATH' ) ) {
6    exit( 0 );
7}
8
9/**
10 * Jetpack Tiled Gallery Item class.
11 */
12abstract class Jetpack_Tiled_Gallery_Item {
13    /**
14     * Is the image grayscale.
15     *
16     * @var bool
17     */
18    public $grayscale;
19
20    /**
21     * The image title.
22     *
23     * @var string
24     */
25    public $image_title;
26
27    /**
28     * The image alt.
29     *
30     * @var string
31     */
32    public $image_alt;
33
34    /**
35     * The image size.
36     *
37     * @var string|null
38     */
39    public $size;
40
41    /**
42     * The original file.
43     *
44     * @var string|bool
45     */
46    public $orig_file;
47
48    /**
49     * The image attachment link.
50     *
51     * @var string
52     */
53    public $link;
54
55    /**
56     * The image URL.
57     *
58     * @var string
59     */
60    public $img_src;
61
62    /**
63     * The image srcset.
64     *
65     * @var string
66     */
67    public $img_srcset;
68
69    /**
70     * The image data.
71     *
72     * @var object
73     */
74    public $image;
75
76    /**
77     * Constructor function.
78     *
79     * @param object $attachment_image - the attachment image.
80     * @param string $needs_attachment_link - the attachment link.
81     * @param bool   $grayscale - if the image is in grayscale.
82     */
83    public function __construct( $attachment_image, $needs_attachment_link, $grayscale ) {
84        $this->image     = $attachment_image;
85        $this->grayscale = $grayscale;
86
87        $this->image_title = $this->image->post_title;
88
89        $this->image_alt = get_post_meta( $this->image->ID, '_wp_attachment_image_alt', true );
90        // If no Alt value, use the caption
91        if ( empty( $this->image_alt ) && ! empty( $this->image->post_excerpt ) ) {
92            $this->image_alt = trim( wp_strip_all_tags( $this->image->post_excerpt ) );
93        }
94        // If still no Alt value, use the title
95        if ( empty( $this->image_alt ) && ! empty( $this->image->post_title ) ) {
96            $this->image_alt = trim( wp_strip_all_tags( $this->image->post_title ) );
97        }
98
99        $this->orig_file = wp_get_attachment_url( $this->image->ID );
100        $this->link      = $needs_attachment_link
101            ? get_attachment_link( $this->image->ID )
102            // The filter will photonize the URL if and only if Photon is active
103            : apply_filters( 'jetpack_photon_url', $this->orig_file );
104
105        $img_args = array(
106            'w' => $this->image->width,
107            'h' => $this->image->height,
108        );
109        // If h and w are the same, there's a reasonably good chance the image will need cropping to avoid being stretched.
110        if ( $this->image->height === $this->image->width ) {
111            $img_args['crop'] = true;
112        }
113        // The function will always photonoize the URL (even if Photon is
114        // not active). We need to photonize the URL to set the width/height.
115        $this->img_src = Image_CDN_Core::cdn_url( $this->orig_file, $img_args );
116
117        $image_meta = wp_get_attachment_metadata( $attachment_image->ID );
118        $size_array = array( absint( $this->image->width ), absint( $this->image->height ) );
119
120        $this->img_srcset = wp_calculate_image_srcset( $size_array, $this->img_src, $image_meta, $attachment_image->ID );
121    }
122
123    /**
124     * Handle the fuzzy image meta.
125     *
126     * @return array
127     */
128    public function fuzzy_image_meta() {
129        $meta     = wp_get_attachment_metadata( $this->image->ID );
130        $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
131        if ( ! empty( $img_meta ) ) {
132            foreach ( $img_meta as $k => $v ) {
133                if ( 'latitude' === $k || 'longitude' === $k ) {
134                    unset( $img_meta[ $k ] );
135                }
136            }
137        }
138
139        return $img_meta;
140    }
141
142    /**
143     * Return the meta width.
144     *
145     * @return int|string
146     */
147    public function meta_width() {
148        $meta = wp_get_attachment_metadata( $this->image->ID );
149        return isset( $meta['width'] ) ? (int) $meta['width'] : '';
150    }
151
152    /**
153     * Return the meta height.
154     *
155     * @return int|string
156     */
157    public function meta_height() {
158        $meta = wp_get_attachment_metadata( $this->image->ID );
159        return isset( $meta['height'] ) ? (int) $meta['height'] : '';
160    }
161
162    /**
163     * Return the medium file info.
164     *
165     * @return array|string
166     */
167    public function medium_file() {
168        $medium_file_info = wp_get_attachment_image_src( $this->image->ID, 'medium' );
169        $medium_file      = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
170        return $medium_file;
171    }
172
173    /**
174     * Return large file info.
175     *
176     * @return array|string
177     */
178    public function large_file() {
179        $large_file_info = wp_get_attachment_image_src( $this->image->ID, 'large' );
180        $large_file      = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
181        return $large_file;
182    }
183}
184
185/**
186 * Tiled gallery rectangular item class.
187 */
188class Jetpack_Tiled_Gallery_Rectangular_Item extends Jetpack_Tiled_Gallery_Item { // phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound, Generic.Classes.OpeningBraceSameLine.ContentAfterBrace
189    /**
190     * Constructor function.
191     *
192     * @param object $attachment_image - the attachment image.
193     * @param string $needs_attachment_link - the attachment link.
194     * @param bool   $grayscale - if the image is in grayscale.
195     */
196    public function __construct( $attachment_image, $needs_attachment_link, $grayscale ) {
197        parent::__construct( $attachment_image, $needs_attachment_link, $grayscale );
198
199        $this->size = 'large';
200
201        if ( $this->image->width < 250 ) {
202            $this->size = 'small';
203        }
204    }
205}
206
207/**
208 * Tiled gallery square item class.
209 */
210class Jetpack_Tiled_Gallery_Square_Item extends Jetpack_Tiled_Gallery_Item { // phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound, Generic.Classes.OpeningBraceSameLine.ContentAfterBrace
211}
212
213/**
214 * Tiled gallery circle item class.
215 */
216class Jetpack_Tiled_Gallery_Circle_Item extends Jetpack_Tiled_Gallery_Square_Item { // phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound, Generic.Classes.OpeningBraceSameLine.ContentAfterBrace
217}