PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-composite-products/includes/class-wc-cp-ajax.php

https://gitlab.com/iamgraeme/royalmile
PHP | 215 lines | 128 code | 62 blank | 25 comment | 25 complexity | bfa86b94246af422e35cdbac8ce276de MD5 | raw file
  1. <?php
  2. /**
  3. * Composited Products AJAX Handlers
  4. *
  5. * @class WC_CP_AJAX
  6. * @version 3.2.3
  7. */
  8. // Exit if accessed directly
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. class WC_CP_AJAX {
  13. public static function init() {
  14. // use WC ajax if available, otherwise fall back to WP ajax
  15. if ( WC_CP_Core_Compatibility::use_wc_ajax() ) {
  16. add_action( 'wc_ajax_woocommerce_show_composited_product', __CLASS__ . '::show_composited_product_ajax' );
  17. add_action( 'wc_ajax_woocommerce_show_component_options', __CLASS__ . '::show_component_options_ajax' );
  18. } else {
  19. add_action( 'wp_ajax_woocommerce_show_composited_product', __CLASS__ . '::show_composited_product_ajax' );
  20. add_action( 'wp_ajax_woocommerce_show_component_options', __CLASS__ . '::show_component_options_ajax' );
  21. add_action( 'wp_ajax_nopriv_woocommerce_show_composited_product', __CLASS__ . '::show_composited_product_ajax' );
  22. add_action( 'wp_ajax_nopriv_woocommerce_show_component_options', __CLASS__ . '::show_component_options_ajax' );
  23. }
  24. }
  25. /**
  26. * Display paged component options via ajax. Effective in 'thumbnails' mode only.
  27. *
  28. * @return void
  29. */
  30. public static function show_component_options_ajax() {
  31. $data = array();
  32. header( 'Content-Type: application/json; charset=utf-8' );
  33. if ( ! check_ajax_referer( 'wc_bto_show_product', 'security', false ) ) {
  34. echo json_encode( array(
  35. 'result' => 'failure',
  36. 'component_scenario_data' => array(),
  37. 'options_markup' => sprintf( '<div class="woocommerce-error">%s</div>', __( 'Sorry, there are options to show at the moment. Please refresh the page and try again.', 'woocommerce-composite-products' ) )
  38. ) );
  39. die();
  40. }
  41. if ( isset( $_POST[ 'selected_option' ] ) && isset( $_POST[ 'load_page' ] ) && intval( $_POST[ 'load_page' ] ) > 0 && isset( $_POST[ 'composite_id' ] ) && intval( $_POST[ 'composite_id' ] ) > 0 && ! empty( $_POST[ 'component_id' ] ) ) {
  42. $component_id = intval( $_POST[ 'component_id' ] );
  43. $composite_id = intval( $_POST[ 'composite_id' ] );
  44. $selected_option = ! empty( $_POST[ 'selected_option' ] ) ? intval( $_POST[ 'selected_option' ] ) : '';
  45. $load_page = intval( $_POST[ 'load_page' ] );
  46. } else {
  47. echo json_encode( array(
  48. 'result' => 'failure',
  49. 'component_scenario_data' => array(),
  50. 'options_markup' => sprintf( '<div class="woocommerce-error">%s</div>', __( 'Looks like something went wrong. Please refresh the page and try again.', 'woocommerce-composite-products' ) )
  51. ) );
  52. die();
  53. }
  54. $product = wc_get_product( $composite_id );
  55. $query_args = array(
  56. 'selected_option' => $selected_option,
  57. 'load_page' => $load_page,
  58. );
  59. // Include orderby argument if posted -- if not, the default ordering method will be used
  60. if ( ! empty( $_POST[ 'orderby' ] ) ) {
  61. $query_args[ 'orderby' ] = $_POST[ 'orderby' ];
  62. }
  63. // Include filters argument if posted -- if not, no filters will be applied to the query
  64. if ( ! empty( $_POST[ 'filters' ] ) ) {
  65. $query_args[ 'filters' ] = $_POST[ 'filters' ];
  66. }
  67. // Load Component Options
  68. $current_options = $product->get_current_component_options( $component_id, $query_args );
  69. ob_start();
  70. wc_get_template( 'single-product/component-options.php', array(
  71. 'product' => $product,
  72. 'component_id' => $component_id,
  73. 'component_options' => $current_options,
  74. 'component_data' => $product->get_component_data( $component_id ),
  75. 'selected_option' => $selected_option,
  76. 'selection_mode' => $product->get_composite_selections_style()
  77. ), '', WC_CP()->plugin_path() . '/templates/' );
  78. $component_options_markup = ob_get_clean();
  79. ob_start();
  80. wc_get_template( 'single-product/component-options-pagination.php', array(
  81. 'product' => $product,
  82. 'component_id' => $component_id,
  83. ), '', WC_CP()->plugin_path() . '/templates/' );
  84. $component_pagination_markup = ob_get_clean();
  85. // Calculate scenario data for the displayed component options, including the current selection
  86. if ( $selected_option && ! in_array( $selected_option, $current_options ) ) {
  87. $current_options[] = $selected_option;
  88. }
  89. $scenario_data = $product->get_current_component_scenarios( $component_id, $current_options );
  90. echo json_encode( array(
  91. 'result' => 'success',
  92. 'component_scenario_data' => $scenario_data[ 'scenario_data' ][ $component_id ],
  93. 'options_markup' => $component_options_markup,
  94. 'pagination_markup' => $component_pagination_markup,
  95. ) );
  96. die();
  97. }
  98. /**
  99. * Ajax listener that fetches product markup when a new selection is made.
  100. *
  101. * @param mixed $product_id
  102. * @param mixed $item_id
  103. * @param mixed $container_id
  104. * @return string
  105. */
  106. public static function show_composited_product_ajax( $product_id = '', $component_id = '', $composite_id = '' ) {
  107. global $product;
  108. header( 'Content-Type: application/json; charset=utf-8' );
  109. if ( ! check_ajax_referer( 'wc_bto_show_product', 'security', false ) ) {
  110. echo json_encode( array(
  111. 'result' => 'failure',
  112. 'reason' => 'wc_bto_show_product nonce incorrect',
  113. 'markup' => sprintf( '<div class="component_data woocommerce-error" data-component_set="false" data-price="0" data-regular_price="0" data-product_type="invalid-data">%s</div>', __( 'Sorry, the selected item cannot be purchased at the moment. Please refresh the page and try again.', 'woocommerce-composite-products' ) )
  114. ) );
  115. die();
  116. }
  117. if ( isset( $_POST[ 'product_id' ] ) && intval( $_POST[ 'product_id' ] ) > 0 && isset( $_POST[ 'component_id' ] ) && ! empty( $_POST[ 'component_id' ] ) && isset( $_POST[ 'composite_id' ] ) && ! empty( $_POST[ 'composite_id' ] ) ) {
  118. $product_id = intval( $_POST[ 'product_id' ] );
  119. $component_id = intval( $_POST[ 'component_id' ] );
  120. $composite_id = intval( $_POST[ 'composite_id' ] );
  121. } else {
  122. echo json_encode( array(
  123. 'result' => 'failure',
  124. 'reason' => 'required params missing',
  125. 'markup' => sprintf( '<div class="component_data woocommerce-error" data-component_set="false" data-price="0" data-regular_price="0" data-product_type="invalid-data">%s</div>', __( 'Sorry, the selected item cannot be purchased at the moment.', 'woocommerce-composite-products' ) )
  126. ) );
  127. die();
  128. }
  129. $composite = wc_get_product( $composite_id );
  130. $composited_product = $composite->get_composited_product( $component_id, $product_id );
  131. $product = $composited_product->get_product();
  132. if ( ! $product || ! $composited_product->is_purchasable() ) {
  133. echo json_encode( array(
  134. 'result' => 'failure',
  135. 'reason' => 'product does not exist or is not purchasable',
  136. 'markup' => sprintf( '<div class="component_data woocommerce-error" data-component_set="false" data-price="0" data-regular_price="0" data-product_type="invalid-product">%s</div>', __( 'Sorry, the selected item cannot be purchased at the moment.', 'woocommerce-composite-products' ) )
  137. ) );
  138. die();
  139. }
  140. $composite->sync_composite();
  141. ob_start();
  142. WC_CP()->api->apply_composited_product_filters( $product, $component_id, $composite );
  143. do_action( 'woocommerce_composite_show_composited_product', $product, $component_id, $composite );
  144. WC_CP()->api->remove_composited_product_filters();
  145. $output = ob_get_clean();
  146. echo json_encode( array(
  147. 'result' => 'success',
  148. 'markup' => $output,
  149. ) );
  150. die();
  151. }
  152. }
  153. WC_CP_AJAX::init();