| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Dynamic_Keywords\Contact_Segmentation; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Time to deadline. |
| 7 |
* |
| 8 |
* @package Sellkit\Dynamic_Keywords\Contact_Segmentation |
| 9 |
* @since 1.1.0 |
| 10 |
*/ |
| 11 |
class Time_To_Deadline extends Contact_Segmentation_Base { |
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor. |
| 15 |
* |
| 16 |
* @since 1.1.0 |
| 17 |
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
parent::__construct(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get class id. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_id() { |
| 29 |
return '_time_to_deadline'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get class title. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function get_title() { |
| 38 |
return esc_html__( 'Time to Deadline', 'sellkit' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Render content. |
| 43 |
* |
| 44 |
* @param array $atts array of shortcode arguments. |
| 45 |
* @return string |
| 46 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 47 |
*/ |
| 48 |
public function render_content( $atts ) { |
| 49 |
$values = $this->get_content_meta( 'time-deadline' ); |
| 50 |
|
| 51 |
if ( |
| 52 |
empty( $values ) || |
| 53 |
( empty( $values['time_deadline'] ) && empty( $values['day_deadline'] ) ) |
| 54 |
) { |
| 55 |
return $this->shortcode_content( $atts ); |
| 56 |
} |
| 57 |
|
| 58 |
$time_deadline = strtotime( $values['time_deadline'] ); |
| 59 |
$current_time = strtotime( current_time( 'H:i' ) ); |
| 60 |
|
| 61 |
$result = []; |
| 62 |
|
| 63 |
if ( ! in_array( strtolower( date( 'l' ) ), $values['day_deadline'], true ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if ( $current_time > $time_deadline ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
date_default_timezone_set( 'UTC' ); // phpcs:ignore |
| 72 |
|
| 73 |
$remain_hour = date( 'H', $time_deadline ) - date( 'H', $current_time ); |
| 74 |
$hour_string = 1 === $remain_hour ? esc_html__( 'Hour', 'sellkit' ) : esc_html__( 'Hours', 'sellkit' ); |
| 75 |
|
| 76 |
$remain_mins = date( 'i', $time_deadline ) - date( 'i', $current_time ); |
| 77 |
$mins_string = 1 === $remain_mins ? esc_html__( 'Minute', 'sellkit' ) : esc_html__( 'Minutes', 'sellkit' ); |
| 78 |
|
| 79 |
if ( $remain_mins < 0 ) { |
| 80 |
$remain_mins = 60 + $remain_mins; |
| 81 |
--$remain_hour; |
| 82 |
} |
| 83 |
|
| 84 |
if ( $remain_hour > 0 ) { |
| 85 |
$result['hour'] = $remain_hour . ' ' . $hour_string; |
| 86 |
} |
| 87 |
|
| 88 |
if ( $remain_mins > 0 ) { |
| 89 |
$result['mins'] = $remain_mins . ' ' . $mins_string; |
| 90 |
} |
| 91 |
|
| 92 |
return implode( ' - ', $result ); |
| 93 |
} |
| 94 |
} |
| 95 |
|