Skip to content

Commit 97b1ba1

Browse files
sbassahspivurno
andauthored
Update post date with either a date or time or both. (#614)
* `gw-update-posts.php`: Added support to update post date and time. * `gw-update-posts.php`: Added support to update post date gmt. * `gw-update-posts.php`: Made requested changes. * `gw-update-posts.php`: Added support to set post date. * ~ Functionality was getting beefy so I split it into its own function and tweaked slightly. * ~ Fixed notice. --------- Co-authored-by: David Smith <[email protected]>
1 parent a0a7905 commit 97b1ba1

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function __construct( $args = array() ) {
2727
'terms' => array(),
2828
'meta' => array(),
2929
'featured_image' => false,
30+
'post_date' => array(
31+
'date' => false,
32+
'time' => false,
33+
),
3034
// If property is mapped but no entry value is submitted, delete the property.
3135
// Currently only works with 'featured_image' and custom fields specified in 'meta'.
3236
'delete_if_empty' => false,
@@ -92,25 +96,8 @@ public function update_post_by_entry( $entry, $form ) {
9296
$post->post_name = rgar( $entry, $this->_args['slug'] );
9397
}
9498

95-
if ( $this->_args['date_time'] ) {
96-
$date = $this->_args['date_time']['date'];
97-
$time = $this->_args['date_time']['time'];
98-
99-
$date = rgar( $entry, $date );
100-
$time = rgar( $entry, $time, '00:00 am' );
101-
102-
if ( empty( $date ) ) {
103-
$date = explode( ' ', $post->post_date )[0];
104-
}
105-
106-
if ( $time ) {
107-
list( $hour, $min, $am_pm ) = array_pad( preg_split( '/[: ]/', $time ), 3, false );
108-
if ( strtolower( $am_pm ) == 'pm' ) {
109-
$hour += 12;
110-
}
111-
}
112-
113-
$new_date_time = gmdate( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) );
99+
if ( $this->_args['post_date'] ) {
100+
$new_date_time = $this->get_post_date( $entry, $form );
114101
$post->post_date = $new_date_time;
115102
$post->post_date_gmt = get_gmt_from_date( $new_date_time );
116103
}
@@ -222,4 +209,30 @@ public function return_ids_instead_of_names( $value, $field, $template_name, $po
222209
return $value;
223210
}
224211

212+
public function get_post_date( $entry, $form ) {
213+
214+
if ( ! is_array( $this->_args['post_date'] ) ) {
215+
$post_date_field = GFAPI::get_field( $form, $this->_args['post_date'] );
216+
if ( $post_date_field->get_input_type() === 'date' ) {
217+
$post_date['date'] = $this->_args['post_date'];
218+
$post_date['time'] = '';
219+
} elseif ( $post_date_field->get_input_type() === 'time' ) {
220+
$post_date['time'] = $this->_args['post_date'];
221+
$post_date['date'] = '';
222+
}
223+
}
224+
225+
$date = rgar( $entry, $post_date['date'], gmdate( 'm/d/Y' ) );
226+
$time = rgar( $entry, $post_date['time'], '00:00 am' );
227+
228+
if ( $time ) {
229+
list( $hour, $min, $am_pm ) = array_pad( preg_split( '/[: ]/', $time ), 3, false );
230+
if ( strtolower( $am_pm ) == 'pm' ) {
231+
$hour += 12;
232+
}
233+
}
234+
235+
return gmdate( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) );
236+
}
237+
225238
}

0 commit comments

Comments
 (0)