gcal_api = $gcal_api; // Profile screen add_action( 'show_user_profile', array( $this, 'show_profile' ) ); add_action( 'edit_user_profile', array( $this, 'show_profile' ) ); add_action( 'personal_options_update', array( $this, 'save_profile' ) ); add_action( 'edit_user_profile_update', array( $this, 'save_profile' ) ); // Settings screen add_filter( 'appointments_tabs', array( $this, 'add_setting_tab' ) ); add_filter( 'appointments_get_settings_tab_view-gcal', array( $this, 'setting_tab_view' ) ); add_action( 'admin_init', array( $this, 'save_settings' ), 12 ); add_action( 'admin_init', array( $this, 'reset_settings' ), 12 ); } /** * Add the tab to the settings screen * * @param array $tabs * * @return array */ public function add_setting_tab( $tabs ) { // Set the tab in second place $_tabs_1 = array_slice( $tabs, 0, 1 ); $_tabs_2 = array_slice( $tabs, 1 ); $_tabs_1['gcal'] = __( 'Google Calendar', 'appointments' ); return array_merge( $_tabs_1, $_tabs_2 ); } /** * Set the view for Calendar Settings * * @param string|bool $file * * @return string */ public function setting_tab_view( $file ) { return appointments_plugin_dir() . 'includes/gcal/views/page-settings-tab-gcal.php'; } public function save_settings() { if ( ! isset( $_POST['app-submit-gcalendar'] ) ) { return; } check_admin_referer( 'app-submit-gcalendar' ); $options = appointments_get_options(); $options['gcal_location'] = sanitize_text_field( $_POST['gcal_location'] ); $options['gcal_same_window'] = isset( $_POST['gcal_same_window'] ); $options['gcal'] = isset( $_POST['gcal'] ) && $_POST['gcal'] === 'yes'; appointments_update_options( $options ); $action = $_POST['action']; if ( 'step-1' === $action ) { if ( empty( $_POST['client_id'] ) || empty( $_POST['client_secret'] ) ) { add_settings_error( 'app-gcalendar', 'empty-fields', __( 'All fields are mandatory', 'appointments' ) ); return; } $options['gcal_client_id'] = $_POST['client_id']; $options['gcal_client_secret'] = $_POST['client_secret']; $this->gcal_api->api_manager->set_client_id_and_secret( $options['gcal_client_id'], $options['gcal_client_secret'] ); appointments_update_options( $options ); } elseif ( 'step-2' === $action ) { if ( empty( $_POST['access_code'] ) ) { add_settings_error( 'app-gcalendar', 'empty-fields', __( 'All fields are mandatory', 'appointments' ) ); return; } $result = $this->gcal_api->api_manager->authenticate( $_POST['access_code'] ); if ( is_wp_error( $result ) ) { add_settings_error( 'app-gcalendar', $result->get_error_code(), sprintf( __( 'Authentication failed: %s', 'appointments' ), $result->get_error_message() ) ); return; } $options['gcal_access_code'] = $_POST['access_code']; $options['gcal_token'] = $this->gcal_api->api_manager->get_access_token(); appointments_update_options( $options ); } elseif ( 'step-3' === $action ) { $calendar_id = ! empty( $_POST['gcal_selected_calendar'] ) ? $_POST['gcal_selected_calendar'] : ''; $options['gcal_selected_calendar'] = $calendar_id; $this->gcal_api->api_manager->set_calendar( $calendar_id ); $options['gcal_api_mode'] = $_POST['gcal_api_mode']; $this->gcal_api->api_mode = $options['gcal_api_mode']; $options['gcal_api_allow_worker'] = $_POST['gcal_api_allow_worker']; $options['gcal_api_scope'] = $_POST['gcal_api_scope']; $options['gcal_description'] = $_POST['gcal_description']; $options['gcal_summary'] = $_POST['gcal_summary']; $options['gcal_overwrite'] = isset( $_POST['app-gcal-overwrite'] ); $this->gcal_api->set_description( $options['gcal_description'] ); $this->gcal_api->set_summary( $options['gcal_summary'] ); appointments_update_options( $options ); } wp_redirect( add_query_arg( 'updated', 'true' ) ); exit; } /** * Revoke access and reset Google Calendar settings */ public function reset_settings() { if ( ! isset( $_POST['app-reset-gcalendar'] ) ) { return; } check_admin_referer( 'app-submit-gcalendar' ); $result = $this->gcal_api->api_manager->revoke_token(); if ( ! is_wp_error( $result ) ) { $options = appointments_get_options(); $options['gcal_client_id'] = ''; $options['gcal_client_secret'] = ''; $options['gcal_accesss_code'] = ''; $options['gcal_selected_calendar'] = ''; $options['gcal_token'] = ''; appointments_update_options( $options ); } else { add_settings_error( 'app-gcalendar', $result->get_error_code(), $result->get_error_message() ); } } private function render_errors( $errors ) { if ( ! empty( $errors ) ) { ?>