PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.24
Booktics – Appointment Booking Calendar for Service Businesses v1.0.24
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 / service / service-hooks.php

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

150 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Booktics\Service;
3
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 use Booktics\Category\Controllers\Category_Controller;
7 use Booktics\Contracts\Hookable_Service_Contract;
8
9 class Service_Hooks implements Hookable_Service_Contract {
10
11 /**
12 * Register the service hooks.
13 *
14 * @return void
15 */
16 public function register() {
17 add_filter(
18 'booktics_service_args', array(
19 $this,
20 'booktics_service_args',
21 ), 10, 2
22 );
23 add_action(
24 'booktics_service_created', array(
25 $this,
26 'service_category_sync',
27 ), 10, 2
28 );
29
30 add_action(
31 'booktics_service_updated', array(
32 $this,
33 'service_category_sync',
34 ), 10, 2
35 );
36 }
37
38 /**
39 * Filter service arguments based on the current user's role.
40 *
41 * @param array $args The service arguments.
42 * @param $request
43 *
44 * @return array The filtered service arguments.
45 */
46 public function booktics_service_args( $args, $request ) {
47 $current_user = wp_get_current_user();
48 $roles = $current_user->roles;
49 if ( in_array( 'administrator', $roles, true ) ) {
50 return $args;
51 }
52
53 if ( in_array( 'booktics_team_member', $roles, true ) ) {
54 $args['meta_query'][] = array(
55 'key' => 'team_member',
56 'value' => sprintf( 'i:%d;', intval( $current_user->ID ) ),
57 'compare' => 'LIKE',
58 );
59 }
60
61 return $args;
62 }
63
64 /**
65 * Add service to specified categories
66 *
67 * @param int $service_id
68 * @param array $category_ids
69 *
70 * @return void
71 */
72 private function add_service_to_categories( $service_id, array $category_ids ) {
73 if ( empty( $category_ids ) ) {
74 return;
75 }
76
77 $category_taxonomy = ( new Category_Controller() )->taxonomy;
78
79 foreach ( $category_ids as $category_id ) {
80 $category = get_term_by( 'id', $category_id, $category_taxonomy );
81 if ( $category && ! is_wp_error( $category ) ) {
82 $services = get_term_meta( $category->term_id, 'booktics_category_services', true );
83 if ( ! is_array( $services ) ) {
84 $services = array();
85 }
86 if ( ! in_array( $service_id, $services ) ) {
87 $services[] = $service_id;
88 update_term_meta( $category->term_id, 'booktics_category_services', $services );
89 }
90 }
91 }
92 }
93
94 /**
95 * Remove service from categories except specified ones
96 *
97 * @param int $service_id
98 * @param array $exclude_category_ids Categories to keep the service in
99 *
100 * @return void
101 */
102 private function remove_service_from_categories( $service_id, array $exclude_category_ids = array() ) {
103 $category_taxonomy = ( new Category_Controller() )->taxonomy;
104
105 $existing_categories = get_terms(
106 array(
107 'taxonomy' => $category_taxonomy,
108 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering services by service categories.
109 'meta_query' => array(
110 array(
111 'key' => 'booktics_category_services',
112 'value' => $service_id,
113 'compare' => 'LIKE',
114 ),
115 ),
116 'hide_empty' => false,
117 )
118 );
119
120 if ( ! empty( $existing_categories ) && ! is_wp_error( $existing_categories ) ) {
121 foreach ( $existing_categories as $existing_category ) {
122 if ( in_array( $existing_category->term_id, $exclude_category_ids ) ) {
123 continue;
124 }
125
126 $services = get_term_meta( $existing_category->term_id, 'booktics_category_services', true );
127 if ( is_array( $services ) ) {
128 $services = array_diff( $services, array( $service_id ) );
129 update_term_meta( $existing_category->term_id, 'booktics_category_services', $services );
130 }
131 }
132 }
133 }
134
135
136 /**
137 * @param $service
138 * @param $request
139 *
140 * @return void
141 */
142 public function service_category_sync( $service, $request ) {
143 // Remove service from categories except the new ones
144 $this->remove_service_from_categories( $service->id, $service->categories );
145
146 // Add service to new categories
147 $this->add_service_to_categories( $service->id, $service->categories );
148 }
149 }
150