Skip to content

Commit 28859c9

Browse files
fix: default automation post author to the network bot account (#207) (#421)
Phase 2 of honest content authorship (extrachill-events#207). EventUpsert::resolvePostAuthor() now resolves genuine automation (no submission context, no explicit author config) to the network bot account via ec_get_network_bot_user_id() instead of falling through to WordPressSettingsResolver's first-administrator fallback — the fallback that historically misattributed ~3k automated events (and 153 wire posts) to uid 1. Resolution order: 1. submission user_id (human submitter, incl. Phase 1 anon-resolved) — highest 2. explicit config (system default_author_id, then per-handler post_author) 3. network bot account via ec_get_network_bot_user_id() — the honest default 4. WordPressSettingsResolver (last resort, only if the helper is unavailable) This is config-driven (no magic 32 literal) and layer-pure: data-machine-events is the EC integration layer, so it owns the EC bot-id default rather than punting the decision into the generic data-machine resolver. The existing extrachill/create-user helper lives in extrachill-users (sibling PR). Existing per-flow post_author=32 handler configs continue to work (explicit config still wins); this hardens the no-config case so automation can never again default to a human admin. Backfill of the existing ~3k uid-1 event rows + 153 wire rows is handled by the extrachill-events backfill-authorship CLI (Phase 3). Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent 3360e72 commit 28859c9

1 file changed

Lines changed: 56 additions & 7 deletions

File tree

inc/Steps/Upsert/Events/EventUpsert.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace DataMachineEvents\Steps\Upsert\Events;
1616

1717
use DataMachine\Core\EngineData;
18+
use DataMachine\Core\PluginSettings;
1819
use DataMachineEvents\Steps\Upsert\Events\Venue;
1920
use DataMachineEvents\Steps\Upsert\Events\Promoter;
2021
use DataMachineEvents\Core\Event_Post_Type;
@@ -1085,20 +1086,29 @@ private function extractEventData( int $post_id ): array {
10851086
/**
10861087
* Resolve post author for event creation.
10871088
*
1088-
* When an event is submitted by a logged-in user (via the event submission form),
1089-
* their user_id is stored in initial_data['submission']['user_id']. This takes
1090-
* priority over handler config defaults so submitted events are attributed to
1091-
* the submitter.
1089+
* When an event is submitted by a user (logged-in or anonymous-with-a-
1090+
* resolved-account — see extrachill-events Phase 1), their user_id is stored
1091+
* in initial_data['submission']['user_id'] and takes priority over handler
1092+
* config defaults so submitted events are attributed to the submitter.
10921093
*
1093-
* Resolution order:
1094-
* 1. Submission user_id from engine data (user-submitted events)
1095-
* 2. WordPressSettingsResolver (system defaults / handler config / fallbacks)
1094+
* For genuine automation (no submission context), the author resolves to:
1095+
* 1. An explicitly-configured author (system-wide default_author_id, then
1096+
* per-handler post_author) — respected when set.
1097+
* 2. The network bot account via `ec_get_network_bot_user_id()` — the
1098+
* honest default for headless automation, config-driven so the bot id
1099+
* is not a magic literal (issue #207 Phase 2). This intentionally
1100+
* supersedes WordPressSettingsResolver's first-administrator fallback,
1101+
* which historically misattributed automation to uid 1 (the ~3k uid-1
1102+
* event rows that the backfill in extrachill-events corrects).
1103+
* 3. WordPressSettingsResolver (logged-in user / first admin) — last
1104+
* resort, only when the bot-account helper is unavailable.
10961105
*
10971106
* @param array $handler_config Handler configuration.
10981107
* @param EngineData $engine Engine snapshot helper.
10991108
* @return int Post author ID.
11001109
*/
11011110
private function resolvePostAuthor( array $handler_config, EngineData $engine ): int {
1111+
// 1. Submission user_id (human submitter) — highest priority.
11021112
$submission = $engine->get( 'submission' );
11031113

11041114
if ( is_array( $submission ) && ! empty( $submission['user_id'] ) ) {
@@ -1108,9 +1118,48 @@ private function resolvePostAuthor( array $handler_config, EngineData $engine ):
11081118
}
11091119
}
11101120

1121+
// 2. Explicit author configuration (system default, then handler
1122+
// override). Per-flow post_author wins over the bot default so an
1123+
// operator can still pin a specific author on a flow.
1124+
$explicit = $this->resolve_explicit_author_id( $handler_config );
1125+
if ( $explicit > 0 ) {
1126+
return $explicit;
1127+
}
1128+
1129+
// 3. Network bot account — the honest author for headless automation.
1130+
if ( function_exists( 'ec_get_network_bot_user_id' ) ) {
1131+
$bot_id = (int) ec_get_network_bot_user_id();
1132+
if ( $bot_id > 0 ) {
1133+
return $bot_id;
1134+
}
1135+
}
1136+
1137+
// 4. Last resort: generic resolver (logged-in user / first admin).
11111138
return WordPressSettingsResolver::getPostAuthor( $handler_config );
11121139
}
11131140

1141+
/**
1142+
* Resolve an explicitly-configured author id, if any.
1143+
*
1144+
* Mirrors the config-first portion of WordPressSettingsResolver::getPostAuthor():
1145+
* system-wide default_author_id, then handler-specific post_author. Returns 0
1146+
* when neither is set (the headless-automation case the bot-account default
1147+
* handles). Extracted so resolvePostAuthor() can branch on "no explicit
1148+
* config → bot" without re-running the resolver's first-admin fallback.
1149+
*
1150+
* @param array $handler_config Handler configuration.
1151+
* @return int Explicitly-configured author id, or 0 if none.
1152+
*/
1153+
private function resolve_explicit_author_id( array $handler_config ): int {
1154+
$wp_settings = PluginSettings::get( 'wordpress_settings', array() );
1155+
$default = (int) ( $wp_settings['default_author_id'] ?? 0 );
1156+
if ( $default > 0 ) {
1157+
return $default;
1158+
}
1159+
1160+
return (int) ( $handler_config['post_author'] ?? 0 );
1161+
}
1162+
11141163
/**
11151164
* Build meta_input array for the DM core upsert ability.
11161165
*

0 commit comments

Comments
 (0)