<?php
/**
 * Custom post type Ajuts.
 *
 *
 * @link       https://oficinasuport.xarxaprod.cat
 * @since      1.0.0
 * @package    ofisuport-wp-plugin 
 * @subpackage ofisuport-wp-plugin/includes
 * @author     Jorge - vitrubio.net <jorge@vitrubio.net>
 *
 * @internal never define functions inside callbacks.
 * these functions could be run multiple times; this would result in a fatal error.
 */

/**
 * Start custom post types
 * ----------------------------------------------------------------------------
 * https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/
 * https://developer.wordpress.org/reference/functions/register_post_type/
 */


 // Register Custom Post Type
 function ofisuport_wpplugin_custom_post_ajut_init() {

 	$labels = array(
 		'name'                => _x( 'Ajuts', 'Post Tipu General Name', 'text_domain' ),
 		'singular_name'       => _x( 'Ajut', 'Post Tipu Singular Name', 'text_domain' ),
 		'menu_name'           => __( 'Ajuts', 'text_domain' ),
 		'parent_item_colon'   => __( 'Ajuts mare/pare:', 'text_domain' ),
 	  'all_items'           => __( 'Tots els ajuts', 'text_domain' ),
 		'view_item'           => __( 'Mostra el ajut', 'text_domain' ),
 		'add_new_item'        => __( 'Affegeix un ajut', 'text_domain' ),
 		'add_new'             => __( 'Affegeix ajut', 'text_domain' ),
 		'edit_item'           => __( 'Edita ajut', 'text_domain' ),
 		'update_item'         => __( 'Desa ajut', 'text_domain' ),
 		'search_items'        => __( 'Cerca ajut', 'text_domain' ),
 		'not_ajut'           => __( 'No hem pogut trovar ningĂșn ajut ajut ', 'text_domain' ),
 		'not_ajut_in_trash'  => __( 'No hem trovat cap ajut a la brosa', 'text_domain' )
 	);
 	$rewrite = array(
		'slug'                => 'ajut'
 	);
	// https://wordpress.stackexchange.com/questions/108338/capabilities-and-custom-post-types#108375
	// https://developer.wordpress.org/reference/functions/register_post_type/#capability_type
	//$capabilities =  array(
	//	'read' 					          => 'read_ajut',
	//	'publish_posts' 			    => 'publish_ajut',
	//	'edit_posts'					    => 'edit_ajut',
	//	'edit_published_posts'    => 'edit_published_ajut', 
	//	'edit_others_posts' 	    => 'edit_others_ajut',
	//	'delete_posts' 				    => 'delete_ajut',
	//	'delete_published_posts'	=> 'delete_published_ajut',
	//	'delete_others_posts'     => 'delete_others_ajuts',
	//	'delete_private_posts'    => 'delete_private_ajuts'
	//	);
 	$args = array(
 		'label'               => __( 'ofisuport-ajut', 'text_domain' ),
 		'description'         => __( 'Ajuts', 'text_domain' ),
 		'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ),
    'taxonomies'          => array(
      'ofisuport-ajut-category',
      'ofisuport-ajut-tag'
    ),
 		'hierarchical'        => true,
 		'public'              => true,
 		'show_ui'             => true,
 		'show_in_menu'        => true,
 		'show_in_nav_menus'   => true,
 		'show_in_admin_bar'   => true,
    'show_in_rest'        => true,
 		'menu_position'       => 5,
 		'menu_icon'           => 'dashicons-book',
 		'can_export'          => true,
 		'has_archive'         => true,
 		'exclude_from_search' => false,
 		'publicly_queryable'  => true,
 		'capability_type'     => 'post',
    'rewrite'             => $rewrite,
		//'capabilities' 				=> $capabilities,
		'map_meta_cap' 				=> true //neded to apply the new capabilities.
 	);
 	register_post_type( 'ofisuport-ajut', $args );
 }
 // Hook into the 'init' action
 add_action( 'init', 'ofisuport_wpplugin_custom_post_ajut_init', 0 );


  // flush rewrite and when changing theme or plugin
  // https://developer.wordpress.org/reference/functions/register_post_type/#flushing-rewrite-on-activation
  function ofisuport_ajut_rewrite_flush() {
     ofisuport_wpplugin_custom_post_ajut_init();
     flush_rewrite_rules();
  }
  add_action( 'after_switch_theme', 'ofisuport_ajut_rewrite_flush' );

 // give capabilities once the custom post id activated
