PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.3.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.3.3
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 1.0.5 1.0.6 All 66 releases
better-payment / includes / Classes / Helper.php

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

448 lines 13.2 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 use Better_Payment\Lite\Traits\Helper as TraitsHelper;
7
8 /**
9 * Exit if accessed directly
10 */
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 /**
16 * This class responsible for database work
17 * using wordpress functionality
18 * get_option and update_option.
19 */
20 class Helper extends Controller
21 {
22 use TraitsHelper;
23
24 /**
25 * check is plugin active or not
26 *
27 * @param $plugin
28 * @return bool
29 * @since 0.0.2
30 */
31 public function is_plugin_active($plugin) {
32 if ( !function_exists( 'is_plugin_active' ) ){
33 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
34 }
35
36 return is_plugin_active( $plugin );
37 }
38
39 public static function sanitize_bp_field($key, $arr, $default_value='', $type='text'){
40 $value = $default_value;
41
42 if( !empty( $arr[ $key ] ) ) {
43 $value = $arr[ $key ];
44
45 switch($type){
46 case 'text':
47 $value = sanitize_text_field( $value );
48 break;
49 case 'email':
50 $value = sanitize_email( $value );
51 break;
52 case 'textarea':
53 $value = sanitize_textarea_field( $value );
54 break;
55 default:
56 $value = sanitize_text_field( $value );
57 break;
58 }
59 }
60
61 return $value;
62 }
63
64 /**
65 * Get widget settings
66 *
67 * @since 0.0.1
68 */
69 public function get_better_payment_widget_settings( $page_id, $widget_id ) {
70 $document = \Elementor\Plugin::$instance->documents->get( $page_id );
71 $settings = [];
72 if ( $document ) {
73 $elements = \Elementor\Plugin::instance()->documents->get( $page_id )->get_elements_data();
74 $widget_data = $this->find_element_recursive( $elements, $widget_id );
75 if ( $widget_data ) {
76 $widget = \Elementor\Plugin::instance()->elements_manager->create_element_instance( $widget_data );
77 if ( $widget ) {
78 $settings = $widget->get_settings_for_display();
79 }
80 }
81
82 }
83 return $settings;
84 }
85
86 /**
87 * Find element recursively
88 *
89 * @since 0.0.1
90 */
91 public function find_element_recursive( $elements, $form_id ) {
92
93 foreach ( $elements as $element ) {
94 if ( $form_id === $element[ 'id' ] ) {
95 return $element;
96 }
97
98 if ( !empty( $element[ 'elements' ] ) ) {
99 $element = $this->find_element_recursive( $element[ 'elements' ], $form_id );
100
101 if ( $element ) {
102 return $element;
103 }
104 }
105 }
106
107 return false;
108 }
109
110 public function titleToSnake($text, $divider = '_') {
111 // Handle null or empty values to prevent deprecated warnings.
112 if ( null === $text || '' === $text ) {
113 return 'n_a';
114 }
115
116 $text = (string) $text;
117 $text = preg_replace('~[^\pL\d]+~u', $divider, $text);
118 $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
119 $text = preg_replace('~[^-\w]+~', '', $text);
120 $text = trim($text, $divider);
121 $text = preg_replace('~-+~', $divider, $text);
122 $text = strtolower($text);
123
124 if (empty($text)) {
125 return 'n_a';
126 }
127
128 return $text;
129 }
130
131 public function arrayToString($array, $separator = ', ', $extraString = ''){
132 $string = rtrim( implode($separator, $array ), $separator ) ;
133 if($extraString) {
134 $string .= $extraString;
135 }
136
137 return $string;
138 }
139
140 public function transaction_statuses_with_type(){
141 $statuses = [
142 'completed' => [
143 'statuses' => [
144 'paid',
145 'Completed',
146 'completed',
147 // Stripe's own status for a fully-discounted / $0 Checkout
148 // Session (100% coupon or sale, free trial). The payment
149 // IS settled - there was simply nothing to charge - so it
150 // must classify as completed, not fall through to the
151 // unknown-status default. See Helper::get_type_by_transaction_status().
152 'no_payment_required',
153 ],
154 'color' => '#0ECA86',
155 ],
156 'processing' => [
157 'statuses' => [
158 'pending',
159 'Pending',
160 NULL,
161 ],
162 'color' => '#FFDA15',
163 ],
164 'refunded' => [
165 'statuses' => [
166 'refunded',
167 ],
168 'color' => '#FF0202',
169 ],
170 'unpaid' => [
171 'statuses' => [
172 'unpaid',
173 ],
174 'color' => '#999',
175 ],
176 'all' => [
177 'statuses' => [
178 'paid',
179 'Completed',
180 'completed',
181 'no_payment_required',
182 'unpaid',
183 'refunded',
184 'pending',
185 'Pending',
186 NULL,
187 ],
188 'color' => '#999',
189 ],
190 ];
191
192 return $statuses;
193 }
194
195 public function transaction_statuses_with_type_v2(){
196 $statuses = [
197 'completed' => [
198 'statuses' => [
199 'paid',
200 'Completed',
201 'completed',
202 'success',
203 // See the v1 note: a $0 Stripe session settles as
204 // 'no_payment_required'. Every status bucket is also a
205 // `status IN (...)` filter in DB::get_transactions(), so a
206 // status missing from BOTH 'completed' and 'incomplete'
207 // is invisible under either tab while still showing on
208 // the unfiltered list - which is exactly how a paid $0
209 // order looked like a stray "Incomplete" row.
210 'no_payment_required',
211 ],
212 'color' => '#0ECA86',
213 ],
214 'incomplete' => [
215 'statuses' => [
216 'pending',
217 'Pending',
218 'unpaid',
219 'incomplete',
220 'Incomplete',
221 NULL,
222 ],
223 'color' => '#FFDA15',
224 ],
225 'refunded' => [
226 'statuses' => [
227 'refunded',
228 ],
229 'color' => '#FF0202',
230 ],
231 'all' => [
232 'statuses' => [
233 'paid',
234 'Completed',
235 'completed',
236 'no_payment_required',
237 'unpaid',
238 'refunded',
239 'pending',
240 'Pending',
241 'success',
242 'incomplete',
243 'Incomplete',
244 NULL,
245 ],
246 'color' => '#999',
247 ],
248 ];
249
250 return $statuses;
251 }
252
253 public function get_type_by_transaction_status($status = '', $version = 'v1'){
254 if('v1' === $version){
255 $statuses = $this->transaction_statuses_with_type();
256 } else {
257 $statuses = $this->transaction_statuses_with_type_v2();
258 }
259
260 $status_type = '';
261 if(!empty($status)){
262 foreach($statuses as $type => $statuses_and_color){
263 if(in_array($status, $statuses_and_color['statuses'])){
264 $status_type = $type;
265 break;
266 }
267 }
268 }
269
270 if($status === '' || $status === NULL){
271 $status_type = 'incomplete';
272 }
273
274 return $status_type;
275 }
276
277 public function get_color_by_transaction_status($status = '', $version = 'v1'){
278 if('v1' === $version){
279 $statuses = $this->transaction_statuses_with_type();
280 } else {
281 $statuses = $this->transaction_statuses_with_type_v2();
282 }
283
284 $color = '#999';
285 if(!empty($status)){
286 foreach($statuses as $type => $statuses_and_color){
287 if(in_array($status, $statuses_and_color['statuses'])){
288 $color = $statuses_and_color['color'];
289 break;
290 }
291 }
292 }
293
294 if($status === '' || $status === NULL){
295 $color = '#FFDA15'; //incomplete
296 }
297
298 return $color;
299
300 }
301
302 public function get_color_by_transaction_type($status_type = '', $version = 'v1'){
303 if('v1' === $version){
304 $statuses = $this->transaction_statuses_with_type();
305 } else {
306 $statuses = $this->transaction_statuses_with_type_v2();
307 }
308
309 $color = '';
310 if(!empty($status_type)){
311 foreach($statuses as $type => $statuses_and_color){
312 if($type == $status_type){
313 $color = $statuses_and_color['color'];
314 break;
315 }
316 }
317 }
318
319 return $color;
320 }
321
322 public function get_statuses_by_transaction_type($status_type = 'all', $version = 'v1'){
323 if('v1' === $version){
324 $statuses = $this->transaction_statuses_with_type();
325 } else {
326 $statuses = $this->transaction_statuses_with_type_v2();
327 }
328
329 $transaction_statuses = [];
330 if(!empty($status_type)){
331 foreach($statuses as $type => $statuses_and_color){
332 if($type == $status_type){
333 $transaction_statuses = $statuses_and_color['statuses'];
334 break;
335 }
336 }
337 }
338
339 return $transaction_statuses;
340 }
341
342 public function get_transaction_types($version = 'v1'){
343 if('v1' === $version){
344 $statuses = $this->transaction_statuses_with_type();
345 } else {
346 $statuses = $this->transaction_statuses_with_type_v2();
347 }
348
349 $statuses_types = array_keys($statuses);
350
351 return $statuses_types;
352 }
353
354 /**
355 * Get interval text for recurring and split payment
356 *
357 * @since 1.3.0
358 */
359 public function get_interval_text( $interval = '' ){
360 $interval_text = '';
361
362 switch( $interval ) {
363 case 'day':
364 $interval_text = 'Daily';
365 break;
366
367 case 'month':
368 case 'year':
369 $interval_text = $interval . 'ly';
370 break;
371
372 default :
373 $interval_text = ! empty( $interval ) ? "Per " . esc_html( $interval ) : "";
374 break;
375 }
376
377 return esc_html( ucfirst( $interval_text ) );
378 }
379
380 /**
381 * Output the page header, compatible with both classic and block themes.
382 *
383 * Block themes don't use get_header() — they rely on block template parts.
384 * We detect this with wp_is_block_theme() and render the header template
385 * part via do_blocks() so it appears correctly on CPT single pages that
386 * bypass the theme's block templates.
387 *
388 * @param string $block_header Pre-rendered header markup from do_blocks() (block themes only).
389 * @return void
390 */
391 public static function render_header( string $block_header = '' ): void {
392 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
393 ?>
394 <!doctype html>
395 <html <?php language_attributes(); ?>>
396 <head>
397 <meta charset="<?php bloginfo( 'charset' ); ?>">
398 <?php wp_head(); ?>
399 </head>
400 <body <?php body_class(); ?>>
401 <?php
402 wp_body_open();
403 echo $block_header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
404 } else {
405 get_header();
406 }
407 }
408
409 /**
410 * Output the page footer, compatible with both classic and block themes.
411 *
412 * @param string $block_footer Pre-rendered footer markup from do_blocks() (block themes only).
413 * @return void
414 */
415 public static function render_footer( string $block_footer = '' ): void {
416 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
417 echo $block_footer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
418 wp_footer();
419 ?>
420 </body>
421 </html>
422 <?php
423 } else {
424 get_footer();
425 }
426 }
427 }
428
429
430 // Helper Functions
431 if ( ! function_exists( 'better_payment_dd' ) ) {
432 function better_payment_dd( $data, $show_query = 0, $print_only = 0 ) {
433 global $wpdb;
434
435 if ( 1 === $show_query ) {
436 echo wp_kses_post( $wpdb->last_query );
437 echo wp_kses_post( $wpdb->last_result );
438 echo wp_kses_post( $wpdb->last_error );
439 }
440
441 echo '<pre>';
442 print_r( $data ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
443 if ( ! $print_only ) {
444 wp_die( 'Printed!' );
445 }
446 }
447 }
448