| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin hooks |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Admin; |
| 8 |
use Timetics\Utils\Singleton; |
| 9 |
|
| 10 |
|
| 11 |
class Hooks { |
| 12 |
use Singleton; |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize admin hooks |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function init() { |
| 20 |
add_action('admin_notices', [ $this, 'add_woocommerce_notice_on_admin_dashboard' ] ); |
| 21 |
|
| 22 |
// Dismiss the notice |
| 23 |
add_action('wp_ajax_dismiss_woocommerce_notice', [ $this, 'dismiss_woocommerce_notice'] ); |
| 24 |
|
| 25 |
add_filter('plugin_action_links', [ $this, 'add_settings_plugin_tab' ], 10, 2); |
| 26 |
|
| 27 |
add_action( 'admin_init', [ $this, 'redirect_onboard' ] ); |
| 28 |
|
| 29 |
// Update user roles and permissions for multisite. |
| 30 |
add_action( 'wp_initialize_site', [ $this, 'update_user_role_on_insert_new_site' ], 110 ); |
| 31 |
|
| 32 |
add_action( 'set_user_role', [ $this, 'set_roles' ], 10, 2 ); |
| 33 |
|
| 34 |
add_filter( 'timetics_menu', [ $this, 'update_busyness_menu' ] ); |
| 35 |
|
| 36 |
add_filter( 'timetics_settings', [ $this, 'set_google_auth_url' ] ); |
| 37 |
|
| 38 |
add_filter( 'timetics_get_settings', [ $this, 'set_email_default_content' ] ); |
| 39 |
|
| 40 |
/** |
| 41 |
* Added temporary for leagacy sass. It will remove in future. |
| 42 |
*/ |
| 43 |
do_action('timetics_admin_init'); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add admin notice for |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function add_woocommerce_notice_on_admin_dashboard() { |
| 52 |
|
| 53 |
$woocommerce_notice_dismiss = get_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true ); |
| 54 |
|
| 55 |
if ( $woocommerce_notice_dismiss ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! timetics_get_option( 'wc_integration' ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if ( function_exists( 'WC' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$notice = '<div class="notice notice-danger is-dismissible error">'; |
| 68 |
$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' ) ); |
| 69 |
$notice .= '</div>'; |
| 70 |
|
| 71 |
echo wp_kses_post( $notice ); |
| 72 |
|
| 73 |
$admin_ajax_url = admin_url('admin-ajax.php'); |
| 74 |
|
| 75 |
?> |
| 76 |
<script> |
| 77 |
jQuery(document).ready(function($) { |
| 78 |
$('.notice.is-dismissible').on('click', '.notice-dismiss', function() { |
| 79 |
var data = { |
| 80 |
action: 'dismiss_woocommerce_notice', |
| 81 |
}; |
| 82 |
|
| 83 |
$.post('<?php echo esc_url( $admin_ajax_url ); ?>', data, function(response) { |
| 84 |
console.log(response); |
| 85 |
}); |
| 86 |
}); |
| 87 |
}); |
| 88 |
</script> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Update admin notice |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function dismiss_woocommerce_notice() { |
| 98 |
// Store the notice state in user meta |
| 99 |
update_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true ); |
| 100 |
|
| 101 |
wp_send_json_success('Notice dismissed successfully.'); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add settings on plugin action row |
| 106 |
* |
| 107 |
* @param array $actions |
| 108 |
* @param string $plugin_file |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function add_settings_plugin_tab( $actions, $plugin_file ) { |
| 113 |
// Check if the plugin file matches your target plugin |
| 114 |
if ( 'timetics/timetics.php' === $plugin_file ) { |
| 115 |
// Add your custom tab |
| 116 |
$settings_tab = array( |
| 117 |
'timetics-settings' => sprintf( '<a href="%s">%s</a>', esc_url( admin_url('admin.php?page=timetics#/settings') ), __( 'Settings', 'timetics' ) ), |
| 118 |
); |
| 119 |
// Merge the custom tab with existing actions |
| 120 |
$actions = array_merge( $settings_tab, $actions ); |
| 121 |
} |
| 122 |
return $actions; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Redirect onboarding page after activating |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function redirect_onboard() { |
| 131 |
$onboarding_setup = get_option( 'timetics_onboard_settings' ); |
| 132 |
|
| 133 |
if ( ! $onboarding_setup ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
update_option( 'timetics_onboard_setup', true ); |
| 138 |
update_option( 'timetics_onboard_settings', false ); |
| 139 |
|
| 140 |
exit( esc_url( wp_redirect( admin_url('admin.php?page=timetics#/onboard') ) ) ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Update site admin roles whne new site created |
| 145 |
* |
| 146 |
* @param WP_Site $site_object |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
public function update_user_role_on_insert_new_site( $site_object ) { |
| 151 |
global $wpdb; |
| 152 |
|
| 153 |
$roles = wp_roles()->roles; |
| 154 |
$blog_id = $site_object->blog_id; |
| 155 |
$roles_option_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_roles'; |
| 156 |
|
| 157 |
update_blog_option( $blog_id, $roles_option_key, $roles ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Update user role timetics staff and customer if user is admin |
| 162 |
* |
| 163 |
* @param integer $user_id |
| 164 |
* @param string $role |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
public function set_roles( $user_id, $role ) { |
| 169 |
if ( 'administrator' !== $role ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$user = get_userdata( $user_id ); |
| 174 |
$user->add_role('timetics-staff'); |
| 175 |
$user->add_role('timetics-customer'); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Update busyness menu item |
| 180 |
* |
| 181 |
* @param array $menu_items |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
public function update_busyness_menu($menu_items) { |
| 186 |
$busyness_category = timetics_get_busyness_category(); |
| 187 |
|
| 188 |
if ( ! $busyness_category ) { |
| 189 |
return $menu_items; |
| 190 |
} |
| 191 |
|
| 192 |
$busyness = timetics_get_busyness( $busyness_category ); |
| 193 |
$new_menu_items = []; |
| 194 |
|
| 195 |
foreach ( $menu_items as $menu ) { |
| 196 |
if ( empty( $menu['id'] ) ) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
if ( ! empty( $busyness[$menu['id']] ) ) { |
| 201 |
$menu['title'] = $busyness[$menu['id']]; |
| 202 |
} |
| 203 |
|
| 204 |
$new_menu_items[] = $menu; |
| 205 |
} |
| 206 |
|
| 207 |
return $new_menu_items; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Set google auth url |
| 212 |
* |
| 213 |
* @param array $settings |
| 214 |
* |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
public function set_google_auth_url( $settings ) { |
| 218 |
$settings['google_auth_redirect_uri'] = timetics_get_auth_redirect_uri( 'google-auth' ); |
| 219 |
|
| 220 |
return $settings; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Set default settings |
| 225 |
* |
| 226 |
* @param array $settings [$settings description] |
| 227 |
* |
| 228 |
* @return mixed |
| 229 |
*/ |
| 230 |
public function set_email_default_content( $settings ) { |
| 231 |
|
| 232 |
|
| 233 |
$admin_email = get_option( 'admin_email' ); |
| 234 |
|
| 235 |
$defaults = [ |
| 236 |
|
| 237 |
'booking_created_customer_email_from' => $admin_email, |
| 238 |
'booking_created_customer_email_title' => sprintf('%s', __( 'New meeting scheduled!', 'timetics' )), |
| 239 |
'booking_created_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%customer_name%}', 'timetics' ), __( 'A new meeting has been scheduled.', 'timetics' )), |
| 240 |
|
| 241 |
'booking_created_host_email_from' => $admin_email, |
| 242 |
'booking_created_host_email_title' => sprintf('%s', __( 'New meeting scheduled!', 'timetics' )), |
| 243 |
'booking_created_host_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%host_name%}', 'timetics' ), __( 'A new meeting has been scheduled.', 'timetics' )), |
| 244 |
|
| 245 |
|
| 246 |
'booking_canceled_customer_email_from' => $admin_email, |
| 247 |
'booking_canceled_customer_email_title' => sprintf('%s', __( 'Meeting cancelled', 'timetics' )), |
| 248 |
'booking_canceled_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%customer_name%}', 'timetics' ), __( '{%meeting_title%} has been canceled.', 'timetics' )), |
| 249 |
|
| 250 |
'booking_canceled_host_email_from' => $admin_email, |
| 251 |
'booking_canceled_host_email_title' => sprintf('%s', __( 'Meeting cancelled', 'timetics' )), |
| 252 |
'booking_canceled_host_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%host_name%}', 'timetics' ), __( '{%meeting_title%} has been canceled.', 'timetics' )), |
| 253 |
|
| 254 |
'booking_rescheduled_customer_email_from' => $admin_email, |
| 255 |
'booking_rescheduled_customer_email_title' => sprintf('%s', __( 'Meeting rescheduled!', 'timetics' )), |
| 256 |
'booking_rescheduled_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%host_name%}', 'timetics' ), __( '{%meeting_title%} has been rescheduled', 'timetics' )), |
| 257 |
|
| 258 |
'booking_rescheduled_host_email_from' => $admin_email, |
| 259 |
'booking_rescheduled_host_email_title' => sprintf('%s', __('Meeting rescheduled!', 'timetics' )), |
| 260 |
'booking_rescheduled_host_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%host_name%}', 'timetics' ), __( '{%meeting_title%} has been rescheduled', 'timetics' )), |
| 261 |
|
| 262 |
'booking_reminder_customer_email_from' => $admin_email, |
| 263 |
'booking_reminder_customer_email_title' => sprintf('%s', __( 'Meeting time reminder', 'timetics' )), |
| 264 |
'booking_reminder_customer_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%customer_name%}', 'timetics' ), __( '{%meeting_title%} at {%meeting_date%} {%meeting_time%}', 'timetics' )), |
| 265 |
|
| 266 |
'booking_reminder_host_email_from' => $admin_email, |
| 267 |
'booking_reminder_host_email_title' => sprintf('%s', __( 'Meeting time reminder', 'timetics' )), |
| 268 |
'booking_reminder_host_email_body' => sprintf('<p>%s</p><p>%s</p>', __( 'Hi {%host_name%}', 'timetics' ), __( '{%meeting_title%} at {%meeting_date%} {%meeting_time%}', 'timetics' )), |
| 269 |
]; |
| 270 |
|
| 271 |
$settings = wp_parse_args( $settings, $defaults ); |
| 272 |
|
| 273 |
return $settings; |
| 274 |
} |
| 275 |
} |
| 276 |
|