container = $container; $this->init(); } public function init() { if ($name = $this->container->getSettings()->get('revision', 'name')) { add_rewrite_rule('^admitad\/' . $name . '\.xml$', 'index.php?admitad_revision=xml', 'top'); add_filter('template_redirect', array($this, 'generateRevisionContent'), 1); add_filter('query_vars', array($this, 'addQuery'), 1); flush_rewrite_rules(); } } public function addQuery($vars) { $vars[] = 'admitad_revision'; return $vars; } public function generateRevisionContent() { global $wp_query; if (isset($wp_query->query_vars['admitad_revision'])) { $wp_query->is_404 = false; $username = $this->container->getSettings()->get('revision', 'login'); $secret = $this->container->getSettings()->get('revision', 'password'); if (!isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['PHP_AUTH_PW']) && ($_SERVER['PHP_AUTH_USER'] != $username || $_SERVER['PHP_AUTH_PW'] != $secret)) { header('WWW-Authenticate: Basic realm="Authorization"'); header('HTTP/1.0 401 Unauthorized'); die; }; header('Content-type: application/xml; charset=utf-8'); echo '' . "\n"; echo '' . "\n" . $this->getRevisionContent() . ''; exit; } } protected function getRevisionContent() { $posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'shop_order', 'post_status' => array_keys(wc_get_order_statuses()), )); $content = ''; /** @var \WP_Post $post */ foreach ($posts as $post) { $order = new \WC_Order($post->ID); if (!$this->hasAdmitadUid($order)) { continue; } if (!$status = $this->getOrderStatus($order)) { continue; } $content .= trim(" {$order->get_id()} {$status} ") . "\n"; } return $content; } protected function getOrderStatus(\WC_Order $order) { if (in_array($order->get_status(), array('cancelled', 'refunded', 'failed'))) { return 2; } if ($order->get_status() == 'completed') { return 1; } return 0; } protected function hasAdmitadUid(\WC_Order $order) { $data = $order->get_meta_data(); foreach ($data as $object) { if ($object->key == 'admitad_uid') { return true; } } return true; } }