class-db-assets-manager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. `product_variations_name`.`name` AS `prod_var_type`,
  21. `images`.`guid` AS `prod_img_url`,
  22. `order_products`.`product_qty` AS `qty`
  23. FROM (
  24. (
  25. (
  26. (
  27. (
  28. (
  29. (
  30. `{$wpdb->prefix}wc_orders` `orders`
  31. JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products`
  32. ON
  33. (
  34. `orders`.`id` = `order_products`.`order_id`
  35. )
  36. )
  37. JOIN `{$wpdb->posts}` `products`
  38. ON
  39. (
  40. `products`.`ID` = `order_products`.`product_id`
  41. )
  42. )
  43. JOIN `{$wpdb->posts}` `product_variations`
  44. ON
  45. (
  46. `product_variations`.`ID` = `order_products`.`variation_id`
  47. )
  48. LEFT JOIN `{$wpdb->prefix}wc_product_attributes_lookup` `product_variations_attr`
  49. ON
  50. (
  51. `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'
  52. )
  53. LEFT JOIN `{$wpdb->terms}` `product_variations_name`
  54. ON
  55. (
  56. `product_variations_name`.`term_id` = `product_variations_attr`.`term_id`
  57. )
  58. )
  59. LEFT JOIN `{$wpdb->postmeta}` `product_meta`
  60. ON
  61. (
  62. `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
  63. )
  64. )
  65. LEFT JOIN `{$wpdb->posts}` `images`
  66. ON
  67. (
  68. `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
  69. )
  70. JOIN `{$wpdb->term_relationships}` `tr`
  71. ON
  72. (`products`.`ID` = `tr`.`object_id`)
  73. )
  74. JOIN `{$wpdb->term_taxonomy}` `tt`
  75. ON
  76. (
  77. `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  78. )
  79. )
  80. JOIN `{$wpdb->terms}` `t`
  81. ON
  82. (`tt`.`term_id` = `t`.`term_id`)
  83. )
  84. WHERE
  85. `orders`.`status` = 'wc-processing' AND `tt`.`taxonomy` = 'product_cat'
  86. ORDER BY
  87. `orders`.`id`;";
  88. $wpdb->query($sql);
  89. UtilsLog::log('View created');
  90. }
  91. /**
  92. * Drop the database views.
  93. */
  94. public static function drop_views() {
  95. global $wpdb;
  96. $sql = "DROP VIEW IF EXISTS vsu_orders_car_prod_qty_img";
  97. $wpdb->query($sql);
  98. UtilsLog::log('View dropped');
  99. }
  100. /**
  101. * Activation hook callback.
  102. */
  103. public static function on_activate() {
  104. self::create_views();
  105. }
  106. /**
  107. * Deactivation hook callback.
  108. */
  109. public static function on_deactivate() {
  110. self::drop_views();
  111. }
  112. }