Php 如何使用WP Post状态转换来处理Post过期日期

Php 如何使用WP Post状态转换来处理Post过期日期,php,wordpress,date,time,Php,Wordpress,Date,Time,我是新来的&date/time/PHP这个东西让我的大脑受伤了 1)如何使用注册post状态更改的确切日期和时间(让我们将其命名为valueX) 2)现在我从我的主题选项中带来了其他价值(valueY)-要么是一段时间(例如30天),要么是日期和时间 3)如何使用valueX和valueY进行计算以获得valueZ->这是post更改草稿或删除自身的日期和时间 最后该如何操作: 我在主题选项中有2个输入,1个用于草稿生命兵,1个用于发布后生命兵(输入为int-值为分钟或小时) 如果发布帖子,则会

我是新来的&date/time/PHP这个东西让我的大脑受伤了

1)如何使用注册post状态更改的确切日期和时间(让我们将其命名为valueX

2)现在我从我的主题选项中带来了其他价值(valueY)-要么是一段时间(例如30天),要么是日期和时间

3)如何使用valueXvalueY进行计算以获得valueZ->这是post更改草稿或删除自身的日期和时间

最后该如何操作:

  • 我在主题选项中有2个输入,1个用于草稿生命兵,1个用于发布后生命兵(输入为int-值为分钟或小时)
  • 如果发布帖子,则会保存此事件的日期和时间
  • 将主题选项值(发布后LifePawn int)添加到该时间,结果是草稿更改后的日期
  • 如果时间到了,将post更改为草稿(并节省时间)
  • 将主题选项值(draft LifePawn int)添加到该时间,结果是删除草稿的日期
  • 如果时机成熟,请删除草稿
  • 这听起来很合乎逻辑,但我没有足够的技能来做到这一点

    我的代码:

    请注意:

    • 为了便于阅读,我将其分为两部分——第一部分是发布到草稿的文章,第二部分是 要删除的草稿。每一步都有注释
    • 逻辑是,若编辑已发布的帖子,它不应该重置计时器 若你们在任何给定的时间内把帖子改成草稿,它就会启动草稿 处理并停止发布的后期处理
    • 这似乎太长太复杂了。。这就是为什么我没有 首先添加它。我相信这件事可以轻松得多 对
    • 我使用了WP发布时间和编辑时间,因为我不知道如何节省时间 &使用特定时间的日期来分隔值
    发布->草稿

        if ( !function_exists('tt_icon_time_left') ) {
          function tt_icon_time_left() {
    
            //Set Timezone To Local
            date_default_timezone_set('Europe/London');
    
            // Check post status
            if ( get_post_status($post->ID) == "publish" ) {
    
              //Get Property Publish Date
              $time_post_published = get_the_time( 'H:i:s d.m.Y' );
    
              // Get Active Post Expiration Value From Theme Options
              global $theme_option;
              $post_time_integer = $theme_option['post-time-left'];
    
              // Get Active Post Expiration Value From Meta
              $single_post_time_left = get_post_meta( get_the_ID(), 'post_time_left', true );
    
              // Decide If Theme Options Or Meta Active Post Expiration Value
              if ( $single_post_time_left <= 0 && $post_time_integer <= 0 ) {
                $post_time = 0;
              }
              else if ( $single_post_time_left < $post_time_integer && $single_post_time_left > 0 || $single_post_time_left > $post_time_integer ) {
                $post_time = $single_post_time_left;
              }
              else {
                $post_time = $post_time_integer;
              }
    
              // Active Post Expiration Value To Seconds
              $post_time_seconds = $post_time * 86400;
    
              // Active Post Age In Seconds
              $active_post_age = strtotime($time_post_published);
              $active_post_age = time() - $active_post_age;
    
              // Calculate Active Post Expiration Date
              $post_expiration_time = date( 'H:i:s d.m.Y' ,strtotime( $time_post_published ) + $post_time_seconds );
    
              // Show Icon And Time Left Until Active Post Changes To Draft In Tooltip ONLY SHOWN IN SUBMIT LISTING
              if ( $post_time > 0 && $active_post_age <= $post_time_seconds ) {
                global $post;
                if ( is_page_template( 'template-post-submit-listing.php' ) ) { 
                  return '<i class="fa fa-clock-o" data-toggle="tooltip" title="' . __( 'Post expires on ' .  $post_expiration_time, 'tt' ) . '"></i>'; 
                }
                else {
                  return false;
                }
              }
    
    如果(!function_存在('tt_图标\u时间\u左')){
    功能tt_图标_时间_左(){
    //将时区设置为本地
    日期默认时区设置(“欧洲/伦敦”);
    //检查职位状态
    如果(获取发布状态($post->ID)=“发布”){
    //获取属性发布日期
    $time_post_published=获取时间('H:i:s d.m.Y');
    //从主题选项中获取活动过期后值
    全球$theme_选项;
    $post_time_integer=$theme_选项['post-time-left'];
    //从元数据获取有效的过期后值
    $single_post_time_left=get_post_meta(get_the_ID(),'post_time_left',true);
    //决定主题选项或元活动过期后值
    if($single\u post\u time\u left$post\u time\u integer){
    $post\u time=$single\u post\u time\u left;
    }
    否则{
    $post_time=$post_time_整数;
    }
    //有效过期后值为秒
    $post_time_seconds=$post_time*86400;
    //以秒为单位的有效后期老化
    $active\u post\u age=strottime($time\u post\u published);
    $active_post_age=time()-$active_post_age;
    //计算有效的过期日期
    $post\u expiration\u time=date('H:i:s d.m.Y',strottime($time\u post\u published)+$post\u time\u seconds);
    //仅在提交列表中显示的工具提示中显示图标和剩余时间,直到激活的帖子更改为草稿
    如果($post\u time>0&&$active\u post\u age DELETE

              // Change Active Post To Draft If Time Is Up
              else if ( $post_time > 0 && $active_post_age > $post_time_seconds ) {
                $postt['post_status'] = 'draft';
                wp_update_post($post);
              }
              else {
               return false; 
              }
            }
            // Chech post status
            else if ( get_post_status($post->ID) == "draft" ) {
    
              $time_post_published = $publish_time;
    
              // Get Draft Expiration Value From Theme Options
              global $theme_option;
              $draft_time_integer = $theme_option['draft-time-left'];
    
              // Get Draft Expiration Value From Meta
              $single_draft_time_left = get_post_meta( get_the_ID(), 'draft_time_left', true );
    
              // Decide If Theme Options Or Meta Draft Expiration Value
              if ( $single_draft_time_left <= 0 && $draft_time_integer <= 0 ) {
                $draft_time = 0;
              }
              else if ( $single_draft_time_left < $draft_time_integer && $single_draft_time_left > 0 || $single_draft_time_left > $draft_time_integer ) {
                $draft_time = $single_draft_time_left;
              }
              else {
                $draft_time = $draft_time_integer;
              }
    
              // Draft Expiration Value To Seconds
              $draft_time_seconds = $draft_time * 86400;
    
              // Draft Age In Seconds
              $draft_age = strtotime($time_property_modified);
              $draft_age = time() - $draft_age;
    
              // Calculate Draft Expiration Date
              $draft_expiration_time = date( 'H:i:s d.m.Y' ,strtotime( $time_property_modified ) + $draft_time_seconds );
    
              // Show Icon And Time Left Until Draft Gets Deleted In Tooltip ONLY IN SUBMIT LISTING
              if ( $draft_time && $draft_age <= $draft_time_seconds ) {
                global $post;  
                if ( is_page_template( 'template-property-submit-listing.php' ) ) { 
                  return '<i class="fa fa-clock-o" data-toggle="tooltip" title="' . __( 'Property gets deleted on ' . $draft_expiration_time , 'tt' ) . '"></i>';
                }
                else {
                  return false;
                }
              }
              // Delete Draft If Time Is Up
              else if( $draft_time && $draft_age > $draft_time_seconds ) {
    
                    // I got this
              }
            }
            else {
              return false;
            }
    
          }
        }
    
    //如果时间到了,将活动帖子更改为草稿
    否则如果($post\u time>0&&$active\u post\u age>$post\u time\u seconds){
    $postt['post_status']='draft';
    wp_更新_员额(员额);
    }
    否则{
    返回false;
    }
    }
    //支票邮政地位
    否则如果(获取发布状态($post->ID)=“草稿”){
    $time\u post\u published=$publish\u time;
    //从主题选项获取草稿过期值
    全球$theme_选项;
    $draft_time_integer=$theme_选项['draft-time-left'];
    //从元数据获取草稿过期值
    $single_draft_time_left=get_post_meta(get_the_ID(),'draft_time_left',true);
    //决定主题选项或元草稿过期值
    if($single\u draft\u time\u left$draft\u time\u integer){
    $draft\u time=$single\u draft\u time\u left;
    }
    否则{
    $draft\u time=$draft\u time\u整数;
    }
    //将过期值拖至秒
    $draft_time_seconds=$draft_time*86400;
    //拔模时间(秒)
    $draft\u age=strottime($time\u property\u modified);
    $draft_age=时间()-$draft_age;
    //计算汇票到期日
    $draft\u expiration\u time=日期('H:i:s d.m.Y',strottime($time\u property\u modified)+$draft\u time\u秒);
    //仅在提交列表中的工具提示中显示图标和删除草稿的剩余时间
    if($draft_time&&$draft_age$draft_time_seconds){
    //我知道了
    }
    }
    否则{
    返回false;
    }
    }
    }
    
    1) 如何使用WP Post状态转换来注册准确的日期和时间 post状态更改(我们将其命名为valueX)的影响

    在我看来,更好的处理方法不是使用Post-Transition状态挂钩,而是-因为当管理员更改Post状态时会调用挂钩。我们将只使用一个Post转换状态,在名为
    firstPublishTime
    的自定义字段中注册第一次发布帖子时的

    add_action('transition_post_status', 'first_publish_time_register', 10, 3);
    function first_publish_time_register($new, $old, $post) {
        if ($new == 'publish' && $old != 'publish' && $post->post_type == 'post') { // change this to whatever post type you like
            $firstPublishTime = get_post_meta($post->ID, 'firstPublishTime', true);
            if(empty($firstPublishTime)) {
                // First time the post is publish, register the time
                add_post_meta($post->ID, 'firstPublishTime',  strftime('%F %T'), true);
            }
        }
    }
    
    2) 现在我从我的主题选项(valueY)中带来了其他价值-它是 一段时间(如30天)或日期和时间

    对于下一部分,我认为你有两个主题选项,两个时期:一个为“PU”。
    register_activation_hook(__FILE__, 'cron_postStatus_activation');
    add_action('postStatus_event', 'cron_postStatus');
    function cron_postStatus_activation() {
        wp_schedule_event(time(), 'hourly', 'postStatus_event');
    }
    
    function cron_postStatus() {
        // Get the theme options
        $published_to_draft_delay = get_option('your_theme_option1');
        $draft_to_deleted_delay     = get_option('your_theme_option2');
        if(!$published_to_draft_delay || !$draft_to_deleted_delay) {
            return new WP_Error('broke', __('Theme options unavailable'));
        }
        $published_to_draft_delay = explode('-', $published_to_draft_delay);
        $draft_to_deleted_delay     = explode('-', $draft_to_deleted_delay);
        $published_to_draft_delay = new DateInterval(
            'P'.$published_to_draft_delay[0].'Y'.
            $published_to_draft_delay[1].'M'.
            $published_to_draft_delay[2].'D'.
            'T'.$published_to_draft_delay[3].'H'.
            $published_to_draft_delay[4].'M'.
            $published_to_draft_delay[5].'S'
        );
        $draft_to_deleted_delay     = new DateInterval(
            'P'.$draft_to_deleted_delay[0].'Y'.
            $draft_to_deleted_delay[1].'M'.
            $draft_to_deleted_delay[2].'D'.
            'T'.$draft_to_deleted_delay[3].'H'.
            $draft_to_deleted_delay[4].'M'.
            $draft_to_deleted_delay[5].'S'
        );
        $now = new DateTime();
    
        // Get all the unpublished posts
        $unpublished_posts = new WP_Query(array(
            'posts_per_page'    => -1,
            'post_type'         => 'post', // change this to whatever post type you like
            'meta_key'          => 'expirationDate',
            'post_status'       => 'draft'
        ));
        while($unpublished_posts->have_posts()) {
            $unpublished_posts->the_post();
            $expirationDate = get_post_meta(get_the_ID(), 'expirationDate', true);
            if(!empty($expirationDate)) {
                // Date comparison
                $dt = new DateTime($expirationDate);
                $dt->add($draft_to_deleted_delay);
                if($dt > $now) {
                    // Expiration date reached, unpublish the post
                    wp_delete_post(get_the_ID(), true);
                }
            }
        }
    
        // Get all the published posts
        $published_posts = new WP_Query(array(
            'posts_per_page'    => -1,
            'post_type'         => 'post', // change this to whatever post type you like
            'meta_key'          => 'firstPublishTime'
        ));
        while($published_posts->have_posts()) {
            $published_posts->the_post();
            $first_publish = get_post_meta(get_the_ID(), 'firstPublishTime', true);
            if(!empty($first_publish)) {
                // Date comparison
                $dt = new DateTime($first_publish);
                $dt->add($published_to_draft_delay);
                if($dt > $now) {
                    // Expiration date reached, unpublish the post
                    wp_transition_post_status('draft', 'publish', $published_posts->post);
                    add_post_meta($post->ID, 'expirationDate',  strftime('%F %T'), true);
                }
            }
        }
        wp_reset_postdata();
    }
    
    function getExpirationInfos($post) {
        // Get all informations
        $first_publish = get_post_meta($post->ID, 'firstPublishTime', true);
        $expirationDate = get_post_meta($post->ID, 'expirationDate', true);
        if(empty($first_publish)) {
            return '';
        }
        $published_to_draft_delay = get_option('your_theme_option1');
        $draft_to_deleted_delay     = get_option('your_theme_option2')
        $published_to_draft_delay = explode('-', $published_to_draft_delay);
        $draft_to_deleted_delay     = explode('-', $draft_to_deleted_delay);
        $published_to_draft_delay = new DateInterval(
            'P'.$published_to_draft_delay[0].'Y'.
            $published_to_draft_delay[1].'M'.
            $published_to_draft_delay[2].'D'.
            'T'.$published_to_draft_delay[3].'H'.
            $published_to_draft_delay[4].'M'.
            $published_to_draft_delay[5].'S'
        );
        $draft_to_deleted_delay     = new DateInterval(
            'P'.$draft_to_deleted_delay[0].'Y'.
            $draft_to_deleted_delay[1].'M'.
            $draft_to_deleted_delay[2].'D'.
            'T'.$draft_to_deleted_delay[3].'H'.
            $draft_to_deleted_delay[4].'M'.
            $draft_to_deleted_delay[5].'S'
        );
        $now = new DateTime();
    
        // Calculate time left
        if($post->post_status == 'publish') {
            $dt = new DateTime($first_publish);
            $dt->add($published_to_draft_delay);
        } else {
            if(empty($expirationDate)) {
                return '';
            }
            $dt = new DateTime($expirationDate);
            $dt->add($draft_to_deleted_delay);
        }
        $et = date_diff($dt, $now);
        $expirationTimeString = (($et->y > 0) ? $et->format('y') . ' years, ' : '') .
            (($et->m > 0) ? $et->format('y') . ' months, ' : '') .
            (($et->d > 0) ? $et->format('m') . ' days, ' : '') .
            (($et->h > 0) ? $et->format('h') . ' hours, ' : '') .
            (($et->i > 0) ? $et->format('i') . ' minutes, ' : '') .
            (($et->s > 0) ? $et->format('s') . ' seconds, ' : '');
        $expirationTimeString = substr($expirationTimeString, 0, -2);
    
    
        // Return HTML
        return '<i class="fa fa-clock-o" data-toggle="tooltip" title="' . __( 'Property gets deleted on', 'tt' ) . ' ' . $expirationTimeString . '"></i>';
    }