'', 'status' => 'error' ); } $existing_id = $this->find_existing( $blog_id ); // Auto-generated posts arrive as drafts; only publish when the member has // explicitly gone live in the AiM app (status === 'publish'). $desired_status = ( isset( $item['status'] ) && 'publish' === $item['status'] ) ? 'publish' : 'draft'; $postarr = array( 'post_title' => isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : '', 'post_content' => isset( $item['html'] ) ? wp_kses_post( $item['html'] ) : '', 'post_excerpt' => isset( $item['excerpt'] ) ? sanitize_textarea_field( $item['excerpt'] ) : '', 'post_status' => $desired_status, 'post_type' => 'post', ); // Runs from WP-Cron with no logged-in user. Assign a real author so // publishing behaves like a normal editorial publish (author 0 posts can // trip capability/ownership checks in some setups). $author_id = $this->default_author_id(); if ( $author_id ) { $postarr['post_author'] = $author_id; } if ( ! empty( $item['slug'] ) ) { $postarr['post_name'] = sanitize_title( $item['slug'] ); } if ( $existing_id ) { // An update leaves categories and tags alone (WordPress keeps them when // they aren't passed), so a category the member added by hand stays. $postarr['ID'] = $existing_id; } else { // New post: the blog category plus any suggested category the site // already has (never created), and the tags. $category_ids = $this->category_ids( $item ); if ( ! empty( $category_ids ) ) { $postarr['post_category'] = $category_ids; } if ( ! empty( $item['tags'] ) && is_array( $item['tags'] ) ) { $postarr['tags_input'] = array_values( array_filter( array_map( 'sanitize_text_field', $item['tags'] ) ) ); } } $post_id = wp_insert_post( $postarr, true ); if ( is_wp_error( $post_id ) ) { // Surface the real reason so it can be seen server-side (ack). return array( 'blog_id' => $blog_id, 'status' => 'error', 'error' => $post_id->get_error_code() . ': ' . $post_id->get_error_message(), ); } if ( ! $post_id ) { return array( 'blog_id' => $blog_id, 'status' => 'error', 'error' => 'wp_insert_post returned 0' ); } update_post_meta( $post_id, '_aim_blog_id', $blog_id ); Aim_Be_Seo::store( $post_id, $item ); if ( ! empty( $item['image_url'] ) ) { // Replaces the featured image when the cover changed in AiM (0.1.9+). $this->attach_featured_image( $post_id, esc_url_raw( $item['image_url'] ) ); } return array( 'blog_id' => $blog_id, 'post_id' => (string) $post_id, 'post_url' => get_permalink( $post_id ), 'status' => get_post_status( $post_id ), ); } /** * Names a site's blog category usually goes by, most specific first. Must * match BLOG_CATEGORY_NAMES in the app (lib/blog-engine/cms/wp-categories.ts). * * @var string[] */ const BLOG_CATEGORY_NAMES = array( 'Blog', 'Blogs', 'News', 'Articles', 'Article', 'Insights', 'Posts' ); /** * Category ids for a new post: the member's blog category (or, when it is * empty or no longer exists here, this site's blog-like category) plus any * suggested category that already exists. Nothing is ever created, and * Uncategorized is never chosen. With none, WordPress applies its default. * * @param array $item Post payload from /pending. * @return int[] Category term ids. */ private function category_ids( $item ) { $ids = array(); $blog = $this->find_category( isset( $item['blog_category'] ) ? $item['blog_category'] : '' ); if ( ! $blog ) { foreach ( self::BLOG_CATEGORY_NAMES as $name ) { $blog = $this->find_category( $name ); if ( $blog ) { break; } } } if ( $blog ) { $ids[] = $blog; } if ( ! empty( $item['categories'] ) && is_array( $item['categories'] ) ) { foreach ( $item['categories'] as $name ) { $id = $this->find_category( $name ); if ( $id && ! in_array( $id, $ids, true ) ) { $ids[] = $id; } } } return $ids; } /** * An existing category by name, ignoring case and HTML escaping. * * @param string $name Category name. * @return int Term id, or 0 when the site has no such category (or it is Uncategorized). */ private function find_category( $name ) { $name = is_string( $name ) ? trim( wp_specialchars_decode( $name, ENT_QUOTES ) ) : ''; if ( '' === $name ) { return 0; } $term = get_term_by( 'name', $name, 'category' ); if ( ! $term ) { // get_term_by() matches the stored name, which WordPress keeps escaped. $term = get_term_by( 'name', esc_html( $name ), 'category' ); } if ( ! $term || 'uncategorized' === $term->slug ) { return 0; } return (int) $term->term_id; } /** * The user to attribute imported posts to — the first administrator. Cron has * no current user, so without this posts would be authored by user 0. * * @return int Author user ID, or 0 if none found. */ private function default_author_id() { $admins = get_users( array( 'role' => 'administrator', 'number' => 1, 'fields' => 'ID', 'orderby' => 'ID', 'order' => 'ASC', ) ); return ! empty( $admins ) ? (int) $admins[0] : 0; } /** * Find a post previously created from this AiM blog id. * * @param string $blog_id AiM blog id. * @return int Post ID, or 0. */ private function find_existing( $blog_id ) { $query = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'any', // Idempotency lookup by our own post meta. Runs at most a few // times per sync, so the meta_key/meta_value query is fine. // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_key' => '_aim_blog_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'meta_value' => $blog_id, 'fields' => 'ids', 'posts_per_page' => 1, 'no_found_rows' => true, ) ); return $query->have_posts() ? (int) $query->posts[0] : 0; } /** * Post meta holding the AiM image URL the post's featured image came from * (0.1.9+), so a cover changed in AiM is recognised on the next sync. */ const META_IMAGE_URL = '_aim_image_url'; /** * Make the post's featured image the one AiM sent. * * AiM sends the post's current cover on every sync, and a new cover always * has a new URL. Through 0.1.8 an existing thumbnail was never replaced, so a * cover changed after the first draft never reached the site (AIM-4617). * * - No featured image: sideload AiM's. * - Same URL as the image already set: nothing to do. * - A different URL: sideload it and make it the featured image. * - A post from an older plugin (no stored URL) whose featured image is not * one we sideloaded: the member chose it in WordPress, so it stays. The * URL is recorded, so a later change in AiM still comes through. * * If the download fails the current image stays and nothing is recorded, so * the next sync tries again. * * @param int $post_id Target post. * @param string $image_url Image URL. * @return void */ private function attach_featured_image( $post_id, $image_url ) { if ( empty( $image_url ) ) { return; } $thumbnail_id = (int) get_post_thumbnail_id( $post_id ); if ( $thumbnail_id ) { $stored = (string) get_post_meta( $post_id, self::META_IMAGE_URL, true ); if ( '' === $stored ) { // Posts imported before 0.1.9 have no stored URL. An image we // sideloaded is attached to this post and carries core's // _source_url; one the member uploaded or picked does not. $source = $this->sideloaded_source( $thumbnail_id, $post_id ); if ( '' === $source || $source === $image_url ) { update_post_meta( $post_id, self::META_IMAGE_URL, $image_url ); return; } } elseif ( $stored === $image_url ) { return; } } require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $attachment_id = media_sideload_image( $image_url, $post_id, null, 'id' ); if ( is_wp_error( $attachment_id ) || ! $attachment_id ) { return; } set_post_thumbnail( $post_id, $attachment_id ); update_post_meta( $post_id, self::META_IMAGE_URL, $image_url ); } /** * The URL an attachment was sideloaded from, when it was sideloaded for this * post (media_sideload_image() records it as _source_url). * * @param int $attachment_id Attachment ID. * @param int $post_id The post it should belong to. * @return string Source URL, or '' when the attachment isn't ours. */ private function sideloaded_source( $attachment_id, $post_id ) { if ( (int) wp_get_post_parent_id( $attachment_id ) !== (int) $post_id ) { return ''; } return (string) get_post_meta( $attachment_id, '_source_url', true ); } }