PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.3
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 / Helper.php

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

447 lines 12.4 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 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 'incomplete',
289 'Incomplete',
290 NULL,
291 ],
292 'color' => '#FFDA15',
293 ],
294 'refunded' => [
295 'statuses' => [
296 'refunded',
297 ],
298 'color' => '#FF0202',
299 ],
300 'all' => [
301 'statuses' => [
302 'paid',
303 'Completed',
304 'completed',
305 'unpaid',
306 'refunded',
307 'pending',
308 'Pending',
309 'success',
310 'incomplete',
311 'Incomplete',
312 NULL,
313 ],
314 'color' => '#999',
315 ],
316 ];
317
318 return $statuses;
319 }
320
321 public function get_type_by_transaction_status($status = '', $version = 'v1'){
322 if('v1' === $version){
323 $statuses = $this->transaction_statuses_with_type();
324 } else {
325 $statuses = $this->transaction_statuses_with_type_v2();
326 }
327
328 $status_type = '';
329 if(!empty($status)){
330 foreach($statuses as $type => $statuses_and_color){
331 if(in_array($status, $statuses_and_color['statuses'])){
332 $status_type = $type;
333 break;
334 }
335 }
336 }
337
338 if($status === '' || $status === NULL){
339 $status_type = 'incomplete';
340 }
341
342 return $status_type;
343 }
344
345 public function get_color_by_transaction_status($status = '', $version = 'v1'){
346 if('v1' === $version){
347 $statuses = $this->transaction_statuses_with_type();
348 } else {
349 $statuses = $this->transaction_statuses_with_type_v2();
350 }
351
352 $color = '#999';
353 if(!empty($status)){
354 foreach($statuses as $type => $statuses_and_color){
355 if(in_array($status, $statuses_and_color['statuses'])){
356 $color = $statuses_and_color['color'];
357 break;
358 }
359 }
360 }
361
362 if($status === '' || $status === NULL){
363 $color = '#FFDA15'; //incomplete
364 }
365
366 return $color;
367
368 }
369
370 public function get_color_by_transaction_type($status_type = '', $version = 'v1'){
371 if('v1' === $version){
372 $statuses = $this->transaction_statuses_with_type();
373 } else {
374 $statuses = $this->transaction_statuses_with_type_v2();
375 }
376
377 $color = '';
378 if(!empty($status_type)){
379 foreach($statuses as $type => $statuses_and_color){
380 if($type == $status_type){
381 $color = $statuses_and_color['color'];
382 break;
383 }
384 }
385 }
386
387 return $color;
388 }
389
390 public function get_statuses_by_transaction_type($status_type = 'all', $version = 'v1'){
391 if('v1' === $version){
392 $statuses = $this->transaction_statuses_with_type();
393 } else {
394 $statuses = $this->transaction_statuses_with_type_v2();
395 }
396
397 $transaction_statuses = [];
398 if(!empty($status_type)){
399 foreach($statuses as $type => $statuses_and_color){
400 if($type == $status_type){
401 $transaction_statuses = $statuses_and_color['statuses'];
402 break;
403 }
404 }
405 }
406
407 return $transaction_statuses;
408 }
409
410 public function get_transaction_types($version = 'v1'){
411 if('v1' === $version){
412 $statuses = $this->transaction_statuses_with_type();
413 } else {
414 $statuses = $this->transaction_statuses_with_type_v2();
415 }
416
417 $statuses_types = array_keys($statuses);
418
419 return $statuses_types;
420 }
421 }
422
423
424 //Helper Functions
425 if( !function_exists('better_payment_dd') ){
426 function better_payment_dd($data, $show_query = 0, $print_only = 0) {
427 global $wpdb;
428
429 if(1 === $show_query){
430 // Print last SQL query string
431 echo wp_kses_post( $wpdb->last_query );
432
433 //Print last SQL query result
434 echo wp_kses_post( $wpdb->last_result );
435
436 //Print last SQL query Error
437 echo wp_kses_post( $wpdb->last_error );
438 }
439
440 echo "<pre>";
441 print_r($data);
442 if(!$print_only){
443 wp_die('Printed!');
444 }
445 }
446 }
447