<?php

trait PostType {

    public function register_post_type() {

        $labels = array(
            'name'                  => _x( 'Tranzix Templates', 'Post Type General Name', 'tranzix' ),
            'singular_name'         => _x( 'Tranzix Template', 'Post Type Singular Name', 'tranzix' ),
            'menu_name'             => __( 'Tranzix Templates', 'tranzix' ),
            'name_admin_bar'        => __( 'Tranzix Templates', 'tranzix' ),
            'archives'              => __( 'List Archives', 'tranzix' ),
            'parent_item_colon'     => __( 'Parent List:', 'tranzix' ),
            'all_items'             => __( 'All Tranzix Templates', 'tranzix' ),
            'add_new_item'          => __( 'Add New  Template', 'tranzix' ),
            'add_new'               => __( 'Add New', 'tranzix' ),
            'new_item'              => __( 'New Template', 'tranzix' ),
            'edit_item'             => __( 'Edit Template', 'tranzix' ),
            'update_item'           => __( 'Update  Template', 'tranzix' ),
            'view_item'             => __( 'View Tranzix Template', 'tranzix' ),
            'search_items'          => __( 'Search Tranzix Template', 'tranzix' ),
            'not_found'             => __( 'Not found', 'tranzix' ),
            'not_found_in_trash'    => __( 'Not found in Trash', 'tranzix' )
        );
        $args = array(
            'label'                 => __( 'Post List', 'tranzix' ),
            'labels'                => $labels,
            'supports'              => array( 'title','editor' ),
            'public'                => true,
            'rewrite'               => false,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'show_in_nav_menus'     => false,
            'exclude_from_search'   => true,
            'capability_type'       => 'post',
            'hierarchical'          => false,
              'menu_icon' => 'dashicons-image-rotate-right',
		
			'menu_position' => 60
        );
        register_post_type( 'mr_global_templates', $args );

        add_post_type_support( 'mr_global_templates', 'elementor' );
    }
}

class MyPlugin {

    use PostType;

    public function __construct() {
        add_action( 'init', [ $this, 'register_post_type' ], 60);
    }
}

new MyPlugin();



use Elementor\Plugin;

class MrShortcode{

    private static $_instance = null;

    public static function instance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    private function __construct(){

        add_shortcode('TRANZIX_SHORTCODE', [$this, 'render_shortcode']);

        add_filter( 'widget_text', 'do_shortcode' );
    }

    public function render_shortcode($atts){
        
        $language_support = apply_filters('mr_multilingual_support', false);

        if(!class_exists('Elementor\Plugin')){
            return '';
        }
        if(!isset($atts['id']) || empty($atts['id'])){
            return '';
        }

        $post_id = $atts['id'];

        if($language_support){
            $post_id = apply_filters( 'wpml_object_id', $post_id, 'mr_global_templates' );
        }

        $response = Plugin::instance()->frontend->get_builder_content_for_display($post_id);
        return $response;
    }

}

MrShortcode::instance();



class MrMetaBoxes{

    private static $_instance = null;

    public static function instance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    private function __construct(){

        add_action("add_meta_boxes", [$this, 'add_meta_boxes']);

        add_filter( 'manage_mr_global_templates_posts_columns', [$this, 'add_column'] );
        add_action('manage_mr_global_templates_posts_custom_column', [$this, 'column_data'], 10, 2);
    }

    public function add_meta_boxes(){
        add_meta_box('mr-shortcode-box','Tranzix Shortcode Area',[$this, 'mr_shortcode_box'],'mr_global_templates','side','high');  
    }

    function mr_shortcode_box($post){
        ?>



  <h4 style="font-size: 16px; font-weight: bold; margin-bottom: 10px;">Dinamic Shortcode</h4>
        <input type='text' class='widefat' style="background-color: #f7f7f7; border: none; color: #555; font-size: 14px; font-weight: normal; padding: 8px; margin-bottom: 20px;" value='[TRANZIX_SHORTCODE id="<?php echo $post->ID; ?>"]' readonly="">
    

        <h4 style="font-size: 16px; font-weight: bold; margin-bottom: 10px;">Classic PHP Code</h4>
 		<input type='text' class='widefat' style="background-color: #f7f7f7; border: none; color: #555; font-size: 14px; font-weight: normal; padding: 8px;" value="&lt;?php echo do_shortcode('[TRANZIX_SHORTCODE id=&quot;<?php echo $post->ID; ?>&quot;]'); ?&gt;" readonly="">



        <?php
    }
	
	

    function add_column($columns){
        $columns['mr_post_column'] = __( 'Tranzix Shortcode', 'tranzix' );
        return $columns;
    }

    function column_data($column, $post_id){
        switch ( $column ) {

            case 'mr_post_column' :
                echo '<input type="text" class="widefat" value=\'[TRANZIX_SHORTCODE id="'.$post_id.'"]\' readonly="">';
                break;
        }
    }
	
	

}

MrMetaBoxes::instance();





//This code for Slider loop


 function tranzix_elementor_template_($type = null) {
        $args = [
            'post_type' => 'elementor_library',
            'posts_per_page' => -1,
        ];
        if ($type) {
            $args['tax_query'] = [
                [
                    'taxonomy' => 'elementor_library_type',
                    'field' => 'slug',
                    'terms' => $type,
                ],
            ];
        }
        $template = get_posts($args);
        $tpl = array();
        if (!empty($template) && !is_wp_error($template)) {
            foreach ($template as $post) {
                $tpl[$post->post_name] = $post->post_title;
            }
        }
        return $tpl;
    } 


    function tranzix_elemntor_content( $slug ) {
  
        $content_post = get_posts(array(
            'name' => $slug,
            'posts_per_page' => 1,
            'post_type' => 'elementor_library',
            'post_status' => 'publish'
        ));
        if (array_key_exists(0, $content_post) == true) {
            $id = $content_post[0]->ID;
            return $id;
        }
    }







