PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.1
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.1
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / Classes / Better_Payment_Helper.php

Better_Payment_Helper.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 1.0.1, at includes/Classes/Better_Payment_Helper.php

443 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Classes;
4
5 use Better_Payment\Lite\Controller;
6
7 /**
8 * Exit if accessed directly
9 */
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * This class responsible for database work
16 * using wordpress functionality
17 * get_option and update_option.
18 */
19 class Better_Payment_Helper extends Controller
20 {
21 /**
22 * Check if elementor plugin is activated
23 *
24 * @since 0.0.2
25 */
26 public function elementor_not_loaded()
27 {
28 if (!current_user_can('activate_plugins')) {
29 return;
30 }
31
32 if (isset($_GET['page']) && $_GET['page'] == 'better-payment-setup-wizard') {
33 return;
34 }
35
36 if(wp_doing_ajax()){
37 return;
38 }
39
40 $elementor = 'elementor/elementor.php';
41
42 if ($this->is_plugin_installed($elementor)) {
43 $activation_url = wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $elementor . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $elementor);
44
45 $message = sprintf(__('%1$sBetter Payment%2$s requires %1$sElementor%2$s plugin to be active. Please activate Elementor to continue.', 'better-payment'), "<strong>", "</strong>");
46
47 $button_text = __('Activate Elementor', 'better-payment');
48 } else {
49 $activation_url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=elementor'), 'install-plugin_elementor');
50
51 $message = sprintf(__('%1$sBetter Payment%2$s requires %1$sElementor%2$s plugin to be installed and activated. Please install Elementor to continue.', 'better-payment'), '<strong>', '</strong>');
52 $button_text = __('Install Elementor', 'better-payment');
53 }
54
55 $button = '<p><a href="' . esc_url_raw($activation_url) . '" class="button-primary">' . $button_text . '</a></p>';
56
57 ob_start();
58 printf('<div class="error"><p>%1$s</p>%2$s</div>', wp_kses( __( $message, 'better-payment' ), $this->bp_allowed_tags() ), wp_kses( $button, $this->bp_allowed_tags() ) );
59 ob_end_flush();
60 }
61
62 /**
63 * Check if a plugin is installed
64 *
65 * @since 0.0.2
66 */
67 public function is_plugin_installed($basename)
68 {
69 if (!function_exists('get_plugins')) {
70 include_once ABSPATH . 'wp-admin/includes/plugin.php';
71 }
72
73 $plugins = get_plugins();
74
75 return isset($plugins[$basename]);
76 }
77
78 /**
79 * check is plugin active or not
80 *
81 * @param $plugin
82 * @return bool
83 * @since 0.0.2
84 */
85 public function is_plugin_active($plugin) {
86 if ( !function_exists( 'is_plugin_active' ) ){
87 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
88 }
89
90 return is_plugin_active( $plugin );
91 }
92
93 public static function sanitize_bp_field($key, $arr, $default_value='', $type='text'){
94 $value = $default_value;
95
96 if( !empty( $arr[ $key ] ) ) {
97 $value = $arr[ $key ];
98
99 switch($type){
100 case 'text':
101 $value = sanitize_text_field( $value );
102 break;
103 case 'email':
104 $value = sanitize_email( $value );
105 break;
106 case 'textarea':
107 $value = sanitize_textarea_field( $value );
108 break;
109 default:
110 $value = sanitize_text_field( $value );
111 break;
112 }
113 }
114
115 return $value;
116 }
117
118 /**
119 * Get widget settings
120 *
121 * @since 0.0.1
122 */
123 public function get_better_payment_widget_settings( $page_id, $widget_id ) {
124 $document = \Elementor\Plugin::$instance->documents->get( $page_id );
125 $settings = [];
126 if ( $document ) {
127 $elements = \Elementor\Plugin::instance()->documents->get( $page_id )->get_elements_data();
128 $widget_data = $this->find_element_recursive( $elements, $widget_id );
129 if ( $widget_data ) {
130 $widget = \Elementor\Plugin::instance()->elements_manager->create_element_instance( $widget_data );
131 if ( $widget ) {
132 $settings = $widget->get_settings_for_display();
133 }
134 }
135
136 }
137 return $settings;
138 }
139
140 /**
141 * Find element recursively
142 *
143 * @since 0.0.1
144 */
145 public function find_element_recursive( $elements, $form_id ) {
146
147 foreach ( $elements as $element ) {
148 if ( $form_id === $element[ 'id' ] ) {
149 return $element;
150 }
151
152 if ( !empty( $element[ 'elements' ] ) ) {
153 $element = $this->find_element_recursive( $element[ 'elements' ], $form_id );
154
155 if ( $element ) {
156 return $element;
157 }
158 }
159 }
160
161 return false;
162 }
163
164 /**
165 * Get currency list
166 *
167 * @since 0.0.2
168 */
169 public function get_currency_list(){
170 $currency_list = [
171 'AED' => 'AED',
172 'AUD' => 'AUD',
173 'CAD' => 'CAD',
174 'CZK' => 'CZK',
175 'DKK' => 'DKK',
176 'EUR' => 'EUR',
177 'HKD' => 'HKD',
178 'HUF' => 'HUF',
179 'ILS' => 'ILS',
180 'JPY' => 'JPY',
181 'MXN' => 'MXN',
182 'NOK' => 'NOK',
183 'NZD' => 'NZD',
184 'PHP' => 'PHP',
185 'PLN' => 'PLN',
186 'GBP' => 'GBP',
187 'RUB' => 'RUB',
188 'SGD' => 'SGD',
189 'SEK' => 'SEK',
190 'CHF' => 'CHF',
191 'TWD' => 'TWD',
192 'THB' => 'THB',
193 'USD' => 'USD',
194 'ZAR' => 'ZAR'
195 ];
196
197 return $currency_list;
198 }
199
200 public function titleToSnake($text, $divider = '_') {
201 $text = preg_replace('~[^\pL\d]+~u', $divider, $text);
202 $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
203 $text = preg_replace('~[^-\w]+~', '', $text);
204 $text = trim($text, $divider);
205 $text = preg_replace('~-+~', $divider, $text);
206 $text = strtolower($text);
207
208 if (empty($text)) {
209 return 'n_a';
210 }
211
212 return $text;
213 }
214
215 public function arrayToString($array, $separator = ', ', $extraString = ''){
216 $string = rtrim( implode($separator, $array ), $separator ) ;
217 if($extraString) {
218 $string .= $extraString;
219 }
220
221 return $string;
222 }
223
224 public function transaction_statuses_with_type(){
225 $statuses = [
226 'completed' => [
227 'statuses' => [
228 'paid',
229 'Completed',
230 'completed',
231 ],
232 'color' => '#0ECA86',
233 ],
234 'processing' => [
235 'statuses' => [
236 'pending',
237 'Pending',
238 NULL,
239 ],
240 'color' => '#FFDA15',
241 ],
242 'refunded' => [
243 'statuses' => [
244 'refunded',
245 ],
246 'color' => '#FF0202',
247 ],
248 'unpaid' => [
249 'statuses' => [
250 'unpaid',
251 ],
252 'color' => '#999',
253 ],
254 'all' => [
255 'statuses' => [
256 'paid',
257 'Completed',
258 'completed',
259 'unpaid',
260 'refunded',
261 'pending',
262 'Pending',
263 NULL,
264 ],
265 'color' => '#999',
266 ],
267 ];
268
269 return $statuses;
270 }
271
272 public function transaction_statuses_with_type_v2(){
273 $statuses = [
274 'completed' => [
275 'statuses' => [
276 'paid',
277 'Completed',
278 'completed',
279 'success',
280 ],
281 'color' => '#0ECA86',
282 ],
283 'incomplete' => [
284 'statuses' => [
285 'pending',
286 'Pending',
287 'unpaid',
288 NULL,
289 ],
290 'color' => '#FFDA15',
291 ],
292 'refunded' => [
293 'statuses' => [
294 'refunded',
295 ],
296 'color' => '#FF0202',
297 ],
298 'all' => [
299 'statuses' => [
300 'paid',
301 'Completed',
302 'completed',
303 'unpaid',
304 'refunded',
305 'pending',
306 'Pending',
307 'success',
308 NULL,
309 ],
310 'color' => '#999',
311 ],
312 ];
313
314 return $statuses;
315 }
316
317 public function get_type_by_transaction_status($status = '', $version = 'v1'){
318 if('v1' === $version){
319 $statuses = $this->transaction_statuses_with_type();
320 } else {
321 $statuses = $this->transaction_statuses_with_type_v2();
322 }
323
324 $status_type = '';
325 if(!empty($status)){
326 foreach($statuses as $type => $statuses_and_color){
327 if(in_array($status, $statuses_and_color['statuses'])){
328 $status_type = $type;
329 break;
330 }
331 }
332 }
333
334 if($status === '' || $status === NULL){
335 $status_type = 'incomplete';
336 }
337
338 return $status_type;
339 }
340
341 public function get_color_by_transaction_status($status = '', $version = 'v1'){
342 if('v1' === $version){
343 $statuses = $this->transaction_statuses_with_type();
344 } else {
345 $statuses = $this->transaction_statuses_with_type_v2();
346 }
347
348 $color = '#999';
349 if(!empty($status)){
350 foreach($statuses as $type => $statuses_and_color){
351 if(in_array($status, $statuses_and_color['statuses'])){
352 $color = $statuses_and_color['color'];
353 break;
354 }
355 }
356 }
357
358 if($status === '' || $status === NULL){
359 $color = '#FFDA15'; //incomplete
360 }
361
362 return $color;
363
364 }
365
366 public function get_color_by_transaction_type($status_type = '', $version = 'v1'){
367 if('v1' === $version){
368 $statuses = $this->transaction_statuses_with_type();
369 } else {
370 $statuses = $this->transaction_statuses_with_type_v2();
371 }
372
373 $color = '';
374 if(!empty($status_type)){
375 foreach($statuses as $type => $statuses_and_color){
376 if($type == $status_type){
377 $color = $statuses_and_color['color'];
378 break;
379 }
380 }
381 }
382
383 return $color;
384 }
385
386 public function get_statuses_by_transaction_type($status_type = 'all', $version = 'v1'){
387 if('v1' === $version){
388 $statuses = $this->transaction_statuses_with_type();
389 } else {
390 $statuses = $this->transaction_statuses_with_type_v2();
391 }
392
393 $transaction_statuses = [];
394 if(!empty($status_type)){
395 foreach($statuses as $type => $statuses_and_color){
396 if($type == $status_type){
397 $transaction_statuses = $statuses_and_color['statuses'];
398 break;
399 }
400 }
401 }
402
403 return $transaction_statuses;
404 }
405
406 public function get_transaction_types($version = 'v1'){
407 if('v1' === $version){
408 $statuses = $this->transaction_statuses_with_type();
409 } else {
410 $statuses = $this->transaction_statuses_with_type_v2();
411 }
412
413 $statuses_types = array_keys($statuses);
414
415 return $statuses_types;
416 }
417 }
418
419
420 //Helper Functions
421 if( !function_exists('better_payment_dd') ){
422 function better_payment_dd($data, $show_query = 0, $print_only = 0) {
423 global $wpdb;
424
425 if(1 === $show_query){
426 // Print last SQL query string
427 echo wp_kses_post( $wpdb->last_query );
428
429 //Print last SQL query result
430 echo wp_kses_post( $wpdb->last_result );
431
432 //Print last SQL query Error
433 echo wp_kses_post( $wpdb->last_error );
434 }
435
436 echo "<pre>";
437 print_r($data);
438 if(!$print_only){
439 wp_die('Printed!');
440 }
441 }
442 }
443