| 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 |
], |
| 148 |
'color' => '#0ECA86', |
| 149 |
], |
| 150 |
'processing' => [ |
| 151 |
'statuses' => [ |
| 152 |
'pending', |
| 153 |
'Pending', |
| 154 |
NULL, |
| 155 |
], |
| 156 |
'color' => '#FFDA15', |
| 157 |
], |
| 158 |
'refunded' => [ |
| 159 |
'statuses' => [ |
| 160 |
'refunded', |
| 161 |
], |
| 162 |
'color' => '#FF0202', |
| 163 |
], |
| 164 |
'unpaid' => [ |
| 165 |
'statuses' => [ |
| 166 |
'unpaid', |
| 167 |
], |
| 168 |
'color' => '#999', |
| 169 |
], |
| 170 |
'all' => [ |
| 171 |
'statuses' => [ |
| 172 |
'paid', |
| 173 |
'Completed', |
| 174 |
'completed', |
| 175 |
'unpaid', |
| 176 |
'refunded', |
| 177 |
'pending', |
| 178 |
'Pending', |
| 179 |
NULL, |
| 180 |
], |
| 181 |
'color' => '#999', |
| 182 |
], |
| 183 |
]; |
| 184 |
|
| 185 |
return $statuses; |
| 186 |
} |
| 187 |
|
| 188 |
public function transaction_statuses_with_type_v2(){ |
| 189 |
$statuses = [ |
| 190 |
'completed' => [ |
| 191 |
'statuses' => [ |
| 192 |
'paid', |
| 193 |
'Completed', |
| 194 |
'completed', |
| 195 |
'success', |
| 196 |
], |
| 197 |
'color' => '#0ECA86', |
| 198 |
], |
| 199 |
'incomplete' => [ |
| 200 |
'statuses' => [ |
| 201 |
'pending', |
| 202 |
'Pending', |
| 203 |
'unpaid', |
| 204 |
'incomplete', |
| 205 |
'Incomplete', |
| 206 |
NULL, |
| 207 |
], |
| 208 |
'color' => '#FFDA15', |
| 209 |
], |
| 210 |
'refunded' => [ |
| 211 |
'statuses' => [ |
| 212 |
'refunded', |
| 213 |
], |
| 214 |
'color' => '#FF0202', |
| 215 |
], |
| 216 |
'all' => [ |
| 217 |
'statuses' => [ |
| 218 |
'paid', |
| 219 |
'Completed', |
| 220 |
'completed', |
| 221 |
'unpaid', |
| 222 |
'refunded', |
| 223 |
'pending', |
| 224 |
'Pending', |
| 225 |
'success', |
| 226 |
'incomplete', |
| 227 |
'Incomplete', |
| 228 |
NULL, |
| 229 |
], |
| 230 |
'color' => '#999', |
| 231 |
], |
| 232 |
]; |
| 233 |
|
| 234 |
return $statuses; |
| 235 |
} |
| 236 |
|
| 237 |
public function get_type_by_transaction_status($status = '', $version = 'v1'){ |
| 238 |
if('v1' === $version){ |
| 239 |
$statuses = $this->transaction_statuses_with_type(); |
| 240 |
} else { |
| 241 |
$statuses = $this->transaction_statuses_with_type_v2(); |
| 242 |
} |
| 243 |
|
| 244 |
$status_type = ''; |
| 245 |
if(!empty($status)){ |
| 246 |
foreach($statuses as $type => $statuses_and_color){ |
| 247 |
if(in_array($status, $statuses_and_color['statuses'])){ |
| 248 |
$status_type = $type; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if($status === '' || $status === NULL){ |
| 255 |
$status_type = 'incomplete'; |
| 256 |
} |
| 257 |
|
| 258 |
return $status_type; |
| 259 |
} |
| 260 |
|
| 261 |
public function get_color_by_transaction_status($status = '', $version = 'v1'){ |
| 262 |
if('v1' === $version){ |
| 263 |
$statuses = $this->transaction_statuses_with_type(); |
| 264 |
} else { |
| 265 |
$statuses = $this->transaction_statuses_with_type_v2(); |
| 266 |
} |
| 267 |
|
| 268 |
$color = '#999'; |
| 269 |
if(!empty($status)){ |
| 270 |
foreach($statuses as $type => $statuses_and_color){ |
| 271 |
if(in_array($status, $statuses_and_color['statuses'])){ |
| 272 |
$color = $statuses_and_color['color']; |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if($status === '' || $status === NULL){ |
| 279 |
$color = '#FFDA15'; //incomplete |
| 280 |
} |
| 281 |
|
| 282 |
return $color; |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
public function get_color_by_transaction_type($status_type = '', $version = 'v1'){ |
| 287 |
if('v1' === $version){ |
| 288 |
$statuses = $this->transaction_statuses_with_type(); |
| 289 |
} else { |
| 290 |
$statuses = $this->transaction_statuses_with_type_v2(); |
| 291 |
} |
| 292 |
|
| 293 |
$color = ''; |
| 294 |
if(!empty($status_type)){ |
| 295 |
foreach($statuses as $type => $statuses_and_color){ |
| 296 |
if($type == $status_type){ |
| 297 |
$color = $statuses_and_color['color']; |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return $color; |
| 304 |
} |
| 305 |
|
| 306 |
public function get_statuses_by_transaction_type($status_type = 'all', $version = 'v1'){ |
| 307 |
if('v1' === $version){ |
| 308 |
$statuses = $this->transaction_statuses_with_type(); |
| 309 |
} else { |
| 310 |
$statuses = $this->transaction_statuses_with_type_v2(); |
| 311 |
} |
| 312 |
|
| 313 |
$transaction_statuses = []; |
| 314 |
if(!empty($status_type)){ |
| 315 |
foreach($statuses as $type => $statuses_and_color){ |
| 316 |
if($type == $status_type){ |
| 317 |
$transaction_statuses = $statuses_and_color['statuses']; |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
return $transaction_statuses; |
| 324 |
} |
| 325 |
|
| 326 |
public function get_transaction_types($version = 'v1'){ |
| 327 |
if('v1' === $version){ |
| 328 |
$statuses = $this->transaction_statuses_with_type(); |
| 329 |
} else { |
| 330 |
$statuses = $this->transaction_statuses_with_type_v2(); |
| 331 |
} |
| 332 |
|
| 333 |
$statuses_types = array_keys($statuses); |
| 334 |
|
| 335 |
return $statuses_types; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Get interval text for recurring and split payment |
| 340 |
* |
| 341 |
* @since 1.3.0 |
| 342 |
*/ |
| 343 |
public function get_interval_text( $interval = '' ){ |
| 344 |
$interval_text = ''; |
| 345 |
|
| 346 |
switch( $interval ) { |
| 347 |
case 'day': |
| 348 |
$interval_text = 'Daily'; |
| 349 |
break; |
| 350 |
|
| 351 |
case 'month': |
| 352 |
case 'year': |
| 353 |
$interval_text = $interval . 'ly'; |
| 354 |
break; |
| 355 |
|
| 356 |
default : |
| 357 |
$interval_text = ! empty( $interval ) ? "Per " . esc_html( $interval ) : ""; |
| 358 |
break; |
| 359 |
} |
| 360 |
|
| 361 |
return esc_html( ucfirst( $interval_text ) ); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Output the page header, compatible with both classic and block themes. |
| 366 |
* |
| 367 |
* Block themes don't use get_header() — they rely on block template parts. |
| 368 |
* We detect this with wp_is_block_theme() and render the header template |
| 369 |
* part via do_blocks() so it appears correctly on CPT single pages that |
| 370 |
* bypass the theme's block templates. |
| 371 |
* |
| 372 |
* @param string $block_header Pre-rendered header markup from do_blocks() (block themes only). |
| 373 |
* @return void |
| 374 |
*/ |
| 375 |
public static function render_header( string $block_header = '' ): void { |
| 376 |
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 377 |
?> |
| 378 |
<!doctype html> |
| 379 |
<html <?php language_attributes(); ?>> |
| 380 |
<head> |
| 381 |
<meta charset="<?php bloginfo( 'charset' ); ?>"> |
| 382 |
<?php wp_head(); ?> |
| 383 |
</head> |
| 384 |
<body <?php body_class(); ?>> |
| 385 |
<?php |
| 386 |
wp_body_open(); |
| 387 |
echo $block_header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 388 |
} else { |
| 389 |
get_header(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Output the page footer, compatible with both classic and block themes. |
| 395 |
* |
| 396 |
* @param string $block_footer Pre-rendered footer markup from do_blocks() (block themes only). |
| 397 |
* @return void |
| 398 |
*/ |
| 399 |
public static function render_footer( string $block_footer = '' ): void { |
| 400 |
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 401 |
echo $block_footer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 402 |
wp_footer(); |
| 403 |
?> |
| 404 |
</body> |
| 405 |
</html> |
| 406 |
<?php |
| 407 |
} else { |
| 408 |
get_footer(); |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
// Helper Functions |
| 415 |
if ( ! function_exists( 'better_payment_dd' ) ) { |
| 416 |
function better_payment_dd( $data, $show_query = 0, $print_only = 0 ) { |
| 417 |
global $wpdb; |
| 418 |
|
| 419 |
if ( 1 === $show_query ) { |
| 420 |
echo wp_kses_post( $wpdb->last_query ); |
| 421 |
echo wp_kses_post( $wpdb->last_result ); |
| 422 |
echo wp_kses_post( $wpdb->last_error ); |
| 423 |
} |
| 424 |
|
| 425 |
echo '<pre>'; |
| 426 |
print_r( $data ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 427 |
if ( ! $print_only ) { |
| 428 |
wp_die( 'Printed!' ); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|