*/
public static function screens(): array {
$screens = array(
array(
'label' => __( 'Dashboard', 'schedulereed' ),
'route' => '',
'cap' => Capabilities::OWN,
),
array(
'label' => __( 'Calendar', 'schedulereed' ),
'route' => 'calendar',
'cap' => Capabilities::OWN,
),
array(
'label' => __( 'Bookings', 'schedulereed' ),
'route' => 'bookings',
'cap' => Capabilities::OWN,
),
array(
'label' => __( 'Customers', 'schedulereed' ),
'route' => 'customers',
'cap' => Capabilities::DESK,
),
array(
'label' => __( 'Services', 'schedulereed' ),
'route' => 'services',
'cap' => Capabilities::MANAGE,
),
array(
'label' => __( 'Staff', 'schedulereed' ),
'route' => 'staff',
'cap' => Capabilities::MANAGE,
),
array(
'label' => __( 'Emails', 'schedulereed' ),
'route' => 'emails',
'cap' => Capabilities::SETTINGS,
),
array(
'label' => __( 'Settings', 'schedulereed' ),
'route' => 'settings',
'cap' => Capabilities::SETTINGS,
),
);
if ( ! Capabilities::can_desk() && Capabilities::own_staff_id() > 0 ) {
// Staff edit their own profile, hours and time off.
$screens[] = array(
'label' => __( 'My profile', 'schedulereed' ),
'route' => 'staff/' . Capabilities::own_staff_id(),
'cap' => Capabilities::OWN,
);
}
/**
* Add admin screens, e.g. Pro's reports.
*
* @param array $screens [{label, route, cap}].
*/
return (array) apply_filters( 'schedulereed_admin_screens', $screens );
}
public function add_menu(): void {
$cap = current_user_can( Capabilities::MANAGE ) || current_user_can( 'manage_options' ) ? Capabilities::MANAGE : ( current_user_can( Capabilities::DESK ) ? Capabilities::DESK : Capabilities::OWN );
if ( current_user_can( 'manage_options' ) && ! current_user_can( Capabilities::MANAGE ) ) {
// Caps were not installed yet, e.g. after a manual file upload.
Capabilities::install();
$cap = 'manage_options';
}
$pending = Capabilities::can_desk() ? Services::bookings()->search(
array(
'status' => array( 'pending' ),
'from' => time(),
'per_page' => 1,
)
)['total'] : 0;
$title = esc_html( Brand::menu_label() ) . ( $pending > 0 ? sprintf( ' %1$d', (int) $pending ) : '' );
$this->hook = (string) add_menu_page(
Brand::name(),
$title,
$cap,
self::SLUG,
array( $this, 'render' ),
'dashicons-calendar-alt',
26
);
foreach ( self::screens() as $i => $screen ) {
add_submenu_page(
self::SLUG,
$screen['label'],
$screen['label'],
current_user_can( 'manage_options' ) ? 'manage_options' : $screen['cap'],
0 === $i ? self::SLUG : self::SLUG . '#/' . $screen['route'],
0 === $i ? array( $this, 'render' ) : '__return_null'
);
}
}
public function render(): void {
echo '
' . esc_html__( 'Loading…', 'schedulereed' ) . '
';
if ( ! file_exists( SCHEDULEREED_DIR . 'build/admin.asset.php' ) ) {
echo '' . esc_html__( 'The admin app is missing. Run `npm install && npm run build` in the plugin folder.', 'schedulereed' ) . '
';
}
}
/**
* Load the admin app on its screen.
*
* @param string $hook Current admin page hook.
*/
public function enqueue( string $hook ): void {
if ( $hook !== $this->hook || ! Assets::register( self::HANDLE, 'admin' ) ) {
return;
}
wp_enqueue_media();
Assets::localize( self::HANDLE );
$user = wp_get_current_user();
wp_add_inline_script(
self::HANDLE,
'window.schedulereed = Object.assign( window.schedulereed || {}, ' . wp_json_encode(
array(
/**
* What the admin app gets in window.schedulereed.admin, e.g. Pro's options.
*
* @param array $config Config.
*/
'admin' => apply_filters(
'schedulereed_admin_config',
array(
'screens' => array_values(
array_filter(
self::screens(),
static fn( array $s ): bool => current_user_can( $s['cap'] ) || current_user_can( 'manage_options' )
)
),
'can' => array(
'manage' => Capabilities::can_manage(),
'desk' => Capabilities::can_desk(),
'settings' => Capabilities::can_settings(),
),
'staffId' => Capabilities::own_staff_id(),
'user' => array(
'id' => $user->ID,
'name' => $user->display_name,
'email' => $user->user_email,
),
'setupDone' => Setup::is_done(),
'currency' => Settings::currency(),
'timezone' => Settings::timezone()->getName(),
'locale' => str_replace( '_', '-', get_user_locale() ),
'weekStarts' => (int) get_option( 'start_of_week', 1 ),
'bookingUrl' => Settings::booking_page() > 0 ? (string) get_permalink( Settings::booking_page() ) : '',
'pro' => Edition::is_pro(),
'brand' => Brand::name(),
'logo' => Brand::logo(),
'upgradeUrl' => Edition::upgrade_url(),
)
),
)
) . ' );',
'before'
);
/**
* The admin app is about to load, e.g. for Pro to add its script first.
*
* @param string $handle The admin app's script handle.
*/
do_action( 'schedulereed_admin_enqueue', self::HANDLE );
wp_enqueue_script( self::HANDLE );
wp_enqueue_style( self::HANDLE );
wp_enqueue_style( 'wp-components' );
}
/**
* Open the setup wizard once, right after the plugin is first activated.
*/
public function redirect_after_activation(): void {
if ( ! get_option( Installer::REDIRECT_OPTION ) ) {
return;
}
delete_option( Installer::REDIRECT_OPTION );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only reads whether several plugins were activated at once.
$bulk = isset( $_GET['activate-multi'] );
if ( $bulk || wp_doing_ajax() || is_network_admin() || Setup::is_done() || ! current_user_can( 'manage_options' ) ) {
return;
}
wp_safe_redirect( admin_url( 'admin.php?page=' . self::SLUG . '#/setup' ) );
exit;
}
/**
* Links under the plugin name on the Plugins screen.
*
* @param string[] $links Links.
* @return string[]
*/
public function action_links( array $links ): array {
array_unshift( $links, sprintf( '%s', esc_url( admin_url( 'admin.php?page=' . self::SLUG . '#/settings' ) ), esc_html__( 'Settings', 'schedulereed' ) ) );
return $links;
}
}