Upload files to "/"
This commit is contained in:
328
autocart.php
Normal file
328
autocart.php
Normal file
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Autocart
|
||||
Description: A plugin for displaying a search form and handling external requests.
|
||||
*/
|
||||
|
||||
// require_once plugin_dir_path(__FILE__) . 'custom-twig-extension.php';
|
||||
|
||||
// Add shortcode for the form
|
||||
|
||||
$vendor_path = ABSPATH . '/vendor/autoload.php';
|
||||
if (file_exists($vendor_path)) {
|
||||
require_once $vendor_path;
|
||||
|
||||
if (!class_exists('Timber\Timber')) {
|
||||
// Timber is not active.. so display a notice
|
||||
update_option('autocart_dependencies', false);
|
||||
|
||||
} else {
|
||||
Timber\Timber::init();
|
||||
update_option('autocart_dependencies', true);
|
||||
|
||||
}
|
||||
} else {
|
||||
update_option('autocart_dependencies', false);
|
||||
}
|
||||
|
||||
function autocart_shortcode() {
|
||||
ob_start();
|
||||
include plugin_dir_path(__FILE__) . 'autocart_assets/templates/search-form.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
add_shortcode('autocart', 'autocart_shortcode');
|
||||
|
||||
// Enqueue jQuery, index.js and classes
|
||||
function enqueue_custom_scripts() {
|
||||
wp_enqueue_script('jquery');
|
||||
wp_enqueue_script('index', get_template_directory_uri() . '/autocart_assets/js/index.js', array('jquery'), '1.0', true);
|
||||
wp_enqueue_script('vehicle-thumbnail', get_template_directory_uri() . '/autocart_assets/js/VehicleThumbnail.js', array(), '1.0', true);
|
||||
wp_enqueue_script('make-filter', get_template_directory_uri() . '/autocart_assets/js/MakeFilter.js', array(), '1.0', true);
|
||||
wp_enqueue_script('model-filter', get_template_directory_uri() . '/autocart_assets/js/ModelFilter.js', array(), '1.0', true);
|
||||
wp_enqueue_script('trim-filter', get_template_directory_uri() . '/autocart_assets/js/TrimFilter.js', array(), '1.0', true);
|
||||
wp_enqueue_script('data-store', get_template_directory_uri() . '/autocart_assets/js/DataStore.js', array(), '1.0', true);
|
||||
wp_enqueue_script('applied-filters', get_template_directory_uri() . '/autocart_assets/js/AppliedFilters.js', array(), '1.0', true);
|
||||
wp_enqueue_script('filter-manager', get_template_directory_uri() . '/autocart_assets/js/FilterManager.js', array(), '1.0', true);
|
||||
|
||||
// Pass the ajax_url to script.js
|
||||
wp_localize_script('index', 'ajax_object', array('ajax_url' => 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) { ?>
|
||||
<div class="notice notice-error is-dismissible">
|
||||
<p><?php echo esc_html($error_message); ?></p>
|
||||
</div>
|
||||
<?php
|
||||
// Deactivate the plugin to prevent activation
|
||||
deactivate_plugins(plugin_basename(__FILE__));
|
||||
}
|
||||
|
||||
if(!$check_dependencies) {
|
||||
custom_inventory_search_timber_notice();
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
delete_option('autocart_activation_error');
|
||||
delete_option('autocart_dependencies');
|
||||
}
|
||||
|
||||
|
||||
// Add AJAX action to retrieve vehicle data
|
||||
function handle_autocart_filters() {
|
||||
|
||||
$nonce = $_POST['nonce'];
|
||||
if (!wp_verify_nonce($nonce, 'update-filters-ajax-nonce')) {
|
||||
die('Invalid nonce');
|
||||
}
|
||||
|
||||
$price = isset($_POST['price']) ? sanitize_text_field($_POST['price']) : '';
|
||||
$model = isset($_POST['model']) ? sanitize_text_field($_POST['model']) : '';
|
||||
$make = isset($_POST['make']) ? sanitize_text_field($_POST['make']) : '';
|
||||
|
||||
$externalToken = get_option('autocart_external_token');
|
||||
|
||||
$searchData = array(
|
||||
'price' => 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');
|
||||
|
||||
|
||||
|
21
custom-twig-extension.php
Normal file
21
custom-twig-extension.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
class CustomTwigExtension extends \Twig\Extension\AbstractExtension implements \Twig\Extension\GlobalsInterface
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getGlobals(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function jsonDecode($json)
|
||||
{
|
||||
return json_decode($json, true);
|
||||
}
|
||||
}
|
7
index.js
Normal file
7
index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
// index.js
|
||||
|
||||
|
||||
import './node_modules/@material/web/button/filled-button.js';
|
||||
import './node_modules/@material/web/button/outlined-button.js';
|
||||
import './node_modules/@material/web/checkbox/checkbox.js';
|
||||
import './node_modules/@material/web/slider/slider.js';
|
15
package.json
Normal file
15
package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "autocart",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@material/web": "^1.1.1",
|
||||
"tslib": "^2.6.2"
|
||||
}
|
||||
}
|
46
timber-check.php
Normal file
46
timber-check.php
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
<?php
|
||||
$vendor_path = ABSPATH . '/vendor/autoload.php';
|
||||
if (file_exists($vendor_path)) {
|
||||
require_once $vendor_path;
|
||||
|
||||
if (!class_exists('Timber\Timber')) {
|
||||
// Timber is not active.. so display a notice
|
||||
update_option('autocart_dependencies', false);
|
||||
|
||||
} else {
|
||||
Timber\Timber::init();
|
||||
update_option('autocart_dependencies', true);
|
||||
|
||||
}
|
||||
} else {
|
||||
update_option('autocart_dependencies', false);
|
||||
}
|
||||
|
||||
|
||||
// Display notice for missing Timber plugin
|
||||
function custom_inventory_search_timber_notice() {
|
||||
?>
|
||||
<div class="notice notice-error is-dismissible">
|
||||
<p><?php _e('The <strong>Custom Inventory Search</strong> plugin requires the <strong>Timber</strong> plugin to be installed. To install Timber, you can use the following command:', 'custom-inventory-search'); ?></p>
|
||||
<pre><code>composer require timber/timber</code> <span class="dashicons dashicons-clipboard" onclick="copyToClipboard()"></span></pre>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function copyToClipboard() {
|
||||
const commandToCopy = 'composer require timber/timber';
|
||||
|
||||
const textArea = document.createElement('textarea');
|
||||
textArea.value = commandToCopy;
|
||||
document.body.appendChild(textArea);
|
||||
textArea.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
|
||||
// Optionally, you can provide feedback to the user
|
||||
alert('Command copied to clipboard!');
|
||||
}
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user