diff --git a/inc/class-provider.php b/inc/class-provider.php index 1e73fd0..cdd77ea 100644 --- a/inc/class-provider.php +++ b/inc/class-provider.php @@ -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. * @@ -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 ); + } } diff --git a/inc/namespace.php b/inc/namespace.php index 05579fd..79f8ac8 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -2,13 +2,14 @@ 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' ); @@ -16,14 +17,13 @@ function bootstrap() : void { } /** - * 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() ); } /**