class-auxels-frontend-assets.php
5 months ago
frontend-ajax.php
1 year ago
index.php
9 years ago
templates-post.php
9 years ago
frontend-ajax.php
348 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 = auxin_sanitize_input( $_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_grid': |
| 81 | require_once( AUXNEW_INC_DIR . '/elements/recent-news-grid.php' ); |
| 82 | |
| 83 | // Get the element markup |
| 84 | $element_markup = auxin_widget_recent_news_grid_callback( $ajax_args ); |
| 85 | break; |
| 86 | |
| 87 | case 'aux_recent_news_big_grid': |
| 88 | require_once( AUXNEW_INC_DIR . '/elements/recent-news-big-grid.php' ); |
| 89 | |
| 90 | // Get the element markup |
| 91 | $element_markup = auxin_widget_recent_news_big_grid_callback( $ajax_args ); |
| 92 | break; |
| 93 | |
| 94 | case 'aux_recent_portfolios_grid': |
| 95 | require_once( AUXPFO_INC_DIR . '/elements/recent-portfolios.php' ); |
| 96 | |
| 97 | // Get the element markup |
| 98 | $element_markup = auxin_widget_recent_portfolios_grid_callback( $ajax_args ); |
| 99 | break; |
| 100 | |
| 101 | case 'aux_flexible_recent_posts': |
| 102 | require_once( AUXPRO_INC_DIR . '/elements/flexible-recent-posts.php' ); |
| 103 | |
| 104 | // Get the element markup |
| 105 | $element_markup = auxin_widget_flexible_recent_posts_callback( $ajax_args ); |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | wp_send_json_error( __( 'Not a valid handler.', 'auxin-elements' ) ); |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | // if the output is empty |
| 114 | if( empty( $element_markup ) ){ |
| 115 | wp_send_json_error( __( 'No data received.', 'auxin-elements' ) ); |
| 116 | } |
| 117 | |
| 118 | wp_send_json_success( $element_markup ); |
| 119 | } |
| 120 | |
| 121 | add_action( 'wp_ajax_load_more_element', 'auxels_ajax_handler_element_load_more' ); |
| 122 | add_action( 'wp_ajax_nopriv_load_more_element', 'auxels_ajax_handler_element_load_more' ); |
| 123 | |
| 124 | /** |
| 125 | * Remove Product from Cart via Ajax |
| 126 | * |
| 127 | * @return void |
| 128 | */ |
| 129 | function auxels_remove_product_from_cart() { |
| 130 | |
| 131 | if ( !class_exists( 'WooCommerce' ) ) |
| 132 | return; |
| 133 | |
| 134 | global $woocommerce; |
| 135 | |
| 136 | try { |
| 137 | |
| 138 | $nonce = !empty( $_POST['verify_nonce'] ) ? sanitize_text_field( $_POST['verify_nonce'] ) : ''; |
| 139 | $id = !empty( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : ''; |
| 140 | $cart_item_key = !empty( $_POST['cart_item_key'] ) ? sanitize_text_field( $_POST['cart_item_key'] ) : ''; |
| 141 | if( ! isset( $_POST['product_id'] ) || ! wp_verify_nonce( $nonce, 'remove_cart-' . $id ) ){ |
| 142 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Verification failed!', 'auxin-elements') ) ); |
| 143 | } |
| 144 | |
| 145 | $cart = $woocommerce->cart; |
| 146 | $cart->remove_cart_item( $cart_item_key ); |
| 147 | |
| 148 | $cart->calculate_totals(); |
| 149 | |
| 150 | $args = !empty( $_POST['args'] ) ? auxin_sanitize_input( $_POST['args'] ) : array( |
| 151 | 'title' => '', |
| 152 | 'css_class' => '', |
| 153 | 'dropdown_class' => '', |
| 154 | 'color_class' => 'aux-black', |
| 155 | 'action_on' => 'click', |
| 156 | 'cart_url' => '#', |
| 157 | 'dropdown_skin' => '', |
| 158 | ); |
| 159 | |
| 160 | $count = (int) $cart->cart_contents_count; |
| 161 | if ( $count > 0 ) { |
| 162 | $items = auxin_get_cart_items( $args ); |
| 163 | } else { |
| 164 | $items = '<div class="aux-card-box aux-empty-cart"><img src="'. esc_url( AUXIN_URL . 'images/other/empty-cart.svg' ) . '">' . esc_html__( 'Cart is empty', 'auxin-elements' ) . '</div>'; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | $response = array( |
| 169 | 'fragments' => apply_filters( |
| 170 | 'woocommerce_add_to_cart_fragments', |
| 171 | array( |
| 172 | '.aux-cart-wrapper .aux-card-dropdown' => '<div class="aux-card-dropdown aux-phone-off">' . $items . '</div>' , |
| 173 | ) |
| 174 | ), |
| 175 | 'cart_hash' => WC()->cart->get_cart_hash(), |
| 176 | 'items' => $items, |
| 177 | 'total' => $woocommerce->cart->get_cart_subtotal(), |
| 178 | 'count' => $count, |
| 179 | 'empty' => sprintf( '<div class="aux-card-box">%s</div>', __( 'Your cart is currently empty.', 'auxin-elements' ) ), |
| 180 | 'notif' => sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-message">%s</div>', __('Item has been removed from your shopping cart.', 'auxin-elements') ) |
| 181 | ); |
| 182 | |
| 183 | wp_send_json_success( $response ); |
| 184 | |
| 185 | } catch (Exception $e) { |
| 186 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('An Error Occurred!', 'auxin-elements') ) ); |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | add_action( 'wp_ajax_auxels_remove_from_cart', 'auxels_remove_product_from_cart' ); |
| 191 | add_action( 'wp_ajax_nopriv_auxels_remove_from_cart', 'auxels_remove_product_from_cart' ); |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Add to Cart via Ajax |
| 196 | */ |
| 197 | function auxels_add_product_to_cart() { |
| 198 | |
| 199 | if ( ! class_exists( 'WooCommerce' ) ) |
| 200 | return; |
| 201 | |
| 202 | global $woocommerce; |
| 203 | |
| 204 | try { |
| 205 | |
| 206 | $product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : ''; |
| 207 | $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( absint( $_POST['quantity'] ) ); |
| 208 | $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); |
| 209 | |
| 210 | if( empty( $product_id ) ){ |
| 211 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Verification failed!', 'auxin-elements') ) ); |
| 212 | } else { |
| 213 | // Add item to cart |
| 214 | if( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) ) { |
| 215 | $args = !empty( $_POST['args'] ) ? auxin_sanitize_input( $_POST['args'] ) : array( |
| 216 | 'title' => '', |
| 217 | 'css_class' => '', |
| 218 | 'dropdown_class' => '', |
| 219 | 'color_class' => 'aux-black', |
| 220 | 'action_on' => 'click', |
| 221 | 'cart_url' => '#', |
| 222 | 'dropdown_skin' => '', |
| 223 | 'size' => 'thumbnail', |
| 224 | 'icon' => auxin_get_option( 'product_cart_icon', 'auxicon-shopping-cart-1-1' ) |
| 225 | ); |
| 226 | $items = auxin_get_cart_items( $args ); |
| 227 | $count = $woocommerce->cart->cart_contents_count; |
| 228 | $total = auxin_get_cart_basket( $args, $count ); |
| 229 | |
| 230 | $data = array( |
| 231 | 'fragments' => apply_filters( |
| 232 | 'woocommerce_add_to_cart_fragments', |
| 233 | array( |
| 234 | '.aux-cart-wrapper .aux-card-dropdown' => '<div class="aux-card-dropdown aux-phone-off">' . $items . '</div>' , |
| 235 | ) |
| 236 | ), |
| 237 | 'cart_hash' => WC()->cart->get_cart_hash(), |
| 238 | 'items' => $items, |
| 239 | 'total' => $total, |
| 240 | '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') ) |
| 241 | ); |
| 242 | // Send json success |
| 243 | wp_send_json_success( $data ); |
| 244 | } else { |
| 245 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('Sorry, this product cannot be purchased.', 'auxin-elements') ) ); |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | } catch( Exception $e ){ |
| 251 | wp_send_json_error( sprintf( '<div class="aux-woocommerce-ajax-notification woocommerce-error">%s</div>', __('An Error Occurred!', 'auxin-elements') ) ); |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | add_action( 'wp_ajax_auxels_add_to_cart', 'auxels_add_product_to_cart' ); |
| 256 | add_action( 'wp_ajax_nopriv_auxels_add_to_cart', 'auxels_add_product_to_cart' ); |
| 257 | |
| 258 | /** |
| 259 | * Get refreshed cart fragments |
| 260 | */ |
| 261 | function auxels_get_refreshed_fragments() { |
| 262 | global $woocommerce; |
| 263 | |
| 264 | $args = isset( $_POST['args'] ) ? auxin_sanitize_input( $_POST['args'] ) : array( |
| 265 | 'title' => '', |
| 266 | 'css_class' => '', |
| 267 | 'dropdown_class' => '', |
| 268 | 'color_class' => 'aux-black', |
| 269 | 'action_on' => 'click', |
| 270 | 'cart_url' => '#', |
| 271 | 'dropdown_skin' => '', |
| 272 | 'size' => 'thumbnail', |
| 273 | 'icon' => auxin_get_option( 'product_cart_icon', 'auxicon-shopping-cart-1-1' ) |
| 274 | ); |
| 275 | |
| 276 | $count = (int) $woocommerce->cart->cart_contents_count; |
| 277 | if ( $count > 0 ) { |
| 278 | $items = auxin_get_cart_items( $args ); |
| 279 | } else { |
| 280 | $items = '<div class="aux-card-box aux-empty-cart"><img src="'. esc_url( AUXIN_URL . 'images/other/empty-cart.svg' ) . '">' . esc_html__( 'Cart is empty', 'auxin-elements' ) . '</div>'; |
| 281 | } |
| 282 | |
| 283 | $total = auxin_get_cart_basket( $args, $count ); |
| 284 | |
| 285 | $data = array( |
| 286 | 'cart_hash' => WC()->cart->get_cart_hash(), |
| 287 | 'items' => $items, |
| 288 | 'total' => $total, |
| 289 | ); |
| 290 | |
| 291 | // Send json success |
| 292 | wp_send_json_success( $data ); |
| 293 | } |
| 294 | add_action( 'wp_ajax_auxels_get_refreshed_fragments', 'auxels_get_refreshed_fragments' ); |
| 295 | add_action( 'wp_ajax_nopriv_auxels_get_refreshed_fragments', 'auxels_get_refreshed_fragments' ); |
| 296 | |
| 297 | /** |
| 298 | * Ajax Search Handler |
| 299 | * |
| 300 | * @return void |
| 301 | */ |
| 302 | function auxels_ajax_search() { |
| 303 | |
| 304 | $available_search_post_types = auxin_get_available_post_types_for_search(); |
| 305 | |
| 306 | if ( ! empty( $_GET['post_types'] ) ) { |
| 307 | $post_types = auxin_sanitize_input( $_GET['post_types'] ); |
| 308 | } else { |
| 309 | $post_types = array_keys( $available_search_post_types ); |
| 310 | } |
| 311 | |
| 312 | $s = trim( sanitize_text_field( $_GET['s'] ) ); |
| 313 | if ( "0" == $cat = trim( sanitize_text_field( $_GET['cat'] ) ) ) { |
| 314 | $cat = ''; |
| 315 | } |
| 316 | |
| 317 | // Start Searching First Post type |
| 318 | $search_instance = new Auxels_Search_Post_Type($s,$cat,$post_types[0],''); |
| 319 | |
| 320 | if ( ! empty( $_GET['per_page'] ) && is_numeric( $_GET['per_page'] ) ) { |
| 321 | $search_instance->set_query_args( array( 'posts_per_page' => sanitize_text_field( $_GET['per_page'] ) ) ); |
| 322 | } |
| 323 | |
| 324 | $results = array(); |
| 325 | foreach( $post_types as $key => $post_type ) { |
| 326 | $search_instance->set_query_args( array( 'post_type' => $post_type ) ); |
| 327 | switch ($post_type) { |
| 328 | case 'product': |
| 329 | $result = $search_instance->search_products(); |
| 330 | break; |
| 331 | case 'portfolio': |
| 332 | $result = $search_instance->search_portfolio(); |
| 333 | break; |
| 334 | default: |
| 335 | $result = $search_instance->search_general_post_types(); |
| 336 | break; |
| 337 | } |
| 338 | $result['fromTitle'] = sprintf( esc_html__( 'From %s', 'auxin-elements' ), esc_html( $available_search_post_types[ $post_type ] ) ); |
| 339 | $result['noResultMessage'] = sprintf( esc_html__( "Nothing found in %s", 'auxin-elements' ), esc_html( $available_search_post_types[ $post_type ] ) ); |
| 340 | $results[] = $result; |
| 341 | |
| 342 | } |
| 343 | |
| 344 | wp_send_json_success( $results ); |
| 345 | } |
| 346 | |
| 347 | add_action( 'wp_ajax_aux_modern_search_handler', 'auxels_ajax_search' ); |
| 348 | add_action( 'wp_ajax_nopriv_aux_modern_search_handler', 'auxels_ajax_search'); |