| 1 |
<?php |
| 2 |
|
| 3 |
namespace LWS\WOOREWARDS; |
| 4 |
|
| 5 |
// don't call the file directly |
| 6 |
if (!defined('ABSPATH')) exit(); |
| 7 |
|
| 8 |
/** Conveniences Class for shared functions*/ |
| 9 |
class Conveniences |
| 10 |
{ |
| 11 |
public static function install() |
| 12 |
{ |
| 13 |
$me = &self::instance(); |
| 14 |
\add_filter('lws_woorewards_get_pools_by_args', array($me, 'getPrefabPool'), 20, 2); //Lowest priority than WooRewards Pro |
| 15 |
} |
| 16 |
|
| 17 |
/** @return object singleton instance */ |
| 18 |
static function &instance() |
| 19 |
{ |
| 20 |
static $_inst = false; |
| 21 |
if (false === $_inst) |
| 22 |
$_inst = new self(); |
| 23 |
return $_inst; |
| 24 |
} |
| 25 |
|
| 26 |
/** prevent outside instanciation */ |
| 27 |
protected function __construct() |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
/** Test if the content should be displayed or not |
| 32 |
* @param $stepVersion → Version after which the legacy content is not displayed anymore |
| 33 |
* @return (bool) if we assume current install should use legacy feature. */ |
| 34 |
function isLegacyShown($stepVersion) |
| 35 |
{ |
| 36 |
static $installVers = false; |
| 37 |
if (false === $installVers) |
| 38 |
$installVers = \get_option('lws_woorewards_install_version', '0'); |
| 39 |
return (!$installVers || \version_compare($installVers, $stepVersion, '<')); |
| 40 |
} |
| 41 |
|
| 42 |
/** Returns the Prefab Pool if it exists |
| 43 |
* Hooked to 'lws_woorewards_get_pools_by_args' */ |
| 44 |
function getPrefabPool($pools, $atts) |
| 45 |
{ |
| 46 |
if (false === $pools) { |
| 47 |
$pools = \LWS\WOOREWARDS\Collections\Pools::instanciate()->load(array( |
| 48 |
'numberposts' => 1, |
| 49 |
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 50 |
array( |
| 51 |
'key' => 'wre_pool_prefab', |
| 52 |
'value' => 'yes', |
| 53 |
'compare' => 'LIKE' |
| 54 |
), |
| 55 |
array( |
| 56 |
'key' => 'wre_pool_type', |
| 57 |
'value' => \LWS\WOOREWARDS\Core\Pool::T_STANDARD, |
| 58 |
'compare' => 'LIKE' |
| 59 |
) |
| 60 |
), |
| 61 |
'deep' => true, |
| 62 |
)); |
| 63 |
|
| 64 |
if (!(isset($atts['force']) && $atts['force'])) |
| 65 |
$pools = $pools->filterByUserCan(\get_current_user_id()); |
| 66 |
} |
| 67 |
return $pools; |
| 68 |
} |
| 69 |
|
| 70 |
/* Get Available WooCommerce coupons for the provided user */ |
| 71 |
public function getCoupons($userId, $where=false) |
| 72 |
{ |
| 73 |
$user = \get_user_by('ID', $userId); |
| 74 |
if (!$user->user_email) return []; |
| 75 |
|
| 76 |
global $wpdb; |
| 77 |
$query = \LWS\Adminpanel\Tools\Request::from($wpdb->posts, 'p'); |
| 78 |
$query->select(['p.ID', 'p.post_content', 'p.post_title', 'p.post_excerpt', 'MAX(e.meta_value) AS expiry_date']); |
| 79 |
$query->group('p.ID'); |
| 80 |
$query->innerJoin($wpdb->postmeta, 'm', ["p.ID = m.post_id AND m.meta_key='customer_email'"]); |
| 81 |
$query->leftJoin($wpdb->postmeta, 'l', ["l.post_id = m.post_id AND l.meta_key='usage_limit'"]); |
| 82 |
$query->leftJoin($wpdb->postmeta, 'u', ["u.post_id = m.post_id AND u.meta_key='usage_count'"]); |
| 83 |
$query->leftJoin($wpdb->postmeta, 'e', ["e.post_id = m.post_id AND e.meta_key='date_expires'"]); |
| 84 |
$query->where([ |
| 85 |
"m.meta_value = %s", |
| 86 |
"post_type = 'shop_coupon'", |
| 87 |
"post_status = 'publish'", |
| 88 |
"(e.meta_value is NULL OR e.meta_value = '' OR e.meta_value >= %s)", |
| 89 |
"(u.meta_value < l.meta_value OR u.meta_value IS NULL OR l.meta_value IS NULL OR l.meta_value=0)", |
| 90 |
]); |
| 91 |
$query->arg(\serialize([$user->user_email])); |
| 92 |
$query->arg(\strtotime(\gmdate('Y-m-d'))); |
| 93 |
if ($where) { |
| 94 |
$query->where($where); |
| 95 |
} |
| 96 |
|
| 97 |
$result = $query->getResults(OBJECT_K); |
| 98 |
if (!$result) return []; |
| 99 |
|
| 100 |
$ids = implode(",", array_map('intval', array_keys($result))); |
| 101 |
$query = "SELECT p.ID, v.meta_value AS coupon_amount, f.meta_value AS wr_prod_ids, o.meta_value AS product_ids, w.meta_value AS discount_type" |
| 102 |
. " FROM {$wpdb->posts} as p" |
| 103 |
. " LEFT JOIN {$wpdb->postmeta} as w ON p.ID = w.post_id AND w.meta_key='discount_type'" |
| 104 |
. " LEFT JOIN {$wpdb->postmeta} as v ON p.ID = v.post_id AND v.meta_key='coupon_amount'" |
| 105 |
. " LEFT JOIN {$wpdb->postmeta} as o ON p.ID = o.post_id AND o.meta_key='product_ids'" |
| 106 |
. " LEFT JOIN {$wpdb->postmeta} as f ON p.ID = f.post_id AND f.meta_key='woorewards_product_list'" |
| 107 |
. " WHERE p.ID IN ({$ids})"; |
| 108 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $query built from wpdb tables and intval'd IDs |
| 109 |
$sub = $wpdb->get_results($query, OBJECT_K); |
| 110 |
foreach ($sub as $id => $info) { |
| 111 |
foreach ($info as $k => $v) |
| 112 |
$result[$id]->$k = $v; |
| 113 |
} |
| 114 |
return $result; |
| 115 |
} |
| 116 |
|
| 117 |
/** Avoid overstock WC_Order::add_order_note and pack them in our own metabox. |
| 118 |
* As WC, comment the order. |
| 119 |
* @param $order (\WC_Order|int) |
| 120 |
* @param $note (string) the message |
| 121 |
* @param $source mixed (\LWS\WOOREWARDS\Core\Pool|string|false) the pool, the stack id or any relevant origin |
| 122 |
* @return integer the new comment id or false on error. */ |
| 123 |
public static function addOrderNote($order, $note, $source=false) |
| 124 |
{ |
| 125 |
return \LWS\WOOREWARDS\Core\OrderNote::add($order, $note, $source); |
| 126 |
} |
| 127 |
|
| 128 |
/** @param $user (int|\WP_User|string) user Id or instance, a string is assumed as user email. |
| 129 |
* @param $exceptOrderId (false|int|[int]) ignore given order id. |
| 130 |
* @param $source (string) a caller reference (for information purpose only). |
| 131 |
* @param $extendedEmail (bool|string) look not only in billing email but associated user current email too. Can be a email string to test. |
| 132 |
* @return (int) */ |
| 133 |
public static function getOrderCount($user, $exceptOrderId=false, $source=false, $extendedEmail=false) |
| 134 |
{ |
| 135 |
if (!$user) { |
| 136 |
if ($extendedEmail && \is_string($extendedEmail)) |
| 137 |
$user = $extendedEmail; |
| 138 |
else |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
$userId = 0; |
| 143 |
$email = ''; |
| 144 |
if (\is_object($user)) { |
| 145 |
$userId = (int)$user->ID; |
| 146 |
$email = $user->user_email; |
| 147 |
} elseif (\is_numeric($user)) { |
| 148 |
$userId = \intval($user); |
| 149 |
$user = \get_user_by('ID', $userId); |
| 150 |
if ($user && $user->exists()) |
| 151 |
$email = $user->user_email; |
| 152 |
} else { // assume a string email |
| 153 |
$email = $user; |
| 154 |
} |
| 155 |
$transient = $userId . $email; |
| 156 |
|
| 157 |
if ($extendedEmail) { |
| 158 |
if (\is_string($extendedEmail) && \strtolower($email) != \strtolower($extendedEmail)) { |
| 159 |
$transient .= ('-' . $extendedEmail); |
| 160 |
} else { |
| 161 |
$transient .= '-='; |
| 162 |
$extendedEmail = $email; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$isListOfExceptOrderId = false; |
| 167 |
if ($exceptOrderId) { |
| 168 |
if (\is_array($exceptOrderId)) { |
| 169 |
$isListOfExceptOrderId = true; |
| 170 |
$exceptOrderId = \array_map('\intval', $exceptOrderId); |
| 171 |
\sort($exceptOrderId, SORT_NUMERIC); |
| 172 |
$exceptOrderId = \implode(',', $exceptOrderId); |
| 173 |
$transient .= ('_' . $exceptOrderId); |
| 174 |
} else { |
| 175 |
$exceptOrderId = \intval($exceptOrderId); |
| 176 |
$transient .= ('_' . $exceptOrderId); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
static $cache = array(); |
| 181 |
if (isset($cache[$transient])) { |
| 182 |
return $cache[$transient]; |
| 183 |
} |
| 184 |
|
| 185 |
global $wpdb; |
| 186 |
if (\LWS\Adminpanel\Tools\Conveniences::isHPOS()) { |
| 187 |
// order has his owr table |
| 188 |
$query = \LWS\Adminpanel\Tools\Request::from($wpdb->prefix . 'wc_orders', 'p'); |
| 189 |
$query->where("p.type='shop_order'"); |
| 190 |
$query->select('COUNT(p.id)'); |
| 191 |
|
| 192 |
$where = array(); |
| 193 |
if ($userId || $extendedEmail) { |
| 194 |
if ($userId) { |
| 195 |
$where[] = 'p.customer_id = %d'; |
| 196 |
$query->arg($userId); |
| 197 |
} |
| 198 |
if ($extendedEmail) { |
| 199 |
$query->leftJoin($wpdb->users, 'u', "p.customer_id=u.ID"); |
| 200 |
$where[] = 'u.user_email = %s'; |
| 201 |
$query->arg($extendedEmail); |
| 202 |
if (\is_string($extendedEmail) && $email != $extendedEmail) { |
| 203 |
$where[] = 'u.user_email = %s'; |
| 204 |
$query->arg($email); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
if ($email) { |
| 209 |
$where[] = 'p.billing_email = %s'; |
| 210 |
$query->arg($email); |
| 211 |
if ($extendedEmail && \is_string($extendedEmail) && $email != $extendedEmail) { |
| 212 |
$where[] = 'p.billing_email = %s'; |
| 213 |
$query->arg($extendedEmail); |
| 214 |
} |
| 215 |
} |
| 216 |
if (count($where) > 1) { |
| 217 |
$where['condition'] = 'OR'; |
| 218 |
$query->where($where); |
| 219 |
} elseif ($where) { |
| 220 |
$query->where($where[0]); |
| 221 |
} |
| 222 |
|
| 223 |
$status = \apply_filters('lws_woorewards_ignored_order_status_for_count', array(), $source); |
| 224 |
if ($status) { |
| 225 |
if (count($status) > 1) { |
| 226 |
$status = \implode("','", \array_map(function($s) { |
| 227 |
return \esc_sql('wc-' . $s); |
| 228 |
}, $status)); |
| 229 |
$query->where(sprintf("p.status NOT IN ('%s')", $status)); |
| 230 |
} else { |
| 231 |
$status = \reset($status); |
| 232 |
$query->where('p.status <> %s')->arg('wc-' . $status); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if ($exceptOrderId) { |
| 237 |
if ($isListOfExceptOrderId) { |
| 238 |
$query->where(sprintf('p.id NOT IN (%s)'), $exceptOrderId); |
| 239 |
} else { |
| 240 |
$query->where('p.id <> %d')->arg($exceptOrderId); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $cache[$transient] = (int)$query->getVar(); |
| 245 |
} else { |
| 246 |
// order in WP_Post |
| 247 |
$union = array(); |
| 248 |
|
| 249 |
if ($userId || $extendedEmail) { |
| 250 |
$query = \LWS\Adminpanel\Tools\Request::from($wpdb->postmeta, 'c'); |
| 251 |
$query->select('c.post_id as order_id'); |
| 252 |
$query->where("c.meta_key='_customer_user'"); |
| 253 |
|
| 254 |
$where = array(); |
| 255 |
if ($userId) { |
| 256 |
$where[] = 'c.meta_value = %d'; |
| 257 |
$query->arg($userId); |
| 258 |
} |
| 259 |
if ($extendedEmail) { |
| 260 |
$query->leftJoin($wpdb->users, 'u', "c.meta_value=u.ID"); |
| 261 |
$where[] = 'u.user_email = %s'; |
| 262 |
$query->arg($extendedEmail); |
| 263 |
if (\is_string($extendedEmail) && $email != $extendedEmail) { |
| 264 |
$where[] = 'u.user_email = %s'; |
| 265 |
$query->arg($email); |
| 266 |
} |
| 267 |
} |
| 268 |
if (count($where) > 1) { |
| 269 |
$where['condition'] = 'OR'; |
| 270 |
$query->where($where); |
| 271 |
} elseif ($where) { |
| 272 |
$query->where($where[0]); |
| 273 |
} |
| 274 |
$union[] = $query->toString(); |
| 275 |
} |
| 276 |
if ($email) { |
| 277 |
$query = \LWS\Adminpanel\Tools\Request::from($wpdb->postmeta, 'm'); |
| 278 |
$query->select('m.post_id as order_id'); |
| 279 |
$query->where("m.meta_key='_billing_email'"); |
| 280 |
if ($extendedEmail && \is_string($extendedEmail) && $email != $extendedEmail) { |
| 281 |
$query->where('(m.meta_value = %s OR m.meta_value = %s)'); |
| 282 |
$query->arg($email)->arg($extendedEmail); |
| 283 |
} else { |
| 284 |
$query->where('m.meta_value = %s')->arg($email); |
| 285 |
} |
| 286 |
$union[] = $query->toString(); |
| 287 |
} |
| 288 |
|
| 289 |
$wOrder = array( |
| 290 |
"p.post_type='shop_order'", |
| 291 |
); |
| 292 |
$status = \apply_filters('lws_woorewards_ignored_order_status_for_count', array(), $source); |
| 293 |
if ($status) { |
| 294 |
if (count($status) > 1) { |
| 295 |
$status = \implode("','", \array_map(function($s) { |
| 296 |
return \esc_sql('wc-' . $s); |
| 297 |
}, $status)); |
| 298 |
$wOrder[] = sprintf("p.post_status NOT IN ('%s')", $status); |
| 299 |
} else { |
| 300 |
$status = \reset($status); |
| 301 |
$wOrder[] = sprintf("p.post_status <> '%s'", \esc_sql('wc-' . $status)); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if ($exceptOrderId) { |
| 306 |
if ($isListOfExceptOrderId) { |
| 307 |
$wOrder[] = sprintf('p.ID NOT IN (%s)', $exceptOrderId); |
| 308 |
} else { |
| 309 |
$wOrder[] = sprintf('p.ID <> %d', (int)$exceptOrderId); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
$wOrder = \implode(' AND ', $wOrder); |
| 314 |
if ($union) { |
| 315 |
$union = \implode(" UNION ", $union); |
| 316 |
$query = "SELECT COUNT(p.ID) FROM (" |
| 317 |
. " {$union}" |
| 318 |
. " ) as a" |
| 319 |
. " INNER JOIN {$wpdb->posts} as p ON p.ID=a.order_id AND {$wOrder}"; |
| 320 |
} else { |
| 321 |
$query = "SELECT COUNT(p.ID) FROM {$wpdb->posts} as p" |
| 322 |
. " WHERE {$wOrder}"; |
| 323 |
} |
| 324 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- dynamic query built from sanitized parts |
| 325 |
return $cache[$transient] = (int)$wpdb->get_var($query); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/** @return array order status (as in database status field) |
| 331 |
* indicating a processed order. */ |
| 332 |
static public function getOrderDoneStatus(): array |
| 333 |
{ |
| 334 |
static $list = null; |
| 335 |
if (null === $list) { |
| 336 |
// WC_Order like status values |
| 337 |
$paid = \wc_get_is_paid_statuses(); |
| 338 |
// Database like status values |
| 339 |
$all = \wc_get_order_statuses(); |
| 340 |
// get first list but as database like format |
| 341 |
foreach ($paid as $index => $status) { |
| 342 |
$prefixed = 'wc-' . $status; |
| 343 |
if (isset($all[$prefixed])) $paid[$index] = $prefixed; |
| 344 |
} |
| 345 |
$paid[] = 'wc-refunded'; |
| 346 |
$list = \apply_filters('lws_woorewards_product_ordered_status_list', $paid); |
| 347 |
} |
| 348 |
return $list; |
| 349 |
} |
| 350 |
} |