Completed
Push — add/photon-thumbnail-disabled-... ( ef6228...eb45e6 )
by
unknown
09:02
created

drop-in.php ➔ jetpack_enable_noresize_mode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/*
3
 * Plugin Name: Drop in Photon noresize mode
4
 * Plugin URI: https://jetpack.com/support/site-accelerator/
5
 * Description: Enables the noresize mode for Photon, allowing to avoid intermediate size files generation.
6
 * Author: Automattic
7
 * Version: 0.1-alpha
8
 * Author URI: https://jetpack.com
9
 * License: GPL2+
10
 * Text Domain: jetpack-dropin
11
 *
12
 * @package jetpack-11287
13
 *
14
 * This is a drop-in testing package for https://github.com/Automattic/jetpack/pull/11287
15
 */
16
17
function jetpack_enable_noresize_mode() {
18
	// The main objective of noresize mode is to disable additional resized image versions creation.
19
	// This filter handles removal of additional sizes.
20
	add_filter( 'intermediate_image_sizes_advanced', 'wpcom_intermediate_sizes' );
21
22
	// This allows to assign the Photon domain to images that normally use the home URL as base.
23
	add_filter( 'jetpack_photon_domain', 'jetpack_filter_photon_norezise_mode_domain', 10, 2 );
24
25
	add_filter( 'the_content', 'jetpack_filter_content_add', 0 );
26
27
	// Jetpack hooks in at six nines (999999) so this filter does at seven.
28
	add_filter( 'the_content', 'jetpack_filter_content_remove', 9999999 );
29
30
	// Regular Photon operation mode filter doesn't run when is_admin(), so we need an additional filter.
31
	// This is temporary until Jetpack allows more easily running these filters for is_admin().
32
	if ( is_admin() ) {
33
		$photon = Jetpack_Photon::instance();
34
		add_filter( 'image_downsize', array( $photon, 'filter_image_downsize' ), 5, 3 );
35
		add_filter( 'jetpack_photon_admin_allow_image_downsize', 'jetpack_filter_photon_noresize_allow_downsize', 10, 2 );
36
	}
37
}
38
39
/**
40
 * This is our catch-all to strip dimensions from intermediate images in content.
41
 * Since this primarily only impacts post_content we do a little dance to add the filter early
42
 * to `the_content` and then remove it later on in the same hook.
43
 *
44
 * @param String $content the post content.
45
 * @return String the post content unchanged.
46
 */
47
function jetpack_filter_content_add( $content ) {
48
	add_filter( 'jetpack_photon_pre_image_url', array( __CLASS__, 'strip_image_dimensions_maybe' ) );
49
	return $content;
50
}
51
52
/**
53
 * Removing the content filter that was set previously.
54
 *
55
 * @param String $content the post content.
56
 * @return String the post content unchanged.
57
 */
58
function jetpack_filter_content_remove( $content ) {
59
	remove_filter( 'jetpack_photon_pre_image_url', array( __CLASS__, 'strip_image_dimensions_maybe' ) );
60
	return $content;
61
}
62
63
/**
64
 * Short circuits the Photon filter to enable Photon processing for any URL.
65
 *
66
 * @param String $photon_url a proposed Photon URL for the media file.
67
 * @param String $image_url the original media URL.
68
 * @return String an URL to be used for the media file.
69
 */
70
function jetpack_filter_photon_norezise_mode_domain( $photon_url, $image_url ) {
71
	return $photon_url;
72
}
73
74
/**
75
 * Allows any image that gets passed to Photon to be resized via Photon.
76
 *
77
 * @param Boolean $allow whether to allow the image to get resized with Photon.
78
 * @param Array   $params an array containing image data, attachment ID and size variant.
79
 * @return Boolean
80
 */
81
function jetpack_filter_photon_noresize_allow_downsize( $allow, $params ) {
82
	return true;
83
}
84
85
/**
86
 * Disables intermediate sizes to disallow resizing.
87
 *
88
 * @param Array $sizes an array containing image sizes.
89
 * @return Boolean
90
 */
91
function wpcom_intermediate_sizes( $sizes ) {
92
	return array();
93
}
94
95
add_action( 'plugins_loaded', 'jetpack_enable_noresize_mode' );
96