PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.14
Booktics – Appointment Booking Calendar for Service Businesses v1.0.14
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / base / models / service-model.php

service-model.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.14, at base/models/service-model.php

138 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'meta_query' => array(
91 array(
92 'key' => 'connected_services',
93 'value' => "i:{$service_id}",
94 'compare' => 'LIKE',
95 ),
96 ),
97 'fields' => 'ids',
98 );
99
100 $extra_service_post_ids = get_posts( $args );
101
102 foreach ( $extra_service_post_ids as $post_id ) {
103 try {
104 $extra_service_model = new Extra_Service_Model( $post_id );
105 if ( ! $extra_service_model ) {
106 error_log( "Failed to instantiate Extra_Service_Model for post_id: $post_id" );
107 continue;
108 }
109
110 $extra_service_array = $extra_service_model->to_array();
111 $connected_services = isset( $extra_service_array['connected_services'] ) ? $extra_service_array['connected_services'] : array();
112
113 // Remove this service ID from the connected_services array
114 if ( is_array( $connected_services ) && in_array( $service_id, $connected_services, true ) ) {
115 $connected_services = array_values(
116 array_filter(
117 $connected_services, function ( $id ) use ( $service_id ) {
118 return $id != $service_id;
119 }
120 )
121 );
122
123 $extra_service_array['connected_services'] = $connected_services;
124 $result = $extra_service_model->update( $extra_service_array );
125 if ( is_wp_error( $result ) ) {
126 error_log( 'Failed to update Extra_Service_Model during cleanup: ' . $result->get_error_message() );
127 }
128 }
129 } catch ( \Throwable $e ) {
130 error_log( 'Exception in cleanup_connected_extra_services: ' . $e->getMessage() );
131 }
132 }
133 } catch ( \Throwable $e ) {
134 error_log( 'Exception in cleanup_connected_extra_services: ' . $e->getMessage() );
135 }
136 }
137 }
138