PostType


Source

File: classes/PostType.php

class PostType{

	public $course_capabilities;

	public function __construct() {

		$this->course_capabilities = [
			'read_post'              => 'read_course',
			'publish_posts'          => 'publish_courses',
			'create_posts'           => 'edit_courses',
			'edit_posts'             => 'edit_courses',
			'edit_post'              => 'edit_course',
			'edit_others_posts'      => 'edit_others_courses',
			'delete_posts'           => 'delete_courses',
			'delete_others_posts'    => 'delete_others_courses',
			'read_private_posts'     => 'read_private_courses',
			'edit_private_posts'     => 'edit_private_courses',
			'delete_private_posts'   => 'delete_private_courses',
			'delete_post'            => 'delete_course',
			'edit_published_posts'   => 'edit_published_courses',
			'delete_published_posts' => 'delete_published_courses',
		];

		add_action( 'init', [ $this, 'register_course_post_type' ] );
		add_action( 'init', [ $this, 'register_course_categories' ] );
		add_action( 'init', array( $this, 'register_lecture_post_type' ) );
		add_action( 'init', array( $this, 'register_quiz_post_type' ) );


	}

	public function register_course_post_type(){
		$show_in_rest = (bool) dozent_get_option('enable_course_post_type_rest_api');

		$course_slug = dozent_get_option('course_base_slug', 'courses');
		$course_base_slug = apply_filters('dozent_course_base_slug', $course_slug);

		$labels = [
			'name'               => _x( 'Courses', 'post type general name', 'dozent' ),
			'singular_name'      => _x( 'Course', 'post type singular name', 'dozent' ),
			'menu_name'          => _x( 'Courses', 'admin menu', 'dozent' ),
			'name_admin_bar'     => _x( 'Course', 'add new on admin bar', 'dozent' ),
			'add_new'            => _x( 'Add New', 'course', 'dozent' ),
			'add_new_item'       => __( 'Add New Course', 'dozent' ),
			'new_item'           => __( 'New Course', 'dozent' ),
			'edit_item'          => __( 'Edit Course', 'dozent' ),
			'view_item'          => __( 'View Course', 'dozent' ),
			'all_items'          => __( 'All Courses', 'dozent' ),
			'search_items'       => __( 'Search Courses', 'dozent' ),
			'parent_item_colon'  => __( 'Parent Courses:', 'dozent' ),
			'not_found'          => __( 'No courses found.', 'dozent' ),
			'not_found_in_trash' => __( 'No courses found in Trash.', 'dozent' )
		];

		$args = [
			'labels'            => $labels,
			'description'       => __( 'Description.', 'dozent' ),
			'public'            => true,
			'publicly_queryable'=> true,
			'show_ui'           => true,
			'show_in_menu'      => 'dozent',
			'query_var'         => true,
			'rewrite'           => [ 'slug' => $course_base_slug ],
			'capability_type'   => 'post',
			'has_archive'       => true,
			'hierarchical'      => false,
			'menu_position'     => null,
			'taxonomies'        => ['course_category'],
			'supports'          => ['title', 'editor', 'author', 'thumbnail', 'excerpt'],
			'show_in_rest'      => $show_in_rest,
			'capabilities'      => $this->course_capabilities,
		];

		register_post_type( 'dozent_course', $args );
	}

	public function register_course_categories(){
		$category_base_slug = dozent_get_option('course_category_base_slug', 'course-category');
		$category_base = apply_filters('dozent_course_category_base', $category_base_slug);

		$labels = [
			'name'                       => _x( 'Course Category', 'taxonomy general name', 'dozent' ),
			'singular_name'              => _x( 'Course Category', 'taxonomy singular name', 'dozent' ),
			'search_items'               => __( 'Search Course Category', 'dozent' ),
			'popular_items'              => __( 'Popular Course Category', 'dozent' ),
			'all_items'                  => __( 'All Course Category', 'dozent' ),
			'parent_item'                => null,
			'parent_item_colon'          => null,
			'edit_item'                  => __( 'Edit Course Category', 'dozent' ),
			'update_item'                => __( 'Update Course Category', 'dozent' ),
			'add_new_item'               => __( 'Add New Course Category', 'dozent' ),
			'new_item_name'              => __( 'New Course Category Name', 'dozent' ),
			'separate_items_with_commas' => __( 'Separate course categories with commas', 'dozent' ),
			'add_or_remove_items'        => __( 'Add or remove course categories', 'dozent' ),
			'choose_from_most_used'      => __( 'Choose from the most used course categories', 'dozent' ),
			'not_found'                  => __( 'No course categories found.', 'dozent' ),
			'menu_name'                  => __( 'Course Category', 'dozent' ),
		];

		$args = [
			'hierarchical'          => true,
			'labels'                => $labels,
			'show_ui'               => true,
			'show_admin_column'     => true,
			'update_count_callback' => '_update_post_term_count',
			'query_var'             => true,
			'show_in_rest'          => true,
			'rewrite'               => [ 'slug' => $category_base],
		];

		register_taxonomy( 'course_category', 'dozent_course', $args );
	}


