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

439 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 'AUD' => 'AUD',
172 'CAD' => 'CAD',
173 'CZK' => 'CZK',
174 'DKK' => 'DKK',
175 'EUR' => 'EUR',
176 'HKD' => 'HKD',
177 'HUF' => 'HUF',
178 'ILS' => 'ILS',
179 'JPY' => 'JPY',
180 'MXN' => 'MXN',
181 'NOK' => 'NOK',
182 'NZD' => 'NZD',
183 'PHP' => 'PHP',
184 'PLN' => 'PLN',
185 'GBP' => 'GBP',
186 'RUB' => 'RUB',
187 'SGD' => 'SGD',
188 'SEK' => 'SEK',
189 'CHF' => 'CHF',
190 'TWD' => 'TWD',
191 'THB' => 'THB',
192 'USD' => 'USD'
193 ];
194
195 return $currency_list;
196 }
197
198 public function titleToSnake($text, $divider = '_') {
199 $text = preg_replace('~[^\pL\d]+~u', $divider, $text);
200 $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
201 $text = preg_replace('~[^-\w]+~', '', $text);
202 $text = trim($text, $divider);
203 $text = preg_replace('~-+~', $divider, $text);
204 $text = strtolower($text);
205
206 if (empty($text)) {
207 return 'n_a';
208 }
209
210 return $text;
211 }
212
213 public function arrayToString($array, $separator = ', ', $extraString = ''){
214 $string = rtrim( implode($separator, $array ), $separator ) ;
215 if($extraString) {
216 $string .= $extraString;
217 }
218
219 return $string;
220 }
221
222 public function transaction_statuses_with_type(){
223 $statuses = [
224 'completed' => [
225 'statuses' => [
226 'paid',
227 'Completed',
228 'completed',
229 ],
230 'color' => '#0ECA86',
231 ],
232 'processing' => [
233 'statuses' => [
234 'pending',
235 'Pending',
236 NULL,
237 ],
238 'color' => '#FFDA15',
239 ],
240 'refunded' => [
241 'statuses' => [
242 'refunded',
243 ],
244 'color' => '#FF0202',
245 ],
246 'unpaid' => [
247 'statuses' => [
248 'unpaid',
249 ],
250 'color' => '#999',
251 ],
252 'all' => [
253 'statuses' => [
254 'paid',
255 'Completed',
256 'completed',
257 'unpaid',
258 'refunded',
259 'pending',
260 'Pending',
261 NULL,
262 ],
263 'color' => '#999',
264 ],
265 ];
266
267 return $statuses;
268 }
269
270 public function transaction_statuses_with_type_v2(){
271 $statuses = [
272 'completed' => [
273 'statuses' => [
274 'paid',
275 'Completed',
276 'completed',
277 ],
278 'color' => '#0ECA86',
279 ],
280 'incomplete' => [
281 'statuses' => [
282 'pending',
283 'Pending',
284 'unpaid',
285 NULL,
286 ],
287 'color' => '#FFDA15',
288 ],
289 'refunded' => [
290 'statuses' => [
291 'refunded',
292 ],
293 'color' => '#FF0202',
294 ],
295 'all' => [
296 'statuses' => [
297 'paid',
298 'Completed',
299 'completed',
300 'unpaid',
301 'refunded',
302 'pending',
303 'Pending',
304 NULL,
305 ],
306 'color' => '#999',
307 ],
308 ];
309
310 return $statuses;
311 }
312
313 public function get_type_by_transaction_status($status = '', $version = 'v1'){
314 if('v1' === $version){
315 $statuses = $this->transaction_statuses_with_type();
316 } else {
317 $statuses = $this->transaction_statuses_with_type_v2();
318 }
319
320 $status_type = '';
321 if(!empty($status)){
322 foreach($statuses as $type => $statuses_and_color){
323 if(in_array($status, $statuses_and_color['statuses'])){
324 $status_type = $type;
325 break;
326 }
327 }
328 }
329
330 if($status === '' || $status === NULL){
331 $status_type = 'incomplete';
332 }
333
334 return $status_type;
335 }
336
337 public function get_color_by_transaction_status($status = '', $version = 'v1'){
338 if('v1' === $version){
339 $statuses = $this->transaction_statuses_with_type();
340 } else {
341 $statuses = $this->transaction_statuses_with_type_v2();
342 }
343
344 $color = '#999';
345 if(!empty($status)){
346 foreach($statuses as $type => $statuses_and_color){
347 if(in_array($status, $statuses_and_color['statuses'])){
348 $color = $statuses_and_color['color'];
349 break;
350 }
351 }
352 }
353
354 if($status === '' || $status === NULL){
355 $color = '#FFDA15'; //incomplete
356 }
357
358 return $color;
359
360 }
361
362 public function get_color_by_transaction_type($status_type = '', $version = 'v1'){
363 if('v1' === $version){
364 $statuses = $this->transaction_statuses_with_type();
365 } else {
366 $statuses = $this->transaction_statuses_with_type_v2();
367 }
368
369 $color = '';
370 if(!empty($status_type)){
371 foreach($statuses as $type => $statuses_and_color){
372 if($type == $status_type){
373 $color = $statuses_and_color['color'];
374 break;
375 }
376 }
377 }
378
379 return $color;
380 }
381
382 public function get_statuses_by_transaction_type($status_type = 'all', $version = 'v1'){
383 if('v1' === $version){
384 $statuses = $this->transaction_statuses_with_type();
385 } else {
386 $statuses = $this->transaction_statuses_with_type_v2();
387 }
388
389 $transaction_statuses = [];
390 if(!empty($status_type)){
391 foreach($statuses as $type => $statuses_and_color){
392 if($type == $status_type){
393 $transaction_statuses = $statuses_and_color['statuses'];
394 break;
395 }
396 }
397 }
398
399 return $transaction_statuses;
400 }
401
402 public function get_transaction_types($version = 'v1'){
403 if('v1' === $version){
404 $statuses = $this->transaction_statuses_with_type();
405 } else {
406 $statuses = $this->transaction_statuses_with_type_v2();
407 }
408
409 $statuses_types = array_keys($statuses);
410
411 return $statuses_types;
412 }
413 }
414
415
416 //Helper Functions
417 if( !function_exists('better_payment_dd') ){
418 function better_payment_dd($data, $show_query = 0, $print_only = 0) {
419 global $wpdb;
420
421 if(1 === $show_query){
422 // Print last SQL query string
423 echo $wpdb->last_query;
424
425 //Print last SQL query result
426 echo $wpdb->last_result;
427
428 //Print last SQL query Error
429 echo $wpdb->last_error;
430 }
431
432 echo "<pre>";
433 print_r($data);
434 if(!$print_only){
435 wp_die('Printed!');
436 }
437 }
438 }
439