Skip to content

Commit

Permalink
announce subject to Observers registry before Handlers registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfame committed Dec 27, 2024
1 parent 1bd9e01 commit 10b0ccb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/plugin/class-subjects-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function create_item( $request ): WP_REST_Response|WP_Error {
update_post_meta( $item['ID'], 'subject_type', $subject_type->value );

try {
Handlers_Registry::handle( $subject_type, Subject::from_post( $item['ID'] ) );
$this->announce_subject( $subject_type, Subject::from_post( $item['ID'] ) );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
Expand Down Expand Up @@ -322,7 +322,7 @@ public function update_item( $request ): WP_REST_Response|WP_Error {
$subject_type = $this->get_subject_type( $request );

try {
Handlers_Registry::handle( $subject_type, Subject::from_post( $item['ID'] ) );
$this->announce_subject( $subject_type, Subject::from_post( $item['ID'] ) );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
Expand Down Expand Up @@ -465,4 +465,16 @@ private function get_subject_type( $request ): SubjectType {
preg_match( '/\/subjects\/([^\/]+)/', $request->get_route(), $matches );
return SubjectType::tryFrom( $matches[1] );
}

/**
* Announce subject to Observers_Registry & Handlers_Registry
*
* @param SubjectType $subject_type Type of subject to announce.
* @param Subject $subject Subject instance to announce.
* @throws Exception Handlers_Registry::handle can throw an exception.
*/
private function announce_subject( SubjectType $subject_type, Subject $subject ): void {
Observers_Registry::observe( $subject_type, $subject );
Handlers_Registry::handle( $subject_type, $subject );
}
}

0 comments on commit 10b0ccb

Please sign in to comment.