post_type !== 'product') { return; } global $wpdb; $product_id = $post_id; $table = $wpdb->prefix . 'aipt_draft_content'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_job_products'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_content_review'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_content_snapshots'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_blueprint_source_cache'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_blueprint_results'; $wpdb->delete($table, ['subject_type' => 'product', 'subject_id' => $product_id], ['%s', '%d']); $wpdb->delete($table, ['product_id' => $product_id], ['%d']); $table = $wpdb->prefix . 'aipt_blueprint_decisions'; $wpdb->delete($table, ['subject_type' => 'product', 'subject_id' => $product_id], ['%s', '%d']); $table = $wpdb->prefix . 'aipt_blueprint_assignments'; $wpdb->delete($table, ['target_type' => 'product', 'target_id' => $product_id], ['%s', '%d']); $table = $wpdb->prefix . 'aipt_blueprint_sources'; $wpdb->delete($table, ['scope_type' => 'product', 'scope_id' => $product_id], ['%s', '%d']); $table = $wpdb->prefix . 'aipt_catalog_watch'; $wpdb->delete($table, ['product_id' => $product_id], ['%d']); } /** * Clean image_alt content-snapshot rows when the attachment they * reference (subject_id) is permanently deleted. * * An image_alt snapshot's product_id row would otherwise survive a * deleted attachment forever — it isn't touched by on_product_deleted() * (the PRODUCT still exists) and the pruning cron only enforces the * per-group ring-buffer cap, never checks whether the referenced * attachment is gone. * * @param int $post_id Post ID being deleted. * @param \WP_Post $post Post object. */ public static function on_attachment_deleted(int $post_id): void { global $wpdb; $table = $wpdb->prefix . 'aipt_content_snapshots'; $wpdb->delete($table, ['field_type' => 'image_alt', 'subject_id' => $post_id], ['%s', '%d']); $review_table = $wpdb->prefix . 'aipt_content_review'; $wpdb->delete($review_table, ['field_type' => 'image_alt', 'subject_id' => $post_id], ['%s', '%d']); $blueprint_results_table = $wpdb->prefix . 'aipt_blueprint_results'; $wpdb->delete($blueprint_results_table, ['subject_type' => 'attachment', 'subject_id' => $post_id], ['%s', '%d']); } /** * Clean content structure category/term-scoped records when a term is * permanently deleted. * * Category-scope rows (assignments with target_type='category', sources * with scope_type='category') are modeled on the `product_cat` taxonomy * specifically — they are guarded on $taxonomy so a term id that * happens to collide with an unrelated taxonomy's term (pre-4.2 shared * terms) can never delete an unrelated category binding. Term-target * assignments (the taxonomy generator) carry the real taxonomy name in * their own `taxonomy` column, so that delete is precise for any * taxonomy without needing the same guard. Results/decisions have no * taxonomy column at all (subject_type='term' + subject_id is the only * identity available), so they are cleaned for any taxonomy's term. * * @param int $term_id Term ID being deleted. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. * @param mixed $deleted_term Copy of the deleted term object (unused). */ public static function on_term_deleted(int $term_id, int $tt_id, string $taxonomy, $deleted_term): void { global $wpdb; if ('product_cat' === $taxonomy) { $table = $wpdb->prefix . 'aipt_blueprint_assignments'; $wpdb->delete($table, ['target_type' => 'category', 'taxonomy' => 'product_cat', 'target_id' => $term_id], ['%s', '%s', '%d']); $table = $wpdb->prefix . 'aipt_blueprint_sources'; $wpdb->delete($table, ['scope_type' => 'category', 'scope_id' => $term_id], ['%s', '%d']); } $table = $wpdb->prefix . 'aipt_blueprint_assignments'; $wpdb->delete($table, ['target_type' => 'term', 'taxonomy' => $taxonomy, 'target_id' => $term_id], ['%s', '%s', '%d']); $table = $wpdb->prefix . 'aipt_blueprint_results'; $wpdb->delete($table, ['subject_type' => 'term', 'subject_id' => $term_id], ['%s', '%d']); $table = $wpdb->prefix . 'aipt_blueprint_decisions'; $wpdb->delete($table, ['subject_type' => 'term', 'subject_id' => $term_id], ['%s', '%d']); $table = $wpdb->prefix . 'aipt_content_snapshots'; $wpdb->delete($table, ['subject_type' => 'term', 'subject_id' => $term_id, 'taxonomy' => $taxonomy], ['%s', '%d', '%s']); } /** * Clean related records when an automation job is deleted. * * Call this from wherever jobs are deleted (AutomationJobsController or AutomationJobManager). * * @param int $job_id The job ID being deleted. */ public static function on_job_deleted(int $job_id): void { global $wpdb; $table = $wpdb->prefix . 'aipt_job_products'; $wpdb->delete($table, ['job_id' => $job_id], ['%d']); $table = $wpdb->prefix . 'aipt_job_logs'; $wpdb->delete($table, ['job_id' => $job_id], ['%d']); $table = $wpdb->prefix . 'aipt_draft_content'; $wpdb->delete($table, ['job_id' => $job_id], ['%d']); } /** * Clean related records when a content structure is deleted. * * A Blueprint is not a WordPress post, so there is no delete hook to * attach to — call this explicitly from the Blueprint delete REST * handler (after its own impact-preview confirmation), the same * direct-call shape as {@see on_job_deleted()}. Blueprint-scope sources' * own per-product cache rows are deleted first (no FK cascade on a * dbDelta-managed table), then the source rows themselves. Execution * snapshots are NOT removed here: running jobs keep their pins, and * unreferenced snapshots are pruned later by BlueprintCleanup. * * @param int $blueprint_id The Blueprint ID being deleted. * @return array{assignments:int,sources:int,sourceCache:int,results:int} Rows removed, for the REST layer's response. */ public static function on_blueprint_deleted(int $blueprint_id): array { global $wpdb; $counts = [ 'assignments' => 0, 'sources' => 0, 'sourceCache' => 0, 'results' => 0, ]; $assignments_table = $wpdb->prefix . 'aipt_blueprint_assignments'; $deleted = $wpdb->delete($assignments_table, ['blueprint_id' => $blueprint_id], ['%d']); $counts['assignments'] = false !== $deleted ? (int) $deleted : 0; $sources_table = $wpdb->prefix . 'aipt_blueprint_sources'; $cache_table = $wpdb->prefix . 'aipt_blueprint_source_cache'; $source_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$sources_table} WHERE scope_type = %s AND scope_id = %d", 'blueprint', $blueprint_id ) ); foreach ($source_ids as $source_id) { $cache_deleted = $wpdb->delete($cache_table, ['source_id' => (int) $source_id], ['%d']); $counts['sourceCache'] += false !== $cache_deleted ? (int) $cache_deleted : 0; } if (!empty($source_ids)) { $sources_deleted = $wpdb->delete($sources_table, ['scope_type' => 'blueprint', 'scope_id' => $blueprint_id], ['%s', '%d']); $counts['sources'] = false !== $sources_deleted ? (int) $sources_deleted : 0; } $results_table = $wpdb->prefix . 'aipt_blueprint_results'; $results_deleted = $wpdb->delete($results_table, ['blueprint_id' => $blueprint_id], ['%d']); $counts['results'] = false !== $results_deleted ? (int) $results_deleted : 0; return $counts; } }