prefix . 'ai_app_onsite_app_properties'; $appropriates = $wpdb->get_results("SELECT * FROM $table_appropriate", ARRAY_A); if (empty($appropriates)) { // error_log('No data found in app properties'); return; } $is_preview = isset($_POST['is_preview_button']) && $_POST['is_preview_button'] === 'true'; $appropriates[0]['title_color'] = ($is_preview && isset($_POST['title_color'])) ? $_POST['title_color'] : $appropriates[0]['title_color']; $appropriates[0]['font_color'] = ($is_preview && isset($_POST['font_color'])) ? $_POST['font_color'] : $appropriates[0]['font_color']; $appropriates[0]['bg_color'] = ($is_preview && isset($_POST['background_color'])) ? $_POST['background_color'] : $appropriates[0]['bg_color']; $appropriates[0]['app_corner'] = ($is_preview && isset($_POST['app_corner'])) ? $_POST['app_corner'] : $appropriates[0]['app_corner']; $appropriates[0]['app_height'] = ($is_preview && isset($_POST['app_height'])) ? $_POST['app_height'] : $appropriates[0]['app_height']; $titleColors = json_decode($appropriates[0]['title_color'], true); $fontColors = json_decode($appropriates[0]['font_color'], true); $bgColors = json_decode($appropriates[0]['bg_color'], true); $appCorner = $appropriates[0]['app_corner']; $appHeight = $appropriates[0]['app_height']; $title_color_selected = null; $font_color_selected = null; $bg_color_selected = null; $app_corner_selected = null; $app_height_selected = null; if(!is_null($titleColors)) { foreach ($titleColors as $color) { if (isset($color['title_color_selected'])) { $title_color_selected = $color['title_color_selected']; break; } } } if(!is_null($fontColors)) { foreach ($fontColors as $color) { if (isset($color['font_color_selected'])) { $font_color_selected = $color['font_color_selected']; break; } } } if(!is_null($bgColors)) { foreach ($bgColors as $color) { if (isset($color['bg_color_selected'])) { $bg_color_selected = $color['bg_color_selected']; break; } } } if(!is_null($appCorner)) { $app_corner_selected = $appCorner; } if(!is_null($appHeight)) { $app_height_selected = $appHeight; } // Use POST if provided, else fallback to DB values $title_color = $title_color_selected; $font_color = $font_color_selected; $background_color = $bg_color_selected; $app_corner = $app_corner_selected; $app_height = $app_height_selected; $this->appProperty = [ 'app_name' => $appropriates[0]['app_name'], 'app_logo' => $appropriates[0]['app_logo'], 'app_disclaimer' => $appropriates[0]['app_disclaimer'], 'app_description' => $appropriates[0]['app_description'], 'submit_button_txt' => $appropriates[0]['submit_button_txt'], 'title_color' => 'style="color:' . $title_color . ';"', 'font_color' => 'style="color:' . $font_color . ';"', 'small_font_color' => 'style="color:' . $font_color . '; opacity:0.5;"', 'app_description_color' => 'style="color:' . $font_color . '; opacity:0.5; text-align: center; font-size: 20px;"', 'background_color' => $background_color, 'btn_color' => 'style="background-color:' . $title_color . ';"' ]; switch ($app_corner) { case 'sharp-corner': $this->appProperty['app_corner'] = 'border-radius: 0px;'; break; case 'soft-corner': $this->appProperty['app_corner'] = 'border-radius: 10px;'; break; case 'round-corner': $this->appProperty['app_corner'] = 'border-radius: 25px;'; break; default: $this->appProperty['app_corner'] = 'border-radius: 10px;'; break; } switch ($app_height) { case 'no-height': $this->appProperty['app_height'] = 'box-shadow: none;'; break; case 'slight-lift': $this->appProperty['app_height'] = 'box-shadow: 0px 8px 12px rgba(0, 0, 0, 0.2);'; break; case 'more-lift': $this->appProperty['app_height'] = 'box-shadow: 0px 12px 18px rgba(0, 0, 0, 0.3);'; break; default: $this->appProperty['app_height'] = 'box-shadow: 0px 8px 12px rgba(0, 0, 0, 0.2);'; break; } //New Logic $appName = isset($appropriates[0]['app_name']) && !empty($appropriates[0]['app_name']) ? $appropriates[0]['app_name'] : 'aiapp'; if ($appName == "aiapp") { $appName = $appropriates[0]['app_name_show']; } if ($appName !== "aiapp") { $shortCodeGenrated = $appropriates[0]['short_code_genrated']; $words = explode(' ', $appName); $words = array_map(function ($word) { return strtolower($word); }, $words); $appNameWithShortCode = implode('_', $words) . '_aiao'; if ($shortCodeGenrated === 'NO' || $shortCodeGenrated === '_aiao') { // Register shortcode add_shortcode($appNameWithShortCode, array($this, 'AI_APP_ONSITE_Create_OpenAi_App_Form_Shortcode')); $wpdb->update( $table_appropriate, array( 'short_code_genrated' => $appNameWithShortCode, ), array('app_id' => $appropriates[0]['app_id']), // where clause: update based on the existing row's ID array('%s'), array('%d') // where format ); } else { add_shortcode($shortCodeGenrated, array($this, 'AI_APP_ONSITE_Create_OpenAi_App_Form_Shortcode')); } } //End New Logic } // AJAX callback function public function ai_app_onsite_create_app_preview_form() { global $wpdb; $table_name_settings = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; $table_name_app_properties = $wpdb->prefix . 'ai_app_onsite_app_properties'; // if $this->appProperty is not initialized return if (empty($this->appProperty)) { wp_send_json_error('App properties are not initialized.'); return; } // Check if the table exists if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_settings'") != $table_name_settings) { $results = array('status' => '404', 'message' => 'No App found'); wp_send_json($results); } // Check if the table has rows $rows_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name_settings"); if ($rows_count == 0) { $results = array('status' => '404', 'message' => 'No rows found in the table.'); wp_send_json($results); } // Fetch the data $results = $wpdb->get_results("SELECT * FROM $table_name_settings ORDER BY field_position ASC", ARRAY_A); $table_appropriate = $wpdb->prefix . 'ai_app_onsite_app_properties'; $appropriates = $wpdb->get_results("SELECT * FROM $table_appropriate", ARRAY_A); $app_logo = isset($appropriates[0]['app_logo']) && !empty($appropriates[0]['app_logo']) ? $appropriates[0]['app_logo'] : ''; $app_logo_image_url = wp_get_attachment_url($app_logo); $form_width_size = $results[0]['app_width'] ?? '70'; $form_style = 'style="background-color:' . $this->appProperty['background_color'] . '; max-width: ' . $form_width_size . '% ; '.$this->appProperty['app_corner'].' ; '.$this->appProperty['app_height'].'"'; $formHTML = '
'; // App Logo start if (!empty($app_logo_image_url)) { $formHTML .= '
'; $formHTML .= 'App Logo'; $formHTML .= '
'; } // App Logo End //short code name $formHTML .= ''; $formHTML .= '

appProperty['title_color'] . '>' . $this->appProperty['app_name'] . '

'; $formHTML .= '

appProperty['app_description_color'] . '>' . $this->appProperty['app_description'] . '

'; $formHTML .= wp_nonce_field("ai_app_onsite_properties_nonce", "ai_app_onsite_properties_nonce_field", true, false); $formHTML .= ''; // Add form fields foreach ($results as $field) { $formHTML .= '
'; $formHTML .= ''; $formHTML .= '
'; if ($field['field_type'] === 'textarea') { $formHTML .= ''; $formHTML .= '

'; $formHTML .= '

appProperty['small_font_color'] . '>

'; } elseif ($field['field_type'] === 'dropdown') { $formHTML .= ''; $formHTML .= '

'; $formHTML .= '

appProperty['small_font_color'] . '>' . $field['field_sample_text'] . '

'; } elseif ($field['field_type'] === 'textbox') { $formHTML .= 'appProperty['font_color'] . '>'; $formHTML .= '

'; /* $formHTML .= '

appProperty['small_font_color'].'>'.$field['field_sample_text'].'

';*/ } else { $formHTML .= 'appProperty['font_color'] . ' oninput="this.value = this.value.slice(0, ' . $field['field_maxchar'] . ');">'; $formHTML .= '

'; /* $formHTML .= '

appProperty['small_font_color'].'>'.$field['field_sample_text'].'

';*/ } $formHTML .= '
'; } // Add buttons $formHTML .= '
'; $formHTML .= ''; $formHTML .= '
'; $formHTML .= '

appProperty['small_font_color'] . '>Disclaimer: ' . $this->appProperty['app_disclaimer'] . '

'; $formHTML .= '

Built with AIappOnsite

'; // Close form tag $formHTML .= '
'; $is_preview = isset($_POST['is_preview_button']) && $_POST['is_preview_button'] === 'true'; if($is_preview){ $appropriates[0]['title_color'] = stripslashes($_POST['title_color']); $appropriates[0]['font_color'] = stripslashes($_POST['font_color']); $appropriates[0]['bg_color'] = stripslashes($_POST['background_color']); $appropriates[0]['app_corner'] = sanitize_text_field($_POST['app_corner']); $appropriates[0]['app_height'] = sanitize_text_field($_POST['app_height']); } // Return the data wp_send_json([ 'form_html' => $formHTML, 'appropriates' => $appropriates ]); // Don't forget to exit exit; } // AJAX callback function Update preview styles function ai_app_onsite_update_style_properties_only_callback() { global $wpdb; // Validate AJAX and nonce if ( !isset($_POST['action']) || sanitize_text_field(wp_unslash($_POST['action'])) !== 'ai_app_onsite_update_style_properties_only' || !isset($_POST['ai_app_onsite_app_properties_nonce_field']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ai_app_onsite_app_properties_nonce_field'])), 'ai_app_onsite_app_properties_nonce') ) { wp_send_json_error('Unauthorized request'); } // Get sanitized values $titleColorsJson = isset($_POST['titleColors']) ? stripslashes(sanitize_text_field(wp_unslash($_POST['titleColors']))) : ''; $fontColorsJson = isset($_POST['fontColor']) ? stripslashes(sanitize_text_field(wp_unslash($_POST['fontColor']))) : ''; $bgColorsJson = isset($_POST['bgColor']) ? stripslashes(sanitize_text_field(wp_unslash($_POST['bgColor']))) : ''; $app_corner = isset($_POST['app-corner']) ? sanitize_text_field(wp_unslash($_POST['app-corner'])) : ''; $app_height = isset($_POST['app-height']) ? sanitize_text_field(wp_unslash($_POST['app-height'])) : ''; $table_name = $wpdb->prefix . 'ai_app_onsite_app_properties'; $existing_settings = $wpdb->get_row("SELECT * FROM $table_name"); if ($existing_settings) { $updated = $wpdb->update( $table_name, array( 'title_color' => $titleColorsJson, 'font_color' => $fontColorsJson, 'bg_color' => $bgColorsJson, 'app_corner' => $app_corner, 'app_height' => $app_height ), array('id' => $existing_settings->id), array('%s', '%s', '%s', '%s', '%s'), array('%d') ); if ($wpdb->last_error) { wp_send_json_error('Error saving data: ' . $wpdb->last_error); } wp_send_json_success('Settings updated successfully.'); } else { wp_send_json_error('No existing settings found to update.'); } } // Define shortcode function public function ai_app_onsite_create_openAi_app_form_shortcode(): string { global $wpdb; $table_appropriate = $wpdb->prefix . 'ai_app_onsite_app_properties'; $appropriates = $wpdb->get_results("SELECT * FROM $table_appropriate", ARRAY_A); // Make AJAX call to retrieve form HTML $form_html_json = wp_remote_get(admin_url('admin-ajax.php') . '?action=ai_app_onsite_create_app_preview_form'); // Check if request was successful and data is available if (!is_wp_error($form_html_json) && $form_html_json['response']['code'] == 200) { // Decode JSON response $form_html = json_decode($form_html_json['body'], true); $form_html = $form_html['form_html']; // Check if decoding was successful if ($form_html !== null) { // Use $this->appProperty here $form_html .= '
' . $this->appProperty['app_name'] . ' Feedback:

