is_gravityforms_supported()) { require_once('google-calendar-api.php'); } } /** * Initialize hooks required for admin. * * @since 1.0 * * @return void */ public function init() { parent::init(); $this->add_delayed_payment_support( array( 'option_label' => esc_html__('Add data to Google Calendar only when payment is received.', 'alpha-addons-for-gravity-forms'), ) ); add_action('wp_ajax_alphagf_gc_revoke_token', [$this, 'revoke_token']); } public function styles() { $styles = array( array( 'handle' => 'alphagf_settings_style', 'src' => ALPHAGF_ASSETS . '/css/alphagf_addons_admin.css', 'version' => $this->_version, 'enqueue' => array( array( 'admin_page' => array('plugin_settings'), 'tab' => 'alphagf_google_calendar' ) ) ) ); return array_merge(parent::styles(), $styles); } public function scripts() { $scripts = array( array( 'handle' => 'alphagf_google_calendar_admin_script', 'src' => trailingslashit($this->get_base_url()) . 'js/alphagf-google-calendar.js', 'version' => $this->_version, 'deps' => array('jquery'), 'in_footer' => true, 'callback' => array($this, 'localize_admin_scripts'), 'enqueue' => array( array( 'admin_page' => array('plugin_settings'), 'tab' => 'alphagf_google_calendar' ), ) ), ); return array_merge(parent::scripts(), $scripts); } public function localize_admin_scripts() { wp_localize_script('alphagf_google_calendar_admin_script', 'alphagf_gc_vars', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('alphagf_gc_nonce'), 'settings_url' => admin_url('admin.php?page=gf_settings&subview=alphagf_google_calendar'), )); } public function feed_settings_fields() { return array( array( 'title' => esc_html__('Google Calendar Settings', 'alpha-addons-for-gravity-forms'), 'fields' => array( array( 'label' => esc_html__('Enable', 'alpha-addons-for-gravity-forms'), 'name' => 'is_enable', 'type' => 'toggle', 'default_value' => false, ), array( 'name' => 'calendar_list', 'label' => esc_html__('Calendar List', 'alpha-addons-for-gravity-forms'), 'type' => 'select', 'required' => true, 'choices' => $this->get_calender_lists(), 'dependency' => array( 'live' => true, 'fields' => array( array( 'field' => 'is_enable', ), ), ) ), array( 'label' => esc_html__('Event Type', 'alpha-addons-for-gravity-forms'), 'name' => 'event_type', 'type' => 'select', 'required' => true, 'choices' => array( array( 'label' => esc_html__('All Day Event', 'alpha-addons-for-gravity-forms'), 'value' => 'event', ) ), 'dependency' => array( 'live' => true, 'fields' => array( array( 'field' => 'is_enable', ), ), ) ), array( 'name' => 'event', 'type' => 'field_map', 'field_map' => array( array( 'name' => 'title', 'label' => esc_html__('Event Title', 'alpha-addons-for-gravity-forms'), 'required' => false, 'tooltip' => esc_html__('Please choose the event title field', 'alpha-addons-for-gravity-forms'), ), array( 'name' => 'start_date', 'label' => esc_html__('Start Date', 'alpha-addons-for-gravity-forms'), 'required' => true, 'field_type' => array('date'), 'tooltip' => esc_html__('Please choose the start date', 'alpha-addons-for-gravity-forms'), ), array( 'name' => 'end_date', 'label' => esc_html__('End Date', 'alpha-addons-for-gravity-forms'), 'required' => true, 'field_type' => array('date'), 'tooltip' => esc_html__('Please choose the start date', 'alpha-addons-for-gravity-forms'), ), array( 'name' => 'location', 'label' => esc_html__('Location', 'alpha-addons-for-gravity-forms'), 'required' => false, 'tooltip' => esc_html__('Please choose the event location', 'alpha-addons-for-gravity-forms'), ) ), 'dependency' => array( 'live' => true, 'fields' => array( array( 'field' => 'is_enable', ) ), ) ), ) ), array( 'title' => esc_html__('Conditional logic', 'alpha-addons-for-gravity-forms'), 'fields' => array( array( 'type' => 'feed_condition', 'name' => 'feed-condition', 'label' => esc_html__('Conditions', 'alpha-addons-for-gravity-forms'), 'checkbox_label' => esc_html__('Enable conditional processing', 'alpha-addons-for-gravity-forms') ), ), ) ); } public function get_calender_lists() { $calender_lists = get_transient(ALPHAGF_Google_Calendar_API::ALPHAGF_GC_CALENDER_LIST); if (! is_array($calender_lists) || empty($calender_lists)) { $calender_lists = ALPHAGF_Google_Calendar_API::get_calendar_lists(); } $choices = [ [ 'label' => esc_html__('Select a calendar', 'alpha-addons-for-gravity-forms'), 'value' => '', ], ]; if (! empty($calender_lists)) { foreach ($calender_lists as $value) { if (empty($value['id'])) { continue; } $choices[] = [ 'label' => rgar($value, 'summary', $value['id']), 'value' => $value['id'], ]; } } return $choices; } public function process_feed($feed, $entry, $form) { if (! rgars($feed, 'meta/is_enable')) { return $entry; } if (! $this->initialize_gc_token()) { $this->add_feed_error(esc_html__('Google Calendar is not connected.', 'alpha-addons-for-gravity-forms'), $feed, $entry, $form); return $entry; } $calendar_id = rgars($feed, 'meta/calendar_list'); $start_date = $this->normalize_date($this->get_field_value($form, $entry, rgars($feed, 'meta/event_start_date'))); $end_date = $this->normalize_date($this->get_field_value($form, $entry, rgars($feed, 'meta/event_end_date'))); if (empty($calendar_id) || ! $start_date) { $this->add_feed_error(esc_html__('Unable to create the event: a calendar and a valid start date are required.', 'alpha-addons-for-gravity-forms'), $feed, $entry, $form); return $entry; } // All-day events use an exclusive end date, so add a day to include the chosen end date. // This also keeps single-day events (start == end) valid, which Google otherwise rejects. if (! $end_date || $end_date < $start_date) { $end_date = $start_date; } $end_date = gmdate('Y-m-d', strtotime($end_date . ' +1 day')); $data = [ 'summary' => $this->get_field_value($form, $entry, rgars($feed, 'meta/event_title')), 'location' => $this->get_field_value($form, $entry, rgars($feed, 'meta/event_location')), 'start' => [ 'date' => $start_date, ], 'end' => [ 'date' => $end_date, ], 'reminders' => [ 'useDefault' => false, ] ]; $result = ALPHAGF_Google_Calendar_API::make_request($calendar_id, $data, 'POST'); if (is_wp_error($result)) { $this->add_feed_error( sprintf( /* translators: %s: Error message returned by Google. */ esc_html__('Unable to send into google calendar: %s', 'alpha-addons-for-gravity-forms'), $result->get_error_message() ), $feed, $entry, $form ); } else { $this->add_note($entry['id'], esc_html__('Entry sent successfully to google calendar', 'alpha-addons-for-gravity-forms'), 'success'); $this->log_debug(__METHOD__ . '(): Event created successfully.'); // phpcs:ignore } return $entry; } /** * Convert a date field value to Y-m-d, which is what Google expects for all-day events. * * @return string Empty string when the value isn't a date. */ private function normalize_date($value) { $value = trim((string) $value); if ('' === $value) { return ''; } $timestamp = strtotime($value); return $timestamp ? gmdate('Y-m-d', $timestamp) : ''; } public function plugin_settings_fields() { return array( array( 'title' => esc_html__('Google Calendar Settings', 'alpha-addons-for-gravity-forms'), // translators: %1 is an opening tag, and %2 is a closing tag. 'description' => sprintf(esc_html__('Create a Google Console app to integrate your Google Calendar with Gravity Forms. %1$sClick here%2$s to create your app.', 'alpha-addons-for-gravity-forms'), '', ''), 'fields' => array( array( 'name' => 'client_id', 'label' => esc_html__('Client ID', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', // 'feedback_callback' => [$this, 'is_key_valid'], ), array( 'name' => 'client_secret', 'label' => esc_html__('Client Secret', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', ), array( 'name' => 'redirect_uri', 'label' => esc_html__('Redirect URI', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', 'default_value' => admin_url('admin.php?page=gf_settings&subview=alphagf_google_calendar'), ), array( 'name' => 'client_token', 'label' => esc_html__('Client Token Code', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', 'feedback_callback' => [$this, 'token_validation_callback'], ), array( 'name' => 'authorize_btn', 'type' => 'authorize_account', 'class' => 'medium', ), ), ), ); } public function token_validation_callback() { $code = rgpost('_gform_setting_client_token'); if (get_option('alphagf_gc_auth_verified', 0)) { return true; } if (empty($code)) { return ''; } $result = ALPHAGF_Google_Calendar_API::authenticate($code); if (! $result) { return false; } return true; } public function settings_authorize_account($field) { $settings = $this->get_plugin_settings(); $is_auth_verified = get_option('alphagf_gc_auth_verified', 0); if (! rgar($field, 'value')) { $field['value'] = esc_html__('Generate Token', 'alpha-addons-for-gravity-forms'); } $client_id = rgar($settings, 'client_id'); $redirect_uri = rgar($settings, 'redirect_uri'); if (empty($redirect_uri)) { $redirect_uri = admin_url('admin.php?page=gf_settings&subview=alphagf_google_calendar'); } $auth_url = self::ALPHAGF_OAUTH2_AUTH_URL . '?scope=' . rawurlencode(self::ALPHAGF_AUTH_CALENDAR) . '&redirect_uri=' . rawurlencode($redirect_uri) . '&response_type=code&prompt=consent&client_id=' . rawurlencode((string) $client_id) . '&access_type=offline'; if ($is_auth_verified) { $html = $this->authorized_account(); $html .= '' . esc_html__('Revoke Token', 'alpha-addons-for-gravity-forms') . ''; } else { $html = '' . esc_html__('Generate Token', 'alpha-addons-for-gravity-forms') . ''; } return $html; } public function authorized_account() { $verified = get_option('alphagf_gc_auth_verified', 0); $auth_account = get_option('alphagf_gc_auth_account', []); if ($verified && !empty($auth_account)) { $html = '
'; $html .= '
'; $html .= wp_kses_post( sprintf( /* translators: 1: User Email Address, 2: Date of authorization */ __('Authorized connection to %1$s on %2$s', 'alpha-addons-for-gravity-forms'), '' . esc_html($auth_account['email']) . '', // %1$s esc_html(wp_date('F d, Y \a\t h:i:s A', (int) rgar($auth_account, 'time'))) // %2$s ) ); $html .= '
'; $html .= '
'; return $html; } return ''; } public function revoke_token() { check_ajax_referer('alphagf_gc_nonce', 'nonce'); if (! GFCommon::current_user_can_any('gravityforms_edit_settings')) { wp_send_json_error(array('message' => esc_html__('Sorry, you are not allowed to do that.', 'alpha-addons-for-gravity-forms')), 403); } $settings = get_option('alphagf_gc_token_data', []); // Revoking the refresh token also invalidates every access token issued from it. $token = rgar($settings, 'refresh_token') ? $settings['refresh_token'] : rgar($settings, 'access_token'); $result = ALPHAGF_Google_Calendar_API::revoke_token($token); if (! $result) { wp_send_json_error(); } wp_send_json_success($result); } public function can_create_feed() { return $this->initialize_gc_token(); } public function initialize_gc_token() { $token = get_option('alphagf_gc_token_data', []); if (isset($token['access_token']) && $token['access_token'] != '') { return true; } return false; } }