Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
66.67% covered (warning)
66.67%
2 / 3
CRAP
n/a
0 / 0
jetpack_wpcom_ai_marker_register_flag
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
jetpack_wpcom_ai_marker_tool_path
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
6.22
jetpack_wpcom_ai_marker_image_editors
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Loads the WordPress.com GD image editor.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Feature_Flags\Feature_Flags;
9
10const JETPACK_WPCOM_AI_MARKER_FLAG = 'wpcom-ai-marker';
11
12/**
13 * Registers the feature flag.
14 *
15 * Runs as this file loads, so the flag is listed wherever flags are read or toggled.
16 */
17function jetpack_wpcom_ai_marker_register_flag() {
18    Feature_Flags::register(
19        JETPACK_WPCOM_AI_MARKER_FLAG,
20        array(
21            'default'     => true,
22            'description' => 'Keep the AI marker in images WordPress.com edits or converts. Turn off to stop running exiftool and c2patool.',
23            'owner'       => 'jetpack-mu-wpcom',
24        )
25    );
26}
27jetpack_wpcom_ai_marker_register_flag();
28
29/**
30 * Returns the path of exiftool or c2patool, or '' when the host has none.
31 *
32 * @param string $tool 'exiftool' or 'c2patool'.
33 * @return string
34 */
35function jetpack_wpcom_ai_marker_tool_path( $tool ) {
36    if ( ! Feature_Flags::is_enabled( JETPACK_WPCOM_AI_MARKER_FLAG ) ) {
37        return '';
38    }
39
40    $candidates = 'c2patool' === $tool
41        ? array( defined( 'C2PATOOL' ) ? C2PATOOL : '/usr/local/bin/c2patool' )
42        : array( '/usr/local/bin/exiftool', '/usr/bin/exiftool' );
43
44    $path = '';
45    foreach ( $candidates as $candidate ) {
46        if ( is_executable( $candidate ) ) {
47            $path = $candidate;
48            break;
49        }
50    }
51
52    /**
53     * Filters the path of exiftool or c2patool. Return '' to skip that tool.
54     *
55     * @since $$next-version$$
56     *
57     * @param string $path The tool's path, or '' when the host has none.
58     * @param string $tool 'exiftool' or 'c2patool'.
59     */
60    return apply_filters( 'jetpack_wpcom_ai_marker_tool_path', $path, $tool );
61}
62
63/**
64 * Swaps core's GD editor for the WordPress.com one, when the host has exiftool.
65 *
66 * @param string[] $editors Image editor class names.
67 * @return string[]
68 */
69function jetpack_wpcom_ai_marker_image_editors( $editors ) {
70    $index = array_search( 'WP_Image_Editor_GD', $editors, true );
71    if ( false === $index || '' === jetpack_wpcom_ai_marker_tool_path( 'exiftool' ) ) {
72        return $editors;
73    }
74
75    require_once __DIR__ . '/class-jetpack-wpcom-image-editor-gd.php';
76    $editors[ $index ] = 'Jetpack_WPCOM_Image_Editor_GD';
77
78    return $editors;
79}
80add_filter( 'wp_image_editors', 'jetpack_wpcom_ai_marker_image_editors' );