|null The publication, or null when the story has none for this site. */ public function resolve( StoryPayload $payload, bool $global_export = false ): ?array { $found = $global_export ? $this->find_global_export_publication( $payload ) : $this->find_platform_publication( $payload ); if ( null !== $found ) { return $found; } return $payload->inline_publication(); } /** * Finding the publication a global export targets. * * Also stores its platform ID: this is how an unpaired site learns its platform. * * @param StoryPayload $payload Story payload. * * @return array|null The publication, or null when none is flagged. */ private function find_global_export_publication( StoryPayload $payload ): ?array { foreach ( $payload->publications() as $publication ) { if ( empty( $publication['globalExportPublication'] ) ) { continue; } if ( isset( $publication['platform'] ) && is_numeric( $publication['platform'] ) ) { Options::set( self::PLATFORM_OPTION, (int) $publication['platform'] ); } return $publication; } return null; } /** * Finding the publication for this site's platform. * * @param StoryPayload $payload Story payload. * * @return array|null The publication, or null when the story targets other platforms only. */ private function find_platform_publication( StoryPayload $payload ): ?array { $platform_id = Options::get_int( self::PLATFORM_OPTION ); if ( 0 === $platform_id ) { return null; } $previous_publication_id = $this->previously_imported_publication_id( $payload->post_id() ); $found = null; foreach ( $payload->publications() as $publication ) { if ( ! isset( $publication['platform'] ) || ! is_numeric( $publication['platform'] ) ) { continue; } if ( (int) $publication['platform'] !== $platform_id ) { continue; } $found = $publication; if ( ! empty( $publication['isMainPublication'] ) ) { break; } if ( $previous_publication_id > 0 && $this->publication_id( $publication ) === $previous_publication_id ) { break; } } return $found; } /** * Reading the publication a post was imported from last time. * * @param int $post_id Post ID, or 0 on a create. * * @return int Publication ID, or 0 when unknown. */ private function previously_imported_publication_id( int $post_id ): int { if ( $post_id <= 0 ) { return 0; } $stored = get_post_meta( $post_id, MetaKeys::PUBLICATION_ID, true ); if ( is_numeric( $stored ) ) { return (int) $stored; } $legacy = get_post_meta( $post_id, MetaKeys::legacy_post_meta()[ MetaKeys::PUBLICATION_ID ], true ); return is_numeric( $legacy ) ? (int) $legacy : 0; } /** * Reading a publication's own ID. * * @param array $publication Publication data. * * @return int Publication ID, or 0 when absent. */ private function publication_id( array $publication ): int { return isset( $publication['id'] ) && is_numeric( $publication['id'] ) ? (int) $publication['id'] : 0; } /** * Finding where a publication sits in a story's list. * * @param array $story Story as Kordiam holds it. * @param int $publication_id Publication ID to locate. * * @return int|null The index, or null when the story does not contain it. */ public static function position_of( array $story, int $publication_id ): ?int { $publications = $story['publications'] ?? null; if ( ! is_array( $publications ) || $publication_id <= 0 ) { return null; } foreach ( $publications as $index => $publication ) { if ( ! is_array( $publication ) || ! isset( $publication['id'] ) || ! is_numeric( $publication['id'] ) ) { continue; } if ( (int) $publication['id'] === $publication_id && is_int( $index ) ) { return $index; } } return null; } /** * Reading the Kordiam status ID off a publication. * * @param array|null $publication Publication data. * * @return int Status ID, or 0 when absent. */ public static function status_id( ?array $publication ): int { $status_id = $publication['status']['id'] ?? null; return is_numeric( $status_id ) ? (int) $status_id : 0; } /** * Reading the Kordiam status name off a publication. * * @param array|null $publication Publication data. * * @return string Status name, or an empty string when absent. */ public static function status_name( ?array $publication ): string { $name = $publication['status']['name'] ?? null; return is_scalar( $name ) ? (string) $name : ''; } /** * Reading the Kordiam category ID off a publication. * * @param array|null $publication Publication data. * * @return int Category ID, or 0 when the publication has no category. */ public static function category_id( ?array $publication ): int { $category_id = $publication['category']['id'] ?? null; return is_numeric( $category_id ) ? (int) $category_id : 0; } /** * Reading a publication's custom fields. * * @param array|null $publication Publication data. * * @return array> Custom fields. */ public static function custom_fields( ?array $publication ): array { $fields = $publication['customFields'] ?? null; if ( ! is_array( $fields ) ) { return array(); } $valid = array(); foreach ( $fields as $field ) { if ( is_array( $field ) ) { $valid[] = $field; } } return $valid; } /** * Reading the scheduled start date of a publication. * * @param array|null $publication Publication data. * * @return array{date: string, time: string}|null The schedule, or null when unscheduled. */ public static function schedule( ?array $publication ): ?array { $start = $publication['single']['start'] ?? null; if ( ! is_array( $start ) || ! isset( $start['date'] ) || ! is_scalar( $start['date'] ) ) { return null; } $date = trim( (string) $start['date'] ); if ( '' === $date ) { return null; } $time = isset( $start['time'] ) && is_scalar( $start['time'] ) ? trim( (string) $start['time'] ) : ''; return array( 'date' => $date, 'time' => $time, ); } }