<?php
/**
 * Registrar un custom post type
 *
 * @see get_post_type_labels().
 */

function TkNa_free_post_type() {
    $icon = 'PHN2ZyBpZD0ibWVudSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB2aWV3Qm94PSIwIDAgMTE5LjgxIDk4LjMzIj48ZGVmcz48c3R5bGU+LmNscy0xe2ZpbGw6I2ZmZjtmaWxsLXJ1bGU6ZXZlbm9kZDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPnRhcm9raW5hLW1lbnU8L3RpdGxlPjxwYXRoIGNsYXNzPSJjbHMtMSIgZD0iTTExNi4xNSw2Ni4zOUM5OS45Myw3NC4yMyw4My42NSw4Miw2Ny41LDkwYTM2LjQyLDM2LjQyLDAsMCwxLTI2LjcsMi43MmMtMTEuMzctMy4wOC0yMi44LTYtMzQuMTctOUE1LjkzLDUuOTMsMCwwLDAsLjgyLDg1YzEuMjYuMzcsMi4yOS42OSwzLjMyLDEsMTUuMzksNCwzMC43OCw4LDQ2LjE1LDEyLjEzYTcsNywwLDAsMCw1LjE3LS41NFE4My42LDgzLjg0LDExMS43OCw3MC4yOGMyLjU0LTEuMjIsNS0yLjQ4LDcuOS0zLjg3QTMuMTMsMy4xMywwLDAsMCwxMTYuMTUsNjYuMzlaIi8+PHBhdGggY2xhc3M9ImNscy0xIiBkPSJNNDIuMjEsNjEuODVjNC44OCwxLjU0LDguNzcsMS4yMywxMy4xNy0xLjQ3LDYtMy42OCwxMi03LjIzLDE2LjYzLTEyLjc3LDcuNjQtOS4xLDEwLjg0LTE5LjUxLDguNDItMzEuNTZhMy4yOCwzLjI4LDAsMCwwLTIuNjEtMi44M0M2My4zLDksNDguOCw0LjcsMzQuMjkuNDNjLTIuNi0uNzYtMi42LS43NC0yLjE5LDJhMzQuMTIsMzQuMTIsMCwwLDEtMi44NiwyMC40MkE1MC4zNyw1MC4zNywwLDAsMSw0LjMxLDQ3LjEzYy0xLjQzLjY2LTIuODcsMS4zLTQuMzEsMS45NWwuMTQuNTNjLjU0LjE3LDEuMDguMzYsMS42My41MkMxNS4yNiw1NCwyOC44Myw1Ny42MSw0Mi4yMSw2MS44NVoiLz48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MC41Myw3NC42NmE2LjEsNi4xLDAsMCwwLDQuNTctLjQ0UTgwLjE5LDYyLDEwNS4zNSw0OS45MUwxMTkuNTcsNDNjLS41Mi0uMzYtLjY3LS41NC0uODYtLjU5LTEwLjA3LTIuNjctMjAuMTQtNS4zLTMwLjItOEM4NywzNCw4Ni42NSwzNC44Miw4Ni4yNiwzNiw4Mi4xNCw0OC43OSw3My44Nyw1Ny44NSw2Mi44LDY0LjM2Yy0zLjIxLDEuODgtNi41MiwzLjU3LTkuNyw1LjQ5YTcuNCw3LjQsMCwwLDEtNi4zOS44M0MzNC4wOCw2NywyMS4zOSw2My40Nyw4Ljc4LDU5LjY3Yy0zLjEtLjk0LTUuMzYuMS04LjExLDEuODdsMy41LjkzQzE5LjYzLDY2LjUyLDM1LjA5LDcwLjU1LDUwLjUzLDc0LjY2WiIvPjxwYXRoIGNsYXNzPSJjbHMtMSIgZD0iTTExMy43Myw1NC45MUM5OC4zMSw2Mi40Miw4Mi44MSw2OS43Myw2Ny40NCw3Ny4zNUEzNi4yMSwzNi4yMSwwLDAsMSw0MSw4MC4wOGMtMTAtMi43LTIwLTUuMDgtMjkuOTUtOC0zLjg0LTEuMTQtNi44My0uMTMtMTAuMjUsMiwuNjkuMjQsMSwuMzksMS4zNi40OHEyNC4yMyw2LjM1LDQ4LjQzLDEyLjczYTYuMDgsNi4wOCwwLDAsMCw0LjU2LS40NVE4My41Myw3My4wNiwxMTIsNTkuMzRjMi40NS0xLjE4LDQuODktMi4zOCw3LjgxLTMuODFDMTE3LjQ0LDU0LjczLDExNS43Nyw1My45MiwxMTMuNzMsNTQuOTFaIi8+PC9zdmc+';
    $labelsPT = array(
        'name'                  => _x( 'Cards', 'General name', 'tarokina-free' ),
        'singular_name'         => _x( 'Card', 'Singular name', 'tarokina-free' ),
        'menu_name'             => _x( 'Tarokina free', 'Admin Menu text', 'tarokina-free' ),
        'name_admin_bar'        => _x( 'Add Card', 'Add New on Toolbar', 'tarokina-free' ),
        'add_new'               => __( 'Add Card', 'tarokina-free' ),
        'add_new_item'          => __( 'Add New Card', 'tarokina-free' ),
        'new_item'              => __( 'New Card', 'tarokina-free' ),
        'edit_item'             => __( 'Edit Card', 'tarokina-free' ),
        'view_item'             => __( 'View Card', 'tarokina-free' ),
        'all_items'             => __( 'Cards', 'tarokina-free' ),
        'search_items'          => __( 'Search', 'tarokina-free' ),
        'not_found'             => __( 'No Cards. Click on the install button on the tarot page.', 'tarokina-free' ).'&nbsp;&nbsp;<a style=" text-decoration:underline;font-weight:bold" href="'.admin_url( 'edit.php?post_type=tarokkina_free&page=crb_carbon_fields_container_tarot_free.php' ).'">'.__( 'Go to install', 'tarokina-free' ).'</a>',
        'not_found_in_trash'    => __( 'No Cards found in Trash.', 'tarokina-free' ),
        'featured_image'        => __( 'Tarot card image', 'tarokina-free' ),
        'set_featured_image'    => __( 'Set cover image', 'tarokina-free' ),
        'remove_featured_image' => __( 'Remove cover image', 'tarokina-free' ),
        'use_featured_image'    => __( 'Use as cover image', 'tarokina-free' ),
        'archives'              => __( 'Card archives', 'tarokina-free' ),
        'insert_into_item'      => __( 'Insert into Card', 'tarokina-free' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Card', 'tarokina-free' ),
        'filter_items_list'     => __( 'Filter Cards list', 'tarokina-free' ),
        'items_list_navigation' => __( 'Cards list navigation', 'tarokina-free' ),
        'items_list'            => __( 'Cards list', 'tarokina-free' )
    );

    $argsPT = array(
        'labels'             => $labelsPT,
        'public'             => false,
        'publicly_queryable' => false,
        'has_archive'        => true,
        'map_meta_cap'       => true,
        'capability_type'    => 'post',
        'capabilities' => array(
          'create_posts'       => 'do_not_allow',
        ),
        '_edit_link'         => 'post.php?post_type=tarokkina_free&post=%d',
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'Card_tarokkina_free' ),
        'hierarchical'       => false,
        'menu_position'      => null,
        'menu_icon'          => 'data:image/svg+xml;base64,'.$icon,
        'show_in_rest'       => false,
        'supports'           => array('title','thumbnail')
    );


    if (current_user_can('administrator')) {
      register_post_type( 'tarokkina_free', $argsPT );
    }



  // Taxonomía Categoría
    $labelsCat1 = array(
      'name'              => _x( 'Decks', 'taxonomy general name', 'tarokina-free' ),
      'singular_name'     => _x( 'Deck', 'taxonomy singular name', 'tarokina-free' ),
      'search_items'      => __( 'Search', 'tarokina-free' ),
      'all_items'         => __( 'All Decks', 'tarokina-free' ),
      'edit_item'         => __( 'Edit deck', 'tarokina-free' ),
      'update_item'       => __( 'Updated deck', 'tarokina-free' ),
      'add_new_item'      => __( 'Add new deck', 'tarokina-free' ),
      'new_item_name'     => __( 'New deck', 'tarokina-free' ),
      'menu_name'         => __( 'Decks', 'tarokina-free' ),
      'not_found'         => __( 'No decks found', 'tarokina-free' )
    );

    $argsCat1 = array(
      'public'             => false,
      'publicly_queryable' => false,
      'hierarchical'      => true,
      'labels'            => $labelsCat1,
      'show_ui'           => true,
      'show_in_menu'       => false,
      'show_admin_column' => true,
      'query_var'         => true,
      'show_in_rest'       => false,
      'rewrite'           => array( 'slug' => 'decks','with_front' => false )
    );

  register_taxonomy( 'tarokkina_free-cat', array( 'tarokkina_free' ), $argsCat1 );



  // quitar filtros
  add_filter('months_dropdown_results', '__return_empty_array');
  //add_filter( 'bulk_actions-' . 'edit-tarokkina_free', '__return_empty_array' );

  add_filter( 'bulk_actions-edit-tarokkina_free', 'TkNa_free_bulk_actions' );
  function TkNa_free_bulk_actions( $actions ){
  unset( $actions[ 'trash' ] );
  return $actions;
  }


  // Imagenes
  add_image_size( 'tarokkina_free-mini', 170 );


  // Columnas en el post type
  add_filter( 'manage_tarokkina_free_posts_columns', 'TkNa_free_filter_posts_columns' );
  function TkNa_free_filter_posts_columns( $columns ) {
    $columns['image'] = __( 'image','tarokina-free' );
    return $columns;
  }



  add_filter( 'manage_tarokkina_free_posts_columns', 'TkNa_free_realestate_columns' );
  function TkNa_free_realestate_columns( $columns ) {

      $columns = array(
        'cb' => $columns['cb'],
        'image' => __( '' ,'tarokina-free' ),
        'title' => __( 'Card','tarokina-free' ),
        'taxonomy-tarokkina_free-cat' => __( 'Deck','tarokina-free' )
      );

    return $columns;
  }



  add_action( 'manage_tarokkina_free_posts_custom_column', 'TkNa_free_realestate_column', 10, 2);
  function TkNa_free_realestate_column( $column, $post_id ) {

    // Image column /  Cambiar tamaño sin imagen
    if ( 'image' === $column ) {
      $imgPT =  get_the_post_thumbnail( $post_id, 'tarokkina_free-mini');
      $imageT = (!empty($imgPT)) ? $imgPT : '<div class="no-image"></div>';
      $arr = array(
          'img' => array(
              'src' => array(),
              'width' => array(),
              'height' => array()
          ),
          'div' => array(
            'class' => array()
          )
      );

      echo '<a href="'.admin_url('post.php?post_type=tarokkina_free&post='.$post_id.'&action=edit').'">'.wp_kses( $imageT, $arr ).'</a>';

    }

  }


  /**
   * Remove columns from category-cat
   *
   */
  add_filter('manage_edit-tarokkina_free-cat_columns', function ( $columns ) {
    $columns['name'] = __( 'Decks','tarokina-free' );
    $columns['posts'] = __( 'Cards','tarokina-free' );
    if( isset( $columns['description'] ) || isset( $columns['slug'] ) )
        unset( $columns['description'] );
        unset( $columns['slug'] );
    return $columns;
  });


  // CSS TABLA
  add_action('admin_head', 'TkNa_free_cssTable');
  function TkNa_free_cssTable() {
      global $post_type;
      if ( 'tarokkina_free' == $post_type ) {
          ?><style>
          .column-image {width: 56px !important;}
          .column-image img {width: 100% !important; height:auto !important}
          @media only all and (max-width:853px) { 
              .column-taxonomy-tarokkina_free-cat,
              .column-image {
                  display: none !important;
              }
          }
          </style><?php
      }
  }


}

