WordPress 文章定时发布代码流程

440 阅读1分钟

大致流程:当编辑的发布时间晚于服务器的当前时间时,系统会通过wp_schedule_single_event()注册一个一次性的crontab脚本。

一:修改post的入口文件:wordpress\wp-admin\post.php

case 'editpost':   
  check_admin_referer( 'update-post_' . $post_id );   
  
  //修改post
  $post_id = edit_post();   
  
  // Session cookie flag that the post was saved.   
  if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {      
       setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );   
  }   
redirect_post( $post_id ); // Send user on their way while we keep working.   
exit;

二:修改post。wordpress\wp-admin\includes\post.php   edit_post()->wp_update_post()

*****
add_meta( $post_ID );

update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
//修改post
$success = wp_update_post( $translated );*****

三:处理好数据后,将数据更新到db。wordpress\wp-includes\post.php  wp_update_post()->wp_insert_post()

*******
return wp_insert_post( $postarr, $wp_error );
}

四:更新db,插入数据后,触发hook。

*******
if ( 'attachment' !== $postarr['post_type'] ) {   
    //触发post状态变更后的hook
    wp_transition_post_status( $data['post_status'], $previous_status, $post );
} 
**********

五:wp_transition_post_status()->do_action()

function wp_transition_post_status( $new_status, $old_status, $post ) {
    do_action( 'transition_post_status', $new_status, $old_status, $post );
    do_action( "{$old_status}_to_{$new_status}", $post );

    //当为将来发布时:do_action("future_post", $post-> ID, $post)
    do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );}

六:触发future_post 并执行。

function _future_post_hook( $deprecated, $post ) { 
    //删除之前的hook  
    wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );   
    
    //注册cron
    wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) );
}

七:使用wp-cli 查看cron。可以看到publish_future_post已经白注册到cron

+------------------------------------+---------------------+-----------------------+---------------+
| hook                               | next_run_gmt        | next_run_relative     | recurrence    |
+------------------------------------+---------------------+-----------------------+---------------+
| publish_future_post                | 2020-12-16 09:18:00 | 1 minute 11 seconds   | Non-repeating |
| wp_privacy_delete_old_export_files | 2020-12-16 09:44:35 | 27 minutes 46 seconds | 1 hour        |
| wp_version_check                   | 2020-12-16 14:44:35 | 5 hours 27 minutes    | 12 hours      |
| wp_update_plugins                  | 2020-12-16 14:44:35 | 5 hours 27 minutes    | 12 hours      |
| wp_update_themes                   | 2020-12-16 14:44:35 | 5 hours 27 minutes    | 12 hours      |
| recovery_mode_clean_expired_keys   | 2020-12-17 02:44:35 | 17 hours 27 minutes   | 1 day         |
| wp_scheduled_delete                | 2020-12-17 02:46:22 | 17 hours 29 minutes   | 1 day         |
| delete_expired_transients          | 2020-12-17 02:46:22 | 17 hours 29 minutes   | 1 day         |
| wp_scheduled_auto_draft_delete     | 2020-12-17 03:35:03 | 18 hours 18 minutes   | 1 day         |
| wsal_delete_logins                 | 2020-12-17 07:51:11 | 22 hours 34 minutes   | 1 day         |
| wsal_log_files_pruning             | 2020-12-17 07:51:11 | 22 hours 34 minutes   | 1 day         |
| wpseo-reindex                      | 2020-12-17 08:30:35 | 23 hours 13 minutes   | 1 day         |
| wpseo_permalink_structure_check    | 2020-12-17 08:30:35 | 23 hours 13 minutes   | 1 day         |
| wp_site_health_scheduled_check     | 2020-12-23 02:44:35 | 6 days 17 hours       | 1 week        |
+------------------------------------+---------------------+-----------------------+---------------+