PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / trunk
Social Media Auto Poster – Schedule & Publish to Buffer vtrunk
6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 All 124 releases
wp-to-buffer / lib / social / includes / class-date.php

class-date.php in Social Media Auto Poster – Schedule & Publish to Buffer trunk, at lib/social/includes/class-date.php

177 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Date class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Helper functions for changing dates and returning time offsets
13 * based on the WordPress configuration.
14 *
15 * @package WPZinc\Social
16 * @author WP Zinc
17 * @version 4.6.9
18 */
19 class Date {
20
21 /**
22 * Holds the base class object.
23 *
24 * @since 4.6.9
25 *
26 * @var object
27 */
28 public $base;
29
30 /**
31 * Constructor
32 *
33 * @since 4.6.9
34 *
35 * @param object $base Base Plugin Class.
36 */
37 public function __construct( $base ) {
38
39 // Store base class.
40 $this->base = $base;
41
42 }
43
44 /**
45 * Helper method to return the adjusted date and time based on the given parameters
46 *
47 * @since 4.6.9
48 *
49 * @param mixed $date Date.
50 * @param string $before_or_after Whether to subtract (before) or add (after) to the date.
51 * @param int $days Day(s) to add or subtract.
52 * @param int $hours Hour(s) to add or subtract.
53 * @param int $minutes Minute(s) to add or subtract.
54 * @return string Adjusted Date and Time (yyyy-mm-dd hh:ii:ss)
55 */
56 public function adjust_date_time( $date, $before_or_after, $days, $hours, $minutes ) {
57
58 // Bail if no date.
59 if ( ! $date ) {
60 return $date;
61 }
62
63 // Add or subtract days, hours and minutes from the date.
64 switch ( $before_or_after ) {
65 /**
66 * Subtract
67 */
68 case 'before':
69 $date = strtotime( '-' . $days . ' days -' . $hours . ' hours -' . $minutes . ' minutes', strtotime( $date ) );
70 break;
71
72 /**
73 * Add
74 */
75 default:
76 $date = strtotime( '+' . $days . ' days +' . $hours . ' hours +' . $minutes . ' minutes', strtotime( $date ) );
77 break;
78 }
79
80 return gmdate( 'Y-m-d H:i:s', $date );
81 }
82
83 /**
84 * Returns the UTC Date and Time for the given Date and Time, based on WordPress' GMT Offset.
85 *
86 * When sending a specific date and time to schedule a status, the datetime that we send via the API must be in UTC.
87 * The social media service can then apply its timezone offset as defined by the user account's settings.
88 *
89 * For example, calling this function with 2018-09-01 13:00:00 in a UTC+1 timezone will return as 2018-09-01 12:00:00.
90 * The social media service will then schedule for 2018-09-01 13:00:00, because the social media services' timezone (UTC+1)
91 * will (in this case) add an hour back to the scheduled datetime.
92 *
93 * @since 4.6.9
94 *
95 * @param string $date_time Date and Time (yyyy-mm-dd HH:ii:ss).
96 * @return string UTC Date and Time (yyyy-mm-dd HH:ii:ss)
97 */
98 public function get_utc_date_time( $date_time ) {
99
100 // If there is no offset, the date and time is already UTC.
101 $gmt_offset = get_option( 'gmt_offset' );
102 if ( ! $gmt_offset ) {
103 return $date_time;
104 }
105
106 // Convert the GMT offset to an offset value e.g. +0300, -0530.
107 $gmt_offset = $this->convert_wordpress_gmt_offset_to_offset_value( $gmt_offset );
108
109 // Offset the date and time by the timezone.
110 $date_object = date_create( $date_time, timezone_open( $gmt_offset ) );
111 date_timezone_set( $date_object, timezone_open( 'UTC' ) );
112
113 // Return adjusted date and time.
114 return date_format( $date_object, 'Y-m-d H:i:s' );
115
116 }
117
118 /**
119 * Converts WordPress' GMT Offset (e.g. -5, +3.3) to an offset value compatible with
120 * WordPress' DateTime object (e.g. -0500, +0330)
121 *
122 * @since 3.6.2
123 *
124 * @param float $gmt_offset GMT Offset.
125 * @return string GMT Offset Value
126 */
127 public function convert_wordpress_gmt_offset_to_offset_value( $gmt_offset ) {
128
129 // Don't do anything if the offset is zero.
130 if ( $gmt_offset == 0 ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
131 return '+0000';
132 }
133
134 // Define the GMT offset string e.g. +0100, -0300 etc.
135 if ( $gmt_offset > 0 ) {
136 if ( $gmt_offset < 10 ) {
137 $gmt_offset = '0' . abs( $gmt_offset );
138 } else {
139 $gmt_offset = abs( $gmt_offset );
140 }
141
142 $gmt_offset = '+' . $gmt_offset;
143 } elseif ( $gmt_offset < 0 ) {
144 if ( $gmt_offset > -10 ) {
145 $gmt_offset = '0' . abs( $gmt_offset );
146 } else {
147 $gmt_offset = abs( $gmt_offset );
148 }
149
150 $gmt_offset = '-' . $gmt_offset;
151 }
152
153 // If the GMT offset contains .5, change this to :30.
154 // Otherwise pad the GMT offset.
155 if ( strpos( $gmt_offset, '.5' ) !== false ) {
156 $gmt_offset = str_replace( '.5', ':30', $gmt_offset );
157 } else {
158 $gmt_offset .= '00';
159 }
160
161 /**
162 * Converts WordPress' GMT Offset (e.g. -5, +3.3) to an offset value compatible with
163 * WordPress' DateTime object (e.g. -0500, +0330)
164 *
165 * @since 3.6.2
166 *
167 * @param string $gmt_offset GMT Offset (e.g. -0500, +0330).
168 */
169 $gmt_offset = apply_filters( $this->base->plugin->filter_name . '_common_convert_wordpress_gmt_offset_to_offset_value', $gmt_offset );
170
171 // Return.
172 return $gmt_offset;
173
174 }
175
176 }
177