AdminDismissibleNotice.php
4 years ago
AdminErrorNotice.php
6 years ago
AdminNotice.php
4 years ago
Filters.php
9 years ago
OrderQuery.php
2 years ago
SitewideAdminDismissibleNotice.php
2 years ago
OrderQuery.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\WordPress; |
| 4 | |
| 5 | |
| 6 | class OrderQuery |
| 7 | { |
| 8 | private $post_id = false; |
| 9 | |
| 10 | public $query = ['post_type' => 'shop_order']; |
| 11 | |
| 12 | public function getOrders($offset = 0, $limit = 0, $post_id = false) |
| 13 | { |
| 14 | global $wpdb; |
| 15 | |
| 16 | $query = $this->getQuery($offset, $limit, $post_id); |
| 17 | |
| 18 | return $wpdb->get_results($query); |
| 19 | } |
| 20 | |
| 21 | public function getQuery($offset = 0, $limit = 0, $post_id = false) { |
| 22 | |
| 23 | if($post_id){ |
| 24 | $this->post_id = $post_id; |
| 25 | }else if($this->post_id){ |
| 26 | $post_id = $this->post_id; |
| 27 | } |
| 28 | |
| 29 | // default order_by |
| 30 | $order_by = ' Order By id ASC '; |
| 31 | |
| 32 | $post_id_where = ''; |
| 33 | |
| 34 | // Handle RTE exports. |
| 35 | if(isset(\XmlExportEngine::$exportOptions['enable_real_time_exports']) |
| 36 | && \XmlExportEngine::$exportOptions['enable_real_time_exports']){ |
| 37 | $limit = 1; |
| 38 | // We want to get the newest order as this is only used when generating an example file. |
| 39 | $order_by = ' Order By id DESC '; |
| 40 | } |
| 41 | |
| 42 | // Handle RTE or other exports where a single post_id is provided. |
| 43 | if($post_id){ |
| 44 | $post_id_where = ' AND id = "'.$post_id.'" '; |
| 45 | } |
| 46 | |
| 47 | // Order by - allow override |
| 48 | $order_by = apply_filters('wp_all_export_order_by', $order_by); |
| 49 | |
| 50 | global $wpdb; |
| 51 | |
| 52 | $defaultQuery = "SELECT * FROM {$wpdb->prefix}wc_orders "; |
| 53 | |
| 54 | if(!\PMXE_Plugin::$session) { |
| 55 | $customWhere = \XmlExportEngine::$exportOptions['whereclause']; |
| 56 | $customJoins = \XmlExportEngine::$exportOptions['joinclause']; |
| 57 | } else { |
| 58 | $customWhere = \PMXE_Plugin::$session->get('whereclause'); |
| 59 | $customJoins = \PMXE_Plugin::$session->get('joinclause'); |
| 60 | } |
| 61 | if (is_countable($customJoins) && count($customJoins)) { |
| 62 | foreach($customJoins as $join) { |
| 63 | $defaultQuery = $defaultQuery . $join; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $defaultQuery .= " WHERE status != 'auto-draft' AND type = 'shop_order' "; |
| 68 | |
| 69 | $defaultQuery = $defaultQuery . $customWhere . $post_id_where; |
| 70 | |
| 71 | $export_id = $this->get_export_id(); |
| 72 | $export = new \PMXE_Export_Record(); |
| 73 | $export->getById($export_id); |
| 74 | |
| 75 | if ($this->is_export_new_stuff()) { |
| 76 | |
| 77 | if ($export->iteration > 0) { |
| 78 | $postsToExclude = array(); |
| 79 | $postList = new \PMXE_Post_List(); |
| 80 | |
| 81 | $postsToExcludeSql = 'SELECT post_id FROM ' . $postList->getTable() . ' WHERE export_id = %d AND iteration < %d'; |
| 82 | $results = $wpdb->get_results($wpdb->prepare($postsToExcludeSql, $export->id, $export->iteration)); |
| 83 | |
| 84 | foreach ($results as $result) { |
| 85 | $postsToExclude[] = $result->post_id; |
| 86 | } |
| 87 | |
| 88 | if (count($postsToExclude)) { |
| 89 | $defaultQuery .= $this->get_exclude_query_where($postsToExclude); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | if ($this->is_export_modfified_stuff() && !empty($export->registered_on)) { |
| 96 | |
| 97 | $export_id = $this->get_export_id(); |
| 98 | $export = new \PMXE_Export_Record(); |
| 99 | $export->getById($export_id); |
| 100 | |
| 101 | $defaultQuery .= $this->get_modified_query_where($export); |
| 102 | } |
| 103 | |
| 104 | // Add order by |
| 105 | $defaultQuery = $defaultQuery . $order_by; |
| 106 | |
| 107 | // Don't set a limit when we are filtering by a single ID anyway. |
| 108 | if (!$post_id && isset($offset) && isset($limit) && $limit) { |
| 109 | $limit_query = " LIMIT $offset, $limit "; |
| 110 | $defaultQuery = $defaultQuery . $limit_query; |
| 111 | } |
| 112 | |
| 113 | return $defaultQuery; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | public function get_exclude_query_where($postsToExclude) |
| 118 | { |
| 119 | global $wpdb; |
| 120 | |
| 121 | return " AND ({$wpdb->prefix}wc_orders.id NOT IN (" . implode(',', $postsToExclude) . "))"; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | public function get_modified_query_where($export) |
| 126 | { |
| 127 | global $wpdb; |
| 128 | |
| 129 | return " AND {$wpdb->prefix}wc_orders.date_updated_gmt > '" . $export->registered_on . "' "; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return bool |
| 134 | */ |
| 135 | protected function is_export_new_stuff() |
| 136 | { |
| 137 | |
| 138 | $export_id = $this->get_export_id(); |
| 139 | |
| 140 | return (!empty(\XmlExportEngine::$exportOptions['export_only_new_stuff']) && |
| 141 | $export_id); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return bool |
| 146 | */ |
| 147 | protected function is_export_modfified_stuff() |
| 148 | { |
| 149 | |
| 150 | $export_id = $this->get_export_id(); |
| 151 | |
| 152 | return (!empty(\XmlExportEngine::$exportOptions['export_only_modified_stuff']) && |
| 153 | $export_id); |
| 154 | } |
| 155 | |
| 156 | private function get_export_id() |
| 157 | { |
| 158 | $input = new \PMXE_Input(); |
| 159 | $export_id = $input->get('id', 0); |
| 160 | |
| 161 | if(!$export_id) { |
| 162 | $export_id = $input->get('export_id', 0); |
| 163 | } |
| 164 | |
| 165 | return $export_id; |
| 166 | } |
| 167 | |
| 168 | } |