| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Ui\Editlists; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** Add points to a set of user. */ |
| 8 |
class UsersPointsBulkAction extends \LWS\Adminpanel\EditList\Action |
| 9 |
{ |
| 10 |
private $key = 'points_add'; |
| 11 |
|
| 12 |
function input() |
| 13 |
{ |
| 14 |
$help = esc_attr(__("Enter '=42' to set a total of 42 points to selected users instead of adding it to current amounts.", 'woorewards-lite')); |
| 15 |
$str = "<label for='{$this->key}' title='$help'>".__("Add/Subtract points", 'woorewards-lite')."</label>"; |
| 16 |
$str .= " <input type='text' pattern='[0-9]+' name='{$this->key}' id='{$this->key}' size='4' class='lws-input lws-ignore-confirm'/>"; |
| 17 |
return $str; |
| 18 |
} |
| 19 |
|
| 20 |
function apply($user_ids) |
| 21 |
{ |
| 22 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by editlist framework |
| 23 |
if (!isset($_POST[$this->key])) |
| 24 |
return false; |
| 25 |
$points = \sanitize_text_field(\wp_unslash($_POST[$this->key]));// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 26 |
$set = false; |
| 27 |
if( substr($points, 0, 1) == '=' ) |
| 28 |
{ |
| 29 |
$points = substr($points, 1); |
| 30 |
$set = true; |
| 31 |
} |
| 32 |
if( !is_numeric($points) ) |
| 33 |
return false; |
| 34 |
else if( $set ) |
| 35 |
$points = max(0, \intval($points)); |
| 36 |
|
| 37 |
// load the default pool |
| 38 |
$pools = \LWS\WOOREWARDS\Collections\Pools::instanciate()->load(array( |
| 39 |
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 40 |
array( |
| 41 |
'key' => 'wre_pool_prefab', |
| 42 |
'value' => 'yes', |
| 43 |
'compare' => 'LIKE' |
| 44 |
), |
| 45 |
array( |
| 46 |
'key' => 'wre_pool_type', |
| 47 |
'value' => \LWS\WOOREWARDS\Core\Pool::T_STANDARD, |
| 48 |
'compare' => 'LIKE' |
| 49 |
) |
| 50 |
) |
| 51 |
)); |
| 52 |
|
| 53 |
if( empty($pool = $pools->get(0)) ) |
| 54 |
{ |
| 55 |
\lws_admin_add_notice_once('lws-wre-users-points-add', __("Cannot load default loyalty system.", 'woorewards-lite'), array('level'=>'error')); |
| 56 |
return false; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
$count = 0; |
| 61 |
$reason = __("Commercial operation", 'woorewards-lite'); |
| 62 |
foreach( $user_ids as $user_id ) |
| 63 |
{ |
| 64 |
if( $set ) |
| 65 |
$count += $pool->setPoints($user_id, $points, $reason)->tryUnlock($user_id); |
| 66 |
else if( $points < 0 ) |
| 67 |
$count += $pool->usePoints($user_id, \absint($points), $reason)->tryUnlock($user_id); |
| 68 |
else |
| 69 |
$count += $pool->addPoints($user_id, $points, $reason)->tryUnlock($user_id); |
| 70 |
} |
| 71 |
|
| 72 |
$msg = _n("Points added to user.", "Points added to users.", count($user_ids), 'woorewards-lite'); |
| 73 |
if( $count > 0 ) |
| 74 |
$msg .= '<br/>' . _n("A reward was generated.", "Few rewards generated.", $count, 'woorewards-lite'); |
| 75 |
\lws_admin_add_notice_once('lws-wre-users-points-add', $msg, array('level'=>'success')); |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
?> |