-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwild-pack-elementor.php
128 lines (90 loc) · 4.06 KB
/
wild-pack-elementor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Plugin Name: Wild Pack - Elementor Addons
* Description: Some addons to make the work easier. Includes a few addons i made for myself to ease the work.
* Version: 1.3.0
* Author: WildWomble
* Author URI: https://github.com/WildWomble
* Text Domain: elementor-wild-pack
* Domain Path: /languages
* Elementor tested up to: 3.18.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'WW__FILE__', __FILE__ );
define( 'WW_PLUGIN_BASE', plugin_basename( WW__FILE__ ) );
define( 'WW_URL', plugins_url( '/', WW__FILE__ ) );
define( 'WW_PATH', plugin_dir_path( WW__FILE__ ) );
require_once( WW_PATH . 'includes/wild-pack.php' );
/**
* Registering the needed CSS and JS
*/
function widget_scripts() {
/* Styles */
wp_register_style( 'ww-custom-bundle-css', plugins_url( 'assets/css/custom-bundle.css', __FILE__ ), array(), '1.0.0', 'all' );
wp_register_style( 'ww-restaurant-menu-css', plugins_url( 'assets/css/restaurant-menu.css', __FILE__ ), array(), '1.0.0', 'all' );
wp_register_style( 'ww-rmwoo-css', plugins_url( 'assets/css/restaurant-menu-woo.css', __FILE__ ), array(), '1.0.0', 'all' );
/* Scripts */
wp_register_script( 'ww-custom-bundle-js', plugins_url( 'assets/js/custom-bundle.js', __FILE__ ), array('jquery'), '1.0.0', true );
wp_register_script( 'ww-restaurant-menu-js', plugins_url( 'assets/js/restaurant-menu.js', __FILE__ ), array('jquery'), '1.0.0', true );
wp_register_script( 'ww-rmwoo-js', plugins_url( 'assets/js/restaurant-menu-woo.js', __FILE__ ), array('jquery'), '1.0.0', true );
wp_localize_script( 'ww-custom-bundle-js', 'localize',
array(
'_ajax_url' => admin_url( 'admin-ajax.php' ),
'_ajax_nonce' => wp_create_nonce( '_ajax_nonce' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'widget_scripts' );
/**
* Add category to make it easier to find the addons in the editor
*/
function add_elementor_widget_categories( $elements_manager ) {
$elements_manager->add_category(
'wild-pack-category',
[
'title' => esc_html__( 'Wild Pack', 'elementor-wild-pack' ),
'icon' => 'fa fa-plug',
]
);
}
add_action( 'elementor/elements/categories_registered', 'add_elementor_widget_categories' );
/**
* Making a custom action for the form submission, check that the nonce is valid and sending back feedback in case of error.
*/
add_action( 'wp_ajax_custom_bundle_submit', 'custom_bundle_submit_form' );
add_action( 'wp_ajax_nopriv_custom_bundle_submit', 'custom_bundle_submit_form' );
function custom_bundle_submit_form() {
if ( ! wp_verify_nonce( $_POST['_ajax_nonce'], '_ajax_nonce' ) ) {
die( __( 'Error', 'elementor-wild-pack' ) );
}
$variations = ( empty( $_POST['edm_variations'] ) ) ? null : $_POST['edm_variations'];
$variations_count = count( $variations );
global $woocommerce;
$product_id = sanitize_text_field( $_POST['edm_product_id'] );
$steps = sanitize_text_field( $_POST['edm_steps'] ) - 1;
$message_error = array(
esc_html__( 'Please choose all the required products from the list', 'elementor-wild-pack' ),
esc_html__( '[Error] The product could not be added to cart.', 'elementor-wild-pack' ),
esc_html__( '[Error - Dev] A product must be selected.', 'elementor-wild-pack' )
);
if( !empty( $product_id ) ) {
if( !empty($variations) && $variations_count == $steps ) {
for( $i = 0; $i < $variations_count; $i++ ) {
$custom_variations['option_' . $i] = sanitize_text_field( $variations[$i]['value'] );
}
if( $woocommerce->cart->add_to_cart( $product_id, 1, 0, $custom_variations ) ) {
WC_AJAX::get_refreshed_fragments();
} else {
$res = [ 'code' => '0', 'message' => $message_error[1] ];
}
} else {
$res = [ 'code' => '-1', 'message' => $message_error[0] ];
}
} else {
$res = [ 'code' => '-1', 'message' => $message_error[2] ];
}
wp_send_json( $res );
wp_die();
}