__( 'AddCal', 'advanced-form-integration' ), 'tasks' => array( 'create_event' => __( 'Create Event', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_addcal_settings_tab', 10, 1 ); /** * Register AddCal settings tab. * * @param array $tabs Existing tabs. * * @return array */ function adfoin_addcal_settings_tab( $tabs ) { $tabs['addcal'] = __( 'AddCal', 'advanced-form-integration' ); return $tabs; } add_action( 'adfoin_settings_view', 'adfoin_addcal_settings_view', 10, 1 ); /** * Render AddCal settings UI. * * @param string $current_tab Current tab slug. */ function adfoin_addcal_settings_view( $current_tab ) { if ( 'addcal' !== $current_tab ) { return; } $title = __( 'AddCal', 'advanced-form-integration' ); $key = 'addcal'; $arguments = wp_json_encode( array( 'platform' => $key, 'fields' => array( array( 'key' => 'apiToken', 'label' => __( 'API Token', 'advanced-form-integration' ), 'hidden' => true, ), ), ) ); $instructions = sprintf( '

%s

  1. %s
  2. %s
  3. %s
', esc_html__( 'Generate an API token inside your AddCal dashboard and paste it below.', 'advanced-form-integration' ), esc_html__( 'Open the AddCal dashboard and create an API token from the user menu → “API token”.', 'advanced-form-integration' ), esc_html__( 'Copy the token into the API Token field and click “Add”.', 'advanced-form-integration' ), sprintf( /* translators: 1: opening anchor tag, 2: closing anchor tag. */ esc_html__( 'See the %1$sAddCal API docs%2$s for field descriptions and examples.', 'advanced-form-integration' ), '', '' ) ); echo adfoin_platform_settings_template( $title, $key, $arguments, $instructions ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } add_action( 'wp_ajax_adfoin_get_addcal_credentials', 'adfoin_get_addcal_credentials', 10, 0 ); /** * AJAX: return stored credentials. */ function adfoin_get_addcal_credentials() { adfoin_verify_nonce(); wp_send_json_success( adfoin_read_credentials( 'addcal' ) ); } add_action( 'wp_ajax_adfoin_save_addcal_credentials', 'adfoin_save_addcal_credentials', 10, 0 ); /** * AJAX: save credentials payload. */ function adfoin_save_addcal_credentials() { adfoin_verify_nonce(); $platform = isset( $_POST['platform'] ) ? sanitize_text_field( wp_unslash( $_POST['platform'] ) ) : ''; if ( 'addcal' === $platform ) { $data = isset( $_POST['data'] ) ? adfoin_array_map_recursive( 'sanitize_text_field', wp_unslash( $_POST['data'] ) ) : array(); adfoin_save_credentials( $platform, $data ); } wp_send_json_success(); } /** * Print credential dropdown options. */ function adfoin_addcal_credentials_list() { $credentials = adfoin_read_credentials( 'addcal' ); foreach ( $credentials as $credential ) { printf( '', esc_attr( $credential['id'] ), esc_html( $credential['title'] ) ); } } add_action( 'adfoin_action_fields', 'adfoin_addcal_action_fields', 10, 1 ); /** * Output the AddCal action template. */ function adfoin_addcal_action_fields() { ?> 'title', 'description' => 'description', 'location' => 'location', 'is_all_day' => 'is_all_day', 'is_virtual' => 'is_virtual', 'organiser_name' => 'organiser_name', 'organiser_email'=> 'organiser_email', 'recurrence_rule'=> 'recurrence_rule', 'series_title' => 'series_title', 'has_rsvp' => 'has_rsvp', 'rsvp_limit' => 'rsvp_limit', 'date_start' => 'date_start', 'date_end' => 'date_end', 'timezone' => 'timezone', 'busy_type' => 'busy_type', 'reminder_before'=> 'reminder_before', 'short_link' => 'short_link', 'team_uid' => 'team_uid', 'calendar_uid' => 'calendar_uid', 'calendar_name' => 'calendar_name', 'image_url' => 'image_url', 'location_url' => 'location_url', 'internal_name' => 'internal_name', 'is_draft' => 'is_draft', ); $payload = array(); foreach ( $map as $field_key => $api_key ) { if ( empty( $field_data[ $field_key ] ) ) { continue; } $value = adfoin_get_parsed_values( $field_data[ $field_key ], $posted_data ); if ( '' === $value || null === $value ) { continue; } switch ( $field_key ) { case 'is_all_day': case 'is_virtual': case 'has_rsvp': case 'is_draft': $payload[ $api_key ] = adfoin_addcal_boolean_value( $value ); break; case 'description': // AddCal renders the description as HTML, so plain-text line breaks // from textarea fields would otherwise collapse into one line. $payload[ $api_key ] = adfoin_addcal_format_description( $value ); break; case 'date_start': case 'date_end': $payload[ $api_key ] = adfoin_addcal_normalize_date( $value ); break; case 'timezone': $payload[ $api_key ] = trim( $value ); break; case 'reminder_before': $payload[ $api_key ] = max( 0, (int) $value ); break; case 'rsvp_limit': $payload[ $api_key ] = (int) $value; break; default: $payload[ $api_key ] = $value; break; } } // The API rejects events without an end, so default to one hour after start. if ( empty( $payload['date_end'] ) && ! empty( $payload['date_start'] ) ) { $start = strtotime( $payload['date_start'] ); if ( false !== $start ) { $payload['date_end'] = gmdate( 'Y-m-d H:i:s', $start + HOUR_IN_SECONDS ); } } $required = array( 'title', 'date_start', 'date_end' ); foreach ( $required as $field ) { $api_key = $map[ $field ]; if ( empty( $payload[ $api_key ] ) ) { return new WP_Error( 'adfoin_addcal_missing_field', sprintf( /* translators: %s field name. */ __( 'The %s field is required for AddCal events.', 'advanced-form-integration' ), $api_key ) ); } } return $payload; } /** * Perform HTTP request against AddCal. * * @param string $endpoint Endpoint path. * @param string $method HTTP method. * @param array $data Payload. * @param string $cred_id Credential ID. * @param array $record Record used for logging. * @param array $query Query params. * * @return array|WP_Error */ function adfoin_addcal_request( $endpoint, $method = 'POST', $data = array(), $cred_id = '', $record = array(), $query = array() ) { $credentials = adfoin_get_credentials_by_id( 'addcal', $cred_id ); $api_token = isset( $credentials['apiToken'] ) ? trim( $credentials['apiToken'] ) : ''; if ( empty( $api_token ) ) { return new WP_Error( 'adfoin_addcal_missing_token', __( 'AddCal API token is missing.', 'advanced-form-integration' ) ); } $url = 'https://addcal.co/' . ltrim( $endpoint, '/' ); if ( ! empty( $query ) ) { $url = add_query_arg( $query, $url ); } $args = array( 'method' => strtoupper( $method ), 'timeout' => 20, 'headers' => array( 'Authorization' => 'Bearer ' . $api_token, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), ); if ( in_array( $args['method'], array( 'POST', 'PUT', 'PATCH' ), true ) && ! empty( $data ) ) { $args['body'] = wp_json_encode( $data ); } $response = wp_remote_request( esc_url_raw( $url ), $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } if ( is_wp_error( $response ) ) { return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code >= 400 ) { $body = wp_remote_retrieve_body( $response ); $message = ''; if ( $body ) { $decoded = json_decode( $body, true ); if ( isset( $decoded['message'] ) ) { $message = $decoded['message']; } elseif ( isset( $decoded['error'] ) ) { $message = $decoded['error']; } else { $message = wp_strip_all_tags( $body ); } } return new WP_Error( 'adfoin_addcal_http_error', $message ? $message : __( 'AddCal API request failed.', 'advanced-form-integration' ) ); } return $response; } /** * Convert plain-text descriptions to HTML paragraphs, leaving HTML untouched. * * @param string $value Raw description. * * @return string */ function adfoin_addcal_format_description( $value ) { $value = trim( (string) $value ); if ( $value === wp_strip_all_tags( $value ) ) { return wpautop( $value ); } return $value; } /** * Normalize a date string into a format the AddCal API accepts. * * The API rejects day-first dates such as 21/11/2026, which date pickers * commonly produce, so those are converted to Y-m-d H:i:s. The value keeps * its wall-clock time; the timezone field (or calendar timezone) applies. * * @param string $value Raw date. * * @return string */ function adfoin_addcal_normalize_date( $value ) { $value = trim( (string) $value ); // ISO 8601 / Y-m-d values are already accepted as-is. if ( preg_match( '/^\d{4}-\d{2}-\d{2}/', $value ) ) { return $value; } $formats = array( 'd/m/Y H:i:s', 'd/m/Y H:i', 'd/m/Y g:i A', 'd/m/Y g:i a', 'd/m/Y', 'd.m.Y H:i:s', 'd.m.Y H:i', 'd.m.Y', 'd-m-Y H:i:s', 'd-m-Y H:i', 'd-m-Y', ); // Slash dates are ambiguous; only treat them as day-first when PHP can't // parse them month-first (e.g. 21/11/2026). if ( false === strtotime( $value ) || ! preg_match( '#^\d{1,2}/\d{1,2}/\d{4}#', $value ) ) { foreach ( $formats as $format ) { $date = DateTime::createFromFormat( '!' . $format, $value ); $errors = DateTime::getLastErrors(); if ( $date && ( ! $errors || ( 0 === $errors['warning_count'] && 0 === $errors['error_count'] ) ) ) { return $date->format( false === strpos( $format, ':' ) ? 'Y-m-d' : 'Y-m-d H:i:s' ); } } } $timestamp = strtotime( $value ); return false === $timestamp ? $value : gmdate( 'Y-m-d H:i:s', $timestamp ); } /** * Normalize boolean-ish values. * * @param mixed $value Raw value. * * @return bool */ function adfoin_addcal_boolean_value( $value ) { if ( is_bool( $value ) ) { return $value; } $value = strtolower( trim( (string) $value ) ); return in_array( $value, array( '1', 'true', 'yes', 'on' ), true ); }