From d249f015ff724d2a36d6d40480c1aecea23bde30 Mon Sep 17 00:00:00 2001 From: Russell Fair Date: Sat, 8 Jun 2019 13:40:11 -0400 Subject: [PATCH 1/4] add filter for replacing media attachment with local image --- airplane-mode.php | 15 ++++++++++++++- lib/img/airplane.svg | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 lib/img/airplane.svg diff --git a/airplane-mode.php b/airplane-mode.php index ed0cd14..343c7b9 100644 --- a/airplane-mode.php +++ b/airplane-mode.php @@ -145,6 +145,9 @@ private function __construct() { add_filter( 'airplane_mode_parse_style', array( $this, 'bypass_asset_block' ), 10, 2 ); add_filter( 'airplane_mode_parse_script', array( $this, 'bypass_asset_block' ), 10, 2 ); + // Filter Attachment Url + add_filter( 'wp_get_attachment_url', array( $this, 'get_local_image_url' ), 10, 1 ); + // Our activation / deactivation triggers. register_activation_hook( __FILE__, array( $this, 'create_setting' ) ); register_deactivation_hook( __FILE__, array( $this, 'remove_setting' ) ); @@ -211,7 +214,7 @@ private function __construct() { // Add back the upload tab. add_action( 'install_themes_upload', 'install_themes_upload', 10, 0 ); - // Define core contants for more protection. + // Define core contants for more protection. if ( ! defined( 'AUTOMATIC_UPDATER_DISABLED' ) ) { define( 'AUTOMATIC_UPDATER_DISABLED', true ); } @@ -1257,6 +1260,16 @@ public function count_http_requests() { $this->http_count++; } + /** + * Generates a url to a locally stored image. + * Note: The actual SVG needs to be replaced with something licensed for distribution. + * + * @return string url to the airplane svg image. + */ + public function get_local_image_url() { + return plugin_dir_url( plugin_basename( __FILE__ ) ) . 'lib/img/airplane.svg'; + } + // End class. } diff --git a/lib/img/airplane.svg b/lib/img/airplane.svg new file mode 100644 index 0000000..ab979f1 --- /dev/null +++ b/lib/img/airplane.svg @@ -0,0 +1,4 @@ + + + + From 419d6869549032f66ae57b39a33fe89911248ae3 Mon Sep 17 00:00:00 2001 From: Russell Fair Date: Sat, 8 Jun 2019 13:47:09 -0400 Subject: [PATCH 2/4] add filter to replace avatar images with local image --- airplane-mode.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/airplane-mode.php b/airplane-mode.php index 343c7b9..ce9a609 100644 --- a/airplane-mode.php +++ b/airplane-mode.php @@ -148,7 +148,10 @@ private function __construct() { // Filter Attachment Url add_filter( 'wp_get_attachment_url', array( $this, 'get_local_image_url' ), 10, 1 ); - // Our activation / deactivation triggers. + // Filter Avatar Image Src + add_filter( 'get_avatar', array( $this, 'filter_avatars' ), 10, 1 ); + + // Our activation / deactivation triggers. register_activation_hook( __FILE__, array( $this, 'create_setting' ) ); register_deactivation_hook( __FILE__, array( $this, 'remove_setting' ) ); @@ -1266,10 +1269,24 @@ public function count_http_requests() { * * @return string url to the airplane svg image. */ - public function get_local_image_url() { + public static function get_local_image_url() { return plugin_dir_url( plugin_basename( __FILE__ ) ) . 'lib/img/airplane.svg'; } + public static function replace_image_src( $avatar ) { + return sprintf('src="%s"', self::get_local_image_url() ); + } + + /** + * Filter to replace the avatar image url src with a local image. + * + * @param $avatar + * @return null|string|string[] + */ + public function filter_avatars($avatar ) { + return \preg_replace_callback('~ src=([^"]+) ~', array( $this, 'replace_image_src' ), $avatar ); + } + // End class. } From 864da1eb8bde237bf950507afa143142dc6a0909 Mon Sep 17 00:00:00 2001 From: Russell Fair Date: Sat, 8 Jun 2019 13:56:44 -0400 Subject: [PATCH 3/4] refactor get avatar to work with the existing functionality, replacing svg data --- airplane-mode.php | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/airplane-mode.php b/airplane-mode.php index ce9a609..d096bae 100644 --- a/airplane-mode.php +++ b/airplane-mode.php @@ -88,10 +88,13 @@ private function __construct() { add_action( 'script_loader_src', array( $this, 'block_script_load' ), 100 ); add_action( 'admin_init', array( $this, 'remove_update_crons' ) ); add_action( 'admin_init', array( $this, 'remove_schedule_hook' ) ); - add_filter( 'embed_oembed_html', array( $this, 'block_oembed_html' ), 1, 4 ); add_filter( 'get_avatar', array( $this, 'replace_gravatar' ), 1, 5 ); - add_filter( 'map_meta_cap', array( $this, 'prevent_auto_updates' ), 10, 2 ); + add_filter( 'wp_get_attachment_url', array( $this, 'get_local_image_url' ), 10, 1 ); + add_filter( 'the_content', array( $this, 'filter_content_images' ), 10, 1 ); + + + add_filter( 'map_meta_cap', array( $this, 'prevent_auto_updates' ), 10, 2 ); add_filter( 'default_avatar_select', array( $this, 'default_avatar' ) ); // Kill all the http requests. @@ -145,12 +148,6 @@ private function __construct() { add_filter( 'airplane_mode_parse_style', array( $this, 'bypass_asset_block' ), 10, 2 ); add_filter( 'airplane_mode_parse_script', array( $this, 'bypass_asset_block' ), 10, 2 ); - // Filter Attachment Url - add_filter( 'wp_get_attachment_url', array( $this, 'get_local_image_url' ), 10, 1 ); - - // Filter Avatar Image Src - add_filter( 'get_avatar', array( $this, 'filter_avatars' ), 10, 1 ); - // Our activation / deactivation triggers. register_activation_hook( __FILE__, array( $this, 'create_setting' ) ); register_deactivation_hook( __FILE__, array( $this, 'remove_setting' ) ); @@ -478,7 +475,7 @@ public function replace_gravatar( $avatar, $id_or_email, $size, $default, $alt ) } // Swap out the file for a base64 encoded image. - $image = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; + $image = self::get_local_image_url(); $avatar = "{$alt}"; // Return the avatar. @@ -1273,7 +1270,12 @@ public static function get_local_image_url() { return plugin_dir_url( plugin_basename( __FILE__ ) ) . 'lib/img/airplane.svg'; } + /** + * @param $avatar + * @return string + */ public static function replace_image_src( $avatar ) { + wp_die( var_dump( $avatar ) ); return sprintf('src="%s"', self::get_local_image_url() ); } @@ -1283,8 +1285,26 @@ public static function replace_image_src( $avatar ) { * @param $avatar * @return null|string|string[] */ - public function filter_avatars($avatar ) { - return \preg_replace_callback('~ src=([^"]+) ~', array( $this, 'replace_image_src' ), $avatar ); + public function filter_avatars( $avatar ) { + return \preg_replace_callback('~src="([^"]*)~', array( $this, 'replace_image_src' ), $avatar ); + } + + /** + * Filters for instance of an image tag in the content, and replaces with local image. + * + * @param string $content the post content. + * @return mixed + */ + public function filter_content_images($content ) { + if ( \preg_match_all('#]*)>#i', $content, $matches ) ) { + foreach ( $matches[0] as $match ) { + if ( \preg_match_all('#]*)src="([^"]*)#i', $match, $image_url ) ) { + preg_match_all('#]*)sizes="([^"]*)(max-width: (\d+)px)#i', $match, $image_size ); + $content = \str_replace($image_url[2], sprintf('%s" width="%spx', self::get_local_image_url(), $image_size[4][0] ), $content ); + } + } + } + return $content; } // End class. From df42fd7e54c03fbc6028440d7affbb34b17dbe09 Mon Sep 17 00:00:00 2001 From: Russell Fair Date: Sat, 8 Jun 2019 13:58:03 -0400 Subject: [PATCH 4/4] remove left over avatar functions, add content filter --- airplane-mode.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/airplane-mode.php b/airplane-mode.php index d096bae..a4bf087 100644 --- a/airplane-mode.php +++ b/airplane-mode.php @@ -1270,25 +1270,6 @@ public static function get_local_image_url() { return plugin_dir_url( plugin_basename( __FILE__ ) ) . 'lib/img/airplane.svg'; } - /** - * @param $avatar - * @return string - */ - public static function replace_image_src( $avatar ) { - wp_die( var_dump( $avatar ) ); - return sprintf('src="%s"', self::get_local_image_url() ); - } - - /** - * Filter to replace the avatar image url src with a local image. - * - * @param $avatar - * @return null|string|string[] - */ - public function filter_avatars( $avatar ) { - return \preg_replace_callback('~src="([^"]*)~', array( $this, 'replace_image_src' ), $avatar ); - } - /** * Filters for instance of an image tag in the content, and replaces with local image. *