'POST', 'callback' => 'addifect_publish_post_data', // NATIVE WP PERMISSION CHECK: Can this specific user edit this specific post?. 'permission_callback' => function ( WP_REST_Request $request ) { $post_id = $request->get_param( 'post_id' ); return current_user_can( 'edit_post', $post_id ); }, 'args' => array( 'post_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'template_data' => array( 'required' => true, 'type' => 'object', // Note: Ensure your sanitize function allows the nested 'blocks' and 'ssr' HTML/CSS strings. 'sanitize_callback' => 'addifect_recursive_sanitize_array', ), ), ) ); } ); /** * Publish Addifect CMS Post Data. * Splits the payload: JSON goes to Meta (for the Editor), HTML goes to post_content (for SEO/Frontend). * * @param WP_REST_Request $request REST request object. * @return WP_REST_Response|WP_Error */ function addifect_publish_post_data( WP_REST_Request $request ) { $post_id = $request->get_param( 'post_id' ); $template_data = $request->get_param( 'template_data' ); // 1. Validate Post Exists if ( ! get_post( $post_id ) ) { return new WP_Error( 'invalid_post', 'The specified post does not exist.', array( 'status' => 404 ) ); } // 2. Extract the Payload Components $blocks = isset( $template_data['blocks'] ) ? $template_data['blocks'] : array(); $ssr_html = isset( $template_data['ssr']['html'] ) ? $template_data['ssr']['html'] : ''; $ssr_css = isset( $template_data['ssr']['css'] ) ? $template_data['ssr']['css'] : ''; /** * 3. Publish Raw JSON & CSS to Post Meta * The editor will use '_addifect_post_blocks' to rehydrate the workspace. * The frontend will inject '_addifect_post_css' into the document . */ update_post_meta( $post_id, '_addifect_post_blocks', wp_slash( $blocks ) ); update_post_meta( $post_id, '_addifect_post_css', wp_slash( $ssr_css ) ); /** * 4. Publish SSR HTML to Native WP Content * We briefly remove KSES filters so WP doesn't strip out custom attributes (like data-radius) * from the generated SSR HTML. Because the user has 'edit_post' caps, this is standard practice. */ remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); $post_update = array( 'ID' => $post_id, 'post_content' => $ssr_html, ); // Update the native WordPress post. $update_result = wp_update_post( $post_update, true ); // Re-apply filters for safety on other WP operations. add_filter( 'content_save_pre', 'wp_filter_post_kses' ); add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); // 5. Handle Errors or Return Success if ( is_wp_error( $update_result ) ) { return $update_result; } return new WP_REST_Response( array( 'success' => true, 'post_id' => $post_id, 'message' => 'Post SSR and Data published successfully.', ), 200 ); }