$step ) { if ( class_exists( 'AIEO_Workflow_Advertised_Offers' ) && ! AIEO_Workflow_Advertised_Offers::may_run( $wf, false, (int) $idx ) ) { continue; } $enqueued += self::launch_step( $wf_id, (int) $idx, $step, $batch ); } return $enqueued; } private static function social_steps( $wf ) { $steps = $wf['steps'] ?? null; if ( ! is_array( $steps ) && isset( $wf['steps_json'] ) ) { $steps = json_decode( (string) $wf['steps_json'], true ); } $out = array(); foreach ( (array) $steps as $i => $s ) { if ( is_array( $s ) && 'social_post' === ( $s['type'] ?? '' ) ) { $out[ $i ] = $s; } } return $out; } private static function batch_key( $wf ) { $interval = strtolower( (string) ( $wf['trigger_config']['interval'] ?? 'weekly' ) ); if ( 'daily' === $interval ) { return gmdate( 'Ymd' ); } if ( 'monthly' === $interval ) { return gmdate( 'Ym' ); } return gmdate( 'oW' ); } private static function launch_step( $wf_id, $step_idx, array $step, $batch ) { global $wpdb; $platforms = array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) ( $step['platforms'] ?? array() ) ) ) ) ); $object_type = sanitize_key( (string) ( $step['object_type'] ?? 'post' ) ); $object_id = (int) ( $step['object_id'] ?? 0 ); if ( ! $platforms || $object_id <= 0 || ! class_exists( 'AIEO_Social_Store' ) ) { return 0; } if ( class_exists( 'AIEO_Social_AI_Writer' ) ) { $object_id = (int) AIEO_Social_AI_Writer::promotable_object_id( $object_id ); } if ( 'product_variation' === $object_type ) { $object_type = 'product'; } $t = $wpdb->prefix . 'aieo_social_posts'; $already = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$t} WHERE wf_id = %d AND step_idx = %d AND launch_batch = %s", $wf_id, $step_idx, $batch ) ); if ( $already > 0 ) { return 0; } $link = get_permalink( $object_id ); if ( ! is_string( $link ) || '' === $link ) { self::log_skip( $wf_id, $step_idx, $object_id, 'no permalink — object missing or not published' ); return 0; } $status = get_post_status( $object_id ); if ( 'publish' !== $status ) { self::log_skip( $wf_id, $step_idx, $object_id, 'destination is ' . (string) $status . ', not public' ); return 0; } $link = (string) $link; $image = (string) get_the_post_thumbnail_url( $object_id, 'large' ); $bodies = self::resolve_bodies( $step, $platforms, $object_id, $link ); $enqueued = 0; $placement = class_exists( 'AIEO_Workflow_Advertised_Offers' ) ? AIEO_Workflow_Advertised_Offers::campaign_placement( (int) $wf_id, (int) $step_idx ) : null; foreach ( $platforms as $platform ) { $text = (string) ( $bodies[ $platform ] ?? '' ); if ( '' === $text ) { continue; } $tagged = self::tag_link( $link, $platform, $object_id, $placement ); $manifest = wp_json_encode( array( 'text' => $text, 'link' => $tagged, 'image_url' => $image ) ); foreach ( self::accounts_for( $platform, $step ) as $acc_id ) { $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$t} ( wf_id, step_idx, launch_batch, platform, account_id, object_type, object_id, variant_label, media_json, status, created_at ) VALUES ( %d, %d, %s, %s, %d, %s, %d, 'v1', %s, 'scheduled', %s )", $wf_id, $step_idx, $batch, $platform, (int) $acc_id, $object_type, $object_id, $manifest, current_time( 'mysql', true ) ) ); $new_id = (int) $wpdb->insert_id; if ( $new_id > 0 && class_exists( 'AIEO_Social_Jobs' ) ) { AIEO_Social_Jobs::schedule( $new_id, time() + 30 + ( $enqueued * 5 ) ); $enqueued++; } } } return $enqueued; } private static function accounts_for( $platform, array $step ) { $chosen = (int) ( $step[ 'account_' . $platform ] ?? 0 ); if ( $chosen > 0 ) { return AIEO_Social_Store::can_post( AIEO_Social_Store::get_account( $chosen ) ) ? array( $chosen ) : array(); } $ids = array(); foreach ( AIEO_Social_Store::get_accounts( $platform ) as $a ) { if ( AIEO_Social_Store::can_post( $a ) ) { $ids[] = (int) $a['id']; } } return $ids; } private static function resolve_bodies( array $step, array $platforms, $object_id, $link ) { $out = array(); $blank = array(); $stored = self::stored_captions( $step, $platforms ); foreach ( $platforms as $p ) { $b = $stored[ $p ]; if ( '' !== $b ) { $out[ $p ] = $b; } else { $blank[] = $p; } } if ( $blank && class_exists( 'AIEO_Social_AI_Writer' ) ) { $post = get_post( $object_id ); $ctx = array( 'platforms' => $blank, 'title' => $post ? get_the_title( $post ) : '', 'excerpt' => $post ? wp_trim_words( wp_strip_all_tags( ( class_exists( 'AIEO_DMM_Offer_Live_Facts' ) ? AIEO_DMM_Offer_Live_Facts::strip( (string) get_post_field( 'post_content', $post ) ) : (string) get_post_field( 'post_content', $post ) ) ), 60, '…' ) : '', 'url' => (string) $link, 'type' => $post ? ( 'product' === get_post_type( $post ) ? 'product' : ( 'page' === get_post_type( $post ) ? 'page' : 'post' ) ) : 'post', 'object_id' => (int) $object_id, ); $gen = AIEO_Social_AI_Writer::generate( $ctx ); if ( ! is_wp_error( $gen ) && ! empty( $gen['posts'] ) ) { foreach ( $gen['posts'] as $g ) { $plat = sanitize_key( (string) ( $g['platform'] ?? '' ) ); if ( '' !== $plat && ! isset( $out[ $plat ] ) ) { $hash = ! empty( $g['hashtags'] ) ? "\n\n" . implode( ' ', array_map( static function ( $h ) { $h = ltrim( (string) $h, '#' ); return '' !== $h ? '#' . $h : ''; }, (array) $g['hashtags'] ) ) : ''; $out[ $plat ] = trim( (string) ( $g['text'] ?? '' ) . $hash ); } } } } return $out; } public static function stored_captions( array $step, $platforms = null ) { if ( ! is_array( $platforms ) ) { $platforms = array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) ( $step['platforms'] ?? array() ) ) ) ) ); } $out = array(); foreach ( $platforms as $p ) { $out[ $p ] = trim( (string) ( $step[ 'body_' . $p ] ?? ( $step['body'] ?? '' ) ) ); } return $out; } private static function tag_link( $link, $platform, $post_id, $placement = null ) { $link = (string) $link; if ( '' === $link ) { return $link; } if ( is_array( $placement ) && class_exists( 'AIEO_DMM_Campaign_Links' ) ) { $placement['utm'] = array( 'utm_source' => sanitize_key( $platform ) ); if ( isset( AIEO_DMM_Campaign_Links::parameters( $link, $placement )['utm_campaign'] ) ) { return add_query_arg( 'aieo_post_id', (int) $post_id, AIEO_DMM_Campaign_Links::tag( $link, $placement ) ); } } return add_query_arg( array( 'utm_source' => sanitize_key( $platform ), 'utm_medium' => 'social', 'utm_campaign' => 'social_' . (int) $post_id, 'aieo_src' => 'social', 'aieo_post_id' => (int) $post_id, ), $link ); } private static function log_skip( $wf_id, $step_idx, $object_id, $why ) { $key = 'aieo_social_broadcast_skips'; $list = get_option( $key, array() ); if ( ! is_array( $list ) ) { $list = array(); } $list[ (int) $wf_id . ':' . (int) $step_idx ] = array( 'object_id' => (int) $object_id, 'why' => (string) $why, 'at' => current_time( 'mysql', true ), ); if ( count( $list ) > 200 ) { $list = array_slice( $list, -200, null, true ); } update_option( $key, $list, false ); error_log( sprintf( 'AIEO_Social_Broadcast: step %d/%d skipped for object %d — %s', (int) $wf_id, (int) $step_idx, (int) $object_id, (string) $why ) ); } }