| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Booktics\Models; |
| 9 | 9 | |
| 10 | 10 | use Booktics\Abstracts\Post_Model; |
| 11 | +use BookticsPro\Models\Extra_Service_Model; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Service_Model class |
| 14 | 15 | */ |
| @@ -30,13 +31,13 @@ | ||
| 30 | 31 | 'status' => '', |
| 31 | 32 | 'enable_custom_holiday' => false, |
| 32 | 33 | 'enable_custom_schedule' => false, |
| 33 | 34 | 'enable_additional_service_duration' => false, |
| 34 | - 'interval_time' => '', | |
| 35 | - 'before_buffer_time' => '', | |
| 36 | - 'after_buffer_time' => '', | |
| 35 | + 'interval_time' => 0, | |
| 36 | + 'before_buffer_time' => 0, | |
| 37 | + 'after_buffer_time' => 0, | |
| 37 | 38 | 'team_member' => [], |
| 38 | - 'show_end_time' => '', | |
| 39 | + 'show_end_time' => false, | |
| 39 | 40 | 'additional_durations' => [], |
| 40 | 41 | 'schedule' => [], |
| 41 | 42 | 'custom' => [], |
| 42 | 43 | 'holiday' => [], |
| @@ -48,6 +49,98 @@ | ||
| 48 | 49 | * @return string Post type name |
| 49 | 50 | */ |
| 50 | 51 | public function get_post_type() { |
| 51 | 52 | return 'booktics-service'; |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Delete a record and cleanup connected extra services | |
| 57 | + * | |
| 58 | + * | |
| 59 | + * @return WP_Post|false|null Post data on success, false or null on failure. | |
| 60 | + */ | |
| 61 | + public function delete() { | |
| 62 | + if ( ! $this->post ) { | |
| 63 | + return false; | |
| 64 | + } | |
| 65 | + | |
| 66 | + // Get the service ID before deletion | |
| 67 | + $service_id = $this->post->ID; | |
| 68 | + | |
| 69 | + // Cleanup connected extra services before deleting the service | |
| 70 | + $this->cleanup_connected_extra_services( $service_id ); | |
| 71 | + | |
| 72 | + // Call parent delete method to delete the service post | |
| 73 | + return parent::delete(); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Cleanup connected extra services by removing this service ID from their connected_services array | |
| 78 | + * | |
| 79 | + * @param int $service_id The ID of the service being deleted | |
| 80 | + * | |
| 81 | + * @return void | |
| 82 | + */ | |
| 83 | + private function cleanup_connected_extra_services( $service_id ) { | |
| 84 | + try { | |
| 85 | + // Get all extra services that have this service in their connected_services array | |
| 86 | + $args = array( | |
| 87 | + 'post_type' => 'booktics-ex-service', | |
| 88 | + 'post_status' => array( 'publish', 'active', 'pending' ), | |
| 89 | + 'posts_per_page' => -1, | |
| 90 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering services by meta fields | |
| 91 | + 'meta_query' => array( | |
| 92 | + array( | |
| 93 | + 'key' => 'connected_services', | |
| 94 | + 'value' => "i:{$service_id}", | |
| 95 | + 'compare' => 'LIKE', | |
| 96 | + ), | |
| 97 | + ), | |
| 98 | + 'fields' => 'ids', | |
| 99 | + ); | |
| 100 | + | |
| 101 | + $extra_service_post_ids = get_posts( $args ); | |
| 102 | + | |
| 103 | + foreach ( $extra_service_post_ids as $post_id ) { | |
| 104 | + try { | |
| 105 | + $extra_service_model = new Extra_Service_Model( $post_id ); | |
| 106 | + if ( ! $extra_service_model ) { | |
| 107 | + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 108 | + error_log( "Failed to instantiate Extra_Service_Model for post_id: $post_id" ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 109 | + } | |
| 110 | + continue; | |
| 111 | + } | |
| 112 | + | |
| 113 | + $extra_service_array = $extra_service_model->to_array(); | |
| 114 | + $connected_services = isset( $extra_service_array['connected_services'] ) ? $extra_service_array['connected_services'] : array(); | |
| 115 | + | |
| 116 | + // Remove this service ID from the connected_services array | |
| 117 | + if ( is_array( $connected_services ) && in_array( $service_id, $connected_services, true ) ) { | |
| 118 | + $connected_services = array_values( | |
| 119 | + array_filter( | |
| 120 | + $connected_services, function ( $id ) use ( $service_id ) { | |
| 121 | + return $id != $service_id; | |
| 122 | + } | |
| 123 | + ) | |
| 124 | + ); | |
| 125 | + | |
| 126 | + $extra_service_array['connected_services'] = $connected_services; | |
| 127 | + $result = $extra_service_model->update( $extra_service_array ); | |
| 128 | + if ( is_wp_error( $result ) ) { | |
| 129 | + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 130 | + error_log( 'Failed to update Extra_Service_Model during cleanup: ' . $result->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 131 | + } | |
| 132 | + } | |
| 133 | + } | |
| 134 | + } catch ( \Throwable $e ) { | |
| 135 | + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 136 | + error_log( 'Exception in cleanup_connected_extra_services: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 137 | + } | |
| 138 | + } | |
| 139 | + } | |
| 140 | + } catch ( \Throwable $e ) { | |
| 141 | + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 142 | + error_log( 'Exception in cleanup_connected_extra_services: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 143 | + } | |
| 144 | + } | |
| 52 | 145 | } |
| 53 | 146 | } |