register_post_type(); } else { add_action( 'init', [ $this, 'register_post_type' ] ); } add_action( 'before_delete_post', [ $this, 'handle_ad_deletion' ], 10, 2 ); } /** * Unassigns the ad from all groups when it is permanently deleted. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. */ public function handle_ad_deletion( int $post_id, \WP_Post $post ): void { if ( $post->post_type !== Constants::POST_TYPE_AD ) { return; } $group_repo = adpresso_get_group_repository(); $groups = $group_repo->find_by_contained_ad( $post_id, [ 'publish', 'draft', 'pending', 'private', 'future' ], false, true ); foreach ( $groups as $group_id ) { $group_repo->remove_ad_from_group( $group_id, $post_id ); } } /** * Register the post-type with all of his settings. * * @return void */ public function register_post_type(): void { if ( post_type_exists( Constants::POST_TYPE_AD ) ) { return; } $labels = [ 'name' => __( 'Ads', 'adpresso' ), 'singular_name' => __( 'Ad', 'adpresso' ), 'add_new' => __( 'New Ad', 'adpresso' ), 'add_new_item' => __( 'Add New Ad', 'adpresso' ), 'edit_item' => __( 'Edit Ad', 'adpresso' ), 'new_item' => __( 'New Ad', 'adpresso' ), 'view_item' => __( 'View Ad', 'adpresso' ), 'search_items' => __( 'Search Ads', 'adpresso' ), 'not_found' => __( 'No Ads found', 'adpresso' ), 'not_found_in_trash' => __( 'No Ads found in Trash', 'adpresso' ), 'parent_item_colon' => __( 'Parent Ad:', 'adpresso' ), ]; $args = [ 'labels' => $labels, 'public' => false, 'show_ui' => false, 'show_in_menu' => false, 'supports' => [ 'title', 'author' ], 'capability_type' => 'adpresso_ad', 'map_meta_cap' => true, 'capabilities' => adpresso_get_post_type_capabilities( 'ad' ), 'hierarchical' => false, 'has_archive' => false, 'query_var' => false, 'rewrite' => false, ]; register_post_type( Constants::POST_TYPE_AD, apply_filters( 'adpresso_post_type_ad_args', $args ) ); register_post_status( Constants::AD_STATUS_EXPIRED, [ 'label' => __( 'Expired', 'adpresso' ), 'private' => true, ] ); /** * Fires after the core Ad post type and its statuses are registered. * Allows adding custom post statuses or performing other actions. * * @param string $post_type The registered post type slug (Constants::POST_TYPE_AD). */ do_action( 'adpresso_after_register_ad_post_type', Constants::POST_TYPE_AD ); } }