Skip to content

Commit

Permalink
Merge branch 'develop' into fix/cropped-image
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Aug 3, 2020
2 parents 95bddf3 + 4eb4cab commit 55d9034
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
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
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
53 changes: 29 additions & 24 deletions php/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function init() {

add_action( 'wp_default_scripts', [ $this, 'register_polyfill_scripts' ] );
add_action( 'wp_enqueue_media', [ $this, 'enqueue_media_scripts' ] );
add_action( 'enqueue_block_assets', [ $this, 'enqueue_block_assets' ], 100 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
add_action( 'init', [ $this, 'register_meta' ] );
add_action( 'init', [ $this, 'register_taxonomy' ] );
Expand Down Expand Up @@ -165,6 +166,7 @@ public function enqueue_media_scripts() {
true
);

$post = get_post();
wp_localize_script(
'unsplash-media-selector',
'unsplash',
Expand All @@ -185,33 +187,10 @@ public function enqueue_media_scripts() {
'errors' => [
'generic' => esc_html__( 'The file was unable to be imported into the Media Library. Please try again', 'unsplash' ),
],

'postId' => ( $post ) ? $post->ID : null,
]
);

/*
* If the block editor is available, the featured image selector in the editor will need to be overridden. This
* is an extension of the media selector enqueued above and is separated from it because the required dependencies
* are not available in WP < 5.0. It would not make sense to polyfill these dependencies anyways since the block
* editor is not officially compatible with WP < 5.0.
*/
if ( has_action( 'enqueue_block_assets' ) ) {
$asset_file = $this->dir_path . '/assets/js/featured-image-selector.asset.php';
$asset = is_readable( $asset_file ) ? require $asset_file : [];
$version = isset( $asset['version'] ) ? $asset['version'] : $this->asset_version();

$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : [];
$dependencies[] = 'unsplash-media-selector';

wp_enqueue_script(
'unsplash-featured-image-selector',
$this->asset_url( 'assets/js/featured-image-selector.js' ),
$dependencies,
$version,
true
);
}

// Enqueue media selector CSS.
wp_enqueue_style(
'unsplash-media-selector-style',
Expand All @@ -225,6 +204,32 @@ public function enqueue_media_scripts() {
return true;
}

/**
* Enqueue block editor assets.
*/
public function enqueue_block_assets() {
/*
* If the block editor is available, the featured image selector in the editor will need to be overridden. This
* is an extension of the media selector enqueued above and is separated from it because the required dependencies
* are not available in WP < 5.0. It would not make sense to polyfill these dependencies anyways since the block
* editor is not officially compatible with WP < 5.0.
*/
$asset_file = $this->dir_path . '/assets/js/featured-image-selector.asset.php';
$asset = is_readable( $asset_file ) ? require $asset_file : [];
$version = isset( $asset['version'] ) ? $asset['version'] : $this->asset_version();

$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : [];
$dependencies[] = 'unsplash-media-selector';

wp_enqueue_script(
'unsplash-featured-image-selector',
$this->asset_url( 'assets/js/featured-image-selector.js' ),
$dependencies,
$version,
true
);
}

/**
* Load our admin assets.
*/
Expand Down
25 changes: 23 additions & 2 deletions php/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,31 @@ public function register_routes() {
'/' . $this->rest_base . '/import/(?P<id>[\w-]+)',
[
'args' => [
'id' => [
'id' => [
'description' => esc_html__( 'Unsplash image ID.', 'unsplash' ),
'type' => 'string',
],
'parent' => [
'description' => esc_html__( 'Parent post ID.', 'unsplash' ),
'type' => 'integer',
'default' => 0,
],
'alt' => [
'description' => esc_html__( 'Image alt text.', 'unsplash' ),
'type' => 'string',
],
'title' => [
'description' => esc_html__( 'Image title.', 'unsplash' ),
'type' => 'string',
],
'description' => [
'description' => esc_html__( 'Image description.', 'unsplash' ),
'type' => 'string',
],
'caption' => [
'description' => esc_html__( 'Image caption.', 'unsplash' ),
'type' => 'string',
],
],
[
'methods' => WP_REST_Server::CREATABLE,
Expand Down Expand Up @@ -232,7 +253,7 @@ public function get_import( $request ) {
$image->set_field( 'description', $request->get_param( 'description' ) );
$image->set_field( 'caption', $request->get_param( 'caption' ) );

$importer = new Import( $id, $image );
$importer = new Import( $id, $image, $request->get_param( 'parent' ) );
$attachment_id = $importer->process();
// @codeCoverageIgnoreStart
if ( is_wp_error( $attachment_id ) ) {
Expand Down
15 changes: 13 additions & 2 deletions tests/phpunit/php/class-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,20 @@ public function test_enqueue_media_scripts() {
$plugin = get_plugin_instance();
$plugin->enqueue_media_scripts();
$this->assertTrue( wp_script_is( 'unsplash-media-selector', 'enqueued' ) );
}

/**
* Test for enqueue_block_assets() method.
*
* @see Plugin::enqueue_block_assets()
*/
public function test_enqueue_block_assets() {
wp_set_current_user( self::$admin_id );
set_current_screen( 'post.php' );
$plugin = get_plugin_instance();
$plugin->enqueue_block_assets();

$featured_image_script_loads = version_compare( '5.0', get_bloginfo( 'version' ), '<=' );
$this->assertEquals( $featured_image_script_loads, wp_script_is( 'unsplash-featured-image-selector', 'enqueued' ) );
$this->assertTrue( wp_script_is( 'unsplash-featured-image-selector', 'enqueued' ) );
}

/**
Expand Down

0 comments on commit 55d9034

Please sign in to comment.