1-customers_export_page.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace cx_newsletter;
  3. class customers_export_page
  4. extends service
  5. implements service_interface {
  6. public function process() : self {
  7. return $this;
  8. }
  9. public function prepare() : self {
  10. \add_rewrite_rule(
  11. 'cx-newsletter/customers-export$',
  12. 'index.php?'.$this->param.'=1',
  13. 'top'
  14. );
  15. global $wp_rewrite;
  16. $wp_rewrite->flush_rules(true);
  17. \add_filter('query_vars', [$this, 'add_query_var']);
  18. \add_action('parse_request', [$this, 'parse_request']);
  19. return $this;
  20. }
  21. public function add_query_var(array $vars) : array {
  22. array_push($vars, $this->param);
  23. return $vars;
  24. }
  25. public function parse_request($request) : void {
  26. if (!array_key_exists($this->param, $request->query_vars)) {
  27. return;
  28. }
  29. if (!\current_user_can('export')) {
  30. echo('You must login as user that could export content.');
  31. exit();
  32. }
  33. $mapper = new customers_mapper(
  34. $this->get_database(),
  35. $this->get_tables()
  36. );
  37. $converter = new customers_converter();
  38. $all = [];
  39. foreach ($mapper->load_all() as $customer) {
  40. $customer = $converter->load_object($customer)->get_array();
  41. unset($customer['id']);
  42. array_push($all, $customer);
  43. }
  44. echo(json_encode($all));
  45. header("Content-Type: application/json");
  46. exit();
  47. }
  48. private string $param = 'cx_newsletter_must_export_customers_now';
  49. }