0 ? Prefs::get( $user_id, Prefs::EDITOR ) : null; return is_string( $value ) && in_array( $value, self::CHOICES, true ) ? $value : ''; } public static function can_use_designer( int $user_id ): bool { if ( $user_id <= 0 || ! self::built() ) { return false; } return get_current_user_id() === $user_id ? current_user_can( Access::PAGE_CAP ) : user_can( $user_id, Access::PAGE_CAP ); // phpcs:ignore WordPress.WP.Capabilities.Unknown -- the course post type's primitive (Core\Capabilities). } public static function built(): bool { $dir = CategoryTreePage::build_dir(); $root = untrailingslashit( wp_normalize_path( ROOSTER_LMS_DIR ) ); return '' !== $dir && str_starts_with( $dir . '/', $root . '/' ) && is_readable( $dir . '/' . Script::ENTRY . '.js' ) && is_readable( $dir . '/' . Script::ENTRY . '.asset.php' ); } public static function may_choose( int $user_id ): bool { return self::can_use_designer( $user_id ); } public static function for_user( int $user_id ): string { $choice = self::choice( $user_id ); $want = '' !== $choice ? $choice : self::site(); return self::DESIGNER === $want && self::can_use_designer( $user_id ) ? self::DESIGNER : self::BLOCK; } public static function set( int $user_id, string $editor ): bool { return in_array( $editor, self::CHOICES, true ) && self::may_choose( $user_id ) && Prefs::set( $user_id, Prefs::EDITOR, $editor ); } public static function block_url( int $post_id ): string { return $post_id > 0 ? admin_url( 'post.php?post=' . $post_id . '&action=edit' ) : ''; } public static function url( int $user_id, string $block, string $designer ): string { if ( '' !== $designer && self::DESIGNER === self::for_user( $user_id ) ) { return $designer; } return $block; } public static function actions( int $user_id, string $block, string $designer ): array { $designer = self::can_use_designer( $user_id ) ? $designer : ''; $primary = self::url( $user_id, $block, $designer ); if ( '' === $primary ) { return '' !== $designer ? array( self::designer_action( $designer ) ) : array(); } $out = array( array( 'id' => 'edit', 'label' => __( 'Edit', 'rooster-lms' ), 'href' => $primary, ), ); if ( $primary === $designer && '' !== $block ) { $out[] = array( 'id' => 'block-editor', 'label' => self::other_label( self::BLOCK ), 'href' => $block, ); } elseif ( $primary !== $designer && '' !== $designer ) { $out[] = self::designer_action( $designer ); } return $out; } public static function other_label( string $editor ): string { return self::BLOCK === $editor ? __( 'Edit in the block editor', 'rooster-lms' ) : __( 'Edit with Designer', 'rooster-lms' ); } private static function designer_action( string $designer ): array { return array( 'id' => 'designer', 'label' => self::other_label( self::DESIGNER ), 'href' => $designer, ); } public static function post_urls( \WP_Post $post, int $user_id ): array { if ( ! in_array( $post->post_type, array( PostTypes::COURSE, PostTypes::LESSON, PostTypes::QUIZ ), true ) || ! user_can( $user_id, 'edit_post', $post->ID ) ) { return array( '', '' ); } return array( self::block_url( (int) $post->ID ), self::can_use_designer( $user_id ) ? EditorSwitch::url_for_post( $post, $user_id ) : '' ); } }