Skip to content

Commit

Permalink
Merge 7a26345 into 75b7bb0
Browse files Browse the repository at this point in the history
  • Loading branch information
derekherman committed Aug 10, 2020
2 parents 75b7bb0 + 7a26345 commit f1c47db
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 112 deletions.
1 change: 1 addition & 0 deletions assets/src/block-editor/blocks/image/block.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "unsplash/image",
"title": "Unsplash",
"category": "common",
"supports": {
"lightBlockWrapper": true,
Expand Down
2 changes: 1 addition & 1 deletion assets/src/media-selector/featured-image-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ addFilter(
'editor.MediaUpload',
'unsplash/extend-featured-image',
() => UnsplashMediaUpload,
20
10
);
1 change: 1 addition & 0 deletions assets/src/media-selector/helpers/import-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const importImage = image => {
title,
description,
caption,
parent: getConfig( 'postId' ),
};

return wp
Expand Down
15 changes: 7 additions & 8 deletions php/class-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ public function __construct( $plugin ) {
* Initiate the class.
*/
public function init() {
$this->plugin->add_doc_hooks( $this );
add_action( 'init', [ $this, 'register_blocks' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'register_block_editor_assets' ] );
}

/**
* Register our custom blocks.
*
* @action init
*/
public function register_blocks() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}

$blocks_dir = $this->plugin->dir_path . '/assets/js/blocks/';
$block_folders = [
'image',
Expand All @@ -64,16 +67,12 @@ public function register_blocks() {
$metadata['render_callback'] = [ $this, $callback ];
}

if ( function_exists( '\register_block_type' ) ) {
\register_block_type( $metadata['name'], $metadata );
}
register_block_type( $metadata['name'], $metadata );
}
}

/**
* Load Gutenberg assets.
*
* @action enqueue_block_editor_assets
*/
public function register_block_editor_assets() {
// Register block editor assets.
Expand Down
63 changes: 36 additions & 27 deletions php/class-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ public function __construct( Plugin $plugin ) {
* Initiate the class.
*/
public function init() {
$this->plugin->add_doc_hooks( $this );
add_action( 'attachment_submitbox_misc_actions', [ $this, 'attachment_submitbox_misc_actions' ], 11 );
add_filter( 'wp_get_attachment_url', [ $this, 'wp_get_attachment_url' ], 10, 2 );
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ], 99, 2 );
add_filter( 'rest_prepare_attachment', [ $this, 'rest_prepare_attachment' ], 99, 3 );
add_filter( 'content_save_pre', [ $this, 'replace_hotlinked_images_in_content' ], 99, 1 );
add_filter( 'the_content', [ $this, 'hotlink_images_in_content' ], 99, 1 );
add_filter( 'wp_get_attachment_image_src', [ $this, 'wp_get_attachment_image_src' ], 10, 5 );
add_filter( 'image_downsize', [ $this, 'image_downsize' ], 10, 3 );
add_filter( 'wp_calculate_image_srcset', [ $this, 'wp_calculate_image_srcset' ], 99, 5 );
add_filter( 'get_image_tag', [ $this, 'get_image_tag' ], 10, 6 );
add_filter( 'wp_get_attachment_caption', [ $this, 'wp_get_attachment_caption' ], 10, 2 );
add_filter( 'render_block', [ $this, 'render_block' ], 10, 2 );
add_filter( 'wp_edited_image_metadata', [ $this, 'add_edited_attachment_metadata' ], 10, 3 );
add_filter( 'wp_image_file_matches_image_meta', [ $this, 'make_unsplash_images_cropable' ], 10, 4 );
}

