<?php

if ( ! class_exists( 'ALPHA_Block_Category' ) ) :
	/**
	 * ALPHA_Block_Category Class
	 */
	class ALPHA_Block_Category {

		/**
		 * Constructor
		 */
		public function __construct() {
			if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
				add_filter( 'block_categories_all', array( $this, 'alpb_register_block_category' ), 10, 2 );
			} else {
				add_filter( 'block_categories', array( $this, 'alpb_register_block_category' ), 10, 2 );
			}
		}

		/**
		 * Register block category
		 *
		 * @param array   $categories Categories array.
		 * @param WP_Post $post Post object.
		 * @return array
		 */
		public function alpb_register_block_category( $cats, $post ) {

			$new = array(
				'alphablocks' => array(
					'slug'  => 'alpha-blocks',
					'title' => __( 'Alpha Blocks', 'alpha-blocks' ),
				),
			);
			// just decide here at what position your custom category should appear
			$position = 0; // 2 – After Text and Media, so technically it is a 3rd position

			$cats = array_slice( $cats, 0, $position, true ) + $new + array_slice( $cats, $position, null, true );

			// reset array indexes
			$cats = array_values( $cats );

			return $cats;
		}
	}
endif;
