'POST', 'callback' => 'suiko_rest_create', 'permission_callback' => 'suiko_rest_auth' ) ); }, 20 ); function suiko_rest_create( $request ) { $input = $request->get_json_params(); if ( ! is_array( $input ) || empty( $input['title'] ) || ! isset( $input['content_html'] ) ) { return new WP_Error( 'suiko_bad_request', 'title and content_html are required.', array( 'status' => 400 ) ); } $mode = isset( $input['mode'] ) && 'direct' === $input['mode'] ? 'direct' : ( isset( $input['mode'] ) && 'draft' === $input['mode'] ? 'draft' : suiko_mode() ); $type = isset( $input['type'] ) && in_array( $input['type'], suiko_post_types(), true ) ? $input['type'] : 'post'; $content = suiko_html_to_blocks( (string) $input['content_html'] ); $lost = suiko_lost_tags( $content ); if ( $lost ) { return suiko_lost_tags_error( $lost ); } $data = array( 'post_type' => $type, 'post_status' => 'direct' === $mode ? 'publish' : 'draft', 'post_title' => wp_strip_all_tags( (string) $input['title'] ), 'post_name' => ! empty( $input['slug'] ) ? sanitize_title( basename( (string) $input['slug'] ) ) : '', 'post_content' => $content, 'post_excerpt' => isset( $input['excerpt'] ) && is_string( $input['excerpt'] ) ? wp_strip_all_tags( $input['excerpt'] ) : '', ); // The author: the person who connected the site to Suiko; the site can reassign it. No user is logged in during this request. $author = (int) get_option( 'suiko_author' ); if ( $author && get_userdata( $author ) ) { $data['post_author'] = $author; } if ( 'post' === $type && ! empty( $input['category'] ) && is_string( $input['category'] ) ) { $term = get_term_by( 'name', $input['category'], 'category' ) ?: get_term_by( 'slug', sanitize_title( $input['category'] ), 'category' ); if ( ! $term ) { $new = wp_insert_term( sanitize_text_field( $input['category'] ), 'category' ); $term = is_wp_error( $new ) ? null : get_term( $new['term_id'], 'category' ); } if ( $term ) { $data['post_category'] = array( (int) $term->term_id ); } } $id = wp_insert_post( wp_slash( $data ), true ); if ( is_wp_error( $id ) ) { return $id; } if ( isset( $input['seo'] ) && is_array( $input['seo'] ) ) { suiko_seo_write( $id, $input['seo'] ); } update_post_meta( $id, '_suiko_created_at', gmdate( 'c' ) ); $post = get_post( $id ); return array( 'status' => 'publish' === $post->post_status ? 'published' : 'draft', 'id' => (string) $id, 'slug' => suiko_public_slug( $post ), 'url' => 'publish' === $post->post_status ? get_permalink( $post ) : null, 'edit_url' => admin_url( 'post.php?post=' . (int) $id . '&action=edit' ), ); }