<?php
/**
 * Plugin Name: eForms PDF
 * Description: eForms PDF Customizer is a helpful tool that helps you build and customize the PDF Templates for eForms.
 * Version: 3.3.2
 * Core Builder: 1.6.2
 * Requires PHP: 5.6
 * Author: add-ons.org
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
define( 'SUPERADDONS_PDF_E_FORMS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
require 'vendor/autoload.php';

if(!class_exists('Rednumber_Marketings_CRM_Init')) { 
    if(!defined('REDNUMBER_MARKETING_CRM_PLUGIN_PATH')) {
        define( 'REDNUMBER_MARKETING_CRM_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    }
    if(!defined('REDNUMBER_MARKETING_CRM_PLUGIN_URL')) {
        define( 'REDNUMBER_MARKETING_CRM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    }
    class Rednumber_Marketings_CRM_Init{
        function __construct(){
            register_activation_hook( __FILE__, array($this,'plugin_activation') );
            foreach (glob(REDNUMBER_MARKETING_CRM_PLUGIN_PATH."backend/*.php") as $filename){
                include $filename;
            }
        }
        function plugin_activation(){
            global $wpdb;
            include_once(ABSPATH.'wp-admin/includes/plugin.php');
            $table_name_log = $wpdb->prefix.'crm_logs';
            $table_name = $wpdb->prefix.'crm_marketings';
            if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) {
                $charset_collate = $wpdb->get_charset_collate();
                $sql = "CREATE TABLE $table_name (
                    id INT NOT NULL AUTO_INCREMENT,
                    type VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    form_id VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    datas TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    add_on VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    PRIMARY KEY  (id)
                ) $charset_collate;";
                $sql_log = "CREATE TABLE $table_name_log (
                    id INT NOT NULL AUTO_INCREMENT,
                    type VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    form_id VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    datas TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    add_on VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    action VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                    PRIMARY KEY  (id)
                ) $charset_collate;";
                require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
                dbDelta( $sql );
                dbDelta( $sql_log );
            }
        }
    }
    new Rednumber_Marketings_CRM_Init;
}
if(!class_exists('Rednumber_Marketing_CRM_Backend_Eform')) {  
    include SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH."modules/eform/backend.php"; 
}
if(!class_exists('Rednumber_Marketing_CRM_Frontend_PDF_Eform')) {  
   include SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH."modules/eform/pdf.php"; 
}
if(!class_exists('Rednumber_Marketing_CRM_PDF')) { 
    include SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH."includes/pdf.php"; 
}
if(!class_exists('PDF_Creator_Builder')) {
    if(!defined('SUPERADDONS_PDF_CREATOR_BUILDER_VERSION')) {
        define( 'SUPERADDONS_PDF_CREATOR_BUILDER_VERSION', "1.6.0" );
    }
    if(!defined('SUPERADDONS_PDF_CREATOR_BUILDER_PATH')) {
        define( 'SUPERADDONS_PDF_CREATOR_BUILDER_PATH', plugin_dir_path( __FILE__ )."pdf/" );
    }
    if(!defined('SUPERADDONS_PDF_CREATOR_BUILDER_URL')) {
        define( 'SUPERADDONS_PDF_CREATOR_BUILDER_URL', plugin_dir_url( __FILE__ )."pdf/" );
    }
     class Superaddons_Pdf_Creator_Builder {
        function __construct(){
            $dir = new RecursiveDirectoryIterator(SUPERADDONS_PDF_CREATOR_BUILDER_PATH."backend");
            $ite = new RecursiveIteratorIterator($dir);
            $files = new RegexIterator($ite, "/\.php/", RegexIterator::MATCH);
            foreach ($files as $file) {
                if (!$file->isDir()){
                    require_once $file->getPathname();
                }
            }
            include_once SUPERADDONS_PDF_CREATOR_BUILDER_PATH."libs/phpqrcode.php";
            include_once SUPERADDONS_PDF_CREATOR_BUILDER_PATH."frontend/index.php";
        }
    }
    new Superaddons_Pdf_Creator_Builder;
}

class Superaddons_Pdf_Creator_Eforms_Builder { 
    function __construct(){
        include SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH."pdf/eforms/index.php";
        include SUPERADDONS_PDF_E_FORMS_PLUGIN_PATH."yeekit/document.php";
        add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this,'add_link') );
        register_activation_hook( __FILE__, array($this,'activation') );
    }
    function add_link( $actions ) {
       $actions[] = '<a target="_blank" href="https://pdf.add-ons.org/document/" target="_blank">'.esc_html__( "Document", "pdf-for-wpforms" ).'</a>';
       $actions[] = '<a target="_blank" href="https://add-ons.org/supports/" target="_blank">'.esc_html__( "Supports", "pdf-for-wpforms" ).'</a>';
       return $actions;
    }
    function activation() {
        global $wpdb;
       $check = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'pdf_template'" );
       if( $check < 1 ){            
           $data = file_get_contents(SUPERADDONS_PDF_CREATOR_BUILDER_URL."backend/demo/form-import.json");
            $my_template = array(
              'post_title'    => "PDF Template Demo",
              'post_content'  => "",
              'post_status'   => 'publish',
              'post_type'     => 'pdf_template'
            );
            $id_template = wp_insert_post( $my_template );
            add_post_meta($id_template,"data_email",$data);      
            add_post_meta($id_template,"_builder_pdf_settings_font_family",'dejavu sans');
            $settings = array("dpi"=>96,"orientation"=>"portrait","size"=>"A4","show_page"=>"","footer"=>"","css"=>"");      
            add_post_meta($id_template,"_builder_pdf_settings",$settings);      
       } 
    }
}
new Superaddons_Pdf_Creator_Eforms_Builder;