| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin hooks |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Admin; |
| 8 |
use Timetics\Utils\Singleton; |
| 9 |
|
| 10 |
use function Timetics\timetics; |
| 11 |
|
| 12 |
class Hooks { |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
/** |
| 16 |
* Initialize admin hooks |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function init() { |
| 21 |
add_action('admin_notices', [ $this, 'add_woocommerce_notice_on_admin_dashboard' ] ); |
| 22 |
|
| 23 |
// Dismiss the notice |
| 24 |
add_action('wp_ajax_dismiss_woocommerce_notice', [ $this, 'dismiss_woocommerce_notice'] ); |
| 25 |
|
| 26 |
add_filter('plugin_action_links', [ $this, 'add_settings_plugin_tab' ], 10, 2); |
| 27 |
|
| 28 |
add_action( 'admin_init', [ $this, 'redirect_onboard' ] ); |
| 29 |
|
| 30 |
// Update user roles and permissions for multisite. |
| 31 |
add_action( 'wp_initialize_site', [ $this, 'update_user_role_on_insert_new_site' ], 110 ); |
| 32 |
|
| 33 |
add_action( 'set_user_role', [ $this, 'set_roles' ], 10, 2 ); |
| 34 |
|
| 35 |
add_filter( 'timetics_menu', [ $this, 'update_busyness_menu' ] ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Added temporary for leagacy sass. It will remove in future. |
| 39 |
*/ |
| 40 |
do_action('timetics_admin_init'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add admin notice for |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function add_woocommerce_notice_on_admin_dashboard() { |
| 49 |
|
| 50 |
$woocommerce_notice_dismiss = get_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true ); |
| 51 |
|
| 52 |
if ( $woocommerce_notice_dismiss ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! timetics_get_option( 'wc_integration' ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if ( function_exists( 'WC' ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$notice = '<div class="notice notice-danger is-dismissible error">'; |
| 65 |
$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' ) ); |
| 66 |
$notice .= '</div>'; |
| 67 |
|
| 68 |
echo wp_kses_post( $notice ); |
| 69 |
|
| 70 |
$admin_ajax_url = admin_url('admin-ajax.php'); |
| 71 |
|
| 72 |
?> |
| 73 |
<script> |
| 74 |
jQuery(document).ready(function($) { |
| 75 |
$('.notice.is-dismissible').on('click', '.notice-dismiss', function() { |
| 76 |
var data = { |
| 77 |
action: 'dismiss_woocommerce_notice', |
| 78 |
}; |
| 79 |
|
| 80 |
$.post('<?php echo esc_url( $admin_ajax_url ); ?>', data, function(response) { |
| 81 |
console.log(response); |
| 82 |
}); |
| 83 |
}); |
| 84 |
}); |
| 85 |
</script> |
| 86 |
<?php |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Update admin notice |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function dismiss_woocommerce_notice() { |
| 95 |
// Store the notice state in user meta |
| 96 |
update_user_meta( get_current_user_id(), 'timetics_woocommerce_notice_dismissed', true ); |
| 97 |
|
| 98 |
wp_send_json_success('Notice dismissed successfully.'); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add settings on plugin action row |
| 103 |
* |
| 104 |
* @param array $actions |
| 105 |
* @param string $plugin_file |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function add_settings_plugin_tab( $actions, $plugin_file ) { |
| 110 |
// Check if the plugin file matches your target plugin |
| 111 |
if ( 'timetics/timetics.php' === $plugin_file ) { |
| 112 |
// Add your custom tab |
| 113 |
$settings_tab = array( |
| 114 |
'timetics-settings' => sprintf( '<a href="%s">%s</a>', esc_url( admin_url('admin.php?page=timetics#/settings') ), __( 'Settings', 'timetics' ) ), |
| 115 |
); |
| 116 |
// Merge the custom tab with existing actions |
| 117 |
$actions = array_merge( $settings_tab, $actions ); |
| 118 |
} |
| 119 |
return $actions; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Redirect onboarding page after activating |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function redirect_onboard() { |
| 128 |
$onboarding_setup = get_option( 'timetics_onboard_settings' ); |
| 129 |
|
| 130 |
if ( ! $onboarding_setup ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
update_option( 'timetics_onboard_setup', true ); |
| 135 |
update_option( 'timetics_onboard_settings', false ); |
| 136 |
|
| 137 |
exit( wp_redirect( admin_url('admin.php?page=timetics#/onboard') ) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Update site admin roles whne new site created |
| 142 |
* |
| 143 |
* @param WP_Site $site_object |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function update_user_role_on_insert_new_site( $site_object ) { |
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
$roles = wp_roles()->roles; |
| 151 |
$blog_id = $site_object->blog_id; |
| 152 |
$roles_option_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_roles'; |
| 153 |
|
| 154 |
update_blog_option( $blog_id, $roles_option_key, $roles ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Update user role timetics staff and customer if user is admin |
| 159 |
* |
| 160 |
* @param integer $user_id |
| 161 |
* @param string $role |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function set_roles( $user_id, $role ) { |
| 166 |
if ( 'administrator' !== $role ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$user = get_userdata( $user_id ); |
| 171 |
$user->add_role('timetics-staff'); |
| 172 |
$user->add_role('timetics-customer'); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Update busyness menu item |
| 177 |
* |
| 178 |
* @param array $menu_items |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public function update_busyness_menu($menu_items) { |
| 183 |
$busyness_category = timetics_get_busyness_category(); |
| 184 |
|
| 185 |
if ( ! $busyness_category ) { |
| 186 |
return $menu_items; |
| 187 |
} |
| 188 |
|
| 189 |
$busyness = timetics_get_busyness( $busyness_category ); |
| 190 |
$new_menu_items = []; |
| 191 |
|
| 192 |
foreach ( $menu_items as $menu ) { |
| 193 |
if ( empty( $menu['id'] ) ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $busyness[$menu['id']] ) ) { |
| 198 |
$menu['title'] = $busyness[$menu['id']]; |
| 199 |
} |
| 200 |
|
| 201 |
$new_menu_items[] = $menu; |
| 202 |
} |
| 203 |
|
| 204 |
return $new_menu_items; |
| 205 |
} |
| 206 |
} |
| 207 |
|