'', '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'] ) ) { $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; } /** * Sideload a featured image from a URL using core media functions. * * @param int $post_id Target post. * @param string $image_url Image URL. * @return void */ private function attach_featured_image( $post_id, $image_url ) { if ( has_post_thumbnail( $post_id ) || empty( $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 ) ) { set_post_thumbnail( $post_id, $attachment_id ); } } }