Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Jetpack_WPCOM_Image_Editor_GD
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 make_image
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * The GD image editor for WordPress.com sites.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8// This file can load before core's editor classes.
9require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
10require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
11require_once __DIR__ . '/class-jetpack-wpcom-ai-marker.php';
12
13/**
14 * Keeps the source's AI DigitalSourceType, which GD drops on save.
15 *
16 * Global, not namespaced: Photon on WoW declares "Photon_Subsizes_{$class}" with eval(), so a namespaced name would cause a fatal error.
17 */
18class Jetpack_WPCOM_Image_Editor_GD extends WP_Image_Editor_GD {
19
20    /**
21     * The source's AI DigitalSourceType: '' when it has none, null until the first save.
22     *
23     * @var string|null
24     */
25    private $digital_source_type = null;
26
27    /**
28     * Saves the image, then writes the source's AI DigitalSourceType into it.
29     *
30     * @param string   $filename  Path to save the image to.
31     * @param callable $callback  GD function that writes the image.
32     * @param array    $arguments Arguments for the callback.
33     * @return bool
34     */
35    protected function make_image( $filename, $callback, $arguments ) {
36        $marker = new Jetpack_WPCOM_AI_Marker( 'image_editor_ai_marker' );
37        if ( null === $this->digital_source_type ) {
38            $this->digital_source_type = $marker->read( $this->file );
39        }
40
41        $result = parent::make_image( $filename, $callback, $arguments );
42
43        if ( $result && '' !== $this->digital_source_type && ! wp_is_stream( $filename ) ) {
44            $marker->write( $filename, $this->digital_source_type );
45        }
46
47        return $result;
48    }
49}