<?php

/**
 * Rewrites the YOAST Plugin breadcrumbs
 *
 * This function rewrites the YOAST Plugin breadcrumbs
 * to match the directory structure of the Practice
 * News. If the post page is single, the function first
 * gets the post parent slug and then splices it into
 * the breadcrumbs array. This function is too specific
 * as it relies on a fixed structure of 'news/practice-news'.
 * If the customer changes the page structure or titles,
 * this function will break the breadcrumbs!
 *
 * @function my_wpseo_breadcrumb_links()
 *
 * @uses register_post_type()
 *
 * @param (array) $links an array of links to be used in the breadcrumbs
 *
 * @return (array) returns the updated array of links to be used in the breadcrumbs
 *
 * @since 1.1.0 
 *
 */
function my_wpseo_breadcrumb_links( $links ) {
	
	$cpt_object = get_post_type_object( get_post_type() );
	
	if ( $cpt_object && ! $cpt_object->_builtin ) {
		
		$landing_page = get_page_by_path( $cpt_object->rewrite['slug'] );
		
		if ( $landing_page ) {
			
			array_splice( $links, 1, 0, array( array(
				'id' => $landing_page->post_parent,
			) ) );
		}
	}
	return $links;
}
add_filter( 'wpseo_breadcrumb_links', 'my_wpseo_breadcrumb_links' );



/**
 * Registers 'practice-news-posts' post type 
 *
 * This function registers the 'practice-news-posts'
 * posts type which is used for the practice news posts.
 * This function is too specific as it relies on a
 * fixed structure of 'news/practice-news'. If the
 * customer changes the page structure or titles, this
 * function will break the posts page!
 *
 * @function practice_news_post_type()
 *
 * @uses register_post_type()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.3.9 
 *
 */
function practice_news_post_type() {
	
	$labels = array(
		'name' 				 => _x( 'Practice News', 'post type general name' ),
		'singular_name' 	 => _x( 'Article', 'post type singular name' ),
		'menu_name' 		 => __( 'Practice News' ),
		'add_new' 			 => _x( 'Add New', 'team item' ),
		'add_new_item' 		 => __( 'Add New Article' ),
		'edit_item' 		 => __( 'Edit Article' ),
		'new_item' 			 => __( 'New Article' ),
		'view_item' 		 => __( 'View Article' ),
		'search_items' 		 => __( 'Search Articles' ),
		'not_found' 		 => __( 'No Articles Found' ),
		'not_found_in_trash' => __( 'No Articles Found in Trash' ),
		'parent_item_colon'  => '',
	);
	register_post_type( 'practice-news-posts', array(
		'labels' 	   => $labels,
		'public' 	   => true,
		'has_archive'  => 'news/practice-news',
		'menu_icon'    => 'dashicons-format-aside',
		'rewrite' 	   => array( 'slug' => 'news/practice-news', 'with_front' => false ),
		'supports' 	   => array( 'title', 'editor', 'thumbnail' ),
		'show_in_rest' => true,
		'hierarchical' => true,
	) );
}
add_action( 'init', 'practice_news_post_type', 0 );