} */ public static function parse( $body, $type = null, $delivery = null ) { $data = json_decode( (string) $body, true ); if ( ! is_array( $data ) ) { return self::invalid( 'body' ); } $event = self::text( $data, 'event' ); if ( ! in_array( $event, array( self::READY, self::TEST, self::STANDING, self::UPDATE ), true ) ) { return self::invalid( 'event' ); } if ( null !== $type && '' !== $type && $type !== $event ) { return self::invalid( 'event' ); } $id = self::text( $data, 'id' ); if ( '' === $id || strlen( $id ) > 64 || ! preg_match( '/^[A-Za-z0-9_-]+$/', $id ) ) { return self::invalid( 'id' ); } if ( null !== $delivery && '' !== $delivery && $delivery !== $id ) { return self::invalid( 'id' ); } $site = isset( $data['site'] ) && is_array( $data['site'] ) ? $data['site'] : array(); $read = array( 'type' => $event, 'id' => $id, 'domain' => self::text( $site, 'domain' ), ); if ( self::TEST === $event ) { return self::valid( $read ); } $article = isset( $data['article'] ) && is_array( $data['article'] ) ? $data['article'] : null; if ( null === $article ) { return self::invalid( 'article' ); } $article_id = isset( $article['id'] ) ? $article['id'] : null; if ( ! ( is_int( $article_id ) || ( is_string( $article_id ) && ctype_digit( $article_id ) ) ) || (int) $article_id < 1 ) { return self::invalid( 'article.id' ); } $wordpress = isset( $data['wordpress'] ) && is_array( $data['wordpress'] ) ? $data['wordpress'] : array(); // A read and an update in place name the post ThirdDraft knows, and only that post is read or written. if ( self::STANDING === $event || self::UPDATE === $event ) { if ( ! isset( $wordpress['post_id'] ) || ! is_int( $wordpress['post_id'] ) || $wordpress['post_id'] < 1 ) { return self::invalid( 'wordpress.post_id' ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- the field's name in ThirdDraft's body. } $read['post_id'] = $wordpress['post_id']; } if ( self::STANDING === $event ) { $read['article'] = array( 'id' => (int) $article_id ); return self::valid( $read ); } $title = self::text( $article, 'title' ); if ( '' === $title ) { return self::invalid( 'article.title' ); } if ( ! isset( $article['content_html'] ) || ! is_string( $article['content_html'] ) || '' === trim( $article['content_html'] ) ) { return self::invalid( 'article.content_html' ); } $images = array(); if ( isset( $article['images'] ) && ! is_array( $article['images'] ) ) { return self::invalid( 'article.images' ); } foreach ( isset( $article['images'] ) ? $article['images'] : array() as $image ) { $picture = self::picture( $image ); if ( null === $picture ) { return self::invalid( 'article.images' ); } $images[] = $picture; } $featured = null; if ( isset( $article['featured_image'] ) ) { $featured = self::picture( $article['featured_image'] ); if ( null === $featured ) { return self::invalid( 'article.featured_image' ); } } // An update in place is taken only while the post was last edited exactly when ThirdDraft read it. if ( self::UPDATE === $event ) { $modified = self::text( $wordpress, 'if_modified_gmt' ); if ( ! self::is_moment( $modified ) ) { return self::invalid( 'wordpress.if_modified_gmt' ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- the field's name in ThirdDraft's body. } $read['if_modified_gmt'] = $modified; } $read['article'] = array( 'id' => (int) $article_id, 'title' => $title, 'slug' => self::text( $article, 'slug' ), 'meta_title' => self::text( $article, 'meta_title' ), 'meta_description' => self::text( $article, 'meta_description' ), 'keyword' => self::text( $article, 'keyword' ), 'content_html' => $article['content_html'], 'featured_image' => $featured, 'images' => $images, ); // Publish only when ThirdDraft says so: an event without the choice is kept as a draft. $read['status'] = 'publish' === self::text( $wordpress, 'status' ) ? 'publish' : 'draft'; return self::valid( $read ); } /** * Whether an event id is a ULID, whose order is the order events were made. * * @since 1.0.0 * * @param string $id An event id. * @return bool */ public static function is_ulid( $id ) { return is_string( $id ) && 1 === preg_match( '/^[0-7][0-9A-Z]{25}$/', $id ); } /** * Whether the event a post last took makes this one a repeat or an older one. * * The same id is a retry of what was already taken. A ULID older than the * one taken was made before it, so taking it now would put older text over * newer. * * @since 1.0.0 * * @param string $taken The event id the post last took; '' for none. * @param string $incoming This event's id. * @return bool */ public static function already_taken( $taken, $incoming ) { if ( '' === (string) $taken ) { return false; } if ( $taken === $incoming ) { return true; } return self::is_ulid( $taken ) && self::is_ulid( $incoming ) && strcmp( $taken, $incoming ) > 0; } /** * Whether a value is a moment as the plugin says one: ISO 8601 in UTC, to * the second, `2026-11-18T09:30:00Z`. * * @since 1.2.0 * * @param mixed $value The value. * @return bool */ public static function is_moment( $value ) { return is_string( $value ) && 1 === preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', $value ); } /** * A picture as `{url, alt, id?, sha256?}`, or null when it is not one. * * @param mixed $image One entry. * @return array{url: string, alt: string, id: int|null, sha256: string|null}|null */ private static function picture( $image ) { if ( ! is_array( $image ) ) { return null; } $url = self::text( $image, 'url' ); if ( '' === $url || ! preg_match( '#^https?://#i', $url ) ) { return null; } $id = isset( $image['id'] ) && is_int( $image['id'] ) && $image['id'] > 0 ? $image['id'] : null; $sha256 = null; if ( isset( $image['sha256'] ) ) { if ( ! is_string( $image['sha256'] ) || ! preg_match( '/^[0-9a-fA-F]{64}$/', $image['sha256'] ) ) { return null; } $sha256 = strtolower( $image['sha256'] ); } return array( 'url' => $url, 'alt' => self::text( $image, 'alt' ), 'id' => $id, 'sha256' => $sha256, ); } /** * A string field, trimmed; '' when absent, null or not a string. * * @param array $data The object. * @param string $field The key. * @return string */ private static function text( $data, $field ) { return isset( $data[ $field ] ) && is_string( $data[ $field ] ) ? trim( $data[ $field ] ) : ''; } /** * A readable event. * * @param array $event The fields. * @return array{ok: bool, reason: string, event: array} */ private static function valid( $event ) { return array( 'ok' => true, 'reason' => '', 'event' => $event, ); } /** * An event the plugin cannot take. * * @param string $reason The field that is wrong. * @return array{ok: bool, reason: string, event: array} */ private static function invalid( $reason ) { return array( 'ok' => false, 'reason' => $reason, 'event' => array(), ); } }