PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.5
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.5
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 0.0.5, at includes/Classes/Better_Payment_Helper.php

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