From a870d95e40549d9ff0c9da0b97166ed835e6c58f Mon Sep 17 00:00:00 2001 From: olatechpro Date: Mon, 9 Sep 2024 07:39:37 +0100 Subject: [PATCH] Allow users to chose the publish date in the "New Post" box #1743 --- common/php/class-module.php | 96 +++++++++++++++++++ modules/content-board/content-board.php | 31 ++++++ modules/content-board/lib/content-board.css | 7 +- modules/content-overview/content-overview.php | 31 ++++++ .../content-overview/lib/content-overview.css | 7 +- 5 files changed, 170 insertions(+), 2 deletions(-) diff --git a/common/php/class-module.php b/common/php/class-module.php index 162e1418..6ab27d5e 100644 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -1178,5 +1178,101 @@ public function get_author_markup($post, $can_edit_post) { } } + public function get_publish_date_markup() { + ob_start(); + ?> +
+
+ touch_time(); ?> +
+
+ 0 ) { + $tab_index_attribute = " tabindex=\"$tab_index\""; + } + + $jj = current_time( 'd' ); + $mm = current_time( 'm' ); + $aa = current_time( 'Y' ); + $hh = current_time( 'H' ); + $mn = current_time( 'i' ); + $ss = current_time( 's' ); + + $cur_jj = current_time( 'd' ); + $cur_mm = current_time( 'm' ); + $cur_aa = current_time( 'Y' ); + $cur_hh = current_time( 'H' ); + $cur_mn = current_time( 'i' ); + + $month = ''; + + $day = ''; + $year = ''; + $hour = ''; + $minute = ''; + + echo '
'; + /* translators: 1: Month, 2: Day, 3: Year, 4: Hour, 5: Minute. */ + printf( __( '%1$s %2$s, %3$s at %4$s:%5$s' ), $month, $day, $year, $hour, $minute ); + + echo '
'; + + if ( $multi ) { + return; + } + + echo "\n\n"; + + $map = array( + 'mm' => array( $mm, $cur_mm ), + 'jj' => array( $jj, $cur_jj ), + 'aa' => array( $aa, $cur_aa ), + 'hh' => array( $hh, $cur_hh ), + 'mn' => array( $mn, $cur_mn ), + ); + + foreach ( $map as $timeunit => $value ) { + list( $unit, $curr ) = $value; + + echo '' . "\n"; + $cur_timeunit = 'cur_' . $timeunit; + echo '' . "\n"; + } + ?> + $status ]; + // set post date + if ( ! empty( $_POST['mm'] ) ) { + $aa = sanitize_text_field($_POST['aa']); + $mm = sanitize_text_field($_POST['mm']); + $jj = sanitize_text_field($_POST['jj']); + $hh = sanitize_text_field($_POST['hh']); + $mn = sanitize_text_field($_POST['mn']); + $ss = sanitize_text_field($_POST['ss']); + $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa; + $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm; + $jj = ( $jj > 31 ) ? 31 : $jj; + $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj; + $hh = ( $hh > 23 ) ? $hh - 24 : $hh; + $mn = ( $mn > 59 ) ? $mn - 60 : $mn; + $ss = ( $ss > 59 ) ? $ss - 60 : $ss; + + + $post_date = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss ); + $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); + if ($valid_date ) { + $postArgs['post_date'] = $post_date; + $postArgs['post_date_gmt'] = get_gmt_from_date( $post_date ); + } + } + $postId = wp_insert_post($postArgs); if ($postId) { @@ -1772,6 +1797,12 @@ public function getPostTypeFields($postType) 'value' => 'draft', 'type' => 'status', 'options' => $this->getUserAuthorizedPostStatusOptions($postType) + ], + 'pdate' => [ + 'label' => __('Publish Date', 'publishpress'), + 'value' => 'immediately', + 'type' => 'pdate', + 'html' => $this->get_publish_date_markup() ] ]; diff --git a/modules/content-board/lib/content-board.css b/modules/content-board/lib/content-board.css index a3ec14d5..73c32b7f 100644 --- a/modules/content-board/lib/content-board.css +++ b/modules/content-board/lib/content-board.css @@ -436,9 +436,14 @@ padding-bottom: 5px; } +.content-board-form-table .misc-pub-section { + padding-left: 0; + padding-right: 0; +} + .content-board-form-table input[type="text"], .content-board-form-table textarea, -.content-board-form-table select { +.content-board-form-table select:not(#mm) { width: 250px; } diff --git a/modules/content-overview/content-overview.php b/modules/content-overview/content-overview.php index 6b304aae..dcc8ae14 100644 --- a/modules/content-overview/content-overview.php +++ b/modules/content-overview/content-overview.php @@ -944,6 +944,31 @@ public function update_content_overview_form_action() { 'post_status' => $status ]; + // set post date + if ( ! empty( $_POST['mm'] ) ) { + $aa = sanitize_text_field($_POST['aa']); + $mm = sanitize_text_field($_POST['mm']); + $jj = sanitize_text_field($_POST['jj']); + $hh = sanitize_text_field($_POST['hh']); + $mn = sanitize_text_field($_POST['mn']); + $ss = sanitize_text_field($_POST['ss']); + $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa; + $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm; + $jj = ( $jj > 31 ) ? 31 : $jj; + $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj; + $hh = ( $hh > 23 ) ? $hh - 24 : $hh; + $mn = ( $mn > 59 ) ? $mn - 60 : $mn; + $ss = ( $ss > 59 ) ? $ss - 60 : $ss; + + + $post_date = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss ); + $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); + if ($valid_date ) { + $postArgs['post_date'] = $post_date; + $postArgs['post_date_gmt'] = get_gmt_from_date( $post_date ); + } + } + $postId = wp_insert_post($postArgs); if ($postId) { @@ -1709,6 +1734,12 @@ public function getPostTypeFields($postType) 'value' => 'draft', 'type' => 'status', 'options' => $this->getUserAuthorizedPostStatusOptions($postType) + ], + 'pdate' => [ + 'label' => __('Publish Date', 'publishpress'), + 'value' => 'immediately', + 'type' => 'pdate', + 'html' => $this->get_publish_date_markup() ] ]; diff --git a/modules/content-overview/lib/content-overview.css b/modules/content-overview/lib/content-overview.css index e59ee2fa..8e0540e0 100644 --- a/modules/content-overview/lib/content-overview.css +++ b/modules/content-overview/lib/content-overview.css @@ -161,9 +161,14 @@ table.content-overview thead td { padding-bottom: 5px; } +.content-overview-form-table .misc-pub-section { + padding-left: 0; + padding-right: 0; +} + .content-overview-form-table input[type="text"], .content-overview-form-table textarea, -.content-overview-form-table select { +.content-overview-form-table select:not(#mm) { width: 250px; }