PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.29
Booktics – Appointment Booking Calendar for Service Businesses v1.0.29
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 / core / category / category-hooks.php

category-hooks.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.29, at core/category/category-hooks.php

96 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Category;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 use Booktics\Contracts\Hookable_Service_Contract;
8 use Booktics\Models\Service_Model;
9
10 class Category_Hooks implements Hookable_Service_Contract {
11
12 /**
13 * Register the hooks for the category service sync
14 *
15 * @return void
16 */
17 public function register() {
18 add_action(
19 'booktics_category_created', array(
20 $this,
21 'category_service_sync',
22 ), 10, 1
23 );
24 add_action(
25 'booktics_category_updated', array(
26 $this,
27 'category_service_sync',
28 ), 10, 1
29 );
30 }
31
32 /**
33 * Sync the services categories
34 *
35 * @param $category
36 * @return void
37 */
38 public function category_service_sync( $category ) {
39 $this->remove_category_from_unlinked_services( $category->id, $category->services );
40 $this->add_category_to_linked_services( $category->id, $category->services );
41 }
42
43 /**
44 * Remove the category from services that are no longer linked to it
45 *
46 * @param int $category_id
47 * @param array $new_service_ids
48 * @return void
49 */
50 private function remove_category_from_unlinked_services( $category_id, array $new_service_ids ) {
51 $args = array(
52 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering booktics categories by meta fields
53 'meta_query' => array(
54 array(
55 'key' => 'categories',
56 'value' => sprintf( 'i:%d;', intval( $category_id ) ),
57 'compare' => 'LIKE',
58 ),
59 ),
60 );
61
62 $services = Service_Model::all( $args );
63 foreach ( $services as $service ) {
64 if ( $service && ! in_array( $service->id, $new_service_ids ) ) {
65 $categories = $service->categories;
66 $categories = array_diff( $categories, array( $category_id ) );
67 Service_Model::find( $service->id )->update(
68 array(
69 'categories' => array_values( $categories ),
70 )
71 );
72 }
73 }
74 }
75
76 /**
77 * Add the category to all linked services
78 *
79 * @param int $category_id
80 * @param array $service_ids
81 * @return void
82 */
83 private function add_category_to_linked_services( $category_id, array $service_ids ) {
84 foreach ( $service_ids as $service_id ) {
85 $service = Service_Model::find( $service_id );
86 if ( $service ) {
87 $service_categories = $service->categories;
88 if ( ! in_array( $category_id, $service_categories ) ) {
89 $service_categories[] = $category_id;
90 $service->update( array( 'categories' => $service_categories ) );
91 }
92 }
93 }
94 }
95 }
96