| 1 |
<?php |
| 2 |
|
| 3 |
namespace LWS\WOOREWARDS\Ui\Shortcodes; |
| 4 |
|
| 5 |
// don't call the file directly |
| 6 |
if (!defined('ABSPATH')) exit(); |
| 7 |
|
| 8 |
/** Displays the value of points in a points to cart system */ |
| 9 |
class PointsValue |
| 10 |
{ |
| 11 |
public static function install() |
| 12 |
{ |
| 13 |
$me = new self(); |
| 14 |
|
| 15 |
/** Shortcode */ |
| 16 |
\add_shortcode('wr_points_value', array($me, 'shortcode')); |
| 17 |
|
| 18 |
/** Admin */ |
| 19 |
\add_filter('lws_woorewards_shortcodes', array($me, 'admin')); |
| 20 |
\add_filter('lws_woorewards_points_shortcodes', array($me, 'adminPro')); |
| 21 |
} |
| 22 |
|
| 23 |
public function admin($fields) |
| 24 |
{ |
| 25 |
$fields['pointsvalue'] = array( |
| 26 |
'id' => 'lws_woorewards_sc_points_value', |
| 27 |
'title' => __("Points Value", 'woorewards-lite'), |
| 28 |
'type' => 'shortcode', |
| 29 |
'extra' => array( |
| 30 |
'shortcode' => '[wr_points_value]', |
| 31 |
'description' => __("This simple shortcode is used to display how much his points are worth.", 'woorewards-lite') . "<br/>" . |
| 32 |
__("This only works if your points and rewards system is set to points on cart.", 'woorewards-lite'), |
| 33 |
'options' => array( |
| 34 |
array( |
| 35 |
'option' => 'text', |
| 36 |
'desc' => __("The text displayed before the points value.", 'woorewards-lite'), |
| 37 |
), |
| 38 |
array( |
| 39 |
'option' => 'raw', |
| 40 |
'desc' => __("(Optional) If set to true, the result will be displayed as a simple text. Otherwise, it will be wrapped in stylable elements", 'woorewards-lite'), |
| 41 |
), |
| 42 |
), |
| 43 |
'flags' => array('current_user_id'), |
| 44 |
) |
| 45 |
); |
| 46 |
return $fields; |
| 47 |
} |
| 48 |
|
| 49 |
public function adminPro($fields) |
| 50 |
{ |
| 51 |
$fields = $this->admin($fields); |
| 52 |
$fields['pointsvalue']['extra']['options'] = array_merge( |
| 53 |
array( |
| 54 |
'system' => array( |
| 55 |
'option' => 'system', |
| 56 |
'desc' => __("The points and rewards system you want to display. You can find this value in <strong>MyRewards → Points and Rewards</strong>, in the <b>Shortcode Attribute</b> column. If you don’t set this value, nothing will be displayed.", 'woorewards-lite'), |
| 57 |
), |
| 58 |
), |
| 59 |
$fields['pointsvalue']['extra']['options'] |
| 60 |
); |
| 61 |
return $fields; |
| 62 |
} |
| 63 |
|
| 64 |
/** Handle RetroCompatibility */ |
| 65 |
protected function parseArgs($atts) |
| 66 |
{ |
| 67 |
$atts = \wp_parse_args($atts, array( |
| 68 |
'text' => '', |
| 69 |
'raw' => true, |
| 70 |
'floating' => false, |
| 71 |
'decorated' => true, |
| 72 |
)); |
| 73 |
return $atts; |
| 74 |
} |
| 75 |
|
| 76 |
/** Displays the user's points value in currency for a specific pool |
| 77 |
* [wr_points_value system='poolname1' text='Your points are worth' raw='true'] |
| 78 |
* @param $system the loyalty system for which to show the value |
| 79 |
* @param $text text displayed before the points value |
| 80 |
* @param $raw if true, the output is a simple text, otherwise, it's wrapped into dom elements |
| 81 |
*/ |
| 82 |
public function shortcode($atts = array(), $content = '') |
| 83 |
{ |
| 84 |
$userId = \apply_filters('lws_woorewards_shortcode_current_user_id', \get_current_user_id(), $atts, 'wr_points_value'); |
| 85 |
if (!$userId) return $content; |
| 86 |
|
| 87 |
$atts = $this->parseArgs($atts); |
| 88 |
$pools = \apply_filters('lws_woorewards_get_pools_by_args', false, $atts); |
| 89 |
// we only display the value of a simple pool |
| 90 |
$pool = $pools->first(); |
| 91 |
if ($pool && $pool->getOption('direct_reward_mode')) { |
| 92 |
// It's a points on cart pool |
| 93 |
$points = $pool->getPoints($userId); |
| 94 |
if ($points < 0) return ''; |
| 95 |
$rate = $pool->getOption('direct_reward_point_rate'); |
| 96 |
$value = $points * $rate; |
| 97 |
$formatted_value = \LWS\Adminpanel\Tools\Conveniences::getCurrencyPrice( |
| 98 |
$value, |
| 99 |
\LWS\Adminpanel\Tools\Conveniences::argIsTrue($atts['floating']), |
| 100 |
\LWS\Adminpanel\Tools\Conveniences::argIsTrue($atts['decorated']), |
| 101 |
); |
| 102 |
if ($atts['raw']) { |
| 103 |
if ($atts['text'] != '') { |
| 104 |
$content .= \wp_kses_post($atts['text']) . ' '; |
| 105 |
} |
| 106 |
$content .= $formatted_value; |
| 107 |
} else { |
| 108 |
$content .= "<span class='wr-points-value-wrapper'>"; |
| 109 |
if ($atts['text'] != '') { |
| 110 |
$content .= "<span class='wr-points-value-text'>" . \wp_kses_post($atts['text']) . " </span>"; |
| 111 |
} |
| 112 |
$content .= "<span class='wr-points-value-value'>" . $formatted_value . " </span>"; |
| 113 |
$content .= "</span>"; |
| 114 |
} |
| 115 |
} |
| 116 |
return $content; |
| 117 |
} |
| 118 |
} |