class-db-assets-manager.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. `{$wpdb->prefix}wc_orders` `orders`
  24. JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products` ON `orders`.`id` = `order_products`.`order_id`
  25. JOIN `{$wpdb->posts}` `products` ON `products`.`ID` = `order_products`.`product_id`
  26. JOIN `{$wpdb->posts}` `images` ON `images`.`post_parent` = `products`.`ID` AND `images`.`post_type` = 'attachment'
  27. JOIN `{$wpdb->posts}` `product_variations` ON `product_variations`.`ID` = `order_products`.`variation_id`
  28. JOIN `{$wpdb->term_relationships}` `tr` ON `products`.`ID` = `tr`.`object_id`
  29. JOIN `{$wpdb->term_taxonomy}` `tt` ON `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  30. JOIN `{$wpdb->terms}` `t` ON `tt`.`term_id` = `t`.`term_id`
  31. WHERE
  32. `orders`.`status` = 'wc-processing'
  33. AND `tt`.`taxonomy` = 'product_cat'
  34. ORDER BY
  35. `orders`.`id`";
  36. $wpdb->query($sql);
  37. }
  38. /**
  39. * Drop the database views.
  40. */
  41. public static function drop_views() {
  42. global $wpdb;
  43. $sql = "DROP VIEW IF EXISTS vsu_orders_car_prod_qty_img";
  44. $wpdb->query($sql);
  45. }
  46. /**
  47. * Activation hook callback.
  48. */
  49. public static function on_activate() {
  50. self::create_views();
  51. }
  52. /**
  53. * Deactivation hook callback.
  54. */
  55. public static function on_deactivate() {
  56. self::drop_views();
  57. }
  58. }