// Post Type
add_action( 'init','TkNa_free_post_type');



add_filter( 'post_row_actions', 'TkNa_free_actions1', 10, 2 );
function TkNa_free_actions1( $actions, $post ) {
    if( $post->post_type === 'tarokkina_free' ) {
        unset( $actions['clone'] );
        unset( $actions['trash'] );
    }
    return $actions;
}

add_action('wp_trash_post', 'TkNa_free_actions2');
function TkNa_free_actions2($post_id) {
    if( get_post_type($post_id) === 'tarokkina_free' ) {
      wp_die('The post you were trying to delete is protected.');
    }
}


add_filter( 'tag_row_actions', 'TkNa_free_actions3', 10, 2 );
function TkNa_free_actions3( $actions, $term ) {
    if ( 'tarokkina_free-cat' === $term->taxonomy ) {
        unset( $actions['delete'] );
    }
    return $actions;
}

add_action( 'pre_delete_term', 'TkNa_free_actions4', 10, 2 );
function TkNa_free_actions4( $term, $taxonomy ) {
    if ( 'tarokkina_free-cat' === $taxonomy ) {
        wp_die( 'The taxonomy you were trying to delete is protected.' );
    }
}

add_action( 'admin_head', function () {
    $current_screen = get_current_screen();


    if ( 'post' === $current_screen->base &&
    'tarokkina_free' === $current_screen->post_type ) :
    ?>
        <style>#delete-action { display: none; }</style>
    <?php
    endif;


    if ( 'term' === $current_screen->base &&
    'tarokkina_free-cat' === $current_screen->taxonomy ) :
    ?>
        <style>#delete-link { display: none; }</style>
    <?php
    endif;
} );

