'GET', 'callback' => 'addifect_get_custom_posts', 'permission_callback' => 'addifect_can_view_post_type', 'args' => array( 'post_type' => array( 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), ), ) ); } ); /** * Retrieve custom posts for Addifect studio with additional metadata. * * Checks user permissions dynamically based on post type capabilities. * * @param WP_REST_Request $request The REST request object. * @return WP_REST_Response|WP_Error REST response with posts or error. */ function addifect_get_custom_posts( $request ) { $post_type = $request->get_param( 'post_type' ); $user = wp_get_current_user(); $args = array( 'post_type' => $post_type, 'posts_per_page' => 100, 'post_status' => 'publish', 'fields' => 'ids', 'no_found_rows' => true, ); // Determine which capability to check based on the post type. $cap = ( 'addifect_design' === $post_type ) ? 'edit_others_addifect_designs' : 'edit_others_posts'; // Apply the author filter if the user lacks the permission. if ( ! current_user_can( $cap ) ) { $args['author'] = $user->ID; } $posts = get_posts( $args ); if ( empty( $posts ) ) { return new WP_REST_Response( array(), 200 ); } $data = array(); foreach ( $posts as $post_id ) { $thumbnail_url = get_the_post_thumbnail_url( $post_id, 'medium' ); $title = esc_html( get_the_title( $post_id ) ); $data[] = array( 'id' => $post_id, 'title' => $title, 'thumb' => $thumbnail_url ? esc_url_raw( $thumbnail_url ) : '', 'addifect_edited' => (bool) get_post_meta( $post_id, '_addifect_edited', true ), ); } return new WP_REST_Response( $data, 200 ); }