namespace, '/admin/setup', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'permission_manage' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'permission_manage' ), 'args' => array( 'spaces' => array( 'type' => 'array', 'items' => array( 'type' => 'string', 'enum' => array_keys( self::starter_spaces() ), ), 'default' => array(), ), 'done' => array( 'type' => 'boolean', 'default' => true, ), ), ), ) ); } /** * Starter spaces offered by the wizard. * * @return array */ public static function starter_spaces() { return array( 'announcements' => array( 'title' => __( 'Announcements', 'spacebay' ), 'description' => __( 'News and updates from the team.', 'spacebay' ), 'settings' => array( 'icon' => 'megaphone', 'color' => '#0f766e', 'who_can_post' => 'admins', ), ), 'general' => array( 'title' => __( 'General', 'spacebay' ), 'description' => __( 'Talk about anything with the community.', 'spacebay' ), 'settings' => array( 'icon' => 'chat', 'color' => '#3f22ff', ), ), 'introductions' => array( 'title' => __( 'Introductions', 'spacebay' ), 'description' => __( 'New here? Say hello and tell us about yourself.', 'spacebay' ), 'settings' => array( 'icon' => 'wave', 'color' => '#b45309', ), ), ); } /** * Setup state. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_item( $request ) { unset( $request ); $spaces = array(); foreach ( self::starter_spaces() as $key => $space ) { $spaces[] = array( 'key' => $key, 'title' => $space['title'], 'description' => $space['description'], 'icon' => $space['settings']['icon'], ); } return rest_ensure_response( array( 'done' => (bool) get_option( self::OPTION ), 'has_spaces' => (bool) SpaceRepository::all( 'space' ), 'starter' => $spaces, 'users_can_register' => (bool) get_option( 'users_can_register' ), 'signup_open' => '' !== Auth::signup_url(), 'signup_pages' => Auth::has_pages(), ) ); } /** * Creates the chosen starter spaces and finishes setup. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function create_item( $request ) { $user_id = get_current_user_id(); $starters = self::starter_spaces(); $created = array(); $existing = wp_list_pluck( SpaceRepository::all( 'space' ), 'title' ); foreach ( array_unique( (array) $request['spaces'] ) as $key ) { if ( ! isset( $starters[ $key ] ) || in_array( $starters[ $key ]['title'], $existing, true ) ) { continue; } $id = SpaceRepository::create( array_merge( $starters[ $key ], array( 'type' => 'space', 'privacy' => 'public', ) ), $user_id ); if ( $id ) { Membership::set( $id, $user_id, 'admin', 'active' ); /** This action is documented in includes/Rest/SpacesController.php */ do_action( 'spacebay_space_created', $id, $user_id ); $created[] = $id; } } if ( $request['done'] ) { update_option( self::OPTION, 1, false ); } return rest_ensure_response( array( 'done' => (bool) get_option( self::OPTION ), 'created' => $created, ) ); } }