| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class ProductQueryService |
| 10 |
{ |
| 11 |
public function getCreatableProducts(array $args = []): array |
| 12 |
{ |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$postsTable = $wpdb->posts; |
| 16 |
$metaTable = $wpdb->postmeta; |
| 17 |
|
| 18 |
$postsPerPage = $args['posts_per_page'] ?? 100; |
| 19 |
$includeOutOfStock = $args['include_out_of_stock'] ?? false; |
| 20 |
$lastId = intval($args['last_creatable_product_id'] ?? 0); |
| 21 |
$productIdMetaKey = ProductMetaKey::basalamProductId(); |
| 22 |
|
| 23 |
$stockCondition = $includeOutOfStock |
| 24 |
? "" |
| 25 |
: "AND stock.meta_value = 'instock'"; |
| 26 |
|
| 27 |
$query = $wpdb->prepare(" |
| 28 |
SELECT p.ID |
| 29 |
FROM {$postsTable} AS p |
| 30 |
LEFT JOIN {$metaTable} AS thumb |
| 31 |
ON thumb.post_id = p.ID |
| 32 |
AND thumb.meta_key = '_thumbnail_id' |
| 33 |
LEFT JOIN {$metaTable} AS basalam |
| 34 |
ON basalam.post_id = p.ID |
| 35 |
AND basalam.meta_key = %s |
| 36 |
LEFT JOIN {$metaTable} AS stock |
| 37 |
ON stock.post_id = p.ID |
| 38 |
AND stock.meta_key = '_stock_status' |
| 39 |
LEFT JOIN {$metaTable} AS price |
| 40 |
ON price.post_id = p.ID |
| 41 |
AND price.meta_key = '_price' |
| 42 |
WHERE p.post_type = 'product' |
| 43 |
AND p.post_status = 'publish' |
| 44 |
AND thumb.meta_value IS NOT NULL |
| 45 |
AND basalam.post_id IS NULL |
| 46 |
AND p.ID > %d |
| 47 |
AND price.meta_value IS NOT NULL |
| 48 |
AND CAST(price.meta_value AS DECIMAL(10,2)) > 1000 |
| 49 |
{$stockCondition} |
| 50 |
ORDER BY p.ID ASC |
| 51 |
LIMIT %d |
| 52 |
", $productIdMetaKey, $lastId, $postsPerPage); |
| 53 |
|
| 54 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Core WP table identifiers from $wpdb properties and a whitelisted static stock-condition fragment, not user input; values are prepared. |
| 55 |
$ids = $wpdb->get_col($query); |
| 56 |
|
| 57 |
return array_map('intval', $ids); |
| 58 |
} |
| 59 |
|
| 60 |
public function getUpdatableProducts(array $args = []): array |
| 61 |
{ |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
$postsTable = $wpdb->posts; |
| 65 |
$metaTable = $wpdb->postmeta; |
| 66 |
|
| 67 |
$postsPerPage = $args['posts_per_page'] ?? 100; |
| 68 |
$lastId = intval($args['last_updatable_product_id'] ?? 0); |
| 69 |
$productIdMetaKey = ProductMetaKey::basalamProductId(); |
| 70 |
|
| 71 |
$query = $wpdb->prepare(" |
| 72 |
SELECT p.ID |
| 73 |
FROM {$postsTable} AS p |
| 74 |
INNER JOIN {$metaTable} AS basalam |
| 75 |
ON basalam.post_id = p.ID |
| 76 |
AND basalam.meta_key = %s |
| 77 |
WHERE p.post_type = 'product' |
| 78 |
AND p.post_status = 'publish' |
| 79 |
AND p.ID > %d |
| 80 |
ORDER BY p.ID ASC |
| 81 |
LIMIT %d |
| 82 |
", $productIdMetaKey, $lastId, $postsPerPage); |
| 83 |
|
| 84 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Core WP table identifiers from $wpdb properties, not user input; values are prepared. |
| 85 |
$ids = $wpdb->get_col($query); |
| 86 |
|
| 87 |
return array_map('intval', $ids); |
| 88 |
} |
| 89 |
} |
| 90 |
|