PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | sal/class.json-api-date.php +103 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,103 @@
1 +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 +/**
3 + * WPCOM_JSON_API_Date class.
4 + *
5 + * @package automattic/jetpack
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 + exit( 0 );
10 +}
11 +
12 +/**
13 + * Base class for WPCOM_JSON_API_Date.
14 + */
15 +class WPCOM_JSON_API_Date {
16 + /**
17 + * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
18 + *
19 + * @param string $date_gmt GMT datetime string.
20 + * @param string $date Optional. Used to calculate the offset from GMT.
21 + *
22 + * @return string
23 + */
24 + public static function format_date( $date_gmt, $date = null ) {
25 + $offset = 0;
26 + $timestamp_gmt = strtotime( "$date_gmt+0000" );
27 +
28 + if ( null === $date ) {
29 + $timestamp = $timestamp_gmt;
30 + $west = 0;
31 + $minutes = 0;
32 + $hours = 0;
33 + } else {
34 + $date_time = date_create( "$date+0000" );
35 + if ( $date_time ) {
36 + $timestamp = date_format( $date_time, 'U' );
37 + } else {
38 + $timestamp = 0;
39 + }
40 +
41 + // "0000-00-00 00:00:00" == -62169984000
42 + if ( -62169984000 === $timestamp_gmt ) {
43 + // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts
44 + // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts.
45 +
46 + // Try to guess the correct offset from the blog's options.
47 + $timezone_string = get_option( 'timezone_string' );
48 +
49 + if ( $timezone_string && $date_time ) {
50 + $timezone = timezone_open( $timezone_string );
51 + if ( $timezone ) {
52 + $offset = $timezone->getOffset( $date_time );
53 + }
54 + } else {
55 + $offset = 3600 * (float) get_option( 'gmt_offset' );
56 + }
57 + } else {
58 + $offset = $timestamp - $timestamp_gmt;
59 + }
60 +
61 + $west = $offset < 0;
62 + $offset = abs( $offset );
63 + $hours = (int) floor( $offset / 3600 );
64 + $offset -= $hours * 3600;
65 + $minutes = (int) floor( $offset / 60 );
66 + }
67 +
68 + return gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );
69 + }
70 +
71 + /**
72 + * Returns ISO 8601 formatted duration interval: P0DT1H10M0S
73 + *
74 + * @param string $time Duration in minutes or hours.
75 + *
76 + * @return null|string
77 + */
78 + public static function format_duration( $time ) {
79 + $timestamp = strtotime( $time, 0 );
80 +
81 + // Bail early if we don't recognize a date.
82 + if ( empty( $timestamp ) ) {
83 + return;
84 + }
85 +
86 + $days = floor( $timestamp / 86400 );
87 + $timestamp %= 86400;
88 +
89 + $hours = floor( $timestamp / 3600 );
90 + $timestamp %= 3600;
91 +
92 + $minutes = floor( $timestamp / 60 );
93 + $timestamp %= 60;
94 +
95 + return sprintf(
96 + 'P%dDT%dH%dM%dS',
97 + $days,
98 + $hours,
99 + $minutes,
100 + $timestamp
101 + );
102 + }
103 +}