Current File : /home/bravrvjk/cepurhuye.rw/wp-content/plugins/bopea-function/inc//date-time.php
<?php
defined('ABSPATH') || die();
class bopea_ago_datetime {
private $opts;
private $date_format;
public function __construct() {
$jl_ago_pos = get_theme_mod('jl_ago_pos', 'after');
$default_opts = array(
'active' => array( 'date' => true, 'time' => true, 'modified_date' => false, 'modified_time' => false ),
'position' => $jl_ago_pos,
'time' => array( 'number' => '13', 'type' => 'months' ),
'ago_label' => ''
);
$this->opts = $this->parse_args( $this->opts, $default_opts);
$filter_list = array();
if ( $this->opts['active']['date'] ):
$filter_list = array_merge( $filter_list, array( 'the_date', 'get_the_date' ) );
endif;
if ( $this->opts['active']['time'] ) :
$filter_list = array_merge( $filter_list, array( 'get_the_time', 'the_time' ) );
endif;
if ( $this->opts['active']['modified_date'] ) :
$filter_list = array_merge( $filter_list, array( 'get_the_modified_date', 'the_modified_date' ) );
endif;
if ( $this->opts['active']['modified_time'] ) :
$filter_list = array_merge( $filter_list, array( 'get_the_modified_time', 'the_modified_time' ) );
endif;
$filters = apply_filters(
'bopea_datetime_filters',
$filter_list
);
foreach ( $filters as $filter ) :
add_filter( $filter, array( &$this, 'convert_date' ), 10, 2 );
endforeach;
}
public function convert_date( $orig_time, $date_format ) {
$this->date_format = $date_format;
if( !$this->can_convert_date() ){
return $orig_time;
}
$time_arr = array(
'minutes' => 60,
'hours' => HOUR_IN_SECONDS,
'days' => DAY_IN_SECONDS,
'months' => YEAR_IN_SECONDS / 12,
);
if ( !$this->opts['active'] ){
return $orig_time;
}
$time = current_time( 'timestamp' );
$limit = (int)$this->opts['time']['number'] * $time_arr[$this->opts['time']['type']] ;
global $post;
$post_time = strpos( current_filter(), 'modified' ) ? strtotime( $post->post_modified ) : strtotime( $post->post_date );
if ( ( $time - $post_time ) <= $limit ) {
if ( class_exists( 'bopeatxt' ) ){
$ago_label = !empty( $this->opts['ago_label'] ) ? $this->opts['ago_label'] : bopeatxt::bopea_s_ago();
}else{
$ago_label = !empty( $this->opts['ago_label'] ) ? $this->opts['ago_label'] : __( 'ago', 'bopea-function' );
}
$bopea_s_min = get_theme_mod('bopea_s_min');
if ( !empty( $bopea_s_min ) ){
$bopea_s_min = $bopea_s_min;
}else{
$bopea_s_min = esc_html__( 'min', 'bopea-function' );
}
$bopea_s_mins = get_theme_mod('bopea_s_mins');
if ( !empty( $bopea_s_mins ) ){
$bopea_s_mins = $bopea_s_mins;
}else{
$bopea_s_mins = esc_html__( 'mins', 'bopea-function' );
}
$bopea_s_hour = get_theme_mod('bopea_s_hour');
if ( !empty( $bopea_s_hour ) ){
$bopea_s_hour = $bopea_s_hour;
}else{
$bopea_s_hour = esc_html__( 'hour', 'bopea-function' );
}
$bopea_s_hours = get_theme_mod('bopea_s_hours');
if ( !empty( $bopea_s_hours ) ){
$bopea_s_hours = $bopea_s_hours;
}else{
$bopea_s_hours = esc_html__( 'hours', 'bopea-function' );
}
$bopea_s_day = get_theme_mod('bopea_s_day');
if ( !empty( $bopea_s_day ) ){
$bopea_s_day = $bopea_s_day;
}else{
$bopea_s_day = esc_html__( 'day', 'bopea-function' );
}
$bopea_s_days = get_theme_mod('bopea_s_days');
if ( !empty( $bopea_s_days ) ){
$bopea_s_days = $bopea_s_days;
}else{
$bopea_s_days = esc_html__( 'days', 'bopea-function' );
}
$bopea_s_week = get_theme_mod('bopea_s_week');
if ( !empty( $bopea_s_week ) ){
$bopea_s_week = $bopea_s_week;
}else{
$bopea_s_week = esc_html__( 'week', 'bopea-function' );
}
$bopea_s_weeks = get_theme_mod('bopea_s_weeks');
if ( !empty( $bopea_s_weeks ) ){
$bopea_s_weeks = $bopea_s_weeks;
}else{
$bopea_s_weeks = esc_html__( 'weeks', 'bopea-function' );
}
$bopea_s_month = get_theme_mod('bopea_s_month');
if ( !empty( $bopea_s_month ) ){
$bopea_s_month = $bopea_s_month;
}else{
$bopea_s_month = esc_html__( 'month', 'bopea-function' );
}
$bopea_s_months = get_theme_mod('bopea_s_months');
if ( !empty( $bopea_s_months ) ){
$bopea_s_months = $bopea_s_months;
}else{
$bopea_s_months = esc_html__( 'months', 'bopea-function' );
}
$bopea_s_year = get_theme_mod('bopea_s_year');
if ( !empty( $bopea_s_year ) ){
$bopea_s_year = $bopea_s_year;
}else{
$bopea_s_year = esc_html__( 'year', 'bopea-function' );
}
$search = array('mins', 'min', 'hours', 'hour', 'days', 'day', 'weeks', 'week', 'months', 'month' , 'year');
$replace = array($bopea_s_mins, $bopea_s_min, $bopea_s_hours, $bopea_s_hour, $bopea_s_days, $bopea_s_day, $bopea_s_weeks, $bopea_s_week, $bopea_s_months, $bopea_s_month, $bopea_s_year);
if ( $this->opts['position'] === 'after' ) {
// return human_time_diff( $post_time, $time ).' '.$ago_label;
$string = human_time_diff( $post_time, $time ).' '.$ago_label;
return str_replace($search, $replace, $string);
} else {
// return $ago_label.' '.human_time_diff( $post_time, $time );
$string = $ago_label.' '.human_time_diff( $post_time, $time );
return str_replace($search, $replace, $string);
}
}
return $orig_time;
}
function can_convert_date(){
return $this->is_amp() || !$this->is_valid_date_format() ? false : true;
}
function is_valid_date_format(){
if($this->date_format === ""){
$this->date_format = get_option( 'date_format' );
}
$this->date_format = preg_replace('/[^\da-z]/i', '', $this->date_format);
return strlen($this->date_format) >= 2;
}
function is_amp(){
return function_exists( 'is_amp_endpoint') && is_amp_endpoint();
}
function parse_args( &$a, $b ) {
$a = (array) $a;
$b = (array) $b;
$r = $b;
foreach ( $a as $k => &$v ) {
if ( is_array( $v ) && isset( $r[ $k ] ) ) {
$r[ $k ] = $this->parse_args( $v, $r[ $k ] );
} else {
$r[ $k ] = $v;
}
}
return $r;
}
}
?>