Skip to content

Commit

Permalink
Merge pull request #2 from humanmade/update-new-api
Browse files Browse the repository at this point in the history
Update to support new version of AMF
  • Loading branch information
roborourke authored Aug 26, 2021
2 parents d0965f5 + 140daae commit a203990
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
51 changes: 50 additions & 1 deletion inc/class-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
namespace AMFUnsplash;

use AssetManagerFramework\Image;
use AssetManagerFramework\Interfaces\Resize;
use AssetManagerFramework\MediaList;
use AssetManagerFramework\Provider as BaseProvider;
use stdClass;
use WP_Post;

class Provider extends BaseProvider {
class Provider extends BaseProvider implements Resize {
/**
* Base URL for the Unsplash API.
*/
const BASE_URL = 'https://api.unsplash.com';

/**
* Return the provider ID.
*
* @return string
*/
public function get_id() : string {
return 'unsplash';
}

/**
* Return the provider name.
*
* @return string
*/
public function get_name() : string {
return __( 'Unsplash', 'amf-unsplash' );
}

/**
* Parse input query args into an Unsplash query.
*
Expand Down Expand Up @@ -308,4 +328,33 @@ public static function track_download( string $id ) : void {
'blocking' => false,
] );
}

/**
* Support dynamically sized images.
*
* @param WP_Post $attachment The current unsplash attachment.
* @param integer $width Target width.
* @param integer $height Target height.
* @param boolean $crop Whether to crop the image or not.
* @return string
*/
public function resize( WP_Post $attachment, int $width, int $height, $crop = false ) : string {
$base_url = wp_get_attachment_url( $attachment->ID );

$query_args = [
'w' => $width,
'h' => $height,
'fit' => $crop ? 'crop' : 'clip',
'crop' => 'faces,focalpoint',
];

if ( is_array( $crop ) ) {
$crop = array_filter( $crop, function ( $value ) {
return $value !== 'center';
} );
$query_args['crop'] = implode( ',', $crop );
}

return add_query_args( urlencode_deep( $query_args ), $base_url );
}
}
12 changes: 6 additions & 6 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace AMFUnsplash;

use AssetManagerFramework\ProviderRegistry;
use WP_Scripts;

/**
* Bootstrap function.
*/
function bootstrap() : void {
add_filter( 'amf/provider_class', __NAMESPACE__ . '\\get_provider' );
add_action( 'amf/register_providers', __NAMESPACE__ . '\\register_provider' );
add_action( 'amf/inserted_attachment', __NAMESPACE__ . '\\track_download', 10, 3 );
add_action( 'wp_default_scripts', __NAMESPACE__ . '\\override_per_page', 100 );
add_action( 'plugins_loaded', __NAMESPACE__ . '\\register_key_setting' );
add_action( 'admin_init', __NAMESPACE__ . '\\register_settings_ui' );
}

/**
* Get the provider for AMF.
* Register the provider for AMF.
*
* @return string
* @return void
*/
function get_provider() : string {
function register_provider( ProviderRegistry $provider_registry ) {
require_once __DIR__ . '/class-provider.php';

return Provider::class;
$provider_registry->register( new Provider() );
}

/**
Expand Down

0 comments on commit a203990

Please sign in to comment.