appProperty['small_font_color'] . '>Disclaimer: ' . $this->appProperty['app_disclaimer'] . '

'; $form_html .= ''; return $form_html; // Return decoded form HTML } else { return 'Error decoding JSON'; // Return error message if decoding failed } } else { return 'Error fetching form HTML'; // Return error message if AJAX request failed } } public static function ai_app_onsite_get_app_properties() { global $wpdb; $table_name_settings = $wpdb->prefix . 'ai_app_onsite_app_properties'; // Check if the table exists if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_settings'") != $table_name_settings) { //die('Table does not exist.'); $results = array('status' => '404', 'message' => 'No rows found in the table.'); wp_send_json($results); } // Check if the table has rows $rows_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name_settings"); if ($rows_count == 0) { //die('No rows found in the table.'); $results = array('status' => '404', 'message' => 'No rows found in the table.'); wp_send_json($results); } // Fetch the data $results = $wpdb->get_results("SELECT * FROM $table_name_settings", ARRAY_A); $generateURLForID = $results[0]['app_logo']; $appLogoImagePreview = wp_get_attachment_url($generateURLForID); if ($appLogoImagePreview) { // Add the image preview URL to the $results array $results[0]['appLogoImagePreview'] = $appLogoImagePreview; } else { $results[0]['appLogoImagePreview'] = 'No preview available'; // Handle cases where no URL is found } wp_send_json($results); // Don't forget to exit exit; } } new AI_APP_ONSITE_App_Preview_Handler(); // Instantiate the class }