	public function register_lecture_post_type(){
		$lecture_slug = dozent_get_option('lecture_base_slug', 'lecture');
		$lecture_base_slug = apply_filters('dozent_lecture_base_slug', $lecture_slug);

		$labels = array(
			'name'               => _x( 'Lectures', 'post type general name', 'dozent' ),
			'singular_name'      => _x( 'Lecture', 'post type singular name', 'dozent' ),
			'menu_name'          => _x( 'Lectures', 'admin menu', 'dozent' ),
			'name_admin_bar'     => _x( 'Lecture', 'add new on admin bar', 'dozent' ),
			'add_new'            => _x( 'Add New', $lecture_base_slug, 'dozent' ),
			'add_new_item'       => __( 'Add New Lecture', 'dozent' ),
			'new_item'           => __( 'New Lecture', 'dozent' ),
			'edit_item'          => __( 'Edit Lecture', 'dozent' ),
			'view_item'          => __( 'View Lecture', 'dozent' ),
			'all_items'          => __( 'Lectures', 'dozent' ),
			'search_items'       => __( 'Search Lectures', 'dozent' ),
			'parent_item_colon'  => __( 'Parent Lectures:', 'dozent' ),
			'not_found'          => __( 'No lectures found.', 'dozent' ),
			'not_found_in_trash' => __( 'No lectures found in Trash.', 'dozent' )
		);

		$args = array(
			'labels'                => $labels,
			'description'           => __( 'Description.', 'dozent' ),
			'public'                => true,
			'publicly_queryable'    => true,
			'show_ui'               => true,
			'show_in_menu'          => false,
			'query_var'             => true,
			'rewrite'               => array( 'slug' => $lecture_base_slug ),
			'menu_icon'             => 'dashicons-list-view',
			'capability_type'       => 'post',
			'has_archive'           => true,
			'hierarchical'          => false,
			'menu_position'         => null,
			'supports'              => array( 'title', 'editor'),
			'show_in_rest'          => true,
			'capabilities'      => $this->course_capabilities,
		);

		register_post_type('dozent_lecture', $args );
	}

	public function register_quiz_post_type(){
		$quiz_slug = dozent_get_option('quiz_base_slug', 'quiz');
		$quiz_base_slug = apply_filters('dozent_quiz_base_slug', $quiz_slug);

		$labels = array(
			'name'               => _x( 'Quizzes', 'post type general name', 'dozent' ),
			'singular_name'      => _x( 'Quiz', 'post type singular name', 'dozent' ),
			'menu_name'          => _x( 'Quizzes', 'admin menu', 'dozent' ),
			'name_admin_bar'     => _x( 'Quiz', 'add new on admin bar', 'dozent' ),
			'add_new'            => _x( 'Add New', $quiz_base_slug, 'dozent' ),
			'add_new_item'       => __( 'Add New Quiz', 'dozent' ),
			'new_item'           => __( 'New Quiz', 'dozent' ),
			'edit_item'          => __( 'Edit Quiz', 'dozent' ),
			'view_item'          => __( 'View Quiz', 'dozent' ),
			'all_items'          => __( 'Quizzes', 'dozent' ),
			'search_items'       => __( 'Search Quizzes', 'dozent' ),
			'parent_item_colon'  => __( 'Parent Quizzes:', 'dozent' ),
			'not_found'          => __( 'No quizs found.', 'dozent' ),
			'not_found_in_trash' => __( 'No quizs found in Trash.', 'dozent' )
		);

		$args = array(
			'labels'                => $labels,
			'description'           => __( 'Description.', 'dozent' ),
			'public'                => true,
			'publicly_queryable'    => true,
			'show_ui'               => true,
			'show_in_menu'          => false,
			'query_var'             => true,
			'rewrite'               => array( 'slug' => $quiz_base_slug ),
			'menu_icon'             => 'dashicons-list-view',
			'capability_type'       => 'post',
			'has_archive'           => true,
			'hierarchical'          => false,
			'menu_position'         => null,
			'supports'              => array( 'title', 'editor'),
			'show_in_rest'          => true,
			'capabilities'      => $this->course_capabilities,
		);

		register_post_type('dozent_quiz', $args );
	}

	public function add_capabilities() {

		if ( is_admin() ) {
			$admin_role = get_role( 'administrator' );
			if ( ( $admin_role ) && ( $admin_role instanceof \WP_Role ) ) {
				if ( ! $admin_role->has_cap( 'delete_private_courses' ) ) {
					foreach ( $this->course_capabilities as $key => $cap ) {
						if ( ! $admin_role->has_cap( $cap ) ) {
							$admin_role->add_cap( $cap );
						}
					}
				}
				if ( ! $admin_role->has_cap( 'enroll_users' ) ) {
					$admin_role->add_cap( 'enroll_users' );
				}
			}
		}

	}

}

Methods