KOK - MANAGER
Edit File: functions.php
<?php /** * Astra functions and definitions * * @link https://developer.wordpress.org/themes/basics/theme-functions/ * * @package Astra * @since 1.0.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } /** * Define Constants */ define( 'ASTRA_THEME_VERSION', '3.7.9' ); define( 'ASTRA_THEME_SETTINGS', 'astra-settings' ); define( 'ASTRA_THEME_DIR', trailingslashit( get_template_directory() ) ); define( 'ASTRA_THEME_URI', trailingslashit( esc_url( get_template_directory_uri() ) ) ); /** * Minimum Version requirement of the Astra Pro addon. * This constant will be used to display the notice asking user to update the Astra addon to the version defined below. */ define( 'ASTRA_EXT_MIN_VER', '3.6.3' ); /** * Setup helper functions of Astra. */ require_once ASTRA_THEME_DIR . 'inc/core/class-astra-theme-options.php'; require_once ASTRA_THEME_DIR . 'inc/core/class-theme-strings.php'; require_once ASTRA_THEME_DIR . 'inc/core/common-functions.php'; require_once ASTRA_THEME_DIR . 'inc/core/class-astra-icons.php'; /** * Update theme */ require_once ASTRA_THEME_DIR . 'inc/theme-update/class-astra-theme-update.php'; require_once ASTRA_THEME_DIR . 'inc/theme-update/astra-update-functions.php'; require_once ASTRA_THEME_DIR . 'inc/theme-update/class-astra-theme-background-updater.php'; require_once ASTRA_THEME_DIR . 'inc/theme-update/class-astra-pb-compatibility.php'; /** * Fonts Files */ require_once ASTRA_THEME_DIR . 'inc/customizer/class-astra-font-families.php'; if ( is_admin() ) { require_once ASTRA_THEME_DIR . 'inc/customizer/class-astra-fonts-data.php'; } require_once ASTRA_THEME_DIR . 'inc/lib/webfont/class-astra-webfont-loader.php'; require_once ASTRA_THEME_DIR . 'inc/customizer/class-astra-fonts.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/custom-menu-old-header.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/container-layouts.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/astra-icons.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/block-editor-compatibility.php'; require_once ASTRA_THEME_DIR . 'inc/core/class-astra-walker-page.php'; require_once ASTRA_THEME_DIR . 'inc/core/class-astra-enqueue-scripts.php'; require_once ASTRA_THEME_DIR . 'inc/core/class-gutenberg-editor-css.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/inline-on-mobile.php'; require_once ASTRA_THEME_DIR . 'inc/dynamic-css/content-background.php'; require_once ASTRA_THEME_DIR . 'inc/class-astra-dynamic-css.php'; require_once ASTRA_THEME_DIR . 'inc/class-astra-global-palette.php'; /** * Custom template tags for this theme. */ require_once ASTRA_THEME_DIR . 'inc/core/class-astra-attr.php'; require_once ASTRA_THEME_DIR . 'inc/template-tags.php'; require_once ASTRA_THEME_DIR . 'inc/widgets.php'; require_once ASTRA_THEME_DIR . 'inc/core/theme-hooks.php'; require_once ASTRA_THEME_DIR . 'inc/admin-functions.php'; require_once ASTRA_THEME_DIR . 'inc/core/sidebar-manager.php'; /** * Markup Functions */ require_once ASTRA_THEME_DIR . 'inc/markup-extras.php'; require_once ASTRA_THEME_DIR . 'inc/extras.php'; require_once ASTRA_THEME_DIR . 'inc/blog/blog-config.php'; require_once ASTRA_THEME_DIR . 'inc/blog/blog.php'; require_once ASTRA_THEME_DIR . 'inc/blog/single-blog.php'; /** * Markup Files */ require_once ASTRA_THEME_DIR . 'inc/template-parts.php'; require_once ASTRA_THEME_DIR . 'inc/class-astra-loop.php'; require_once ASTRA_THEME_DIR . 'inc/class-astra-mobile-header.php'; /** * Functions and definitions. */ require_once ASTRA_THEME_DIR . 'inc/class-astra-after-setup-theme.php'; // Required files. require_once ASTRA_THEME_DIR . 'inc/core/class-astra-admin-helper.php'; require_once ASTRA_THEME_DIR . 'inc/schema/class-astra-schema.php'; if ( is_admin() ) { /** * Admin Menu Settings */ require_once ASTRA_THEME_DIR . 'inc/core/class-astra-admin-settings.php'; require_once ASTRA_THEME_DIR . 'inc/lib/astra-notices/class-astra-notices.php'; } /** * Metabox additions. */ require_once ASTRA_THEME_DIR . 'inc/metabox/class-astra-meta-boxes.php'; require_once ASTRA_THEME_DIR . 'inc/metabox/class-astra-meta-box-operations.php'; /** * Customizer additions. */ require_once ASTRA_THEME_DIR . 'inc/customizer/class-astra-customizer.php'; /** * Astra Modules. */ require_once ASTRA_THEME_DIR . 'inc/modules/related-posts/class-astra-related-posts.php'; /** * Compatibility */ require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-gutenberg.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-jetpack.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/woocommerce/class-astra-woocommerce.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/edd/class-astra-edd.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/lifterlms/class-astra-lifterlms.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/learndash/class-astra-learndash.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-beaver-builder.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-bb-ultimate-addon.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-contact-form-7.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-visual-composer.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-site-origin.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-gravity-forms.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-bne-flyout.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-ubermeu.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-divi-builder.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-amp.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-yoast-seo.php'; require_once ASTRA_THEME_DIR . 'inc/addons/transparent-header/class-astra-ext-transparent-header.php'; require_once ASTRA_THEME_DIR . 'inc/addons/breadcrumbs/class-astra-breadcrumbs.php'; require_once ASTRA_THEME_DIR . 'inc/addons/heading-colors/class-astra-heading-colors.php'; require_once ASTRA_THEME_DIR . 'inc/builder/class-astra-builder-loader.php'; // Elementor Compatibility requires PHP 5.4 for namespaces. if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) { require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-elementor.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-elementor-pro.php'; require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-web-stories.php'; } // Beaver Themer compatibility requires PHP 5.3 for anonymus functions. if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) { require_once ASTRA_THEME_DIR . 'inc/compatibility/class-astra-beaver-themer.php'; } require_once ASTRA_THEME_DIR . 'inc/core/markup/class-astra-markup.php'; add_filter('gform_phone_formats', 'ind_phone_format', 10, 2); function ind_phone_format($phone_formats) { $phone_formats['IND'] = array( 'label' => 'IND', 'mask' => '9999999999', // Only 10 digits 'regex' => '/^(?:\+91)?[1-9][0-9]{9}$/', // Allows "+91" at the start 'instruction' => 'Please enter a valid 10-digit phone number without a leading zero.', ); return $phone_formats; } function get_google_access_token() { $jsonKey = file_get_contents(__DIR__ . '/service-account.json'); $data = json_decode($jsonKey, true); $jwtHeader = base64_encode(json_encode(['alg'=>'RS256','typ'=>'JWT'])); $jwtClaim = base64_encode(json_encode([ "iss" => $data['client_email'], "scope" => "https://www.googleapis.com/auth/spreadsheets", "aud" => "https://oauth2.googleapis.com/token", "exp" => time() + 3600, "iat" => time() ])); $signatureInput = $jwtHeader . '.' . $jwtClaim; openssl_sign($signatureInput, $signature, openssl_pkey_get_private($data['private_key']), 'sha256'); $jwtSignature = base64_encode($signature); $jwt = $jwtHeader . '.' . $jwtClaim . '.' . $jwtSignature; // Request Access Token $response = wp_remote_post("https://oauth2.googleapis.com/token", [ 'body' => [ 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt ] ]); $body = json_decode(wp_remote_retrieve_body($response), true); return $body['access_token'] ?? null; } function convert_file_fields_to_hyperlink($values, $form) { foreach ($form['fields'] as $index => $field) { // Check if field type is 'fileupload' if ($field->type === 'fileupload' && !empty($values[$index])) { $fileUrls = is_array($values[$index]) ? $values[$index] : explode(',', $values[$index]); $hyperlinks = []; foreach ($fileUrls as $fileUrl) { $filename = basename($fileUrl); $hyperlinks[] = '=HYPERLINK("' . $fileUrl . '","' . $filename . '")'; } // Join multiple files with line breaks $values[$index] = implode("\n", $hyperlinks); } } return $values; } function add_row_to_google_sheet($spreadsheetId, $range, $values, $form = null) { $accessToken = get_google_access_token(); // Convert file upload fields to HYPERLINK formulas if ($form) { $values = convert_file_fields_to_hyperlink($values, $form); } $url = "https://sheets.googleapis.com/v4/spreadsheets/$spreadsheetId/values/$range:append?valueInputOption=USER_ENTERED"; $body = json_encode(["values" => [$values]]); $response = wp_remote_post($url, [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json', ], 'body' => $body, ]); return wp_remote_retrieve_body($response); } // Hook: After Gravity Form Submission (Form ID = 6) add_action('gform_after_submission_6', 'send_gform_entry_to_google_sheets', 10, 2); function send_gform_entry_to_google_sheets($entry, $form) { $spreadsheetId = "1yXnPGU-zxyG03z0BVwZ0mxV5qugcng6nhKonbay1ifc"; $range = "BCIFormResponses!A1"; // Convert list fields to readable string $list_field_36 = maybe_unserialize(rgar($entry, '36')); $list_field_36_str = is_array($list_field_36) ? implode(', ', $list_field_36) : $list_field_36; $list_field_37 = maybe_unserialize(rgar($entry, '37')); $list_field_37_str = is_array($list_field_37) ? implode(', ', $list_field_37) : $list_field_37; // Make file uploads clickable links $company_profile_file = rgar($entry, '10') ? '=HYPERLINK("' . rgar($entry, '10') . '","Company Profile")' : ''; $product_catalog_file = rgar($entry, '13') ? '=HYPERLINK("' . rgar($entry, '13') . '","Product Catalogue")' : ''; $values = [ date('Y-m-d H:i:s'), rgar($entry, '3'), // full_name rgar($entry, '5'), // mobile rgar($entry, '6'), // email rgar($entry, '35'), // linkedin rgar($entry, '30'), // bcibatch rgar($entry, '25'), // state rgar($entry, '26'), // city rgar($entry, '4'), // company_name rgar($entry, '9'), // website $company_profile_file, // clickable Company Profile rgar($entry, '11'), // industry $list_field_36_str, // list field $product_catalog_file, // clickable Product / Service Catalogue rgar($entry, '15'), // Alternate Contact Person Name rgar($entry, '17'), // Alternate Person Contact No. rgar($entry, '19'), // Alternate Person Email $list_field_37_str, // list field ]; add_row_to_google_sheet($spreadsheetId, $range, $values); } add_action('gform_after_submission_9', 'send_gform9_entry_to_google_sheets', 10, 2); function send_gform9_entry_to_google_sheets($entry, $form) { $spreadsheetId = "1q6xVQVJrjESU3qK6bostbk343MTl0GhfJRHBvX5guok"; $range = "BNIFormResponses!A1"; // Convert list fields to readable string $list_field_36 = maybe_unserialize(rgar($entry, '36')); $list_field_36_str = is_array($list_field_36) ? implode(', ', $list_field_36) : $list_field_36; $list_field_37 = maybe_unserialize(rgar($entry, '37')); $list_field_37_str = is_array($list_field_37) ? implode(', ', $list_field_37) : $list_field_37; // Make file uploads clickable links $company_profile_file = rgar($entry, '10') ? '=HYPERLINK("' . rgar($entry, '10') . '","Company Profile")' : ''; $product_catalog_file = rgar($entry, '13') ? '=HYPERLINK("' . rgar($entry, '13') . '","Product Catalogue")' : ''; $values = [ date('Y-m-d H:i:s'), rgar($entry, '3'), // full_name rgar($entry, '5'), // mobile rgar($entry, '6'), // email rgar($entry, '35'), // linkedin rgar($entry, '38'), // bnibatch rgar($entry, '25'), // state rgar($entry, '26'), // city rgar($entry, '4'), // company_name rgar($entry, '9'), // website $company_profile_file, // clickable Company Profile rgar($entry, '11'), // industry $list_field_36_str, // list field $product_catalog_file, // clickable Product / Service Catalogue rgar($entry, '15'), // Alternate Contact Person Name rgar($entry, '17'), // Alternate Person Contact No. rgar($entry, '19'), // Alternate Person Email $list_field_37_str, // list field ]; // add_row_to_google_sheet($spreadsheetId, $range, $values, $form); add_row_to_google_sheet($spreadsheetId, $range, $values); } add_action('wp_ajax_save_partial_entry', 'save_partial_entry'); add_action('wp_ajax_nopriv_save_partial_entry', 'save_partial_entry'); function save_partial_entry() { if (!isset($_POST['form_data']) || !isset($_POST['form_id'])) { wp_send_json_error("Missing data"); } $form_id = intval($_POST['form_id']); $form_data = $_POST['form_data']; $timestamp = date('Y-m-d H:i:s'); $full_name = isset($form_data['input_3']) ? sanitize_text_field($form_data['input_3']) : ''; $mobile = isset($form_data['input_5']) ? sanitize_text_field($form_data['input_5']) : ''; $email = isset($form_data['input_6']) ? sanitize_email($form_data['input_6']) : ''; $linkedin = isset($form_data['input_35']) ? esc_url_raw($form_data['input_35']) : ''; $bcibatch = isset($form_data['input_30']) ? sanitize_text_field($form_data['input_30']) : ''; $bnibatch = isset($form_data['input_38']) ? sanitize_text_field($form_data['input_38']) : ''; $state = isset($form_data['input_25']) ? sanitize_text_field($form_data['input_25']) : ''; $city = isset($form_data['input_26']) ? sanitize_text_field($form_data['input_26']) : ''; $values = [[$timestamp, $full_name, $mobile, $email, $linkedin, $bcibatch, $state, $city]]; // ✅ Different sheet per form if ($form_id == 6) { $spreadsheetId = "1yXnPGU-zxyG03z0BVwZ0mxV5qugcng6nhKonbay1ifc"; $range = "BCIPartialData!A1"; $values = [[ $timestamp, $full_name, $mobile, $email, $linkedin, $bcibatch, $state, $city ]]; } elseif ($form_id == 9) { $spreadsheetId = "1q6xVQVJrjESU3qK6bostbk343MTl0GhfJRHBvX5guok"; // <-- Replace $range = "BNIPartialData!A1"; // <-- Replace $values = [[ $timestamp, $full_name, $mobile, $email, $linkedin, $bnibatch, // ✅ Only Form 9 has this $state, $city ]]; } else { wp_send_json_error("Form not configured"); } try { putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/service-account.json'); $client = new \Google_Client(); $client->useApplicationDefaultCredentials(); $client->addScope(\Google_Service_Sheets::SPREADSHEETS); $service = new \Google_Service_Sheets($client); $body = new \Google_Service_Sheets_ValueRange(['values' => $values]); $params = ['valueInputOption' => 'RAW']; $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params); wp_send_json_success("Partial entry saved for Form {$form_id}"); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } } add_filter('gform_field_validation', 'disable_center_validation', 10, 6); function disable_center_validation($result, $value, $form, $field) { $center_field_ids = [26]; // Check if the field ID is one of the center field IDs if (in_array($field->id, $center_field_ids)) { $result['is_valid'] = true; // Disable validation } return $result; } /** * Load deprecated functions */ require_once ASTRA_THEME_DIR . 'inc/core/deprecated/deprecated-filters.php'; require_once ASTRA_THEME_DIR . 'inc/core/deprecated/deprecated-hooks.php'; require_once ASTRA_THEME_DIR . 'inc/core/deprecated/deprecated-functions.php';