Skip to content
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
57 changes: 57 additions & 0 deletions inc/Traits/RequestOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* RequestOne trait.
*
* @package asset-manager-framework
*/

declare( strict_types=1 );

namespace AssetManagerFramework\Traits;

use AssetManagerFramework\Media;
use WP_Query;

trait RequestOne {

/**
* Perform a request to a media provider and return results for a single item.
*
* Typically an implementation of this method will perform a remote request to a media provider service,
* process the results, and return a single Media item.
*
* @param string|int $id The external provider item ID.
* @throws Exception Thrown if an unrecoverable error occurs.
* @return Media A single Media object response.
*/
abstract protected function request_one( $id ) : Media;

/**
* Fetches a single item from the provider.
*
* @param string|int $id The external media ID.
* @return Media
*/
final public function request_item( $id ) : Media {

$item = $this->request_one( str_replace( 'amf-', '', (string) $id ) );

$query = [
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_name__in' => [ $id ],
];

$posts = ( new WP_Query( $query ) )->posts;
$ids = array_column( $posts, 'ID', 'post_name' );

if ( isset( $ids[ $item->id ] ) ) {
$item->id = $ids[ $item->id ];
$item->attachmentExists = true;
}

$item->provider = $this->get_id();

return $item;
}
}
36 changes: 35 additions & 1 deletion inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use AssetManagerFramework\Interfaces\{
Resize
};
use AssetManagerFramework\Traits\{
RequestOne
};
use Exception;
use WP_Http;
use WP_Post;
Expand All @@ -26,6 +29,8 @@ function bootstrap() : void {
// Replace the default wp_ajax_query_attachments handler with our own.
remove_action( 'wp_ajax_query-attachments', 'wp_ajax_query_attachments', 1 );
add_action( 'wp_ajax_query-attachments', __NAMESPACE__ . '\\ajax_query_attachments', 1 );
remove_action( 'wp_ajax_get-attachment', 'wp_ajax_get_attachment', 1 );
add_action( 'wp_ajax_get-attachment', __NAMESPACE__ . '\\ajax_get_attachment', 1 );

// Handle the 'select' event Ajax call in the media manager.
add_action( 'wp_ajax_amf-select', __NAMESPACE__ . '\\ajax_select' );
Expand Down Expand Up @@ -294,7 +299,7 @@ function ajax_query_attachments() : void {
if ( $post_id && ! current_user_can( 'edit_post', $post_id ) ) {
wp_send_json_error();
}

// If no provider is specified and WP attachment IDs are referenced,
// call the original AJAX handler, it's probably a gallery.
if (
Expand Down Expand Up @@ -326,6 +331,35 @@ function ajax_query_attachments() : void {
wp_send_json_success( $items->toArray() );
}

function ajax_get_attachment() {
if ( ! isset( $_REQUEST['id'] ) ) {
wp_send_json_error();
}

try {
$provider = ProviderRegistry::instance()->get( wp_unslash( $_REQUEST['provider'] ?? '' ) );
if (
strpos( $_REQUEST['id'], 'amf-' ) === 0
&& method_exists( $provider, 'request_item' )
) {
$item = $provider->request_item( wp_unslash( $_REQUEST['id'] ) );
wp_send_json_success( $item );
}
} catch ( Exception $e ) {
wp_send_json_error(
[
[
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
],
WP_Http::INTERNAL_SERVER_ERROR
);
}

wp_ajax_get_attachment();
}

function is_amf_asset( ?WP_Post $attachment ) : bool {
return ! empty( $attachment ) && strpos( $attachment->post_name, 'amf-' ) === 0;
}
Expand Down