diff --git a/autocart.php b/autocart.php new file mode 100644 index 0000000..6ca7d47 --- /dev/null +++ b/autocart.php @@ -0,0 +1,328 @@ + admin_url('admin-ajax.php'))); +} +add_action('wp_enqueue_scripts', 'enqueue_custom_scripts'); + + + +// Handle form submission with AJAX +function handle_autocart_form() { + // Check if it's an AJAX request and the nonce is valid + if (isset($_POST['autocart_nonce']) && wp_verify_nonce($_POST['autocart_nonce'], 'autocart_nonce')) { + $externalToken = get_option('autocart_external_token'); // Retrieve the token from where you stored it + + $searchData = array( + + 'make' => sanitize_text_field($_POST['make']), + 'minPrice'=> sanitize_text_field($_POST['minPrice']), + 'maxPrice'=> sanitize_text_field($_POST['maxPrice']), + 'minYear'=> sanitize_text_field($_POST['minYear']), + 'maxYear'=> sanitize_text_field($_POST['maxYear']), + + + ); + + $api_url = 'http://localhost:3000/api/v1/wp'; + $response = wp_remote_post($api_url, array( + 'body' => json_encode($searchData), + 'headers' => array( + 'Content-Type' => 'application/json', + 'Authorization' => $externalToken, // Include the token in the headers + ), + )); + + + if (!is_wp_error($response)) { + + // Save the response to a transient for use on the results page + set_transient('autocart_response', wp_remote_retrieve_body($response), 60); // Adjust expiration time as needed + + // if(isset($_POST['twig'])){ + // Timber::render('autocart_assets/templates/autocart-results.php', array( + // 'vehicles' => json_decode(stripslashes($response['body'])), + // )); + // exit; + // }else{ + // Send a success response + wp_send_json_success( + array( + 'redirect_url' => home_url('index.php/autocart-results/'), + 'vehicles' => json_decode(stripslashes($response['body'])) + )); + // } + + } else { + // Send an error response + wp_send_json_error(array('message' => 'Error in the AJAX request.')); + } + } else { + // Send an error response for invalid nonce + wp_send_json_error(array('message' => 'Invalid nonce.')); + } +} +add_action('wp_ajax_handle_autocart_form', 'handle_autocart_form'); +add_action('wp_ajax_nopriv_handle_autocart_form', 'handle_autocart_form'); + +// Add nonce to the form +function add_autocart_nonce() { + wp_nonce_field('autocart_nonce', 'autocart_nonce'); +} +add_action('autocart_form', 'add_autocart_nonce'); + +function autocart_copy_dir($src, $dst) { + $dir = opendir($src); + @mkdir($dst); + while (($file = readdir($dir))) { + if (($file != '.') && ($file != '..')) { + if (is_dir($src . '/' . $file)) { + autocart_copy_dir($src . '/' . $file, $dst . '/' . $file); + } else { + copy($src . '/' . $file, $dst . '/' . $file); + } + } + } + closedir($dir); +} + +function autocart_delete_dir($dir) { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != '.' && $object != '..') { + if (is_dir($dir . '/' . $object)) { + autocart_delete_dir($dir . '/' . $object); + } else { + unlink($dir . '/' . $object); + } + } + } + rmdir($dir); + } +} + + + +function autocart_remove_template_and_assets() { + $template_destination = get_template_directory() . '/autocart_assets'; + $page = get_page_by_path('autocart-results'); + if ($page) { + wp_delete_post($page->ID, true); + } + if (is_dir($template_destination)) { + autocart_delete_dir($template_destination); + } +} + +function extend_http_request_timeout( $timeout ) { + return 60; +} +add_filter( 'http_request_timeout', 'extend_http_request_timeout' ); + +function autocart_copy_template_to_theme() { + $template_source = plugin_dir_path(__FILE__) . 'autocart_assets'; + $template_destination = get_template_directory() . '/autocart_assets'; + if (!is_dir($template_destination)) { + mkdir($template_destination, 0755); + } + autocart_copy_dir($template_source, $template_destination); + $page = array( + 'post_title' => 'Autocart Results', + 'post_content' => '', + 'post_status' => 'publish', + 'post_type' => 'page', + 'post_name' => 'autocart-results', + ); + $page_id = wp_insert_post($page); + if ($page_id && !is_wp_error($page_id)) { + update_post_meta($page_id, '_wp_page_template', 'autocart_assets/templates/autocart-results.php'); + update_post_meta($page_id, '_wp_page_template_visibility', 'private'); + } + flush_rewrite_rules(); +} + +function get_token(){ + $domainName = $_SERVER['SERVER_NAME']; // + $response = wp_remote_get('http://localhost:3000/api/v1/wp/get-token?domain=' . urlencode($domainName)); + if (is_wp_error($response)) { + $error_message = 'Warning: Autocart activation failed. Unable to obtain the required token. Please check your server configuration and try again. Autocart has been deactivated. ' . $response->get_error_message(); + update_option('autocart_activation_error', $error_message); + } else { + $token = json_decode(wp_remote_retrieve_body($response), true); + if (isset($token['token'])) { + update_option('autocart_external_token', $token['token']); + } + } + +} + + +function autocart_activate(){ + autocart_copy_template_to_theme(); + get_token(); + + +} +// Activation Hook +register_activation_hook(__FILE__, 'autocart_activate'); + +// Deactivation Hook +register_deactivation_hook(__FILE__, 'autocart_remove_template_and_assets'); + + + + +add_action('admin_init', 'autocart_check_activation_status'); + +function autocart_check_activation_status() { +require_once plugin_dir_path(__FILE__) . 'timber-check.php'; + + $error_message = get_option('autocart_activation_error'); + $check_dependencies = get_option('autocart_dependencies'); + if ($error_message || !$check_dependencies) { + add_action('admin_notices', function () use ($error_message, $check_dependencies) { + if($error_message) { ?> +
+

