PluginProbe
MyRewards / trunk
MyRewards vtrunk
5.7.8 5.7.7 5.7.6 trunk 1.2.2 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.1 2.2.0 2.3.0 2.4.0 2.4.1 2.5.0 2.6.4 2.6.6 3.0.0.0 3.1.0 3.1.2 3.1.2.1 3.10.4 3.10.9 All 165 releases
woorewards / include / ui / shortcodes / pointsbalance.php

pointsbalance.php in MyRewards trunk, at include/ui/shortcodes/pointsbalance.php

226 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LWS\WOOREWARDS\Ui\Shortcodes;
4
5 // don't call the file directly
6 if (!defined('ABSPATH')) exit();
7
8 class PointsBalance
9 {
10 public static function install()
11 {
12 $me = new self();
13
14 /** Shortcode */
15 \add_shortcode('wr_points_balance', array($me, 'shortcode'));
16
17 /** Admin */
18 \add_filter('lws_woorewards_shortcodes', array($me, 'admin'));
19 \add_filter('lws_woorewards_points_shortcodes', array($me, 'adminPro'), 4);
20
21 /** Scripts */
22 \add_action('wp_enqueue_scripts', array($me, 'registerScripts'));
23
24 }
25
26 function registerScripts()
27 {
28 \wp_register_style('wr-points-balance', LWS_WOOREWARDS_CSS . '/shortcodes/points-balance.min.css', array(), LWS_WOOREWARDS_VERSION);
29 }
30
31 protected function enqueueScripts()
32 {
33 \wp_enqueue_style('wr-points-balance');
34 }
35
36 /** Get the shortcode admin */
37 public function admin($fields)
38 {
39 $fields['pointsbalance'] = array(
40 'id' => 'lws_woorewards_points_balance',
41 'title' => __("Points Balance", 'woorewards-lite'),
42 'type' => 'shortcode',
43 'extra' => array(
44 'shortcode' => '[wr_points_balance]',
45 'description' => __("Use this shortcode to display their points balance to your customers.", 'woorewards-lite') . "<br/>" .
46 __("Use the following options to change how the balance is displayed.", 'woorewards-lite'),
47 'options' => array(
48 'element' => array(
49 'option' => 'element',
50 'desc' => __("(Optional) Select how the points balance element is displayed. 3 possible values :", 'woorewards-lite'),
51 'options' => array(
52 array(
53 'option' => 'none',
54 'desc' => __("Default value. Simple text without stylable elements", 'woorewards-lite'),
55 ),
56 array(
57 'option' => 'tile',
58 'desc' => __("Stylable tile with a background color", 'woorewards-lite'),
59 ),
60 array(
61 'option' => 'line',
62 'desc' => __("Horizontal display in stylable elements", 'woorewards-lite'),
63 ),
64 ),
65 'example' => '[wr_points_balance element="tile"]'
66 ),
67 'display' => array(
68 'option' => 'display',
69 'desc' => __("(Optional) Select how the points are displayed. 2 possible values :", 'woorewards-lite'),
70 'options' => array(
71 array(
72 'option' => 'formatted',
73 'desc' => __("Default. Points are formatted with the points currency/name", 'woorewards-lite'),
74 ),
75 array(
76 'option' => 'simple',
77 'desc' => __("Only the points balance numeric value is displayed", 'woorewards-lite'),
78 ),
79 ),
80 'example' => '[wr_points_balance display="simple"]'
81 ),
82 'showname' => array(
83 'option' => 'showname',
84 'desc' => __("(Optional) If set, will force the display of the points and rewards system name", 'woorewards-lite'),
85 'example' => '[wr_points_balance showname="yes"]',
86 ),
87 'showcurrency' => array(
88 'option' => 'showcurrency',
89 'desc' => __("(Optional) Set to 'off' to do not append points currency but show only the number", 'woorewards-lite'),
90 'example' => '[wr_points_balance showcurrency="yes"]',
91 ),
92 ),
93 'flags' => array('current_user_id'),
94 )
95 );
96 return $fields;
97 }
98
99 public function adminPro($fields)
100 {
101 $fields = $this->admin($fields);
102 $fields['pointsbalance']['extra']['options'] = array_merge(
103 array(
104 'system' => array(
105 'option' => 'system',
106 'desc' => __("(Optional, comma separated) Select the points and rewards systems you want to show. If left empty, all active systems are displayed", 'woorewards-lite') .
107 "<br/>" . __("You can find the points and rewards systems names in WooRewards → Points and Rewards", 'woorewards-lite'),
108 'example' => '[wr_points_balance system="name_of_your_system"]'
109 ),
110 'layout' => array(
111 'option' => 'layout',
112 'desc' => __("(Optional) Select how the points balance elements are organized . 4 possible values :", 'woorewards-lite'),
113 'options' => array(
114 array(
115 'option' => 'none',
116 'desc' => __("Default value. Simple text without stylable elements", 'woorewards-lite'),
117 ),
118 array(
119 'option' => 'grid',
120 'desc' => __("Elements are displayed in a responsive grid", 'woorewards-lite'),
121 ),
122 array(
123 'option' => 'horizontal',
124 'desc' => __("Elements are displayed in row", 'woorewards-lite'),
125 ),
126 array(
127 'option' => 'vertical',
128 'desc' => __("Elements are displayed on top of another", 'woorewards-lite'),
129 ),
130 ),
131 'example' => '[wr_points_balance layout="grid"]'
132 ),
133 ),
134 $fields['pointsbalance']['extra']['options']
135 );
136 return $fields;
137 }
138
139 /** Shows one or several points balances
140 * [wr_points_balance systems='poolname1, poolname2']
141 * * system → Default: ''
142 * The points and rewards systems for which the balance is displayed. If empty, show all active systems
143 * One value or several ones, comma separated
144 * * layout → Default: 'none'
145 * Defines the presentation of the wrapper.
146 * 4 possible values : grid, vertical, horizontal, none.
147 * * element → Default: 'none'
148 * Defines the presentation of the elements.
149 * 3 possible values : tile, line, none.
150 * * display → Default: 'formatted'
151 * 'simple' → only the points balance numeric value is displayed.
152 * 'formatted' → points are formatted with the points currency/name.
153 * * showname → Default: false
154 * Force the display of the system name even if there's only one system
155 */
156 public function shortcode($atts = array(), $content = '')
157 {
158 $atts = \LWS\Adminpanel\Tools\Conveniences::sanitizeAttr(\wp_parse_args($atts, array(
159 'system' => '',
160 'layout' => 'none',
161 'element' => 'none',
162 'display' => 'formatted',
163 'showname' => '',
164 'showcurrency' => 'true',
165 )));
166 $userId = \apply_filters('lws_woorewards_shortcode_current_user_id', \get_current_user_id(), $atts, 'wr_points_balance');
167
168 /** Basic verifications */
169 if (!$atts['system']) {
170 $atts['showall'] = true;
171 }
172 if (!$userId) {
173 return \do_shortcode((string)$content);
174 }
175 /** Get the data */
176 $pools = \apply_filters('lws_woorewards_get_pools_by_args', false, $atts);
177 if (!($pools && $pools->count())) {
178 return \do_shortcode((string)$content);
179 }
180 // check optional arg
181 if (!\strlen("{$atts['showname']}"))
182 $atts['showname'] = ($pools->count() != 1);
183 else
184 $atts['showname'] = \LWS\Adminpanel\Tools\Conveniences::argIsTrue($atts['showname']);
185
186 $content = '';
187 $this->enqueueScripts();
188
189 foreach ($pools->asArray() as $pool) {
190 // info
191 $points = $pool->getPoints($userId);
192 if ($atts['display'] == 'formatted') {
193 $points = $pool->formatPoints($points, \LWS\Adminpanel\Tools\Conveniences::argIsTrue($atts['showcurrency']));
194 }
195 $name = \esc_attr($pool->getName());
196
197 if ($atts['element'] == 'tile') {
198 $title = $atts['showname'] ? sprintf("<div class='system-name'>%s</div>", $pool->getOption('display_title')) : '';
199 $content .= "<div class='item tile {$name}'>{$title}<div class='points-balance'>{$points}</div></div>";
200 } else if ($atts['element'] == 'line') {
201 $title = $atts['showname'] ? sprintf("<span class='system-name'>%s</span>", $pool->getOption('display_title')) : '';
202 $content .= "<span class='item line {$name}'>{$title}<span class='points-balance'>{$points}</span></span>";
203 } else {
204 if ($atts['showname']) {
205 /* translators: %1$s: system name, %2$s: points balance */
206 $content .= sprintf(_x('%1$s : %2$s', 'wr_points_balance element="none"', 'woorewards-lite'), $pool->getOption('display_title'), $points);
207 } else {
208 $content .= $points;
209 }
210 }
211 }
212
213 // define a container
214 switch (\strtolower(\substr($atts['layout'], 0, 3))) {
215 case 'gri':
216 return "<div class='wr-points-balance wr-shortcode-grid'>{$content}</div>";
217 case 'hor':
218 return "<div class='wr-points-balance wr-shortcode-hflex'>{$content}</div>";
219 case 'ver':
220 return "<div class='wr-points-balance wr-shortcode-vflex'>{$content}</div>";
221 default:
222 return $content;
223 }
224 }
225 }
226