class-db-assets-manager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. )
  48. LEFT JOIN `{$wpdb->postmeta}` `product_meta`
  49. ON
  50. (
  51. `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
  52. )
  53. )
  54. LEFT JOIN `{$wpdb->posts}` `images`
  55. ON
  56. (
  57. `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
  58. )
  59. JOIN `{$wpdb->term_relationships}` `tr`
  60. ON
  61. (`products`.`ID` = `tr`.`object_id`)
  62. )
  63. JOIN `{$wpdb->term_taxonomy}` `tt`
  64. ON
  65. (
  66. `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  67. )
  68. )
  69. JOIN `{$wpdb->terms}` `t`
  70. ON
  71. (`tt`.`term_id` = `t`.`term_id`)
  72. )
  73. WHERE
  74. `orders`.`status` = 'wc-processing' AND `tt`.`taxonomy` = 'product_cat'
  75. ORDER BY
  76. `orders`.`id`;";
  77. $wpdb->query($sql);
  78. UtilsLog::log('View created');
  79. }
  80. /**
  81. * Drop the database views.
  82. */
  83. public static function drop_views() {
  84. global $wpdb;
  85. $sql = "DROP VIEW IF EXISTS vsu_orders_car_prod_qty_img";
  86. $wpdb->query($sql);
  87. UtilsLog::log('View dropped');
  88. }
  89. /**
  90. * Activation hook callback.
  91. */
  92. public static function on_activate() {
  93. self::create_views();
  94. }
  95. /**
  96. * Deactivation hook callback.
  97. */
  98. public static function on_deactivate() {
  99. self::drop_views();
  100. }
  101. }