| @@ -1,53 +1,146 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Service Model | |
| 4 | - * | |
| 5 | - * @package Booktics/Models | |
| 6 | - */ | |
| 7 | - | |
| 8 | -namespace Booktics\Models; | |
| 9 | - | |
| 10 | -use Booktics\Abstracts\Post_Model; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * Service_Model class | |
| 14 | - */ | |
| 15 | -class Service_Model extends Post_Model { | |
| 16 | - /** | |
| 17 | - * Store service data | |
| 18 | - * | |
| 19 | - * @var array<string, mixed> | |
| 20 | - */ | |
| 21 | - protected $fillable = [ | |
| 22 | - 'id' => '', | |
| 23 | - 'title' => '', | |
| 24 | - 'description' => '', | |
| 25 | - 'content' => '', | |
| 26 | - 'link' => '', | |
| 27 | - 'image' => '', | |
| 28 | - 'image_id' => '', | |
| 29 | - 'categories' => [], | |
| 30 | - 'status' => '', | |
| 31 | - 'enable_custom_holiday' => false, | |
| 32 | - 'enable_custom_schedule' => false, | |
| 33 | - 'enable_additional_service_duration' => false, | |
| 34 | - 'interval_time' => '', | |
| 35 | - 'before_buffer_time' => '', | |
| 36 | - 'after_buffer_time' => '', | |
| 37 | - 'team_member' => [], | |
| 38 | - 'show_end_time' => '', | |
| 39 | - 'additional_durations' => [], | |
| 40 | - 'schedule' => [], | |
| 41 | - 'custom' => [], | |
| 42 | - 'holiday' => [], | |
| 43 | - ]; | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Get post type | |
| 47 | - * | |
| 48 | - * @return string Post type name | |
| 49 | - */ | |
| 50 | - public function get_post_type() { | |
| 51 | - return 'booktics-service'; | |
| 52 | - } | |
| 53 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Service Model | |
| 4 | + * | |
| 5 | + * @package Booktics/Models | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Booktics\Models; | |
| 9 | + | |
| 10 | +use Booktics\Abstracts\Post_Model; | |
| 11 | +use BookticsPro\Models\Extra_Service_Model; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Service_Model class | |
| 15 | + */ | |
| 16 | +class Service_Model extends Post_Model { | |
| 17 | + /** | |
| 18 | + * Store service data | |
| 19 | + * | |
| 20 | + * @var array<string, mixed> | |
| 21 | + */ | |
| 22 | + protected $fillable = [ | |
| 23 | + 'id' => '', | |
| 24 | + 'title' => '', | |
| 25 | + 'description' => '', | |
| 26 | + 'content' => '', | |
| 27 | + 'link' => '', | |
| 28 | + 'image' => '', | |
| 29 | + 'image_id' => '', | |
| 30 | + 'categories' => [], | |
| 31 | + 'status' => '', | |
| 32 | + 'enable_custom_holiday' => false, | |
| 33 | + 'enable_custom_schedule' => false, | |
| 34 | + 'enable_additional_service_duration' => false, | |
| 35 | + 'interval_time' => 0, | |
| 36 | + 'before_buffer_time' => 0, | |
| 37 | + 'after_buffer_time' => 0, | |
| 38 | + 'team_member' => [], | |
| 39 | + 'show_end_time' => false, | |
| 40 | + 'additional_durations' => [], | |
| 41 | + 'schedule' => [], | |
| 42 | + 'custom' => [], | |
| 43 | + 'holiday' => [], | |
| 44 | + ]; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Get post type | |
| 48 | + * | |
| 49 | + * @return string Post type name | |
| 50 | + */ | |
| 51 | + public function get_post_type() { | |
| 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 | + } | |
| 145 | + } | |
| 146 | +} | |