settings = $settings; $this->reminders = $reminders; } /** * {@inheritdoc} */ public function register_routes(): void { $permission = Permissions::manage(); register_rest_route( $this->namespace, '/admin/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'show' ), 'permission_callback' => $permission, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'update' ), 'permission_callback' => $permission, ), ) ); register_rest_route( $this->namespace, '/admin/settings/create-page', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_page' ), 'permission_callback' => $permission, ) ); } /** * GET /admin/settings * * @since 0.1.0 * * @return \WP_REST_Response */ public function show() { return rest_ensure_response( array( 'values' => $this->settings->export_for_app(), 'pages' => self::page_options(), ) ); } /** * POST /admin/settings * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function update( WP_REST_Request $request ) { $body = $this->request_body( $request ); if ( array() === $body ) { return $this->error( 'invalid', __( 'No settings were sent.', 'interviewnea' ), 400 ); } $result = $this->settings->update( $body ); if ( $result['errors'] ) { return $this->error( 'invalid', __( 'Please fix the highlighted fields.', 'interviewnea' ), 400, array( 'fields' => $result['errors'] ) ); } // Reminder switches and the portal page change what is scheduled. $this->reminders->schedule_all(); return rest_ensure_response( array( 'values' => $this->settings->export_for_app(), 'pages' => self::page_options(), 'message' => __( 'Settings saved.', 'interviewnea' ), ) ); } /** * POST /admin/settings/create-page * * @since 0.1.0 * * @return \WP_REST_Response|\WP_Error */ public function create_page() { if ( ! current_user_can( 'publish_pages' ) ) { return Permissions::forbidden(); } $page_id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => __( 'Interview Check-in', 'interviewnea' ), 'post_content' => '[interviewnea_waiting_room]', ), true ); if ( is_wp_error( $page_id ) ) { return $this->error( 'page_failed', $page_id->get_error_message(), 500 ); } $this->settings->update( array( 'portal_page' => (int) $page_id ) ); $this->reminders->schedule_all(); return rest_ensure_response( array( 'values' => $this->settings->export_for_app(), 'pages' => self::page_options(), 'message' => __( 'Check-in page created.', 'interviewnea' ), 'url' => get_permalink( (int) $page_id ), ) ); } /** * Published pages for the check-in page picker. * * @since 0.1.0 * * @return array */ public static function page_options(): array { $pages = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, ) ); $out = array(); $ids = array(); foreach ( $pages as $page ) { $out[] = array( 'id' => (int) $page->ID, 'title' => '' !== $page->post_title ? $page->post_title : __( '(no title)', 'interviewnea' ), ); $ids[] = (int) $page->ID; } // The chosen page must stay selectable even when the site has more than 100 pages. $selected = (int) ( new Settings() )->get( 'portal_page', 0 ); if ( $selected && ! in_array( $selected, $ids, true ) && 'page' === get_post_type( $selected ) ) { $out[] = array( 'id' => $selected, 'title' => get_the_title( $selected ), ); } return $out; } }