Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local images #101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions airplane-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -145,7 +148,7 @@ 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 );

// Our activation / deactivation triggers.
// Our activation / deactivation triggers.
register_activation_hook( __FILE__, array( $this, 'create_setting' ) );
register_deactivation_hook( __FILE__, array( $this, 'remove_setting' ) );

Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -472,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 = "<img alt='{$alt}' src='{$image}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' style='background:#eee;' />";

// Return the avatar.
Expand Down Expand Up @@ -1257,6 +1260,34 @@ 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 static function get_local_image_url() {
return plugin_dir_url( plugin_basename( __FILE__ ) ) . 'lib/img/airplane.svg';
}

/**
* 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('#<img([^>]*)>#i', $content, $matches ) ) {
foreach ( $matches[0] as $match ) {
if ( \preg_match_all('#<img([^>]*)src="([^"]*)#i', $match, $image_url ) ) {
preg_match_all('#<img([^>]*)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.
}

Expand Down
4 changes: 4 additions & 0 deletions lib/img/airplane.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.