$event The event, as ThirdDraft_Event read it. * @return array{status: int, body: array} */ public static function take( $event ) { $article = $event['article']; $post_id = self::find( $article['id'] ); $in_place = ThirdDraft_Event::UPDATE === $event['type']; // An update in place is written over the post ThirdDraft names or nowhere: it never makes one. if ( $in_place && $post_id !== $event['post_id'] ) { return ThirdDraft_Answers::not_updated( 'gone' ); } if ( $post_id && ThirdDraft_Event::already_taken( (string) get_post_meta( $post_id, self::EVENT, true ), $event['id'] ) ) { return self::answer( $post_id ); } $refusal = $in_place ? self::refusal( $post_id, $event['if_modified_gmt'] ) : ''; if ( '' !== $refusal ) { return ThirdDraft_Answers::not_updated( $refusal ); } $author = $post_id ? (int) get_post_field( 'post_author', $post_id ) : self::author(); $blocks = self::use_blocks(); $wanted = ThirdDraft_Seo::wanted( $article['title'], $article['meta_title'], $article['meta_description'], $article['keyword'] ); $seo = ThirdDraft_Seo::meta_plan( (int) $post_id, $wanted ); $created = false; if ( ! $post_id ) { $post_id = self::insert_draft( $article, $author, $blocks, $seo ); if ( is_wp_error( $post_id ) ) { return ThirdDraft_Answers::failed(); } $created = true; } $pictures = ThirdDraft_Pictures::copy( $article, $post_id, $author ); if ( is_wp_error( $pictures ) ) { return ThirdDraft_Answers::pictures(); } // Asked again right before the write: copying pictures can take minutes, and a person may have saved the post meanwhile. $refusal = $in_place ? self::refusal( $post_id, $event['if_modified_gmt'] ) : ''; if ( '' !== $refusal ) { return ThirdDraft_Answers::not_updated( $refusal ); } $update = array( 'ID' => $post_id, 'post_title' => $article['title'], 'post_excerpt' => $article['meta_description'], 'post_content' => self::content( $article['content_html'], $pictures['map'], $blocks ), 'meta_input' => array( self::EVENT => $event['id'] ), ); // An update in place leaves the post's status and address as they are: neither is ever sent. if ( ! $in_place ) { $status = self::status_after( $created ? 'draft' : (string) get_post_status( $post_id ), $event['status'] ); if ( $created || ! in_array( get_post_status( $post_id ), self::$live, true ) ) { $update['post_name'] = self::slug( $article, $post_id ); } if ( get_post_status( $post_id ) !== $status ) { $update['post_status'] = $status; } } if ( ! $created ) { $update['meta_input'] = array_merge( $update['meta_input'], $seo['set'], array( ThirdDraft_Seo::HASHES => $seo['hashes'] ), self::jetpack() ); } $saved = wp_update_post( wp_slash( $update ), true ); if ( is_wp_error( $saved ) ) { return ThirdDraft_Answers::failed(); } if ( ! $created ) { foreach ( $seo['delete'] as $key ) { delete_post_meta( $post_id, $key ); } } self::feature( $post_id, $pictures['featured'] ); $hashes = ThirdDraft_Seo::write_aioseo( $post_id, $wanted, $seo['hashes'] ); if ( $hashes !== $seo['hashes'] ) { update_post_meta( $post_id, ThirdDraft_Seo::HASHES, wp_slash( $hashes ) ); } // What this write left the post as, once every write of it is done: the moment a later update in place is held to. $current = self::current( $post_id ); update_post_meta( $post_id, self::MODIFIED, $current ? self::moment( $current->post_modified_gmt ) : '' ); ThirdDraft_Store::log_article( $article['id'], $post_id, $event['id'] ); return self::answer( $post_id ); } /** * How the post of an article ThirdDraft sent stands now — its status and * when it was last edited — for a read that writes nothing. Only the post * ThirdDraft names: one the article's id finds that is another post is * answered as none. * * @since 1.2.0 * * @param array $event The `article.standing` event, as ThirdDraft_Event read it. * @return array{status: int, body: array} */ public static function standing( $event ) { $post_id = self::find( $event['article']['id'] ); $post = $post_id && $post_id === $event['post_id'] ? self::current( $post_id ) : null; if ( ! $post ) { return ThirdDraft_Answers::standing( 0, '', '' ); } return ThirdDraft_Answers::standing( $post_id, (string) $post->post_status, self::moment( $post->post_modified_gmt ) ); } /** * Why an update in place may not be written over a post now: '' when it * may; otherwise 'gone', 'not_live' or 'edited'. Read past this request's * cache, so a save by anybody else since is seen. * * @since 1.2.0 * * @param int $post_id The post ThirdDraft names. * @param string $unchanged When ThirdDraft read it as last edited. * @return string */ public static function refusal( $post_id, $unchanged ) { $post = self::current( $post_id ); if ( ! $post ) { return self::in_place_refusal( false, '', '', $unchanged ); } return self::in_place_refusal( 'post' === $post->post_type, (string) $post->post_status, self::moment( $post->post_modified_gmt ), $unchanged ); } /** * The rule an update in place is held to: the post is there, published, * and was last edited exactly when ThirdDraft read it. A draft, a pending, * scheduled or private post is never written over, so an update never * publishes anything; a post with no readable moment counts as edited. * * @since 1.2.0 * * @param bool $exists Whether the post is there, outside the trash. * @param string $status Its status. * @param string $modified When it was last edited (ThirdDraft_Event::is_moment()); '' when unknown. * @param string $unchanged When ThirdDraft read it as last edited. * @return string '', 'gone', 'not_live' or 'edited'. */ public static function in_place_refusal( $exists, $status, $modified, $unchanged ) { if ( ! $exists || 'trash' === $status ) { return 'gone'; } if ( 'publish' !== $status ) { return 'not_live'; } if ( '' === $modified || ! ThirdDraft_Event::is_moment( $unchanged ) || $modified !== $unchanged ) { return 'edited'; } return ''; } /** * A post's `post_modified_gmt` as the plugin says a moment: ISO 8601 in * UTC, `2026-11-18T09:30:00Z`; '' for none, or WordPress's zero date. * * @since 1.2.0 * * @param mixed $gmt `Y-m-d H:i:s`, in UTC. * @return string */ public static function moment( $gmt ) { if ( ! is_string( $gmt ) || 1 !== preg_match( '/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$/', $gmt, $parts ) || '0000-00-00' === $parts[1] ) { return ''; } return $parts[1] . 'T' . $parts[2] . 'Z'; } /** * The post for an article, outside the trash. * * @since 1.0.0 * * @param int $article_id ThirdDraft's article id. * @return int The post, or 0. */ public static function find( $article_id ) { $found = get_posts( array( 'post_type' => 'post', 'post_status' => array( 'publish', 'future', 'draft', 'pending', 'private' ), 'meta_key' => self::ARTICLE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- one key, once per article. 'meta_value' => (string) (int) $article_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- as above. 'fields' => 'ids', 'numberposts' => 1, 'orderby' => 'ID', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); return empty( $found ) ? 0 : (int) $found[0]; } /** * The status after an event: a live post keeps its own; anything else is * published when ThirdDraft says so and otherwise stays as it is. * * @since 1.0.0 * * @param string $current The post's status now; 'draft' for a new post. * @param string $chosen 'publish' or 'draft', as chosen at ThirdDraft. * @return string */ public static function status_after( $current, $chosen ) { if ( in_array( $current, self::$live, true ) ) { return $current; } return 'publish' === $chosen ? 'publish' : $current; } /** * The first save of a new article: a draft, with the article's id, the * SEO values and Jetpack's "no newsletter" flag in place before any other * plugin hears of the post. Its pictures are still ThirdDraft's here. * * @param array $article The article. * @param int $author Its author. * @param bool $blocks Whether to write blocks. * @param array{set: array, delete: string[], hashes: array} $seo The SEO values. * @return int|WP_Error */ private static function insert_draft( $article, $author, $blocks, $seo ) { $draft = array( 'post_type' => 'post', 'post_status' => 'draft', 'post_title' => $article['title'], 'post_name' => self::slug( $article, 0 ), 'post_excerpt' => $article['meta_description'], 'post_content' => self::content( $article['content_html'], array(), $blocks ), 'post_author' => $author, 'meta_input' => array_merge( array( self::ARTICLE => (string) $article['id'] ), $seo['set'], array( ThirdDraft_Seo::HASHES => $seo['hashes'] ), self::jetpack() ), ); $category = self::category(); if ( $category ) { $draft['post_category'] = array( $category ); } $post_id = wp_insert_post( wp_slash( $draft ), true ); return is_wp_error( $post_id ) ? $post_id : (int) $post_id; } /** * The post's content: blocks, or the HTML as one Classic block. * * @param string $html The article's HTML. * @param array $pictures The copied pictures. * @param bool $blocks Whether to write blocks. * @return string */ private static function content( $html, $pictures, $blocks ) { if ( $blocks ) { $converter = new ThirdDraft_Block_Converter( $pictures ); $converted = $converter->convert( $html ); if ( null !== $converted && '' !== $converted ) { return $converted; } } return ThirdDraft_Block_Converter::classic( $html, $pictures ); } /** * Whether posts here are edited as blocks: the `dom` extension, WordPress * 6.1 or later (list items and quotes as inner blocks), and an editor not * forced to the classic one. * * @return bool */ private static function use_blocks() { if ( ! ThirdDraft_Block_Converter::available() || version_compare( get_bloginfo( 'version' ), '6.1', '<' ) ) { return false; } if ( function_exists( 'use_block_editor_for_post_type' ) ) { return (bool) use_block_editor_for_post_type( 'post' ); } /** This filter is documented in wp-admin/includes/post.php */ return (bool) apply_filters( 'use_block_editor_for_post_type', true, 'post' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress's own filter. } /** * The article's slug, made unique as a published post's would be. * * @param array $article The article. * @param int $post_id The post; 0 for a new one. * @return string */ private static function slug( $article, $post_id ) { $slug = sanitize_title( '' !== $article['slug'] ? $article['slug'] : $article['title'] ); return wp_unique_post_slug( $slug, (int) $post_id, 'publish', 'post', 0 ); } /** * Post as: the setting, else whoever connected, else the first administrator. * * @return int */ private static function author() { $settings = ThirdDraft_Store::settings(); $connection = ThirdDraft_Store::connection(); $candidates = array( $settings['author'], isset( $connection['connected_by'] ) ? (int) $connection['connected_by'] : 0 ); foreach ( $candidates as $user_id ) { if ( $user_id > 0 && false !== get_userdata( $user_id ) ) { return $user_id; } } $admins = get_users( array( 'role' => 'administrator', 'number' => 1, 'orderby' => 'ID', 'fields' => 'ID', ) ); return empty( $admins ) ? 0 : (int) $admins[0]; } /** * Category: the setting when it still exists; 0 for WordPress's default. * * @return int */ private static function category() { $settings = ThirdDraft_Store::settings(); if ( $settings['category'] > 0 && term_exists( $settings['category'], 'category' ) ) { return $settings['category']; } return 0; } /** * Jetpack's flag that keeps a post out of its newsletter email. * * @return array */ private static function jetpack() { return defined( 'JETPACK__VERSION' ) ? array( '_jetpack_dont_email_post_to_subs' => 1 ) : array(); } /** * The featured picture: set on a post that has none or still has the one * the plugin set, never over one a person chose. * * @param int $post_id The post. * @param int $attachment The article's featured picture; 0 for none. * @return void */ private static function feature( $post_id, $attachment ) { if ( ! $attachment ) { return; } $current = (int) get_post_thumbnail_id( $post_id ); if ( $current === $attachment ) { return; } if ( 0 === $current || '' !== (string) get_post_meta( $current, ThirdDraft_Pictures::META, true ) ) { set_post_thumbnail( $post_id, $attachment ); } } /** * The answer for a post: its id, its address while it is published, its * status, and when the plugin's last write left it last edited — never an * edit made since, which a repeated event would otherwise pass off as * ThirdDraft's own. * * @param int $post_id The post. * @return array{status: int, body: array} */ private static function answer( $post_id ) { $status = (string) get_post_status( $post_id ); return ThirdDraft_Answers::taken( $post_id, $status, 'publish' === $status ? (string) get_permalink( $post_id ) : '', (string) get_post_meta( $post_id, self::MODIFIED, true ) ); } /** * A post as it is in the database now, past this request's object cache: * a person may have saved it in another request since it was last read. * * @param int $post_id The post. * @return WP_Post|null */ private static function current( $post_id ) { wp_cache_delete( (int) $post_id, 'posts' ); $post = get_post( (int) $post_id ); return $post instanceof WP_Post ? $post : null; } }