問題描述

我有一個 multi-author 棒球網站。我希望作者能夠每天在四個選定的時間之一安排發佈帖子。這是很重要的,因為一些作者寫遊戲重述,一旦遊戲重新開始,一個遊戲的 auto-publish 是相當毫無價值的。

我正在尋找一個插件來管理這個。通過研究,我發現幾個插件將在管理員設置的一段時間之間選擇一段時間,但是我正在尋找管理員可以設置日曆 (或時間範圍) 的日曆,以允許發佈發佈,並設置每次的最大郵政數量

作者寫了他的帖子,然後提供了一些選擇的日期/時間來安排發佈的帖子。

正如我所提到的,有類似的插件,但是我沒有發現允許管理員設置特定的時間。

有誰知道一個插件或其他方法?該插件不需要是免費的。

更新與”Vision”

管理員設置:設置發佈後允許的日期。設置發佈後允許的時間。設置每次允許排定發佈的帖子數量 (如果每個插槽已經滿足調度號,則不會向作者顯示) 。

作者調度選項:”Publish” 按鈕不存在。必須選擇星期幾發佈帖子>> 必須從可用時間中選擇發佈帖子。

最佳解決方案

我的方法是創建一個小時的 cron 事件,循環通過所有草稿狀態職位和檢查發佈月份 – 保存在 post_meta 中的日期和時間,併發布如果計劃日期不在將來。

發佈日期和時間選擇元框被添加到通過 ajax 在選擇時間時更新的發佈 div 。

更新:

添加插件和日期選擇器 css 到 git:https://github.com/c3mdigital/WP-Schedule

更改 cron 功能以使用 WordPress 中配置的時區進行發佈。

更新:

日期和時間將作為數組存儲在 post_meta 中:month' => 'string', time => int 時間整數將是表示當天小時數 1-24 的值。

選項屏幕有 3 個輸入。

  • 時間:以逗號分隔的數字輸入,代表一天中發佈職位的時間。

  • 每次發佈的帖子數量

  • 日期:逗號分隔的日期數組

我添加了一個 jQuery UI 日期選擇器到發佈編輯屏幕,限制在管理員選擇的日期。日期和時間使用 ajax 進行更新和保存。

更新插件代碼:

 <?php
