Bulk Modifier Uploader Plugin code which is working correctly

PHOTO EMBED

Sat Sep 16 2023 05:16:29 GMT+0000 (Coordinated Universal Time)

Saved by @Alihaan #php

function bulk_modifier_page() {
    global $wpdb;

    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
        // Check if a CSV file is uploaded successfully
        if ($_FILES['csv_file']['error'] == UPLOAD_ERR_OK) {
            $file = $_FILES['csv_file']['tmp_name'];

            // Read the CSV file
            $csv_data = array_map('str_getcsv', file($file));
            $csv_headers = array_shift($csv_data); // Get headers

            // Define the mapping of CSV columns to database columns for wp_za_groups table
            $group_column_mapping = array(
                'group_name' => 'title',
                'priority' => 'priority',
                'apply_to' => 'apply_to',
                'author_id' => 'admin_id',
                'created_at' => 'created_at',
                'created_at_gmt' => 'created_at_gmt',
                'updated_at' => 'updated_at',
                'updated_at_gmt' => 'updated_at_gmt'
            );

            // Define the mapping of CSV columns to database columns for wp_za_types table
            $type_column_mapping = array(
                'title_name' => 'title',
                'type' => 'type',
                'status' => 'status',
                'description' => 'description',
                'required' => 'required',
                'display_description_on_expansion' => 'display_description_on_expansion',
                'hide_description' => 'hide_description',
                'tooltip_description' => 'tooltip_description',
                'created_at' => 'created_at',
                'created_at_gmt' => 'created_at_gmt',
                'updated_at' => 'updated_at',
                'updated_at_gmt' => 'updated_at_gmt'
            );

            // Define the mapping of CSV columns to database columns for wp_za_values table
            $value_column_mapping = array(
                'addons_title' => 'title',
                'price' => 'price',
                'description_value' => 'description',
                'sku' => 'sku',
            );

            // Insert data into the wp_za_groups, wp_za_types, wp_za_values, wp_za_products_to_groups, and wp_za_categories_to_groups tables
            foreach ($csv_data as $row) {
                // Prepare data for wp_za_groups table
                $group_data = array();
                foreach ($csv_headers as $key => $header) {
                    if (isset($group_column_mapping[$header])) {
                        $group_data[$group_column_mapping[$header]] = $row[$key];
                    }
                }

                // Set default values for created_at, created_at_gmt, updated_at, updated_at_gmt
                $group_data['created_at'] = current_time('mysql');
                $group_data['created_at_gmt'] = current_time('mysql', true);
                $group_data['updated_at'] = current_time('mysql');
                $group_data['updated_at_gmt'] = current_time('mysql', true);

                // Insert data into the wp_za_groups table
                $wpdb->insert($wpdb->prefix . 'za_groups', $group_data);
                $group_id = $wpdb->insert_id; // Get the group ID

                // Prepare data for wp_za_types table
                $type_data = array(
                    'group_id' => $group_id,
                    'step' => 0, // Default value
                    'accordion' => 'open', // Default value
                );

                // Map CSV columns to database columns for wp_za_types table and set default values
                foreach ($csv_headers as $key => $header) {
                    if (isset($type_column_mapping[$header])) {
                        if ($header === 'status' && !isset($type_data['status'])) {
                            // Set default status if not present in CSV
                            $type_data['status'] = 'enable';
                        } elseif ($header === 'description' && !isset($type_data['description'])) {
                            // Set default description if not present in CSV
                            $type_data['description'] = 'none';
                        } elseif ($header === 'required' && !isset($type_data['required'])) {
                            // Set default required value if not present in CSV
                            $type_data['required'] = 0;
                        } elseif ($header === 'display_description_on_expansion' && !isset($type_data['display_description_on_expansion'])) {
                            // Set default display_description_on_expansion value if not present in CSV
                            $type_data['display_description_on_expansion'] = 0;
                        } elseif ($header === 'hide_description' && !isset($type_data['hide_description'])) {
                            // Set default hide_description value if not present in CSV
                            $type_data['hide_description'] = 0;
                        } elseif ($header === 'tooltip_description' && !isset($type_data['tooltip_description'])) {
                            // Set default tooltip_description value if not present in CSV
                            $type_data['tooltip_description'] = 0;
                        } else {
                            $type_data[$type_column_mapping[$header]] = $row[$key];
                        }
                    }
                }

                // Set default values for created_at, created_at_gmt, updated_at, updated_at_gmt
                $type_data['created_at'] = current_time('mysql');
                $type_data['created_at_gmt'] = current_time('mysql', true);
                $type_data['updated_at'] = current_time('mysql');
                $type_data['updated_at_gmt'] = current_time('mysql', true);

                // Insert data into the wp_za_types table
                $wpdb->insert($wpdb->prefix . 'za_types', $type_data);
                $type_id = $wpdb->insert_id; // Get the type ID

                // Prepare data for wp_za_values table
                $value_data = array(
                    'type_id' => $type_id,
                    'step' => $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->prefix . "za_values WHERE type_id = $type_id") + 1, // Increment step
                );

                // Map CSV columns to database columns for wp_za_values table and set default values
                foreach ($csv_headers as $key => $header) {
                    if (isset($value_column_mapping[$header])) {
                        if ($header === 'description_value' && !isset($value_data['description'])) {
                            // Set default description if not present in CSV
                            $value_data['description'] = 'no value';
                        } elseif ($header === 'checked' && !isset($value_data['checked'])) {
                            // Set default checked value if not present in CSV
                            $value_data['checked'] = 0;
                        } elseif ($header === 'hide_description' && !isset($value_data['hide_description'])) {
                            // Set default hide_description value if not present in CSV
                            $value_data['hide_description'] = 0;
                        } else {
                            $value_data[$value_column_mapping[$header]] = $row[$key];
                        }
                    }
                }

                // Set default values for created_at, created_at_gmt, updated_at, updated_at_gmt
                $value_data['created_at'] = current_time('mysql');
                $value_data['created_at_gmt'] = current_time('mysql', true);
                $value_data['updated_at'] = current_time('mysql');
                $value_data['updated_at_gmt'] = current_time('mysql', true);

                // Insert data into the wp_za_values table
                $wpdb->insert($wpdb->prefix . 'za_values', $value_data);
                $value_id = $wpdb->insert_id; // Get the value ID

                // Prepare data for wp_za_products_to_groups table
                $product_names = explode(',', $row[array_search('products_name', $csv_headers)]);
                foreach ($product_names as $product_name) {
                    $product_id = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_title = '$product_name' AND post_type = 'product'");

                    if ($product_id) {
                        $product_to_group_data = array(
                            'product_id' => $product_id,
                            'group_id' => $group_id,
                        );

                        // Insert data into the wp_za_products_to_groups table
                        $wpdb->insert($wpdb->prefix . 'za_products_to_groups', $product_to_group_data);
                    }
                }

                // Prepare data for wp_za_categories_to_groups table
                $category_names = explode(',', $row[array_search('category_names', $csv_headers)]);
                foreach ($category_names as $category_name) {
                    // Get the category ID based on the category name
                    $category_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $category_name));

                    if ($category_id) {
                        $category_to_group_data = array(
                            'category_id' => $category_id,
                            'group_id' => $group_id,
                        );

                        // Insert data into the wp_za_categories_to_groups table
                        $wpdb->insert($wpdb->prefix . 'za_categories_to_groups', $category_to_group_data);
                    }
                }
            }

            echo '<div class="updated"><p>Data imported successfully!</p></div>';
        } else {
            echo '<div class="error"><p>Error uploading CSV file.</p></div>';
        }
    }

    // Display the upload form
    ?>
   
    <div class="wrap">
        <h2>Bulk Modifier Upload</h2>
        <form method="post" action="" enctype="multipart/form-data">
            <input type="file" name="csv_file">
            <input type="submit" class="button button-primary" value="Upload CSV & Apply">
        </form>
    </div>
    
    <?php
}
content_copyCOPY