WJECF_Debug_CLI

The WooCommerce Extended Coupon Features WJECF Debug CLI class.

Defined (1)

The class is defined in the following location(s).

/includes/WJECF_Debug_CLI.php  
  1. class WJECF_Debug_CLI extends WP_CLI_Command { 
  2.  
  3. /** 
  4. * Display some debugging information 
  5. */ 
  6. public function debug() { 
  7. WP_CLI::log( sprintf("WJECF Version: %s", WJECF()->version ) ); 
  8.  
  9. $coupon = WJECF_WC()->get_coupon('auto-cheapest-item'); 
  10.  
  11. $args = array( 
  12. 'posts_per_page' => -1,  
  13. 'orderby' => 'title',  
  14. 'order' => 'asc',  
  15. 'post_type' => 'shop_coupon',  
  16. 'post_status' => 'publish',  
  17. );  
  18. $posts = get_posts( $args ); 
  19. foreach ( $posts as $post ) { 
  20. $coupon = WJECF_WC()->get_coupon( $post->ID ); 
  21. $this->execute_test_for_coupon( $coupon ); 
  22.  
  23. $args = array( 
  24. 'posts_per_page' => -1,  
  25. 'orderby' => 'title',  
  26. 'order' => 'asc',  
  27. 'post_type' => array( 'product', 'product_variation' ),  
  28. 'post_status' => 'publish',  
  29. );  
  30. $posts = get_posts( $args ); 
  31. foreach ( $posts as $post ) { 
  32. $product = wc_get_product( $post->ID ); 
  33. $this->execute_test_for_product( $product ); 
  34.  
  35.  
  36. protected function execute_test_for_coupon( $coupon ) { 
  37. //WP_CLI::log( sprintf("Coupon fields: %s", print_r( $coupon->coupon_custom_fields ) ) ); 
  38. //WP_CLI::log( sprintf("Coupon fields: %s", print_r( $coupon->get_meta_data() ) ) ); 
  39.  
  40. $meta_keys = array_keys( $coupon->coupon_custom_fields ); 
  41. $meta_keys[] = '__non_existing__'; 
  42.  
  43. //WP_CLI::log( sprintf("Coupon fields: %s", print_r( $coupon->coupon_custom_fields ) ) ); 
  44. //WP_CLI::log( sprintf("Coupon fields: %s", print_r( $coupon->get_meta_data() ) ) ); 
  45.  
  46. $wrap_leg = WJECF_WC()->wrap( $coupon, false ); $wrap_leg->use_wc27 = false; 
  47. $wrap_new = WJECF_WC()->wrap( $coupon, false ); $wrap_new->use_wc27 = true; 
  48.  
  49. $results = array(); 
  50. $results['new'] = $wrap_new->get_id(); 
  51. $results['legacy'] = $wrap_leg->get_id(); 
  52. $results['old'] = $coupon->id;  
  53. $this->assert_same( $results, sprintf('Same coupon id %s', current( $results ) ) ); 
  54.  
  55. foreach( $meta_keys as $meta_key ) { 
  56. for($i=1; $i>=0; $i--) { 
  57. $single = $i>0; 
  58.  
  59. $results = array(); 
  60. $results['new'] = $wrap_new->get_meta( $meta_key, $single ); 
  61. $results['legacy'] = $wrap_leg->get_meta( $meta_key, $single ); 
  62. $results['old'] = get_post_meta( $coupon->id, $meta_key, $single ); 
  63. $this->assert_same( $results, sprintf('%s: Same value %s', $meta_key, $single ? 'single' : 'multi' ) ); 
  64.  
  65.  
  66. protected function execute_test_for_product( $product ) { 
  67. $wrap_leg = WJECF_WC()->wrap( $product, false ); $wrap_leg->use_wc27 = false; 
  68. $wrap_new = WJECF_WC()->wrap( $product, false ); $wrap_new->use_wc27 = true; 
  69.  
  70. if ($product instanceof WC_Product_Variation) { 
  71. $results = array(); 
  72. $results['new'] = $wrap_new->get_product_or_variation_id(); 
  73. $results['legacy'] = $wrap_leg->get_product_or_variation_id(); 
  74. $results['old'] = $product->variation_id;  
  75. $this->assert_same( $results, sprintf('Same variation id %s', current( $results ) ) ); 
  76.  
  77. $results = array(); 
  78. $results['new'] = $wrap_new->get_variable_product_id(); 
  79. $results['legacy'] = $wrap_leg->get_variable_product_id(); 
  80. $results['old'] = $wrap_leg->get_variable_product_id(); 
  81. $this->assert_same( $results, sprintf('Same variable product (parent) id %s', current( $results ) ) );  
  82. } else { 
  83. $results = array(); 
  84. $results['new'] = $wrap_new->get_id(); 
  85. $results['legacy'] = $wrap_leg->get_id(); 
  86. $results['old'] = $product->id; 
  87. $this->assert_same( $results, sprintf('Same product id %s', current( $results ) ) ); 
  88.  
  89.  
  90.  
  91. }  
  92.  
  93. protected function assert_same( $results, $test_description ) { 
  94. $success = true; 
  95. foreach( $results as $result ) { 
  96. if ( isset( $prev_result ) && $result !== $prev_result ) { 
  97. $success = false; 
  98. break; 
  99. $prev_result = $result; 
  100.  
  101. if ( $success ) { 
  102. WP_CLI::success( $test_description ); 
  103. } else { 
  104. foreach( $results as $key => $result ) { 
  105. WP_CLI::log( sprintf("%s : %s", $key, $this->dd( $result ) ) ); 
  106. WP_CLI::error( $test_description ); 
  107.  
  108. protected function dd( $variable ) { 
  109. return print_r( $variable, true ); 
  110.  
  111.  
  112. // /** 
  113. // * Display expiry information for the given product. 
  114. // *  
  115. // * ## OPTIONS 
  116. // * 
  117. // * <product_id>... 
  118. // * : The product ID. 
  119. // *  
  120. // */ 
  121. // public function product( $args ) { 
  122. // } 
  123.