<?php
namespace Elementor\LearnDash;

defined('ABSPATH') || exit;

class WKFE_PRO_LD_CPT_Hooks {
    public function __construct() {
        $this->alter_post_columns();
        add_action('init',[$this,'adding_shortcode']);
    }

    public function alter_post_columns() {
        add_filter( 'manage_wkfe-ld-template_posts_columns', [$this,'custom_edit_columns'] );
        add_action( 'manage_wkfe-ld-template_posts_custom_column', [$this,'custom_wkfe_ld_template_column'], 10, 2 );
    }

    public function custom_edit_columns($columns) {
        unset($columns['date']);

        $columns['wkfe-ld-template-activation'] = __( 'Activation', 'widgetkit-pro' );
        
        $columns['wkfe-ld-template-shortcode'] = __( 'Shortcode', 'widgetkit-pro' );

        $columns['date'] = __( 'Date', 'widgetkit-pro' );
    
        return $columns;
    }

    public function custom_wkfe_ld_template_column($columns, $post_id) {
        if( $columns == 'wkfe-ld-template-shortcode' ) {
            $shortcode = esc_attr( sprintf( '[%s id="%d"]', 'wkfe-ld-template', $post_id ) );
            printf( '<input type="text" readonly onfocus="this.select()" value="%s" />', $shortcode );
        }

        if( $columns == 'wkfe-ld-template-activation' ) {
            $value = get_post_meta($post_id,'_wkfe_ld_template_activation',true);
            if( $value ) {
                printf( '<span style="background-color:green;padding:5px;display:inline-block;color:#ffff;">%s</span>',esc_html__('Active','widgetkit-pro'));
            } else {
                printf( '<span style="background-color:red;padding:5px;display:inline-block;color:#ffff;">%s</span>',esc_html__('Inactive','widgetkit-pro'));
            }
        }
    }

    public function adding_shortcode() {
        add_shortcode( 'wkfe-ld-template', [ $this, 'shortcode' ] );
    }

    public function shortcode( $atts ) {
        if( empty( $atts['id'] ) ) {
            return '';
        }

        $elementor_instance = \Elementor\Plugin::instance();
        return $elementor_instance->frontend->get_builder_content_for_display( $atts['id'] );
    }
}

new WKFE_PRO_LD_CPT_Hooks();