mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-16 21:23:39 +09:00
- made hooks documentation;
- some optimizations and make single functions for some hooks;
This commit is contained in:
@@ -5,121 +5,190 @@ namespace um\core;
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
if ( ! class_exists( 'Date_Time' ) ) {
|
||||
class Date_Time {
|
||||
|
||||
function __construct() {
|
||||
|
||||
}
|
||||
/**
|
||||
* Class Date_Time
|
||||
* @package um\core
|
||||
*/
|
||||
class Date_Time {
|
||||
|
||||
/***
|
||||
*** @Display time in specific format
|
||||
***/
|
||||
function get_time( $format ) {
|
||||
return current_time( $format );
|
||||
}
|
||||
/**
|
||||
* Date_Time constructor.
|
||||
*/
|
||||
function __construct() {
|
||||
|
||||
/***
|
||||
*** @Show a cool time difference between 2 timestamps
|
||||
***/
|
||||
function time_diff( $from, $to = '' ) {
|
||||
if ( empty( $to ) ) {
|
||||
$to = time();
|
||||
}
|
||||
$diff = (int) abs( $to - $from );
|
||||
if ( $diff < 60 ) {
|
||||
}
|
||||
|
||||
$since = __('just now','ultimate-member');
|
||||
|
||||
} elseif ( $diff < HOUR_IN_SECONDS ) {
|
||||
/**
|
||||
* Display time in specific format
|
||||
*
|
||||
* @param $format
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function get_time( $format ) {
|
||||
return current_time( $format );
|
||||
}
|
||||
|
||||
$mins = round( $diff / MINUTE_IN_SECONDS );
|
||||
if ( $mins <= 1 )
|
||||
$mins = 1;
|
||||
if ( $mins == 1 ) {
|
||||
$since = sprintf( __('%s min','ultimate-member'), $mins );
|
||||
} else {
|
||||
$since = sprintf( __('%s mins','ultimate-member'), $mins );
|
||||
}
|
||||
|
||||
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
|
||||
/**
|
||||
* Show a cool time difference between 2 timestamps
|
||||
*
|
||||
* @param $from
|
||||
* @param string $to
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
function time_diff( $from, $to = '' ) {
|
||||
$since = '';
|
||||
|
||||
$hours = round( $diff / HOUR_IN_SECONDS );
|
||||
if ( $hours <= 1 )
|
||||
$hours = 1;
|
||||
if ( $hours == 1 ) {
|
||||
$since = sprintf( __('%s hr','ultimate-member'), $hours );
|
||||
} else {
|
||||
$since = sprintf( __('%s hrs','ultimate-member'), $hours );
|
||||
}
|
||||
if ( empty( $to ) ) {
|
||||
$to = time();
|
||||
}
|
||||
|
||||
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
|
||||
$diff = (int) abs( $to - $from );
|
||||
if ( $diff < 60 ) {
|
||||
|
||||
$days = round( $diff / DAY_IN_SECONDS );
|
||||
if ( $days <= 1 )
|
||||
$days = 1;
|
||||
if ( $days == 1 ) {
|
||||
$since = sprintf( __('Yesterday at %s','ultimate-member'), date('g:ia', $from ) );
|
||||
} else {
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
}
|
||||
$since = __('just now','ultimate-member');
|
||||
|
||||
} elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
|
||||
} elseif ( $diff < HOUR_IN_SECONDS ) {
|
||||
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
$mins = round( $diff / MINUTE_IN_SECONDS );
|
||||
if ( $mins <= 1 )
|
||||
$mins = 1;
|
||||
if ( $mins == 1 ) {
|
||||
$since = sprintf( __('%s min','ultimate-member'), $mins );
|
||||
} else {
|
||||
$since = sprintf( __('%s mins','ultimate-member'), $mins );
|
||||
}
|
||||
|
||||
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
|
||||
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
|
||||
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
$hours = round( $diff / HOUR_IN_SECONDS );
|
||||
if ( $hours <= 1 )
|
||||
$hours = 1;
|
||||
if ( $hours == 1 ) {
|
||||
$since = sprintf( __('%s hr','ultimate-member'), $hours );
|
||||
} else {
|
||||
$since = sprintf( __('%s hrs','ultimate-member'), $hours );
|
||||
}
|
||||
|
||||
} elseif ( $diff >= YEAR_IN_SECONDS ) {
|
||||
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
|
||||
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date( 'F d, Y', $from ), date('g:ia', $from ) );
|
||||
$days = round( $diff / DAY_IN_SECONDS );
|
||||
if ( $days <= 1 )
|
||||
$days = 1;
|
||||
if ( $days == 1 ) {
|
||||
$since = sprintf( __('Yesterday at %s','ultimate-member'), date('g:ia', $from ) );
|
||||
} else {
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
}
|
||||
|
||||
}
|
||||
} elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
|
||||
|
||||
return apply_filters( 'um_human_time_diff', $since, $diff, $from, $to );
|
||||
}
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
|
||||
/***
|
||||
*** @Get age
|
||||
***/
|
||||
function get_age($then) {
|
||||
if ( !$then ) return '';
|
||||
$then_ts = strtotime($then);
|
||||
$then_year = date('Y', $then_ts);
|
||||
$age = date('Y') - $then_year;
|
||||
if( strtotime('+' . $age . ' years', $then_ts) > current_time( 'timestamp' ) ) $age--;
|
||||
if ( $age == 1 )
|
||||
return sprintf(__('%s year old','ultimate-member'), $age );
|
||||
if ( $age > 1 )
|
||||
return sprintf(__('%s years old','ultimate-member'), $age );
|
||||
if ( $age == 0 )
|
||||
return __('Less than 1 year old','ultimate-member');
|
||||
}
|
||||
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
|
||||
|
||||
/***
|
||||
*** @Reformat dates
|
||||
***/
|
||||
function format($old, $new){
|
||||
$datetime = new \DateTime($old);
|
||||
$output = $datetime->format( $new );
|
||||
return $output;
|
||||
}
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date('F d', $from ), date('g:ia', $from ) );
|
||||
|
||||
/***
|
||||
*** @Get last 30 days as array
|
||||
***/
|
||||
function get_last_days($num = 30, $reverse = true) {
|
||||
$d = array();
|
||||
for($i = 0; $i < $num; $i++) {
|
||||
$d[ date('Y-m-d', strtotime('-'. $i .' days')) ] = date('m/d', strtotime('-'. $i .' days'));
|
||||
}
|
||||
if ($reverse == true){
|
||||
return array_reverse($d);
|
||||
} else {
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
} elseif ( $diff >= YEAR_IN_SECONDS ) {
|
||||
|
||||
}
|
||||
$since = sprintf(__('%s at %s','ultimate-member'), date( 'F d, Y', $from ), date('g:ia', $from ) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* UM hook
|
||||
*
|
||||
* @type filter
|
||||
* @title um_human_time_diff
|
||||
* @description Change human time string
|
||||
* @input_vars
|
||||
* [{"var":"$since","type":"string","desc":"Disable UM Cron?"},
|
||||
* {"var":"$diff","type":"int","desc":"Difference in seconds"},
|
||||
* {"var":"$from","type":"int","desc":"From Timestamp"},
|
||||
* {"var":"$to","type":"int","desc":"To Timestamp"}]
|
||||
* @change_log
|
||||
* ["Since: 2.0"]
|
||||
* @usage add_filter( 'um_human_time_diff', 'function_name', 10, 4 );
|
||||
* @example
|
||||
* <?php
|
||||
* add_filter( 'um_human_time_diff', 'my_human_time_diff', 10, 4 );
|
||||
* function my_human_time_diff( $since, $diff, $from, $to ) {
|
||||
* // your code here
|
||||
* return $since;
|
||||
* }
|
||||
* ?>
|
||||
*/
|
||||
return apply_filters( 'um_human_time_diff', $since, $diff, $from, $to );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get age
|
||||
*
|
||||
* @param $then
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function get_age( $then ) {
|
||||
if ( ! $then ) {
|
||||
return '';
|
||||
}
|
||||
$then_ts = strtotime( $then );
|
||||
$then_year = date( 'Y', $then_ts );
|
||||
$age = date( 'Y' ) - $then_year;
|
||||
if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
|
||||
$age--;
|
||||
}
|
||||
if ( $age == 1 ) {
|
||||
return sprintf( __( '%s year old', 'ultimate-member' ), $age );
|
||||
}
|
||||
if ( $age > 1 ) {
|
||||
return sprintf( __( '%s years old', 'ultimate-member' ), $age );
|
||||
}
|
||||
if ( $age == 0 ) {
|
||||
return __( 'Less than 1 year old', 'ultimate-member' );
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reformat dates
|
||||
*
|
||||
* @param $old
|
||||
* @param $new
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function format( $old, $new ) {
|
||||
$datetime = new \DateTime( $old );
|
||||
$output = $datetime->format( $new );
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get last 30 days as array
|
||||
*
|
||||
* @param int $num
|
||||
* @param bool $reverse
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function get_last_days( $num = 30, $reverse = true ) {
|
||||
$d = array();
|
||||
for ( $i = 0; $i < $num; $i++ ) {
|
||||
$d[ date('Y-m-d', strtotime( '-' . $i . ' days' ) ) ] = date( 'm/d', strtotime( '-' . $i . ' days' ) );
|
||||
}
|
||||
|
||||
return ( $reverse ) ? array_reverse( $d ) : $d;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user