Created
August 2, 2017 15:15
-
-
Save david-binda/e4dcdc325830cb04e2ab1c9ce5e1a156 to your computer and use it in GitHub Desktop.
VIP Go File service layer for Photonfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* VIP: make the Photonfill plugin working with the VIP Go file service instead of WPCOM's Photon. | |
*/ | |
// Hijack the photonfill prefix. | |
add_filter( 'photonfill_hook_prefix', function( $prefix ) { | |
return 'vipgo'; | |
} ); | |
// Rewrite arguments to the ones used by VIP Go File service. | |
add_filter( 'jetpack_photon_pre_args', function( $args, $image_url, $scheme ) { | |
$key_mapping = array( | |
'width' => 'w', | |
'height' => 'h', | |
); | |
foreach( $key_mapping as $old_key => $new_key ) { | |
if ( false === array_key_exists( $new_key, $args ) && true === array_key_exists( $old_key, $args ) ) { | |
$args[$new_key] = $args[$old_key]; | |
unset( $args[$old_key] ); | |
} | |
} | |
// Remove args which are not needed on VIP Go Fileservice | |
$blacklisted_keys = array( | |
'attachment_id', | |
); | |
foreach( $blacklisted_keys as $blacklisted_key ) { | |
unset( $args[$blacklisted_key] ); | |
} | |
return $args; | |
}, 10 ,3 ); | |
// Use Jetpack's function for generating the photon URL and modify the output the way it references the VIP Go file service. | |
function vipgo_photon_url( $image_url, $args = array(), $scheme = null ) { | |
$jetpack_photon_url = jetpack_photon_url( $image_url, $args, $scheme ); | |
// Remove the Photon URL part. | |
foreach( array( 0, 1, 2 ) as $subdomain ) { | |
$vipgo_photon_url = str_replace( "https://i{$subdomain}.wp.com", 'https:/', $jetpack_photon_url ); | |
} | |
// Remove the ssl arg. | |
$vipgo_photon_url = add_query_arg( 'ssl', false, $vipgo_photon_url ); | |
return $vipgo_photon_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment