class-auxels-frontend-assets.php
6 years ago
frontend-ajax.php
6 years ago
index.php
9 years ago
templates-post.php
9 years ago
frontend-ajax.php
275 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Load more ajax handler for "Recent Posts Grid" element |
| 5 | * |
| 6 | * @return void |
| 7 | */ |
| 8 | function auxels_ajax_handler_element_load_more(){ |
| 9 | if( ! defined( 'AUXIN_INC' ) ){ |
| 10 | wp_send_json_success("Phlox theme is required."); |
| 11 | } |
| 12 | if( empty( $_POST["handler"] ) ){ |
| 13 | wp_send_json_success("Please specify a handler."); |
| 14 | } |
| 15 | // Direct call is not alloweded |
| 16 | if( empty( $_POST['action'] ) ){ |
| 17 | wp_send_json_error( __( 'Ajax action not found.', 'auxin-elements' ) ); |
| 18 | } |
| 19 | if( empty( $_POST['args'] ) ){ |
| 20 | wp_send_json_error( __( 'Ajax args is required.', 'auxin-elements' ) ); |
| 21 | } |
| 22 | // Authorize the call |
| 23 | if( ! wp_verify_nonce( $_POST['nonce'], 'auxin_front_load_more' ) ){ |
| 24 | wp_send_json_error( __( 'Authorization failed.', 'auxin-elements' ) ); |
| 25 | } |
| 26 | |
| 27 | $ajax_args = $_POST['args']; |
| 28 | $element_markup = ''; |
| 29 | |
| 30 | // include the required resources |
| 31 | require_once( AUXELS_INC_DIR . '/general-functions.php' ); |
| 32 | require_once( THEME_DIR . AUXIN_INC . 'include/functions.php' ); |
| 33 | require_once( THEME_DIR . AUXIN_INC . 'include/templates/templates-post.php' ); |
| 34 | |
| 35 | // take required actions based on custom handler (element base name) |
| 36 | switch( $_POST['handler'] ) { |
| 37 | |
| 38 | case 'aux_recent_posts': |
| 39 | require_once( AUXELS_INC_DIR . '/elements/recent-posts-grid-carousel.php' ); |
| 40 | |
| 41 | // Get the element markup |
| 42 | $element_markup = auxin_widget_recent_posts_callback( $ajax_args ); |
| 43 | break; |
| 44 | |
| 45 | case 'aux_recent_posts_land_style': |
| 46 | require_once( AUXELS_INC_DIR . '/elements/recent-posts-land-style.php' ); |
| 47 | |
| 48 | // Get the element markup |
| 49 | $element_markup = auxin_widget_recent_posts_land_style_callback( $ajax_args ); |
| 50 | break; |
| 51 | |
| 52 | case 'aux_recent_posts_masonry': |
| 53 | require_once( AUXELS_INC_DIR . '/elements/recent-posts-masonry.php' ); |
| 54 | |
| 55 | // Get the element markup |
| 56 | $element_markup = auxin_widget_recent_posts_masonry_callback( $ajax_args ); |
| 57 | break; |
| 58 | |
| 59 | case 'aux_recent_posts_tiles': |
| 60 | require_once( AUXELS_INC_DIR . '/elements/recent-posts-tiles.php' ); |
| 61 | |
| 62 | // Get the element markup |
| 63 | $element_markup = auxin_widget_recent_posts_tiles_callback( $ajax_args ); |
| 64 | break; |
| 65 | |
| 66 | case 'aux_recent_posts_timeline': |
| 67 | require_once( AUXELS_INC_DIR . '/elements/recent-posts-timeline.php' ); |
| 68 | |
| 69 | // Get the element markup |
| 70 | $element_markup = auxin_widget_recent_posts_timeline_callback( $ajax_args ); |
| 71 | break; |
| 72 | |
| 73 | case 'aux_recent_news': |
| 74 | require_once( AUXNEW_INC_DIR . '/elements/recent-news.php' ); |
| 75 | |
| 76 | // Get the element markup |
| 77 | $element_markup = auxin_widget_recent_news_callback( $ajax_args ); |
| 78 | break; |
| 79 | |
| 80 | case 'aux_recent_news_big_grid': |
| 81 | require_once( AUXNEW_INC_DIR . '/elements/recent-news-big-grid.php' ); |
| 82 | |
| 83 | // Get the element markup |
| 84 | $element_markup = auxin_widget_recent_news_big_grid_callback( $ajax_args ); |
| 85 | break; |
| 86 | |
| 87 | case 'aux_recent_portfolios_grid': |
| 88 | require_once( AUXPFO_INC_DIR . '/elements/recent-portfolios.php' ); |
| 89 | |
| 90 | // Get the element markup |
| 91 | $element_markup = auxin_widget_recent_portfolios_grid_callback( $ajax_args ); |
| 92 | break; |
| 93 | |
| 94 | case 'aux_flexible_recent_posts': |
| 95 | require_once( AUXPRO_INC_DIR . '/elements/flexible-recent-posts.php' ); |
| 96 | |
| 97 | // Get the element markup |
| 98 | $element_markup = auxin_widget_flexible_recent_posts_callback( $ajax_args ); |
| 99 | break; |
| 100 | |
| 101 | default: |
| 102 | wp_send_json_error( __( 'Not a valid handler.', 'auxin-elements' ) ); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | // if the output is empty |
| 107 | if( empty( $element_markup ) ){ |
| 108 | wp_send_json_error( __( 'No data received.', 'auxin-elements' ) ); |
| 109 | } |
| 110 | |
| 111 | wp_send_json_success( $element_markup ); |
| 112 | } |
| 113 | |
| 114 | add_action( 'wp_ajax_load_more_element', 'auxels_ajax_handler_element_load_more' ); |
| 115 | add_action( 'wp_ajax_nopriv_load_more_element', 'auxels_ajax_handler_element_load_more' ); |
| 116 | |
| 117 | /** |
| 118 | * Remove Product from Cart via Ajax |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | function auxels_remove_product_from_cart() { |
| 123 | |
| 124 | if ( !class_exists( 'WooCommerce' ) ) |
| 125 | return; |
| 126 | |
| 127 | global $woocommerce; |
| 128 | |
| 129 | try { |
| 130 | |
| 131 | $nonce = $_POST['verify_nonce']; |
| 132 | $id = $_POST['product_id']; |
| 133 | |
| 134 | if( ! isset( $_POST['product_id'] ) || ! wp_verify_nonce( $nonce, 'remove_cart-' . $id ) ){ |
| 135 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Verification failed!', 'auxin-elements') ) ); |
| 136 | } |
| 137 | |
| 138 | $cart = $woocommerce->cart; |
| 139 | $cart_id = $cart->generate_cart_id($id); |
| 140 | $cart_item_id = $cart->find_product_in_cart($cart_id); |
| 141 | |
| 142 | if( $cart_item_id ) { |
| 143 | $cart->set_quantity( $cart_item_id, 0 ); |
| 144 | } |
| 145 | |
| 146 | $cart->calculate_totals(); |
| 147 | |
| 148 | $response = array( |
| 149 | 'total' => wc_format_decimal( $cart->cart_contents_total, 2 ), |
| 150 | 'count' => $cart->cart_contents_count, |
| 151 | 'empty' => sprintf( '<div class="aux-card-box">%s</div>', __( 'Your cart is currently empty.', 'auxin-elements' ) ), |
| 152 | 'notif' => sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-message">%s</div>', __('Item has been removed from your shopping cart.', 'auxin-elements') ) |
| 153 | ); |
| 154 | |
| 155 | wp_send_json_success( $response ); |
| 156 | |
| 157 | } catch (Exception $e) { |
| 158 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('An Error Occurred!', 'auxin-elements') ) ); |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | add_action( 'wp_ajax_auxels_remove_from_cart', 'auxels_remove_product_from_cart' ); |
| 163 | add_action( 'wp_ajax_nopriv_auxels_remove_from_cart', 'auxels_remove_product_from_cart' ); |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Add to Cart via Ajax |
| 168 | */ |
| 169 | function auxels_add_product_to_cart() { |
| 170 | |
| 171 | if ( ! class_exists( 'WooCommerce' ) ) |
| 172 | return; |
| 173 | |
| 174 | global $woocommerce; |
| 175 | |
| 176 | try { |
| 177 | |
| 178 | $product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : ''; |
| 179 | $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] ); |
| 180 | $verify_nonce = isset( $_POST['verify_nonce'] ) ? $_POST['verify_nonce'] : ''; |
| 181 | $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); |
| 182 | |
| 183 | if( empty( $product_id ) || ! wp_verify_nonce( $verify_nonce, 'aux_add_to_cart-' . $product_id ) ){ |
| 184 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Verification failed!', 'auxin-elements') ) ); |
| 185 | } else { |
| 186 | // Add item to cart |
| 187 | if( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) ) { |
| 188 | $args = isset( $_POST['args'] ) ? $_POST['args'] : array( |
| 189 | 'title' => '', |
| 190 | 'css_class' => '', |
| 191 | 'dropdown_class' => '', |
| 192 | 'color_class' => 'aux-black', |
| 193 | 'action_on' => 'click', |
| 194 | 'cart_url' => '#', |
| 195 | 'dropdown_skin' => '', |
| 196 | ); |
| 197 | $items = auxin_get_cart_items( $args ); |
| 198 | $count = $woocommerce->cart->cart_contents_count; |
| 199 | $total = auxin_get_cart_basket( $args, $count ); |
| 200 | |
| 201 | $data = array( |
| 202 | 'items' => $items, |
| 203 | 'total' => $total, |
| 204 | 'notif' => sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-message"><a href="%s" class="button wc-forward">%s</a> "%s" %s</div>', esc_url( wc_get_cart_url() ) , __( 'View cart', 'auxin-elements' ), get_the_title( $product_id ) , __('has been added to your cart.', 'auxin-elements') ) |
| 205 | ); |
| 206 | // Send json success |
| 207 | wp_send_json_success( $data ); |
| 208 | } else { |
| 209 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Sorry, this product cannot be purchased.', 'auxin-elements') ) ); |
| 210 | } |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } catch( Exception $e ){ |
| 215 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('An Error Occurred!', 'auxin-elements') ) ); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | add_action( 'wp_ajax_auxels_add_to_cart', 'auxels_add_product_to_cart' ); |
| 220 | add_action( 'wp_ajax_nopriv_auxels_add_to_cart', 'auxels_add_product_to_cart' ); |
| 221 | |
| 222 | |
| 223 | |
| 224 | /** |
| 225 | * Ajax Search Handler |
| 226 | * |
| 227 | * @return void |
| 228 | */ |
| 229 | function auxels_ajax_search() { |
| 230 | |
| 231 | $available_search_post_types = auxin_get_available_post_types_for_search(); |
| 232 | |
| 233 | if ( ! empty( $_GET['post_types'] ) ) { |
| 234 | $post_types = explode( ',', sanitize_text_field( $_GET['post_types'] ) ); |
| 235 | } else { |
| 236 | $post_types = array_keys( $available_search_post_types ); |
| 237 | } |
| 238 | |
| 239 | $s = trim( sanitize_text_field( $_GET['s'] ) ); |
| 240 | if ( "0" == $cat = trim( sanitize_text_field( $_GET['cat'] ) ) ) { |
| 241 | $cat = ''; |
| 242 | } |
| 243 | |
| 244 | // Start Searching First Post type |
| 245 | $search_instance = new Auxels_Search_Post_Type($s,$cat,$post_types[0],''); |
| 246 | |
| 247 | if ( ! empty( $_GET['per_page'] ) && is_numeric( $_GET['per_page'] ) ) { |
| 248 | $search_instance->set_query_args( array( 'posts_per_page' => sanitize_text_field( $_GET['per_page'] ) ) ); |
| 249 | } |
| 250 | |
| 251 | $results = array(); |
| 252 | foreach( $post_types as $key => $post_type ) { |
| 253 | $search_instance->set_query_args( array( 'post_type' => $post_type ) ); |
| 254 | switch ($post_type) { |
| 255 | case 'product': |
| 256 | $result = $search_instance->search_products(); |
| 257 | break; |
| 258 | case 'portfolio': |
| 259 | $result = $search_instance->search_portfolio(); |
| 260 | break; |
| 261 | default: |
| 262 | $result = $search_instance->search_general_post_types(); |
| 263 | break; |
| 264 | } |
| 265 | $result['fromTitle'] = sprintf( esc_html__( 'From %s', 'auxin-elements' ), $available_search_post_types[ $post_type ]); |
| 266 | $result['noResultMessage'] = sprintf( esc_html__( "Nothing found in %s", 'auxin-elements' ), $available_search_post_types[ $post_type ]); |
| 267 | $results[] = $result; |
| 268 | |
| 269 | } |
| 270 | |
| 271 | wp_send_json_success( $results ); |
| 272 | } |
| 273 | |
| 274 | add_action( 'wp_ajax_aux_modern_search_handler', 'auxels_ajax_search' ); |
| 275 | add_action( 'wp_ajax_nopriv_aux_modern_search_handler', 'auxels_ajax_search'); |