我有一个我无法弄清楚的问题。我已经设置了自定义 WP cron 计划和具有该计划的 WP cron 事件。我正在使用 PHPStorm 进行调试,并在 check_mail() 挂钩内设置了一个断点。断点永远不会被击中,但 check_mail() 内部的代码会被执行,因为我可以在日志文件中看到该条目(“Got to check_mail()!”)
所有其他断点都工作正常,并且 check_mail() 例程中只有两行代码 - 日志写入和返回。
代码运行但我的断点从未被命中,这是怎么发生的?
在构造函数中:
add_action( 'check_mail', array($this, 'check_mail' ), 10, 0);
if ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) {
add_action( 'init', array( $this, 'schedule_wp_cron' ), 10, 0 );
}
Run Code Online (Sandbox Code Playgroud)
支持例程:
public function schedule_wp_cron()
{
/**
* Avoid rescheduling cron if it's already scheduled.
*/
$args = array( );
if ( !wp_next_scheduled( 'check_mail' )) {
/**
* Schedule mail server polling.
*/
wp_schedule_event( time(), 'custom_interval', 'check_mail');
}
}
public function check_mail( )
{
// **BREAKPOINT SET ON NEXT LINE**
write_log( 'emails', 'Got to check_mail()!' );
//... do something ...
return;
}
public function custom_cron_schedule( $schedules )
{
$schedules[ 'custom_interval' ] = array(
'interval' => 300,
'display' => 'Custom Interval',
);
return $schedules;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
WP Cron 任务通过远程 POST 请求在单独的进程上运行。PhpStrom 不会在代码中设置的任何断点处停止,因为请求中不存在调试会话启动参数。
这是解决方案。过滤器将为所有 WP Cron 请求添加额外的参数。将XDEBUG_SESSION_START值替换为您的值(我使用默认值)并将下面的代码添加到您的插件或主题代码中。即使对于 cron 任务,PhpStorm 也会在任何断点处停止。
add_filter( 'cron_request', 'wp_cron_debug', 10, 2 );
function wp_cron_debug( $args, $doing_cron ) {
$args['url'] = add_query_arg( array(
'XDEBUG_SESSION_START' => 'PHPSTORM' // <== replace PHPSTORM with your key
), $args['url'] );
return $args;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
907 次 |
| 最近记录: |