PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.60
Timetics – Appointment Booking Calendar & Scheduling v1.0.60
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 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.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / admin / hooks.php

hooks.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.60, at core/admin/hooks.php

348 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin hooks
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Admin;
8
9 defined( 'ABSPATH' ) || exit;
10
11 use Timetics\Utils\Singleton;
12
13
14 class Hooks {
15 use Singleton;
16
17 /**
18 * Initialize admin hooks
19 *
20 * @return void
21 */
22 public function init() {
23 add_action('admin_notices', [ $this, 'add_woocommerce_notice_on_admin_dashboard' ] );
24
25 // Dismiss the notice
26 add_action('wp_ajax_dismiss_woocommerce_notice', [ $this, 'dismiss_woocommerce_notice'] );
27
28 add_filter('plugin_action_links', [ $this, 'add_settings_plugin_tab' ], 10, 2);
29
30 add_action( 'admin_init', [ $this, 'redirect_onboard' ] );
31
32 // Update user roles and permissions for multisite.
33 add_action( 'wp_initialize_site', [ $this, 'update_user_role_on_insert_new_site' ], 110 );
34
35 add_action( 'set_user_role', [ $this, 'set_roles' ], 10, 2 );
36
37 add_filter( 'show_admin_bar', [ $this, 'hide_admin_bar_for_customers' ] );
38 add_action( 'admin_init', [ $this, 'block_admin_for_customers' ] );
39
40 add_filter( 'timetics_menu', [ $this, 'update_busyness_menu' ] );
41
42 add_filter( 'timetics_settings', [ $this, 'set_google_auth_url' ] );
43
44 add_filter( 'timetics_get_settings', [ $this, 'set_email_default_content' ] );
45
46 /**
47 * Added temporary for leagacy sass. It will remove in future.
48 */
49 do_action('timetics_admin_init');
50 }
51
52 /**
53 * Add admin notice for
54 *
55 * @return void
56 */
57 public function add_woocommerce_notice_on_admin_dashboard() {
58
59 $woocommerce_notice_dismiss = get_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true );
60
61 if ( $woocommerce_notice_dismiss ) {
62 return;
63 }
64
65 if ( ! timetics_get_option( 'wc_integration' ) ) {
66 return;
67 }
68
69 if ( function_exists( 'WC' ) ) {
70 return;
71 }
72
73 $notice = '<div class="notice notice-danger is-dismissible error">';
74 $notice .= sprintf('<p><strong>%s</strong></p>', __( 'Timetics requires WooCommerce to enable woocommerce integration. You can download <a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a> here.', 'timetics' ) );
75 $notice .= '</div>';
76
77 echo wp_kses_post( $notice );
78
79 $admin_ajax_url = admin_url('admin-ajax.php');
80
81 ?>
82 <script>
83 jQuery(document).ready(function($) {
84 $('.notice.is-dismissible').on('click', '.notice-dismiss', function() {
85 var data = {
86 action: 'dismiss_woocommerce_notice',
87 };
88
89 $.post('<?php echo esc_url( $admin_ajax_url ); ?>', data, function(response) {
90 console.log(response);
91 });
92 });
93 });
94 </script>
95 <?php
96 }
97
98 /**
99 * Update admin notice
100 *
101 * @return void
102 */
103 public function dismiss_woocommerce_notice() {
104 // Store the notice state in user meta
105 update_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true );
106
107 wp_send_json_success('Notice dismissed successfully.');
108 }
109
110 /**
111 * Add settings on plugin action row
112 *
113 * @param array $actions
114 * @param string $plugin_file
115 *
116 * @return array
117 */
118 public function add_settings_plugin_tab( $actions, $plugin_file ) {
119 // Check if the plugin file matches your target plugin
120 if ( 'timetics/timetics.php' === $plugin_file ) {
121 // Add your custom tab
122 $settings_tab = array(
123 'timetics-settings' => sprintf( '<a href="%s">%s</a>', esc_url( admin_url('admin.php?page=timetics#/settings') ), __( 'Settings', 'timetics' ) ),
124 );
125 // Merge the custom tab with existing actions
126 $actions = array_merge( $settings_tab, $actions );
127 }
128 return $actions;
129 }
130
131 /**
132 * Redirect onboarding page after activating
133 *
134 * @return void
135 */
136 public function redirect_onboard() {
137 $onboarding_setup = get_option( 'timetics_onboard_settings' );
138
139 if ( ! $onboarding_setup ) {
140 return;
141 }
142
143 update_option( 'timetics_onboard_setup', true );
144 update_option( 'timetics_onboard_settings', false );
145
146 wp_safe_redirect( admin_url('admin.php?page=timetics#/onboard') );
147 exit;
148 }
149
150 /**
151 * Update site admin roles whne new site created
152 *
153 * @param WP_Site $site_object
154 *
155 * @return void
156 */
157 public function update_user_role_on_insert_new_site( $site_object ) {
158 global $wpdb;
159
160 $roles = wp_roles()->roles;
161 $blog_id = $site_object->blog_id;
162 $roles_option_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_roles';
163
164 update_blog_option( $blog_id, $roles_option_key, $roles );
165 }
166
167 /**
168 * Update user role timetics staff and customer if user is admin
169 *
170 * @param integer $user_id
171 * @param string $role
172 *
173 * @return void
174 */
175 public function set_roles( $user_id, $role ) {
176 if ( 'administrator' !== $role ) {
177 return;
178 }
179
180 $user = get_userdata( $user_id );
181 $user->add_role('timetics-staff');
182 $user->add_role('timetics-customer');
183 }
184
185 /**
186 * Hide the WP admin toolbar for customer-only users.
187 *
188 * @param bool $show
189 *
190 * @return bool
191 */
192 public function hide_admin_bar_for_customers( $show ) {
193 $user = wp_get_current_user();
194
195 if ( ! $user || ! $user->exists() ) {
196 return $show;
197 }
198
199 if ( $this->is_customer_only( $user ) ) {
200 return false;
201 }
202
203 return $show;
204 }
205
206 /**
207 * Redirect customer-only users away from /wp-admin/.
208 *
209 * @return void
210 */
211 public function block_admin_for_customers() {
212 if ( wp_doing_ajax() ) {
213 return;
214 }
215
216 $user = wp_get_current_user();
217
218 if ( $user && $user->exists() && $this->is_customer_only( $user ) ) {
219 wp_safe_redirect( home_url() );
220 exit;
221 }
222 }
223
224 /**
225 * Whether the given user's only Timetics-relevant role is timetics-customer.
226 *
227 * @param \WP_User $user
228 *
229 * @return bool
230 */
231 private function is_customer_only( $user ) {
232 $roles = (array) $user->roles;
233
234 return in_array( 'timetics-customer', $roles, true )
235 && ! array_intersect( $roles, [ 'administrator', 'editor', 'author', 'contributor', 'timetics-staff' ] );
236 }
237
238 /**
239 * Update busyness menu item
240 *
241 * @param array $menu_items
242 *
243 * @return array
244 */
245 public function update_busyness_menu($menu_items) {
246 $busyness_category = timetics_get_busyness_category();
247
248 if ( ! $busyness_category ) {
249 return $menu_items;
250 }
251
252 $busyness = timetics_get_busyness( $busyness_category );
253 $new_menu_items = [];
254
255 foreach ( $menu_items as $menu ) {
256 if ( empty( $menu['id'] ) ) {
257 continue;
258 }
259
260 if ( ! empty( $busyness[$menu['id']] ) ) {
261 $menu['title'] = $busyness[$menu['id']];
262 }
263
264 $new_menu_items[] = $menu;
265 }
266
267 return $new_menu_items;
268 }
269
270 /**
271 * Set google auth url
272 *
273 * @param array $settings
274 *
275 * @return array
276 */
277 public function set_google_auth_url( $settings ) {
278 $settings['google_auth_redirect_uri'] = timetics_get_auth_redirect_uri( 'google-auth' );
279
280 return $settings;
281 }
282
283 /**
284 * Set default settings
285 *
286 * @param array $settings [$settings description]
287 *
288 * @return mixed
289 */
290 public function set_email_default_content( $settings ) {
291
292
293 $admin_email = get_option( 'admin_email' );
294
295 /* translators: {%customer_name%} is a token replaced at runtime with the recipient customer's name */
296 $customer_greeting = __( 'Hi {%customer_name%}', 'timetics' );
297 /* translators: {%host_name%} is a token replaced at runtime with the recipient host's name */
298 $host_greeting = __( 'Hi {%host_name%}', 'timetics' );
299 $new_meeting_msg = __( 'A new meeting has been scheduled.', 'timetics' );
300 /* translators: {%meeting_title%} is a token replaced at runtime with the meeting title */
301 $canceled_msg = __( '{%meeting_title%} has been canceled.', 'timetics' );
302 /* translators: {%meeting_title%} is a token replaced at runtime with the meeting title */
303 $rescheduled_msg = __( '{%meeting_title%} has been rescheduled', 'timetics' );
304 /* translators: {%meeting_title%}, {%meeting_date%}, {%meeting_time%} are tokens replaced at runtime with the meeting details */
305 $reminder_msg = __( '{%meeting_title%} at {%meeting_date%} {%meeting_time%}', 'timetics' );
306
307 $defaults = [
308
309 'booking_created_customer_email_from' => $admin_email,
310 'booking_created_customer_email_title' => sprintf('%s', __( 'New meeting scheduled!', 'timetics' )),
311 'booking_created_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', $customer_greeting, $new_meeting_msg),
312
313 'booking_created_host_email_from' => $admin_email,
314 'booking_created_host_email_title' => sprintf('%s', __( 'New meeting scheduled!', 'timetics' )),
315 'booking_created_host_email_body' => sprintf('<p>%s</p><p>%s</p>', $host_greeting, $new_meeting_msg),
316
317
318 'booking_canceled_customer_email_from' => $admin_email,
319 'booking_canceled_customer_email_title' => sprintf('%s', __( 'Meeting cancelled', 'timetics' )),
320 'booking_canceled_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', $customer_greeting, $canceled_msg),
321
322 'booking_canceled_host_email_from' => $admin_email,
323 'booking_canceled_host_email_title' => sprintf('%s', __( 'Meeting cancelled', 'timetics' )),
324 'booking_canceled_host_email_body' => sprintf('<p>%s</p><p>%s</p>', $host_greeting, $canceled_msg),
325
326 'booking_rescheduled_customer_email_from' => $admin_email,
327 'booking_rescheduled_customer_email_title' => sprintf('%s', __( 'Meeting rescheduled!', 'timetics' )),
328 'booking_rescheduled_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', $host_greeting, $rescheduled_msg),
329
330 'booking_rescheduled_host_email_from' => $admin_email,
331 'booking_rescheduled_host_email_title' => sprintf('%s', __('Meeting rescheduled!', 'timetics' )),
332 'booking_rescheduled_host_email_body' => sprintf('<p>%s</p><p>%s</p>', $host_greeting, $rescheduled_msg),
333
334 'booking_reminder_customer_email_from' => $admin_email,
335 'booking_reminder_customer_email_title' => sprintf('%s', __( 'Meeting time reminder', 'timetics' )),
336 'booking_reminder_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', $customer_greeting, $reminder_msg),
337
338 'booking_reminder_host_email_from' => $admin_email,
339 'booking_reminder_host_email_title' => sprintf('%s', __( 'Meeting time reminder', 'timetics' )),
340 'booking_reminder_host_email_body' => sprintf('<p>%s</p><p>%s</p>', $host_greeting, $reminder_msg),
341 ];
342
343 $settings = wp_parse_args( $settings, $defaults );
344
345 return $settings;
346 }
347 }
348