+
+ sanitize_text_field($_POST['price']), + 'year' => sanitize_text_field($_POST['year']), + 'model' => sanitize_text_field($_POST['model']), + ); + + $api_url = 'http://localhost:3000/api/v1/wp'; + $response = wp_remote_post($api_url, array( + 'body' => json_encode($searchData), + 'headers' => array( + 'Content-Type' => 'application/json', + 'Authorization' => $externalToken, + ), + )); + + // Check if the request was successful + if (!is_wp_error($response)) { + + set_transient('autocart_response', wp_remote_retrieve_body($response), 60); + $data = $response['body']; + echo json_encode($data); + + wp_send_json_success( + array( + + 'vehicles' => json_decode(stripslashes($response['body'])) + )); + exit(); + + } else { + wp_send_json_error(array('message' => 'Error in the AJAX request.')); + } + + + echo json_encode($filtered_data); + + wp_die(); // Terminate AJAX request +} + +add_action('wp_ajax_handle_autocart_filters', 'handle_autocart_filters'); +add_action('wp_ajax_nopriv_handle_autocart_filters', 'handle_autocart_filters'); + + +function enqueue_custom_styles() { + if (is_page_template('autocart_assets/templates/autocart-results.php')) { + wp_enqueue_style('custom-style', get_template_directory_uri() . '/autocart_assets/css/filter-panel.css'); + } +} + + +add_action('wp_enqueue_scripts', 'enqueue_custom_styles'); + + +function enqueue_material_components() { + wp_enqueue_style('material-components', 'https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css'); + wp_enqueue_script('material-components', 'https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js', array('jquery'), null, true); + +} +add_action('wp_enqueue_scripts', 'enqueue_material_components'); + + + diff --git a/custom-twig-extension.php b/custom-twig-extension.php new file mode 100644 index 0000000..983d37e --- /dev/null +++ b/custom-twig-extension.php @@ -0,0 +1,21 @@ + +
+

Custom Inventory Search plugin requires the Timber plugin to be installed. To install Timber, you can use the following command:', 'custom-inventory-search'); ?>

+
composer require timber/timber 
+
+ + + \ No newline at end of file