-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Add Virtual Media Registrar and Migration classes for handling virtual media integration #1784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shreyasikhar
wants to merge
8
commits into
develop
Choose a base branch
from
feat/virtual-media-mgmt
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−0
Open
feat: Add Virtual Media Registrar and Migration classes for handling virtual media integration #1784
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6dd9e3d
feat: Add Virtual Media Registrar and Migration classes for handling …
shreyasikhar d72a95e
feat: Update priority to run after transcoding is complete
shreyasikhar 1a14c7d
feat: Address copilot feedbacks
shreyasikhar b8db395
feat: Reduced the timeout
shreyasikhar 21537c3
feat: Addressed feedbacks
shreyasikhar 31f802d
Merge branch 'develop' into feat/virtual-media-mgmt
shreyasikhar edfb4c6
Merge branch 'develop' into feat/virtual-media-mgmt
shreyasikhar 132c579
Merge branch 'develop' into feat/virtual-media-mgmt
shreyasikhar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| <?php | ||
| /** | ||
| * Register virtual media sites with GoDAM Central. | ||
| * | ||
| * @package GoDAM | ||
| */ | ||
|
|
||
| namespace RTGODAM\Inc; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| use RTGODAM\Inc\Traits\Singleton; | ||
|
|
||
| /** | ||
| * Class Virtual_Media_Registrar. | ||
| */ | ||
| class Virtual_Media_Registrar { | ||
|
|
||
| use Singleton; | ||
|
|
||
| /** | ||
| * Meta flag to avoid repeated registrations. | ||
| * | ||
| * @var string | ||
| */ | ||
| const META_REGISTERED = '_godam_virtual_site_registered'; | ||
|
|
||
| /** | ||
| * Meta key where the virtual job id is stored. | ||
| * | ||
| * @var string | ||
| */ | ||
| const META_ORIGINAL_ID = '_godam_original_id'; | ||
|
|
||
| /** | ||
| * Construct method. | ||
| */ | ||
| protected function __construct() { | ||
| $this->setup_hooks(); | ||
| } | ||
|
|
||
| /** | ||
| * Boot hooks. | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function setup_hooks() { | ||
| add_action( 'added_post_meta', array( $this, 'maybe_register_from_meta_change' ), 10, 3 ); | ||
| add_action( 'updated_post_meta', array( $this, 'maybe_register_from_meta_change' ), 10, 3 ); | ||
| add_action( 'add_attachment', array( $this, 'maybe_register_from_attachment' ), 22, 1 ); | ||
| add_action( 'before_delete_post', array( $this, 'maybe_remove_virtual_media_site_on_delete' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * If _godam_original_id meta is added/updated, attempt registration. | ||
| * | ||
| * @param int $meta_id Meta ID. | ||
| * @param int $post_id Post ID. | ||
| * @param string $meta_key Meta key. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function maybe_register_from_meta_change( $meta_id, $post_id, $meta_key ) { | ||
| if ( self::META_ORIGINAL_ID !== $meta_key ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( 'attachment' !== get_post_type( $post_id ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $this->register_site_for_attachment_if_needed( (int) $post_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Fallback handler when attachment is created. | ||
| * | ||
| * @param int $attachment_id Attachment ID. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function maybe_register_from_attachment( $attachment_id ) { | ||
| $this->register_site_for_attachment_if_needed( (int) $attachment_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Register this site as a virtual media site for the attachment job if needed. | ||
| * | ||
| * @param int $attachment_id Attachment ID. | ||
| * | ||
| * @return true|\WP_Error True if registration is successful or not needed, WP_Error if registration fails. | ||
| */ | ||
| public function register_site_for_attachment_if_needed( $attachment_id ) { | ||
| $job_name = get_post_meta( $attachment_id, self::META_ORIGINAL_ID, true ); | ||
|
|
||
| if ( empty( $job_name ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| $already_registered = get_post_meta( $attachment_id, self::META_REGISTERED, true ); | ||
|
|
||
| if ( ! empty( $already_registered ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| $api_key = get_option( 'rtgodam-api-key' ); | ||
|
|
||
| if ( empty( $api_key ) ) { | ||
| return new \WP_Error( 'godam_missing_api_key', __( 'GoDAM API key is not configured on this site.', 'godam' ) ); | ||
| } | ||
|
|
||
| if ( class_exists( '\\RTGODAM_Transcoder_Rest_Routes' ) ) { | ||
| $callback_url = \RTGODAM_Transcoder_Rest_Routes::get_callback_url(); | ||
| } else { | ||
| $callback_url = rest_url( 'godam/v1/transcoder-callback' ); | ||
| } | ||
|
|
||
| $payload = array( | ||
| 'job_name' => $job_name, | ||
| 'site_url' => get_site_url(), | ||
| 'callback_url' => $callback_url, | ||
| 'api_key' => $api_key, | ||
| ); | ||
|
|
||
| if ( ! defined( 'RTGODAM_API_BASE' ) || empty( RTGODAM_API_BASE ) ) { | ||
| return new \WP_Error( 'godam_missing_api_base', __( 'RTGODAM_API_BASE is not defined.', 'godam' ) ); | ||
| } | ||
|
|
||
| $endpoint = trailingslashit( RTGODAM_API_BASE ) . 'api/method/godam_core.api.transcoder_job.add_virtual_media_site'; | ||
|
|
||
| $response = wp_remote_post( | ||
| $endpoint, | ||
| array( | ||
| 'method' => 'POST', | ||
| 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout | ||
| 'headers' => array( | ||
| 'Content-Type' => 'application/json', | ||
| ), | ||
| 'body' => wp_json_encode( $payload ), | ||
| ) | ||
| ); | ||
|
|
||
| if ( is_wp_error( $response ) ) { | ||
| return new \WP_Error( | ||
| 'godam_virtual_site_register_failed', | ||
| __( 'Failed to connect to GoDAM Central.', 'godam' ), | ||
| array( | ||
| 'error' => $response->get_error_message(), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $code = (int) wp_remote_retrieve_response_code( $response ); | ||
| $body = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
|
|
||
| if ( 200 !== $code ) { | ||
| return new \WP_Error( | ||
| 'godam_virtual_site_register_failed', | ||
| __( 'Failed to register virtual media site with GoDAM Central.', 'godam' ), | ||
| array( | ||
| 'http_code' => $code, | ||
| 'response' => $body, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| update_post_meta( $attachment_id, self::META_REGISTERED, 1 ); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * When an attachment is deleted, if it has a _godam_original_id meta, call Central to remove the virtual media site. | ||
| * | ||
| * @param int $post_id Post ID. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function maybe_remove_virtual_media_site_on_delete( $post_id ) { | ||
| $post_id = (int) $post_id; | ||
|
|
||
| if ( 'attachment' !== get_post_type( $post_id ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Only for virtual media attachments. | ||
| $job_name = get_post_meta( $post_id, self::META_ORIGINAL_ID, true ); | ||
| if ( empty( $job_name ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $job_name = (string) $job_name; | ||
|
|
||
| // If the virtual media registration meta is not present, we can skip the API call. | ||
| $meta_registered = get_post_meta( $post_id, self::META_REGISTERED, 1 ); | ||
|
shreyasikhar marked this conversation as resolved.
Outdated
|
||
| if ( empty( $meta_registered ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Call Central to remove the association. | ||
| $result = $this->remove_virtual_media_site( $job_name ); | ||
|
shreyasikhar marked this conversation as resolved.
|
||
|
|
||
| if ( is_wp_error( $result ) ) { | ||
| error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | ||
| sprintf( | ||
| 'GoDAM: Failed to remove virtual media site for job %s on delete of attachment %d. Error: %s', | ||
| $job_name, | ||
| $post_id, | ||
| $result->get_error_message() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Call GoDAM Central: remove_virtual_media_site | ||
| * | ||
| * @param string $job_name The original job name associated with this virtual media site. | ||
| * @return true|\WP_Error True if removal is successful, WP_Error if it fails. | ||
| */ | ||
| private static function remove_virtual_media_site( string $job_name ) { | ||
| $api_key = get_option( 'rtgodam-api-key' ); | ||
| if ( empty( $api_key ) ) { | ||
| return new \WP_Error( 'godam_missing_api_key', __( 'GoDAM API key is not configured on this site.', 'godam' ) ); | ||
| } | ||
|
|
||
| if ( ! defined( 'RTGODAM_API_BASE' ) || empty( RTGODAM_API_BASE ) ) { | ||
| return new \WP_Error( 'godam_missing_api_base', __( 'RTGODAM_API_BASE is not defined.', 'godam' ) ); | ||
| } | ||
|
|
||
| $endpoint = trailingslashit( RTGODAM_API_BASE ) . 'api/method/godam_core.api.transcoder_job.remove_virtual_media_site'; | ||
|
|
||
| $payload = array( | ||
| 'job_name' => $job_name, | ||
| 'site_url' => get_site_url(), | ||
| 'api_key' => $api_key, | ||
| ); | ||
|
|
||
| $response = wp_remote_post( | ||
| $endpoint, | ||
| array( | ||
| 'method' => 'POST', | ||
| 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout | ||
|
shreyasikhar marked this conversation as resolved.
Outdated
|
||
| 'headers' => array( | ||
| 'Content-Type' => 'application/json', | ||
| ), | ||
| 'body' => wp_json_encode( $payload ), | ||
| ) | ||
| ); | ||
|
|
||
| if ( is_wp_error( $response ) ) { | ||
| return $response; | ||
| } | ||
|
|
||
| $code = (int) wp_remote_retrieve_response_code( $response ); | ||
| $body = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
|
|
||
| if ( 200 !== $code ) { | ||
| return new \WP_Error( | ||
| 'godam_virtual_site_remove_failed', | ||
| __( 'Failed to remove virtual media site from GoDAM Central.', 'godam' ), | ||
| array( | ||
| 'http_code' => $code, | ||
| 'response' => $body, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WordPress fires the
add_attachmenthook before_godam_original_idis set, soregister_site_for_attachment_if_neededwill always early return withtruehere. I think we can remove this hook formaybe_register_from_attachment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are setting the meta here https://github.com/rtCamp/godam/blob/main/inc/classes/rest-api/class-media-library.php#L1581-L1586 on the same
add_attachmenthook with priority 1.