name = __( 'Weekly Schedule', 'appointments' ); } public function get_defaults() { $_workers = appointments_get_workers(); $workers = array( array( 'text' => __( 'Any provider', 'appointments' ), 'value' => 0 ), ); foreach ( $_workers as $worker ) { /** @var Appointments_Worker $worker */ $workers[] = array( 'text' => $worker->get_name(), 'value' => $worker->ID ); } $_services = appointments_get_services(); $services = array( array( 'text' => __( 'Any service', 'appointments' ), 'value' => 0 ), ); foreach ( $_services as $service ) { /** @var Appointments_Service $service */ $services[] = array( 'text' => $service->name, 'value' => $service->ID ); } return array( 'title' => array( 'type' => 'text', 'name' => __( 'Title', 'appointments' ), 'value' => sprintf( '
LOGIN_PAGE will be replaced with your website\'s login page, while REGISTRATION_PAGE will be replaced with your website\'s registration page.', 'appointments' ),
'example' => __( 'You need to login to make an appointment. Please click here to register/login: LOGIN_PAGE', 'appointments' ),
),
'service' => array(
'type' => 'select',
'name' => __( 'Service', 'appointments' ),
'options' => $services,
'value' => 0,
'help' => __( 'Select service only if you want to force the table display the service with entered ID. Default: Service is selected by dropdown.', 'appointments' ),
),
'worker' => array(
'type' => 'select',
'name' => __( 'Provider', 'appointments' ),
'options' => $workers,
'value' => 0,
'help' => __( 'Select service provider only if you want to force the table display the service provider with entered ID. Default: Service provider is selected by dropdown.', 'appointments' ),
),
'long' => array(
'type' => 'checkbox',
'name' => __( 'Long week days', 'appointments' ),
'value' => 0,
'help' => __( 'If checked, long week days are displayed on the schedule table row, e.g. "Saturday" instead of "Sa".', 'appointments' ),
),
'class' => array(
'type' => 'text',
'name' => __( 'CSS Class', 'appointments' ),
'value' => '',
'help' => __( 'A css class name for the schedule table. Default is empty.', 'appointments' ),
),
'add' => array(
'type' => 'text',
'name' => __( 'Number of months added', 'appointments' ),
'value' => 0,
'help' => __( 'Number of months to add to the schedule to use for preceding months\' schedules. Enter 1 for next month, 2 for the other month, so on. Default: "0" (Current month)', 'appointments' ),
),
'_noscript' => array(
'value' => false,
),
'date' => array(
'type' => 'text',
'name' => __( 'Date', 'appointments' ),
'value' => '',
'help' => __( 'Normally calendar starts from the current month. If you want to force it to start from a certain date, enter that date here. Most date formats are supported, but YYYY-MM-DD is recommended. Notes: 1) This value will also affect other subsequent calendars on the same page. 2) It is sufficient to enter a date inside the month. Default: "0" (Current month)', 'appointments' ),
),
'require_provider' => array(
'type' => 'checkbox',
'name' => __( 'Require Provider', 'appointments' ),
'value' => 0,
'help' => __( 'Checking this argument means a timetable will not be rendered unless a service provider has been previously selected.', 'appointments' ),
),
'required_message' => array(
'type' => 'text',
'name' => __( 'Required Message', 'appointments' ),
'value' => __( 'Please, select a service provider.', 'appointments' ),
'help' => __( 'The message that will be shown if service providers are required.', 'appointments' ),
),
'require_service' => array(
'type' => 'checkbox',
'name' => __( 'Require Service', 'appointments' ),
'value' => 0,
'help' => __( 'Checking this argument means a timetable will not be rendered unless a service has been previously selected.', 'appointments' ),
),
'required_service_message' => array(
'type' => 'text',
'name' => __( 'Required Service Message', 'appointments' ),
'value' => __( 'Please, select a service.', 'appointments' ),
'help' => __( 'The message that will be shown if services are required.', 'appointments' ),
),
);
}
public function process_shortcode( $args = array(), $content = '' ) {
$appointments = appointments();
$current_time = current_time( 'timestamp' );
$options = appointments_get_options();
$args = wp_parse_args( $args, $this->_defaults_to_args() );
$service_id = isset( $_REQUEST['app_service_id'] ) ? absint( $_REQUEST['app_service_id'] ) : 0;
if ( appointments_get_services_min_id() && ! $service_id ) {
$service_id = appointments_get_services_min_id();
}
$worker_id = isset( $_REQUEST['app_provider_id'] ) ? absint( $_REQUEST['app_provider_id'] ) : 0;
if ( ! $worker_id ) {
$worker_id = isset( $_REQUEST['app_worker_id'] ) ? absint( $_REQUEST['app_worker_id'] ) : 0;
}
$location_id = isset( $_REQUEST['app_location_id'] ) ? absint( $_REQUEST['app_location_id'] ) : 0;
// Force service
if ( $args['service'] ) {
// Check if such a service exists
if ( ! appointments_get_service( $args['service'] ) ) {
return '';
}
$service_id = absint( $args['service'] );
$_REQUEST['app_service_id'] = $args['service'];
}
$workers_by_service = appointments_get_workers_by_service( $service_id );
$workers_ids = wp_list_pluck( $workers_by_service, 'ID' );
$single_worker = false;
if ( 1 === count( $workers_by_service ) ) {
$single_worker = $workers_by_service[0]->ID;
}
if ( $args['worker'] ) {
// Check if such a worker exists
if ( ! appointments_is_worker( $args['worker'] ) ) {
return '';
}
$worker_id = absint( $args['worker'] );
$_REQUEST['app_provider_id'] = $args['worker'];
} elseif ( $single_worker ) {
// Select the only provider if that is the case
$_REQUEST['app_provider_id'] = $single_worker;
$args['worker'] = $single_worker;
$worker_id = absint( $args['worker'] );
} elseif ( isset( $args['require_provider'] ) &&
$args['require_provider'] == 1 &&
( ( ! isset( $_REQUEST['app_provider_id'] ) || ! in_array( $_REQUEST['app_provider_id'], $workers_ids ) ) || ! isset( $args['worker'] ) ) ) {
$worker_id = 0;
} elseif ( isset( $_REQUEST['app_provider_id'] ) && in_array( $_REQUEST['app_provider_id'], $workers_ids ) ) {
$worker_id = (int) $_REQUEST['app_provider_id'];
$args['worker'] = $worker_id;
} else {
/*
if( is_array( $workers_by_service ) && ! empty( $workers_by_service ) ){
$worker_id = $workers_by_service[0]->ID;
if( isset( $_REQUEST["app_provider_id"] ) ){
$_REQUEST["app_provider_id"] = $_GET["app_provider_id"] = $worker_id;
}
}
*/
}
// Force a date
if ( $args['date'] && ! isset( $_GET['wcalendar'] ) ) {
$time = strtotime( $args['date'], $current_time ) + ( $args['add'] * 7 * 86400 );
$_GET['wcalendar'] = $time;
} else {
if ( isset( $_GET['wcalendar'] ) && (int) $_GET['wcalendar'] ) {
$time = (int) $_GET['wcalendar'] + ( $args['add'] * 7 * 86400 );
} else {
$time = $current_time + ( $args['add'] * 7 * 86400 );
}
}
$slots = array();
if ( is_array( $workers_by_service ) && ! empty( $workers_by_service ) ) {
$slots = appointments_get_weekly_schedule_slots( $time, $service_id, $workers_by_service[0]->ID, $location_id );
} else {
$slots = appointments_get_weekly_schedule_slots( $time, $service_id, $worker_id, $location_id );
}
if ( '' != $args['title'] ) {
$start_day = current( $slots['the_week'] );
end( $slots['the_week'] );
$end_day = current( $slots['the_week'] );
reset( $slots['the_week'] );
$args['title'] = str_replace(
array( 'START', 'END' ),
array(
date_i18n( appointments_get_date_format( 'date' ), strtotime( $start_day ) ),
date_i18n( appointments_get_date_format( 'date' ), strtotime( $end_day ) ),
),
$args['title']
);
} else {
$args['title'] = '';
}
$has_worker = ! empty( $args['require_provider'] ) ? ( isset( $_GET['app_provider_id'] ) && is_numeric( $_GET['app_provider_id'] ) ) : ! empty( $worker_id ) || ( is_array( $workers_by_service ) && ! empty( $workers_by_service ) );
$has_service = ! empty( $args['require_service'] ) ? ( isset( $_GET['app_service_id'] ) && is_numeric( $_GET['app_service_id'] ) ) : ! empty( $service_id );
$defaults = $this->get_defaults();
$c = '';
$c .= '