*/ use Dompdf\Dompdf; use Dompdf\Options; class Advanced_Pdf_Generator_Public { /** * The ID of this plugin. * * @since 0.1.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $advanced_pdf_generator; /** * The version of this plugin. * * @since 0.1.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 0.2.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { ob_start(); $this->advanced_pdf_generator = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 0.3.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Plugin_Name_Loader as all of the hooks are defined * in that particular class. * * The Plugin_Name_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->advanced_pdf_generator, plugin_dir_url( __FILE__ ) . 'css/advanced-pdf-generator-public.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->advanced_pdf_generator.'-swal', 'https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.11.4/sweetalert2.min.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 0.3.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Plugin_Name_Loader as all of the hooks are defined * in that particular class. * * The Plugin_Name_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->advanced_pdf_generator.'-swal', 'https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.11.4/sweetalert2.all.min.js', array(), $this->version, false ); wp_enqueue_script( $this->advanced_pdf_generator, plugin_dir_url( __FILE__ ) . 'js/advanced-pdf-generator-public.js', array( 'jquery' ), $this->version, false ); } /** * Register shortcodes for the public-facing side of the site. * * @since 0.1.0 */ public function register_shortcodes() { add_shortcode( $this->advanced_pdf_generator, array($this, 'advanced_pdf_generator_shortcode') ); } /** * Shortcode function for advanced-pdf-generator tag * * @since 0.4.0 * */ public function advanced_pdf_generator_shortcode( $atts = [] ) { global $wpdb; $table_apdfg_values = $wpdb->prefix . 'apdfg_values'; $values = $wpdb->get_row( "SELECT * FROM $table_apdfg_values LIMIT 1", OBJECT ); $content = "
Template file not found at theme_path.../$this->advanced_pdf_generator/apg-templates/pdf.php
"; } $dompdf->loadHtml($template); $dompdf->render(); $output = $dompdf->output(); ob_start(); if(!session_id()) { session_start(); } $random_str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(10/strlen($x)) )),1,10); $file_name = session_id(). '_' . $random_str .'.pdf'; file_put_contents($uploads['basedir'].'/advanced-pdf-generator/'.$file_name, $output); $file_url = $uploads['baseurl'].'/advanced-pdf-generator/'.$file_name; return $file_url; } /** * Load template file to generate PDF * * @since 0.1.0 * */ public function get_template_file($filename, $name=null, $file_url=null) { if (is_file($filename)) { ob_start(); require $filename; return ob_get_clean(); } return false; } /** * Send email if send options is enabled and triggered * * @since 0.3.0 * */ public function send_email() { $name = sanitize_text_field( $_POST["names"] ); $email = sanitize_email( $_POST["email"] ); $file_url = esc_url( $_POST["file_url"] ); $to = $email; $headers = array('Content-Type: text/html; charset=UTF-8'); $template = $this->get_template_file( get_template_directory() . '/apg-templates/mail.php', $name, $file_url ); if(!$template) { $template = "Hello $name,
Here is your PDF file: $file_url