From cb63ee8fc0f578542cec6e20dfcb37a675d007ba Mon Sep 17 00:00:00 2001 From: olatechpro Date: Thu, 16 May 2024 12:41:07 +0100 Subject: [PATCH] Improve the Schedule date box #1632 --- modules/content-board/content-board.php | 130 +++++++++----- .../content-board/lib/content-board-print.css | 161 +++++++++++++++++- modules/content-board/lib/content-board.css | 9 + modules/content-board/lib/content-board.js | 24 ++- readme.txt | 2 +- 5 files changed, 272 insertions(+), 54 deletions(-) diff --git a/modules/content-board/content-board.php b/modules/content-board/content-board.php index 8b67e1ef..be3ddfa3 100644 --- a/modules/content-board/content-board.php +++ b/modules/content-board/content-board.php @@ -117,7 +117,7 @@ class PP_Content_Board extends PP_Module /** * Custom methods * - * @var Array + * @var array */ private $terms_options = []; @@ -150,6 +150,11 @@ public function __construct() 'content_board_custom_columns' => '', 'content_board_filters' => '', 'content_board_custom_filters' => '', + + 'content_board_scheduled_date' => [ + 'number' => 1, + 'period' => 'weeks' + ], 'post_types' => [ 'post' => 'on', @@ -190,6 +195,7 @@ public function init() add_action('wp_ajax_publishpress_content_board_search_categories', [$this, 'sendJsonSearchCategories']); add_action('wp_ajax_publishpress_content_board_get_form_fields', [$this, 'getFormFieldAjaxHandler']); add_action('wp_ajax_publishpress_content_board_update_post_status', [$this, 'updatePostStatus']); + add_action('wp_ajax_publishpress_content_board_update_schedule_period', [$this, 'updateSchedulePeriod']); // Menu add_filter('publishpress_admin_menu_slug', [$this, 'filter_admin_menu_slug'], 20); @@ -1499,7 +1505,36 @@ public function content_board_customize_filter_form() { return ob_get_clean(); } + + public function updateSchedulePeriod() { + global $publishpress; + + $response['status'] = 'error'; + $response['content'] = esc_html__('An error occured', 'publishpress'); + + + if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_key($_POST['nonce']), 'content_board_action_nonce')) { + $response['content'] = esc_html__('Error validating nonce. Please reload this page and try again.', 'publishpress'); + } elseif (empty($_POST['schedule_number']) || empty($_POST['schedule_period'])) { + $response['content'] = esc_html__('Invalid form request.', 'publishpress'); + } else { + $schedule_number = (int) $_POST['schedule_number']; + $schedule_period = sanitize_text_field($_POST['schedule_period']); + $content_board_scheduled_date = [ + 'number' => $schedule_number, + 'period' => $schedule_period + ]; + $publishpress->update_module_option($this->module->name, 'content_board_scheduled_date', $content_board_scheduled_date); + $response['status'] = 'success'; + $response['content'] = esc_html__('Changes saved!', 'publishpress'); + } + + wp_send_json($response); + } + public function updatePostStatus() { + global $publishpress; + $response['status'] = 'error'; $response['content'] = esc_html__('An error occured', 'publishpress'); @@ -1510,7 +1545,8 @@ public function updatePostStatus() { $response['content'] = esc_html__('Invalid form request.', 'publishpress'); } else { $post_status = sanitize_text_field($_POST['post_status']); - $schedule_date = sanitize_text_field($_POST['schedule_date']); + $schedule_number = (int) $_POST['schedule_number']; + $schedule_period = sanitize_text_field($_POST['schedule_period']); $post_id = (int) $_POST['post_id']; $post_data = get_post($post_id); if (!is_object($post_data) || !isset($post_data->post_type)) { @@ -1531,8 +1567,16 @@ public function updatePostStatus() { $post_args['post_date_gmt'] = get_gmt_from_date($current_date_time); } elseif ($post_status === 'future') { // set future date if new status is schedule - $timestamp = strtotime($schedule_date); + $current_timestamp = time(); + $timestamp = strtotime($schedule_number . ' ' . $schedule_period, $current_timestamp); + $content_board_scheduled_date = [ + 'number' => $schedule_number, + 'period' => $schedule_period + ]; + $publishpress->update_module_option($this->module->name, 'content_board_scheduled_date', $content_board_scheduled_date); + $future_date = date('Y-m-d H:i:s', $timestamp); + $post_args['post_date'] = $future_date; $post_args['post_date_gmt'] = get_gmt_from_date($future_date); } @@ -2504,13 +2548,20 @@ public function printPostForPostType($postType) $post_statuses_slugs = array_column($post_statuses, 'slug'); if (!in_array('future', $post_statuses_slugs)) { // Add Scheduled status - $post_statuses[] = (object) [ + /*$post_statuses[] = (object) [ 'label' => __('Scheduled', 'publishpress'), 'description' => '', 'name' => 'future', 'slug' => 'future', 'position' => count($post_statuses) + 1 - ]; + ];*/ + $post_statuses = array_merge([(object) [ + 'label' => __('Scheduled', 'publishpress'), + 'description' => '', + 'name' => 'future', + 'slug' => 'future', + 'position' => count($post_statuses) + 1 + ]], $post_statuses); } // Group posts by status @@ -2531,44 +2582,41 @@ public function printPostForPostType($postType) $post_status_options = $this->get_post_status_options($post_status_object->slug); if ($post_status_object->slug === 'future') { - $current_time = current_time('timestamp'); - $time_in_two_weeks = strtotime('+1 weeks', $current_time); - $formatted_date = date_i18n('F j, Y H:i', $time_in_two_weeks); - - $metadata_start_name = 'content_board_scheduled_date'; - $date_markup = - sprintf( - '', - esc_attr($metadata_start_name), - esc_attr($formatted_date), - esc_attr(pp_convert_date_format_to_jqueryui_datepicker('Y-m-d H:i:s')), - '' - ); - $date_markup .= sprintf( - '', - esc_attr($metadata_start_name), - esc_attr($formatted_date) - ); + $content_board_scheduled_date = $this->module->options->content_board_scheduled_date; + $number_select = ''; + + $period_options = ['days', 'weeks', 'months', 'years']; + $period_select = ''; + + $date_markup = '
+
'. esc_html__("Default Date", "publishpress") .'
+
+
+ '. $number_select .' + '. $period_select .' +
+ +

'. esc_html__("This is the default publish date from today for posts moving to the Scheduled status.", "publishpress") .'

+ +
+ +
+
+
'; $modal_id = time(); $output_markup = ' 
- '. esc_html__("Schedule Date", "publishpress") .' + '. esc_html__("Default Date", "publishpress") .'
-