/**
* Filter wp_get_attachment_url
*
* @filter wp_get_attachment_url, 10, 2
*
* @param string $url Original URL.
* @param int $id Attachment ID.
*
Expand Down Expand Up @@ -77,8 +88,6 @@ public function get_attachment_url( $attachment_id ) {
* @param array $response Data for admin ajax.
* @param WP_Post $attachment Attachment object.
*
* @filter wp_prepare_attachment_for_js, 99, 2
*
* @return mixed
*/
public function wp_prepare_attachment_for_js( array $response, $attachment ) {
Expand Down Expand Up @@ -112,8 +121,6 @@ public function wp_prepare_attachment_for_js( array $response, $attachment ) {
* @param WP_Post $attachment The original attachment post.
* @param WP_REST_Request $wp_request Request used to generate the response.
*
* @filter rest_prepare_attachment, 99, 3
*
* @return mixed
*/
public function rest_prepare_attachment( $wp_response, $attachment, $wp_request ) {
Expand Down Expand Up @@ -176,8 +183,6 @@ public function rest_prepare_attachment( $wp_response, $attachment, $wp_request

/**
* Add unsplash original link to attachment edit page.
*
* @action attachment_submitbox_misc_actions, 11
*/
public function attachment_submitbox_misc_actions() {
$post = get_post();
Expand Down Expand Up @@ -240,8 +245,6 @@ public function change_full_url( array $sizes, $field, $url ) {
/**
* Filter image downsize.
*
* @filter image_downsize, 10, 3
*
* @param array $should_resize Array.
* @param int $id Attachment ID.
* @param array|string $size Size.
Expand Down Expand Up @@ -289,8 +292,6 @@ public function image_downsize( $should_resize, $id, $size ) {
/**
* Work around for image preview in Attachment screen.
*
* @filter wp_get_attachment_image_src, 10, 5
*
* @param array|false $image rray of image data, or boolean false if no image is available.
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Requested size of image. Image size name, or array of width
Expand Down Expand Up @@ -357,8 +358,6 @@ public function get_attachments_from_content( $content ) {
/**
* Replace hotlinked image URLs in content with ones from WordPress.
*
* @filter content_save_pre, 99, 1
*
* @param string $content Content.
* @return string Converted content with local images.
*/
Expand Down Expand Up @@ -411,8 +410,6 @@ public function replace_hotlinked_images_in_content( $content ) {
*
* @param string $content The raw post content to be filtered.
*
* @filter the_content, 99, 1
*
* @return string Converted content with hotlinked images.
*/
public function hotlink_images_in_content( $content ) {
Expand All @@ -432,8 +429,6 @@ public function hotlink_images_in_content( $content ) {
/**
* Filter source sets to give hotlink images.
*
* @filter wp_calculate_image_srcset, 99, 5
*
* @param array $sources {
* One or more arrays of source data to include in the 'srcset'.
*
Expand Down Expand Up @@ -648,8 +643,6 @@ public function prime_post_caches( array $attachment_ids ) {
* @param string|array $size Size of image. Image size or array of width and height values (in that order).
* Default 'medium'.
*
* @filter get_image_tag, 10, 6
*
* @return string Image tag.
*/
public function get_image_tag( $html, $id, $alt, $title, $align, $size ) {
Expand All @@ -668,8 +661,6 @@ public function get_image_tag( $html, $id, $alt, $title, $align, $size ) {
/**
* Remove html for captions, as some themes esc_html captions before displaying.
*
* @filter wp_get_attachment_caption, 10, 2
*
* @param string $caption Caption for the given attachment.
* @param int $attachment_id Attachment ID.
*
Expand All @@ -688,8 +679,6 @@ public function wp_get_attachment_caption( $caption, $attachment_id ) {
/**
* Filters the content of a single block.
*
* @filter render_block, 10, 2
*
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
*
Expand All @@ -710,8 +699,6 @@ public function render_block( $block_content, $block ) {
/**
* Add Unsplash metadata for edited attachment
*
* @filter wp_edited_image_metadata, 10, 3
*
* @param array $data Array of updated attachment meta data.
* @param int $new_attachment_id Attachment post ID.
* @param int $attachment_id Original Attachment post ID.
Expand All @@ -731,4 +718,26 @@ public function add_edited_attachment_metadata( $data, $new_attachment_id, $atta

return $data;
}

/**
* Filter whether an image path or URI matches image meta.
*
* @param bool $match Whether the image relative path from the image meta
* matches the end of the URI or path to the image file.
* @param string $image_location Full path or URI to the tested image file.
* @param array $image_meta (Unused) The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id The image attachment ID or 0 if not supplied.
*
* @return bool Can an image cropable.
*/
public function make_unsplash_images_cropable( $match, $image_location, $image_meta, $attachment_id ) {
$unsplash_url = $this->get_unsplash_url( $attachment_id );
$cropped = $this->is_cropped_image( $attachment_id );
$is_unsplash = strpos( $image_location, 'images.unsplash.com' );
if ( ! $unsplash_url || $cropped || ( false === $is_unsplash ) ) {
return $match;
}

return true;
}
}
2 changes: 1 addition & 1 deletion php/class-plugin-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function locate_plugin() {
}

$plugin_dir = dirname( dirname( $file_name ) );
$plugin_path = $this->relative_path( $plugin_dir, basename( content_url() ), \DIRECTORY_SEPARATOR );
$plugin_path = $this->relative_path( $plugin_dir, basename( content_url() ), '/' );

$dir_url = content_url( trailingslashit( $plugin_path ) );
$dir_path = $plugin_dir;
Expand Down

0 comments on commit f1c47db

Please sign in to comment.