Skip to content

Commit

Permalink
Add in custom hooks before and after basic functionality runs, allowi…
Browse files Browse the repository at this point in the history
…ng third parties to hook in and do things like logging
  • Loading branch information
dkotter committed Oct 15, 2024
1 parent c90ed51 commit bca8413
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
61 changes: 61 additions & 0 deletions includes/Classifai/Features/TermCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,18 @@ public function generate_embeddings( string $taxonomy ) {
foreach ( $terms as $term_id ) {
$result = $provider->generate_embeddings_for_term( $term_id, false, $this );

/**
* Fires when an embedding is generated for a term.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_generate_embedding
*
* @param {int} $term_id ID of term.
* @param {array|WP_Error} $result Result of embedding generation.
* @param {TermCleanup} $this Feature instance.
*/
do_action( 'classifai_feature_term_cleanup_generate_embedding', $term_id, $result, $this );

if ( is_wp_error( $result ) ) {
return $result;
}
Expand Down Expand Up @@ -1096,6 +1108,18 @@ public function merge_term() {
exit;
}

/**
* Fires before terms are merged together.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_pre_merge_term
*
* @param {int} $from Term ID being merged.
* @param {int} $to Term ID we're merging into.
* @param {string} $taxonomy Taxonomy of terms being merged.
*/
do_action( 'classifai_feature_term_cleanup_pre_merge_term', $from, $to, $taxonomy );

$ret = wp_delete_term(
$from,
$taxonomy,
Expand All @@ -1105,6 +1129,19 @@ public function merge_term() {
)
);

/**
* Fires after terms are merged together.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_post_merge_term
*
* @param {int} $from Term ID being merged.
* @param {int} $to Term ID we're merging into.
* @param {string} $taxonomy Taxonomy of terms being merged.
* @param {bool|int|WP_Error} $ret Result of merge process.
*/
do_action( 'classifai_feature_term_cleanup_post_merge_term', $from, $to, $taxonomy, $ret );

if ( is_wp_error( $ret ) ) {
$this->add_notice(
// translators: %s: Error message.
Expand Down Expand Up @@ -1146,6 +1183,18 @@ public function skip_similar_term() {
];
$redirect = add_query_arg( $args, $this->setting_page_url );

/**
* Fires before a term is skipped.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_pre_skip_term
*
* @param {int} $term Term ID being skipped.
* @param {int} $similar_term Term ID that matched.
* @param {string} $taxonomy Taxonomy of terms being merged.
*/
do_action( 'classifai_feature_term_cleanup_pre_skip_term', $term, $similar_term, $taxonomy );

// SKip/Ignore the similar term.
$term_meta = get_term_meta( $term, 'classifai_similar_terms', true );
if ( is_array( $term_meta ) && isset( $term_meta[ $similar_term ] ) ) {
Expand All @@ -1157,6 +1206,18 @@ public function skip_similar_term() {
}
}

/**
* Fires after a term is skipped.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_post_skip_term
*
* @param {int} $term Term ID being skipped.
* @param {int} $similar_term Term ID that matched.
* @param {string} $taxonomy Taxonomy of terms being merged.
*/
do_action( 'classifai_feature_term_cleanup_post_skip_term', $term, $similar_term, $taxonomy );

$this->add_notice(
esc_html__( 'Skipped similar term.', 'classifai' ),
'success'
Expand Down
12 changes: 12 additions & 0 deletions includes/Classifai/TermCleanupScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ public function run( array $item = [] ) {
);
$res = $term_cleanup->get_similar_terms( $taxonomy, $thresold, $args );

/**
* Fires when a batch of similar terms are calculated.
*
* @since x.x.x
* @hook classifai_feature_term_cleanup_get_similar_terms
*
* @param {array|bool|WP_Error} $res Response from the get_similar_terms method.
* @param {string} $taxonomy Taxonomy of terms we are comparing.
* @param {array} $args Arguments used for getting similar terms.
*/
do_action( 'classifai_feature_term_cleanup_get_similar_terms', $res, $taxonomy, $args );

// Restore original user.
wp_set_current_user( $original_user_id );

Expand Down

0 comments on commit bca8413

Please sign in to comment.