Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Workflows/class-wp-agent-workflow-runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@ public static function default_foreach_handler( array $step, array $context ) {
$as = '' !== $as_value ? $as_value : 'item';
$index_as = '' !== $index_as_value ? $index_as_value : 'index';
$continue_on_error = ! empty( $step['continue_on_error'] );
$handlers = self::default_step_handlers();
$executor = new WP_Agent_Workflow_Step_Executor( $handlers );
$iterations = array();
$handlers = is_array( $context['_workflow_step_handlers'] ?? null )
? $context['_workflow_step_handlers']
: self::default_step_handlers();
/** @var array<string,mixed> $handlers */
$executor = new WP_Agent_Workflow_Step_Executor( $handlers );
$iterations = array();

foreach ( array_values( $items ) as $index => $item ) {
$iteration_context = ( new WP_Agent_Workflow_Run_Context( $context ) )->with_vars(
Expand Down
6 changes: 4 additions & 2 deletions src/Workflows/class-wp-agent-workflow-step-executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function execute( array $step, WP_Agent_Workflow_Run_Context $context ):

$context_array = $context->to_array();
try {
$resolved = 'foreach' === $type
$resolved = 'foreach' === $type
? self::expand_foreach_outer_step( $step, $context_array )
: WP_Agent_Workflow_Bindings::expand( $step, $context_array );
$step_output = call_user_func( $handler, $resolved, $context_array );
$handler_context = $context_array;
$handler_context['_workflow_step_handlers'] = $this->handlers;
$step_output = call_user_func( $handler, $resolved, $handler_context );
} catch ( \Throwable $throwable ) {
$record['status'] = WP_Agent_Workflow_Run_Result::STATUS_FAILED;
$record['ended_at'] = time();
Expand Down
49 changes: 49 additions & 0 deletions tests/workflow-runner-smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,54 @@ public function recent( array $args = array() ): array { return array(); }
smoke_assert( 10, $result8->get_output()['last']['iterations'][0]['last']['id'] ?? 0, 'foreach first iteration receives scoped item', $failures, $passes );
smoke_assert( 1, $result8->get_output()['last']['iterations'][1]['last']['points'] ?? 0, 'foreach second iteration receives scoped item', $failures, $passes );

// ─── foreach reuses constructor-injected handlers for nested steps ────

add_filter( 'wp_agent_workflow_known_step_types', static fn( $types ) => array_merge( (array) $types, array( 'custom_nested' ) ) );

$custom_foreach_spec = WP_Agent_Workflow_Spec::from_array(
array(
'id' => 'demo/foreach-custom-handler',
'inputs' => array(
'items' => array( 'type' => 'array', 'required' => true ),
),
'steps' => array(
array(
'id' => 'custom_each',
'type' => 'foreach',
'items' => '${inputs.items}',
'as' => 'item',
'steps' => array(
array(
'id' => 'custom',
'type' => 'custom_nested',
'prefix' => 'item',
'value' => '${vars.item.id}',
),
),
),
),
)
);

$custom_result = ( new WP_Agent_Workflow_Runner(
null,
array(
'custom_nested' => static function ( array $step, array $context ): array {
unset( $context );
return array( 'label' => (string) ( $step['prefix'] ?? '' ) . '-' . (string) ( $step['value'] ?? '' ) );
},
)
) )->run(
$custom_foreach_spec,
array(
'items' => array(
array( 'id' => 42 ),
),
)
);

smoke_assert( WP_Agent_Workflow_Run_Result::STATUS_SUCCEEDED, $custom_result->get_status(), 'foreach nested custom handler run succeeds', $failures, $passes );
smoke_assert( 'item-42', $custom_result->get_output()['last']['iterations'][0]['last']['label'] ?? '', 'foreach nested step uses constructor-injected handler', $failures, $passes );

echo "Passed: {$passes}, Failed: " . count( $failures ) . "\n";
exit( count( $failures ) > 0 ? 1 : 0 );
Loading