/*
Plugin Name: c3m wp-schedule
Plugin URI:
Description: Gives contributors a jQuery date picker to choose available date and times available to publish posts
Version: 0.1.2
Author: Chris Olbekson
Author URI: http://c3mdigital.com/
License: GPL v2
*/


    register_activation_hook( __FILE__, 'c3m_activate_cron' );
    add_action( 'post_submitbox_misc_actions', 'c3m_create_schedule_meta', 10 );
    add_action( 'admin_print_footer_scripts', 'c3m_echo_js' );
    add_action( 'admin_enqueue_scripts', 'c3m_enqueue_scripts' );

    function c3m_activate_cron() {
        wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'c3m_check_posts' );

    }

    function c3m_check_posts() {
        $args = array(
            'post_status' => array( 'draft', 'pending' ),
            'posts_per_page' => -1,
            );

            $timestamp = current_time( 'timestamp' );
            $posts = get_posts( $args );
            $month = (int) date('m', $timestamp );
            $day = (int) date('d', $timestamp );
            $hour = (int) date('G', $timestamp );

                foreach ( $posts as $post ) {
                    $date = get_post_meta( $post->ID, '_schedule_date', true );

                    if ( !$date ) continue;
                    $sched_date = explode( "-", $date['date'] );

                    if ( (int) $sched_date[0] > $month  ) continue;
                    if ( (int) $sched_date[1] > $day ) continue;
                    if ( (int) $sched_date[1] >= $day && (int)$date['time'] > $hour )  continue;

                    wp_publish_post( $post->ID );

                    }

    }

    function c3m_create_schedule_meta() {
        global $post_ID;

        $date = get_post_meta( $post_ID, '_schedule_date', TRUE );
        $options = c3m_get_options ();
        $times = $options[ 'c3m_hour_string' ];
        $times_available = explode ( ",", $times );
        $time_output = "Choose Time to publish<br/>";
        $time_output .= "<select class='time-div' name='c3m_sched_time' id='" . $post_ID . "' >n";
        $time_output .= "t<option value='-1'>" . esc_html ( 'Select Publish Time' ) . "</option>n";

        foreach ( $times_available as $time ) {
            $time_output .= "t<option value='$time'>" . esc_html ( $time ) . "</option>n";

        }
        $time_output .= "</select>";

        echo '<div id="schedule" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:1px;">';

        if ( !$date ) {
        $output = 'Choose Date to publish';
        $output .= "<input class='sched-div datepicker' type='text' name='c3m_sched_date' id='".$post_ID."' />n";
        $output .= '<br /><br /><div id="sched_time_div">'.$time_output.'</div>';

        echo $output;
        echo '<p id="hidden-p"><a id="save-time" style="margin-left: 10px" class="button">Save</a></p>';

        } else {
            if ( $date['time'] > 12 ) $pm = 'pm'; else $pm = 'am';

            echo '<p style="padding-left: 10px;">Scheduled to publish on: <strong>' . $date['date'] . '</strong><br />';
            echo 'At approx: <strong>' .  $date['time'].$pm. '</strong><br /></p>';
             }

        echo '</div>';

    }

    function c3m_enqueue_scripts() {
        global $pagenow, $typenow;
        if ( ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) && $typenow == 'post' ) {
        wp_enqueue_script( 'jquery-ui-datepicker' );
        wp_enqueue_style ( 'jquery-ui-lightness', plugins_url( 'ui-lightness/jquery-ui-1.8.20.custom.css', __FILE__ )  );
        }

    }

    function c3m_echo_js() {
        global $pagenow, $typenow;
        if ( ( $pagenow=='post.php' || $pagenow=='post-new.php')   && $typenow=='post') {
              $options = c3m_get_options ();
              $dates = $options[ 'c3m_date_string' ];
              $find = '/';
              $replace = '-';
              $dates = str_replace( $find, $replace, $dates );
              $days = explode ( ",", $dates );
              $year = date ( 'Y' );

              ?>

        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery("#publishing-action").hide();
                jQuery(".misc-pub-section-last").hide();
                jQuery("a#save-time").click(function() {
                    var postID = jQuery("#post_ID").val();
                    var pubDate = jQuery(".sched-div").val();
                    var theTime = jQuery(".time-div option:selected").val();
                    console.log( postID, pubDate, theTime );
                    jQuery.ajax({
                        type:'POST',
                        url: ajaxurl,
                        data: {"action": "save_pub_time", post_id: postID, sched: pubDate, time: theTime },
                        success: function(response) {
                            jQuery("#schedule").replaceWith(response);

                        }
                    });

                    return false;

                });

                var enabledDays = [ <?php foreach( $days as $day ) {  ?>
                 "<?php  echo $day.'-'.$year; ?>",
            <?php  } ?>];

            function enableAllTheseDays(date) {
                var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
                for (i = 0; i < enabledDays.length; i++) {
                    if (jQuery.inArray((m + 1) + '-' + d + '-' + y, enabledDays) != -1) {
                        return [true, ''];
                    }
                }
                return [false, ''];
            }
            jQuery('.datepicker').datepicker({
                dateFormat:'mm-dd-yy',
                beforeShowDay:enableAllTheseDays
            });
            });
    </script>

    <?php   }
    }

    add_action ( 'wp_ajax_save_pub_time', 'c3m_ajax_save' );
    function c3m_ajax_save() {
        $post_id = $_POST[ 'post_id' ];
        $date = $_POST[ 'sched' ];
        $time = $_POST[ 'time' ];
        if ( $time > 12 ) $pm = 'pm'; else $pm = 'am';
        update_post_meta ( $post_id, '_schedule_date', array ( 'date' => $date, 'time' => $time ) );
        $output = '<p style="padding-left: 10px;">Scheduled to publish on: <strong>'.$date.'</strong><br />';
        $output .= 'At approx: <strong>'.$time. $pm.'</strong></p><br />';
        echo $output;

        die(1);
    }

    /**
     * @return array
     * Array
     * (
     * [c3m_hour_string] => 11,03,05,07
     * [c3m_allowed_string] => 4
     * [c3m_date_string] => 05/10,05/11,05/12
     * )
     *
     */

     function c3m_get_options() {
        $c3m_options = get_option('c3m_options');
        return $c3m_options;
    }

    add_action( 'admin_menu', 'c3m_create_menu' );
    function c3m_create_menu() {
        add_options_page( 'Manage Post Schedule', 'Manage Post Schedules', 'manage_options', 'post_schedules', 'c3m_schedule_options' );
    }

    function c3m_schedule_options() {
        echo '<div class="wrap">';
        echo '<h2>Manage Post Schedules</h2>';
        echo 'Manages the custom post scheduling options';
        echo '<form action="options.php" method="post">';
        settings_fields( 'c3m_options' );
        do_settings_sections( 'post_schedules' );
        echo '<input name="Submit" type="submit" class="button-primary" value="Save Changes" />';
        echo '</form></div>';

    }
    add_action( 'admin_init', 'c3m_plugin_init' );
    function c3m_plugin_init() {
        register_setting( 'c3m_options', 'c3m_options', 'c3m_validate' );
        add_settings_section( 'plugin_main', 'Post Schedule Dates and Times', 'settings_array', 'post_schedules' );
        add_settings_field( 'c3m_hour_string', 'Enter Post Publish Times (use 2 digit hours seperated by commas. ie 11,16,17  will publish at 11am, 4pm and 5pm):', 'c3m_hour_setting', 'post_schedules', 'plugin_main' );
        add_settings_field( 'c3m_allowed_string', 'Enter how many posts can be published at each time: ', 'c3m_allowed_setting', 'post_schedules', 'plugin_main' );
        add_settings_field( 'c3m_date_string', 'Enter Publish Dates (use month/day seperated by commas ie: 5/5,5/7 for May 5th and May 7th): ', 'c3m_date_setting', 'post_schedules', 'plugin_main' );
        add_settings_field( 'c3m_editor', 'click to load an editor', 'c3m_editor_setting', 'post_schedules', 'plugin_main' );
    }

    function settings_array() {
        echo '<p>Add post schedule date and time settings here</p>';
    }

    function c3m_hour_setting() {
        $options = get_option( 'c3m_options' );
        echo "<input id='c3m_hour_string' name='c3m_options[c3m_hour_string]' size='40' type='text' value='{$options['c3m_hour_string']}' />";
    }

    function c3m_allowed_setting() {
        $options = get_option( 'c3m_options' );
        echo "<input id='c3m_allowed_string' name='c3m_options[c3m_allowed_string]' size='40' type='text' value='{$options['c3m_allowed_string']}' />";
    }

    function c3m_date_setting() {
        $options = get_option( 'c3m_options' );
        echo "<input id='c3m_date_string' name='c3m_options[c3m_date_string]' size='40' type='text' value='{$options['c3m_date_string']}' />";
    }

    function c3m_validate( $input ) {
        $options = get_option( 'c3m_options' );
        $options['c3m_hour_string'] = trim( $input['c3m_hour_string'] );
        $options[ 'c3m_allowed_string' ] = trim ( $input[ 'c3m_allowed_string' ] );
        $options[ 'c3m_date_string' ] = trim ( $input[ 'c3m_date_string' ] );
        return $options;
        // Todo:  Create a real validate function
    }

@Todo:創建保存選項的驗證功能,創建一個計數選項,用於存儲每次排定多少個帖子以限制一次完整的時間,並清理& 記錄代碼。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。