// function ofisuport_wpplugin_add_caps() {
	  // gets the XX role
	  // $admins = get_role( '' );
	  // $admins->add_cap(  );
    // gets the administrator role


//}
//add_action( 'admin_init', 'ofisuport_wpplugin_add_caps');



/**
 * Start custom taxonomies
 * ----------------------------------------------------------------------------
 * https://developer.wordpress.org/plugins/taxonomies/working-with-custom-taxonomies/
 * https://developer.wordpress.org/reference/functions/register_taxonomy/
 */

 // Register Custom Taxonomy Category for Ajuts
 //function ofisuport_wpplugin_ajut_category_register() {

 //	$labels = array(
 //		'name'                       => _x( 'Ajuts Tipus', 'Taxonomy General Name', 'text_domain' ),
 //		'singular_name'              => _x( 'Tipus d`ajut', 'Taxonomy Singular Name', 'text_domain' ),
 //		'menu_name'                  => __( 'Ajuts Tipus', 'text_domain' ),
 //		'all_items'                  => __( 'All type ajuts', 'text_domain' ),
 //		'parent_item'                => __( 'Parent type ajut', 'text_domain' ),
 //		'parent_item_colon'          => __( 'Parent type ajut:', 'text_domain' ),
 //		'new_item_name'              => __( 'New type ajut', 'text_domain' ),
 //		'add_new_item'               => __( 'Add New type ajut', 'text_domain' ),
 //		'edit_item'                  => __( 'Edit type ajut', 'text_domain' ),
 //		'update_item'                => __( 'Update type of ajut', 'text_domain' ),
 //		'separate_items_with_commas' => __( 'Separate type ajut with commas', 'text_domain' ),
 //		'search_items'               => __( 'Search type of ajut', 'text_domain' ),
 //		'add_or_remove_items'        => __( 'Add or remove type of ajut', 'text_domain' ),
 //		'choose_from_most_used'      => __( 'Choose from the most used type of ajut', 'text_domain' ),
 //		'not_ajut'                  => __( 'Tipus d`ajut Not Ajut', 'text_domain' )
 //	);
 //  $rewrite = array(
 //		'slug'                       => 'ajut-tipus'
 //	);
 // //$capabilities =  array(
 // //	'manage_terms'				=> 'OficinaSuport Manage ajuts Tipus',
 // //	'edit_terms'				  => 'OficinaSuport Edit ajut Tipus',
 // //	'delete_terms'				=> 'OficinaSuport Delete ajuts Tipus',
 // //	'assign_terms'				=> 'OficinaSuport Assign ajuts Tipus',
 // //	);
 //	$args = array(
 //		'labels'              => $labels,
 //		'hierarchical'        => true,
 //		'public'              => true,
 //		'show_ui'             => true,
 //		'show_admin_column'   => true,
 //		'show_in_nav_menus'   => true,
 //		'show_tagcloud'       => false,
 //   'rewrite'             => $rewrite,
 // 	//'capabilities' 				=> $capabilities,
 // 	'map_meta_cap' 				=> true //neded to apply the new capabilities.

 //	);
 // register_taxonomy( 'ofisuport-ajut-category', array( 'ofisuport-ajut' ) , $args );
 //}

 // Hook into the 'init' action
 //add_action( 'init', 'ofisuport_wpplugin_ajut_category_register', 0 );

 // Register Custom Taxonomy Tag for Ajuts
