$post_data Post data being assembled. * @param array $task Task data from Kordiam. * * @return array|null Updated post data, or null to skip the story. */ public static function apply( array $post_data, array $task ): ?array { $format_id = self::format_id( $task ); if ( 0 === $format_id ) { return $post_data; } $option_key = self::option_key( $format_id ); if ( Options::is_do_not_import( $option_key ) ) { return self::SKIP_STORY; } $target = Options::get_string( $option_key ); if ( '' === $target || Options::is_no_sync( $option_key ) ) { return $post_data; } if ( 'post_author' === $target ) { return self::apply_author( $post_data, $task ); } $value = self::value_for( $task, $format_id ); if ( null === $value ) { return $post_data; } if ( ! isset( $post_data['meta_input'] ) || ! is_array( $post_data['meta_input'] ) ) { $post_data['meta_input'] = array(); } // Meta is not covered by the kses filters wp_insert_post() applies. $post_data['meta_input'][ $target ] = MetaValue::sanitise( $value ); return $post_data; } /** * Reading a task's format ID. * * @param array $task Task data. * * @return int Format ID, or 0 when the task carries none. */ public static function format_id( array $task ): int { $format_id = $task['format']['id'] ?? null; return is_numeric( $format_id ) ? (int) $format_id : 0; } /** * Building the option key holding a format's mapping. * * @param int $format_id Kordiam format ID. * * @return string Unprefixed option key. */ public static function option_key( int $format_id ): string { return 'task_import_' . $format_id; } /** * Deciding what value a task contributes: text content, or the assignee's email address. * * @param array $task Task data. * @param int $format_id Task format ID. * * @return string|null The value, or null when the task has nothing to contribute. */ private static function value_for( array $task, int $format_id ): ?string { if ( Config::TEXT_TASK_FORMAT_ID === $format_id ) { $content = $task['content'] ?? null; if ( is_scalar( $content ) && '' !== trim( (string) $content ) ) { return (string) $content; } return null; } $name = $task['assignee']['name'] ?? null; if ( is_scalar( $name ) && '' !== trim( (string) $name ) ) { return (string) $name; } return null; } /** * Setting the post author from a task's assignee. * * @param array $post_data Post data being assembled. * @param array $task Task data. * * @return array Post data, with the author set when the assignee is known here. */ private static function apply_author( array $post_data, array $task ): array { $user_id = AuthorResolver::user_id_for_task( $task ); if ( 0 === $user_id ) { return $post_data; } return AuthorResolver::assign( $post_data, $user_id, AuthorResolver::assignee_id( $task ) ); } }