class-db-assets-manager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Database Assets Manager for Studiou WC Order Print Statuses
  4. *
  5. * This class handles the creation and dropping of database assets (views)
  6. * during plugin activation and deactivation.
  7. */
  8. class DB_Assets_Manager {
  9. /**
  10. * Create the necessary database views.
  11. */
  12. public static function create_views() {
  13. global $wpdb;
  14. $sql = "CREATE OR REPLACE VIEW vsu_orders_car_prod_qty_img AS
  15. SELECT
  16. `orders`.`id` AS `order_no`,
  17. `t`.`name` AS `prod_cat`,
  18. `products`.`post_title` AS `prod_name`,
  19. `product_variations`.`post_title` AS `prod_var`,
  20. `images`.`guid` AS `prod_img_url`,
  21. `order_products`.`product_qty` AS `qty`
  22. FROM (
  23. (
  24. (
  25. (
  26. (
  27. (
  28. (
  29. `{$wpdb->prefix}wc_orders` `orders`
  30. JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products`
  31. ON
  32. (
  33. `orders`.`id` = `order_products`.`order_id`
  34. )
  35. )
  36. JOIN `{$wpdb->posts}` `products`
  37. ON
  38. (
  39. `products`.`ID` = `order_products`.`product_id`
  40. )
  41. )
  42. JOIN `{$wpdb->posts}` `product_variations`
  43. ON
  44. (
  45. `product_variations`.`ID` = `order_products`.`variation_id`
  46. )
  47. LEFT JOIN `{$wpdb->prefix}wc_product_attributes_lookup` `product_variations_attr`
  48. ON
  49. (
  50. `product_variations_attr`.`product_id` = `product_variations`.`ID` AND `product_variations_attr`.`product_or_parent_id` = `products`.`ID` AND `product_variations_attr`.`taxonomy` = 'pa_format'
  51. )
  52. LEFT JOIN `{$wpdb->terms}` `product_variations_name`
  53. ON
  54. (
  55. `product_variations_name`.`term_id` = `product_variations_attr`.`term_id`
  56. )
  57. )
  58. LEFT JOIN `{$wpdb->postmeta}` `product_meta`
  59. ON
  60. (
  61. `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
  62. )
  63. )
  64. LEFT JOIN `{$wpdb->posts}` `images`
  65. ON
  66. (
  67. `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
  68. )
  69. JOIN `{$wpdb->term_relationships}` `tr`
  70. ON
  71. (`products`.`ID` = `tr`.`object_id`)
  72. )
  73. JOIN `{$wpdb->term_taxonomy}` `tt`
  74. ON
  75. (
  76. `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  77. )
  78. )
  79. JOIN `{$wpdb->terms}` `t`
  80. ON
  81. (`tt`.`term_id` = `t`.`term_id`)
  82. )
  83. WHERE
  84. `orders`.`status` = 'wc-processing' AND `tt`.`taxonomy` = 'product_cat'
  85. ORDER BY
  86. `orders`.`id`;";
  87. $wpdb->query($sql);
  88. UtilsLog::log('View created');
  89. }
  90. /**
  91. * Drop the database views.
  92. */
  93. public static function drop_views() {
  94. global $wpdb;
  95. $sql = "DROP VIEW IF EXISTS vsu_orders_car_prod_qty_img";
  96. $wpdb->query($sql);
  97. UtilsLog::log('View dropped');
  98. }
  99. /**
  100. * Activation hook callback.
  101. */
  102. public static function on_activate() {
  103. self::create_views();
  104. }
  105. /**
  106. * Deactivation hook callback.
  107. */
  108. public static function on_deactivate() {
  109. self::drop_views();
  110. }
  111. }