// function ofisuport_wpplugin_ajut_tag_register() {
//
//  $labels = array(
//    'name'                       => _x( 'Ajuts Tags', 'Taxonomy General Name', 'text_domain' ),
//    'singular_name'              => _x( 'Ajut Tag', 'Taxonomy Singular Name', 'text_domain' ),
//    'menu_name'                  => __( 'Ajuts Tags', 'text_domain' ),
//    'all_items'                  => __( 'All ajuts tags', 'text_domain' ),
//    'parent_item'                => __( 'Parent ajut tag', 'text_domain' ),
//    'parent_item_colon'          => __( 'Parent ajut tag:', 'text_domain' ),
//    'new_item_name'              => __( 'New ajut tag', 'text_domain' ),
//    'add_new_item'               => __( 'Add New ajut tag', 'text_domain' ),
//    'edit_item'                  => __( 'Edit ajut tag', 'text_domain' ),
//    'update_item'                => __( 'Update ajut tag', 'text_domain' ),
//    'separate_items_with_commas' => __( 'Separate ajut tag with commas', 'text_domain' ),
//    'search_items'               => __( 'Search ajut tags', 'text_domain' ),
//    'add_or_remove_items'        => __( 'Add or remove ajut tag', 'text_domain' ),
//    'choose_from_most_used'      => __( 'Choose from the most used ajut tags', 'text_domain' ),
//    'not_ajut'                  => __( 'Ajut tag Not Ajut', 'text_domain' ),
//  );
//   $rewrite = array(
//    'slug'                       => 'ajut-tag',
//		// 'with_front'								 => false,
//  );
//	//$capabilities =  array(
//	//	'manage_terms'				=> 'OficinaSuport Manage ajuts Tag',
//	//	'edit_terms'				  => 'OficinaSuport Edit ajut Tag',
//	//	'delete_terms'				=> 'OficinaSuport Delete ajuts Tag',
//	//	'assign_terms'				=> 'OficinaSuport Assign ajuts Tag',
//	//	);
//
//  $args = array(
//    'labels'                     => $labels,
//    'hierarchical'               => false,
//    'public'                     => true,
//    'show_ui'                    => true,
//    'show_admin_column'          => true,
//    'show_in_nav_menus'          => true,
//    'show_tagcloud'              => true,
//    'rewrite'                    => $rewrite,
//		//'capabilities' 				=> $capabilities,
//		'map_meta_cap' 				=> true //neded to apply the new capabilities.
//  );
//  register_taxonomy( 'ofisuport-ajut-tag', array( 'ofisuport-ajut' ) , $args );
// }

 // Hook into the 'init' action
 //add_action( 'init', 'ofisuport_wpplugin_ajut_tag_register', 0 );

 // changing the permalink
 // https://wordpress.stackexchange.com/questions/108642/permalinks-custom-post-type-custom-taxonomy-post
 // change in register_post_type the slug 'rewrite' to this
 // $rewrite = array(
	//   'slug'                       => 'ajuts/%ofisuport-ajut-category%',
	//   'with_front'                 => true,
	//   'hierarchical'               => true,
 // );
 // uncomment below
 //
 // function ofisuport_wpplugin_ajut_and_category_permalink( $post_link, $id = 0 ){
 //     $post = get_post($id);
 //     if ( is_object( $post ) && $post->post_type == 'ofisuport-ajut' ){
 //         $terms = wp_get_object_terms( $post->ID, 'ofisuport-ajut-category' );
 //         if( $terms ){
 //             return str_replace( '%ofisuport-ajut-category%' , $terms[0]->slug , $post_link );
 //         }
 //     }
 //     return $post_link;
 // }
 // add_filter( 'post_type_link', 'ofisuport_wpplugin_ajut_and_category_permalink', 1, 2 );