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