# e2pdf/1.01.01/classes/extension/e2pdf-wordpress.php

E2Pdf – Export Pdf Tool for WordPress, version 1.01.01. 718 lines.

- Page: https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/extension/e2pdf-wordpress.php
- Raw: https://pluginprobe.com/plugins/e2pdf/1.01.01/raw/classes/extension/e2pdf-wordpress.php
- Modified: 2018-11-07T14:59:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/extension/e2pdf-wordpress.php#L10-L20`.

```php
<?php

/**
 * E2pdf Wordpress Extension
 * 
 * @copyright  Copyright 2017 https://e2pdf.com
 * @license    GPL v2
 * @version    1
 * @link       https://e2pdf.com
 * @since      0.00.01
 */
if (!defined('ABSPATH')) {
    die('Access denied.');
}

class Extension_E2pdf_Wordpress extends Model_E2pdf_Model {

    private $options;

    function __construct() {
        parent::__construct();
    }

    /**
     * Get info about extension
     * 
     * @return array() - Extension key and name
     */
    public function info() {
        return array(
            'wordpress' => __('Wordpress', 'e2pdf')
        );
    }

    /**
     * Check if needed plugin active
     * 
     * @return bool - Activated/Not Activated plugin
     */
    public function active() {
        return true;
    }

    /**
     * Check if export item function available
     * 
     * @return bool - Item export available/not available
     */
    public function export() {
        return false;
    }

    /**
     * Set option
     * 
     * @param string $attr - Key of option
     * @param string $value - Value of option
     * 
     * @return bool - Status of setting option
     */
    public function set($key, $value) {
        if (!isset($this->options)) {
            $this->options = new stdClass();
        }

        $this->options->$key = $value;
    }

    /**
     * Get option by key
     * 
     * @param string $key - Key to get assigned option value
     * 
     * @return mixed
     */
    public function get($key) {
        if (isset($this->options->$key)) {
            $value = $this->options->$key;
            return $value;
        } else {
            return false;
        }
    }

    /**
     * Get items to work with
     * 
     * @return array() - List of available items
     */
    public function get_items() {
        $content = array();

        $content[] = array(
            'key' => 'post',
            'value' => __('Wordpress Posts', 'e2pdf'),
        );
        $content[] = array(
            'key' => 'page',
            'value' => __('Wordpress Pages', 'e2pdf'),
        );

        return $content;
    }

    /**
     * Get entries for export
     * 
     * @param string $item - Item
     * @param string $name - Entries names
     * 
     * @return array() - Entries list
     */
    public function get_datasets($item = false, $name = false) {

        $datasets = array();

        if ($item) {
            if ($item == 'page') {
                $datasets_tmp = get_pages();
            } elseif ($item == 'post') {
                $datasets_tmp = get_posts();
            }

            if ($datasets_tmp) {
                foreach ($datasets_tmp as $key => $dataset) {
                    $this->set('item', $item);
                    $this->set('dataset', $dataset->ID);
                    $dataset_title = $this->render($name);
                    if (!$dataset_title) {
                        $dataset_title = $dataset->post_title;
                    }
                    $datasets[] = array(
                        'key' => $dataset->ID,
                        'value' => $dataset_title
                    );
                }
            }
        }

        return $datasets;
    }

    /**
     * Get dataset
     * 
     * @param int $dataset - Dataset ID
     * 
     * @return object - Dataset
     */
    public function get_dataset($dataset = false) {

        $dataset = (int) $dataset;

        if (!$dataset) {
            return;
        }

        $data = new stdClass();
        $data->url = $this->helper->get_url(array('post' => $dataset, 'action' => 'edit'), 'post.php?');

        return $data;
    }

    /**
     * Get item
     * 
     * @param string $item - Item
     * 
     * @return object - Item
     */
    public function get_item($item = false) {

        $form = new stdClass();
        if ($item == 'post') {
            $form->name = __('Wordpress Posts', 'e2pdf');
            $form->url = $this->helper->get_url(array('post_type' => $item), 'edit.php?');
        } elseif ($item == 'page') {
            $form->name = __('Wordpress Pages', 'e2pdf');
            $form->url = $this->helper->get_url(array('post_type' => $item), 'edit.php?');
        } else {
            $form->name = '';
            $form->url = 'javascript:void(0);';
        }

        return $form;
    }

    public function load_filters() {
        add_filter('the_content', array($this, 'filter_content'), 10, 2);
    }

    /**
     * Render value according to content
     * 
     * @param string $value - Content
     * @param string $type - Type of rendering value
     * @param array $field - Field details
     * 
     * @return string - Fully rendered value
     */
    public function render($value, $type = false, $field = array()) {

        $value = $this->render_shortcodes($value, $type, $field);
        $value = $this->strip_shortcodes($value);

        if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
            $option = $this->render($field['properties']['option']);
            $options = explode(', ', $value);
            $option_options = explode(', ', $option);
            if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
                return $option;
            } else {
                return "";
            }
        }

        return $value;
    }

    /**
     * Render shortcodes which available in this extension
     * 
     * @param string $value - Content
     * @param string $type - Type of rendering value
     * @param array $field - Field details
     * 
     * @return string - Value with rendered shortcodes
     */
    public function render_shortcodes($value, $type = false, $field = array()) {

        $dataset_id = $this->get('dataset');
        $item_id = $this->get('item');
        $post = get_post($dataset_id);

        $ext_shortcodes = array(
            "[id]" => $post->ID,
            "[post_author]" => get_userdata($post->post_author)->user_nicename,
            "[post_date]" => $post->post_date,
            "[post_date_gmt]" => $post->post_date_gmt,
            "[post_content]" => $post->post_content,
            "[post_title]" => $post->post_title,
            "[post_excerpt]" => $post->post_excerpt,
            "[post_status]" => $post->post_status,
            "[comment_status]" => $post->comment_status,
            "[ping_status]" => $post->ping_status,
            "[post_password]" => $post->post_password,
            "[post_name]" => $post->post_name,
            "[to_ping]" => $post->to_ping,
            "[pinged]" => $post->pinged,
            "[post_modified]" => $post->post_modified,
            "[post_modified_gmt]" => $post->post_modified_gmt,
            "[post_content_filtered]" => $post->post_content_filtered,
            "[post_parent]" => $post->post_parent,
            "[guid]" => $post->guid,
            "[menu_order]" => $post->menu_order,
            "[post_type]" => $post->post_type,
            "[post_mime_type]" => $post->post_mime_type,
            "[comment_count]" => $post->comment_count,
            "[filter]" => $post->filter
        );


        $post_content = false;

        if (false !== strpos($value, '[')) {

            $shortcode_tags = array(
                'meta',
            );
            preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
            $tagnames = array_intersect($shortcode_tags, $matches[1]);

            if (!empty($tagnames)) {
                $pattern = get_shortcode_regex($tagnames);

                preg_match_all("/$pattern/", $value, $shortcodes);

                foreach ($shortcodes[0] as $key => $shortcode_value) {
                    $shortcode = array();
                    $shortcode[1] = $shortcodes[1][$key];
                    $shortcode[2] = $shortcodes[2][$key];
                    $shortcode[3] = $shortcodes[3][$key];
                    $shortcode[4] = $shortcodes[4][$key];
                    $shortcode[5] = $shortcodes[5][$key];
                    $shortcode[6] = $shortcodes[6][$key];

                    $atts = shortcode_parse_atts($shortcode[3]);

                    if (array_key_exists('key', $atts)) {
                        $value = str_replace($shortcode_value, get_post_meta($post->ID, $atts['key'], true), $value);
                    }
                }
            }

            $shortcode_tags = array(
                'e2pdf-format-number',
                'e2pdf-format-date',
                'e2pdf-format-output',
            );
            preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
            $tagnames = array_intersect($shortcode_tags, $matches[1]);

            if (!empty($tagnames)) {

                $pattern = get_shortcode_regex($tagnames);
                preg_match_all("/$pattern/", $value, $shortcodes);
                foreach ($shortcodes[0] as $key => $shortcode_value) {
                    $shortcode = array();
                    $shortcode[1] = $shortcodes[1][$key];
                    $shortcode[2] = $shortcodes[2][$key];
                    $shortcode[3] = $shortcodes[3][$key];
                    $shortcode[4] = $shortcodes[4][$key];
                    $shortcode[5] = $shortcodes[5][$key];
                    $shortcode[6] = $shortcodes[6][$key];

                    if (isset($shortcode['5'])) {

                        if (strpos($shortcode['5'], "[post_content]") !== FALSE && !$this->get('no_filter') && !$post_content) {
                            $ext_shortcodes['[post_content]'] = $this->before_render_post_content($ext_shortcodes['[post_content]']);
                            $ext_shortcodes['[post_content]'] = apply_filters('the_content', $ext_shortcodes['[post_content]']);
                            $ext_shortcodes['[post_content]'] = str_replace("</p>", "</p>&nbsp;\r\n", $ext_shortcodes['[post_content]']);
                            $post_content = true;
                        }

                        $sub_value = str_replace(array_keys($ext_shortcodes), $ext_shortcodes, $shortcode['5']);
                        $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
                    }
                }
            }
        }

        $value = do_shortcode($value);


        if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
            $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
            if ($esig) {
                //process e-signature
                $value = "";
            } else {
                $helper_e2pdf_image = new Helper_E2pdf_Image();
                if (!$helper_e2pdf_image->get_image($value)) {
                    $value = $this->strip_shortcodes($value);
                    if (
                            $value &&
                            trim($value) != "" &&
                            extension_loaded('gd') &&
                            function_exists('imagettftext')
                    ) {
                        if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
                            $penColour = $this->helper->hexColorAllocate($field['properties']['text_color']);
                        } else {
                            $penColour = array(0x14, 0x53, 0x94);
                        }

                        $default_options = array(
                            'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
                            'bgColour' => 'transparent',
                            'penColour' => $penColour
                        );

                        $options = array();
                        $options = array_merge($default_options, $options);

                        $model_e2pdf_font = new Model_E2pdf_Font();

                        $font = false;
                        if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
                            $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
                        }
                        if (!$font) {
                            $font = $model_e2pdf_font->get_font_path('Noto Sans');
                        }

                        $size = 150;
                        if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
                            $size = $field['properties']['text_font_size'];
                        }

                        $model_e2pdf_signature = new Model_E2pdf_Signature();
                        $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
                    } else {
                        $value = "";
                    }
                }
            }
        }


        if (strpos($value, "[post_content]") !== FALSE && !$this->get('no_filter') && !$post_content) {
            $ext_shortcodes['[post_content]'] = $this->before_render_post_content($ext_shortcodes['[post_content]']);
            $ext_shortcodes['[post_content]'] = apply_filters('the_content', $ext_shortcodes['[post_content]']);
            $ext_shortcodes['[post_content]'] = str_replace("</p>", "</p>&nbsp;\r\n", $ext_shortcodes['[post_content]']);
            $post_content = true;
        }

        $value = str_replace(array_keys($ext_shortcodes), $ext_shortcodes, $value);

        return $value;
    }

    public function filter_custom_shortcodes() {
        
    }

    /**
     * Strip unused shortcodes
     * 
     * @param string $value - Content
     * 
     * @return string - Value with removed unused shortcodes
     */
    public function strip_shortcodes($value) {
        $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
        return $value;
    }

    public function auto() {
        $response = array();
        $elements = array();


        $post = $this->get('item');

        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => "<h1>[post_title]</h1>",
            )
        );


        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("Post name", 'e2pdf') . ": [post_name]",
            )
        );

        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("Post type", 'e2pdf') . ": [post_type]",
            )
        );


        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("ID", 'e2pdf') . ": [id]",
            )
        );

        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("Author", 'e2pdf') . ": [post_author]",
            )
        );



        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => "[post_content]",
                'dynamic_height' => '1',
                'height' => '300',
            )
        );


        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("Created", 'e2pdf') . ": [post_date]",
            )
        );

        $elements[] = array(
            'type' => 'e2pdf-html',
            'top' => '20',
            'left' => '20',
            'right' => '20',
            'block' => true,
            'properties' => array(
                'value' => __("Modified", 'e2pdf') . ": [post_modified]",
            )
        );


        $response['page'] = array(
            'bottom' => '20',
            'top' => '20',
        );



        $response['elements'] = $elements;
        return $response;
    }

    /**
     * Action before wordpress post/page render
     * Force to apply e2pdf shortcodes
     * 
     * @param string $content - Content
     * 
     * @return string - Content with update shortcodes
     */
    public function before_render_post_content($content) {

        if (false === strpos($content, '[')) {
            return $content;
        }


        $shortcode_tags = array(
            'e2pdf-exclude',
        );


        preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
        $tagnames = array_intersect($shortcode_tags, $matches[1]);


        if (!empty($tagnames)) {
            $pattern = get_shortcode_regex($tagnames);

            preg_match_all("/$pattern/", $content, $shortcodes);


            foreach ($shortcodes[0] as $key => $shortcode_value) {

                $shortcode = array();
                $shortcode[1] = $shortcodes[1][$key];
                $shortcode[2] = $shortcodes[2][$key];
                $shortcode[3] = $shortcodes[3][$key];
                $shortcode[4] = $shortcodes[4][$key];
                $shortcode[5] = $shortcodes[5][$key];
                $shortcode[6] = $shortcodes[6][$key];

                $shortcode[3] .= " apply='true'";
                $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
            }
        }


        return $content;
    }

    /**
     * Search and update shortcodes for this extension inside content
     * Auto set of dataset id
     * 
     * @param string $content - Content
     * 
     * @return string - Content with updated shortcodes
     */
    public function filter_content($content) {
        global $post;

        if (false === strpos($content, '[')) {
            return $content;
        }

        $shortcode_tags = array(
            'e2pdf-download',
            'e2pdf-view'
        );

        preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
        $tagnames = array_intersect($shortcode_tags, $matches[1]);

        if (!empty($tagnames)) {
            $pattern = get_shortcode_regex($tagnames);

            preg_match_all("/$pattern/", $content, $shortcodes);

            foreach ($shortcodes[0] as $key => $shortcode_value) {

                wp_reset_postdata();
                $extension = false;
                $item = false;

                $shortcode = array();
                $shortcode[1] = $shortcodes[1][$key];
                $shortcode[2] = $shortcodes[2][$key];
                $shortcode[3] = $shortcodes[3][$key];
                $shortcode[4] = $shortcodes[4][$key];
                $shortcode[5] = $shortcodes[5][$key];
                $shortcode[6] = $shortcodes[6][$key];

                $atts = shortcode_parse_atts($shortcode[3]);

                if (array_key_exists('id', $atts)) {
                    $template = new Model_E2pdf_Template();
                    $template->load($atts['id']);
                    if ($template->get('extension') === 'wordpress') {
                        $extension = $template->get('extension');
                        $item = $template->get('item');
                    }
                }

                if ($extension) {
                    if (!array_key_exists('dataset', $atts)) {
                        if (isset($post->ID)) {
                            $dataset_id = $post->ID;
                            $atts['dataset'] = $dataset_id;
                            $shortcode[3] .= " dataset='{$dataset_id}'";
                            $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
                        }
                    } else {
                        $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
                    }
                } elseif ($shortcode[2] === 'e2pdf-view') {
                    $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
                }
            }
        }

        return $content;
    }

    public function verify() {
        $item_id = $this->get('item');
        $dataset_id = $this->get('dataset');

        if ($item_id && $dataset_id && $item_id === get_post_type($dataset_id)) {
            return true;
        }

        return false;
    }

    public function visual_mapper() {

        $vc = "<h3>Wordpress</h3>";
        $vc .= "<div class='e2pdf-grid'>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("ID", "e2pdf"), 'id')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Author", "e2pdf"), 'post_author')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Date", "e2pdf"), 'post_date')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Date (GMT)", "e2pdf"), 'post_date_gmt')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Content", "e2pdf"), 'post_content')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Title", "e2pdf"), 'post_title')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Excerpt", "e2pdf"), 'post_excerpt')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Status", "e2pdf"), 'post_status')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Comment Status", "e2pdf"), 'comment_status')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Ping Status", "e2pdf"), 'ping_status')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Post Password", "e2pdf"), 'post_password')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Post Name", "e2pdf"), 'post_name')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("To Ping", "e2pdf"), 'to_ping')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Ping", "e2pdf"), 'pinged')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Modified Date", "e2pdf"), 'post_modified')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Modified Date (GMT)", "e2pdf"), 'post_modified_gmt')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Filtered Content", "e2pdf"), 'post_content_filtered')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Parent ID", "e2pdf"), 'post_parent')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("GUID", "e2pdf"), 'guid')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Menu Order", "e2pdf"), 'menu_order')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Type", "e2pdf"), 'post_type')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Mime Type", "e2pdf"), 'post_mime_type')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pr10'>{$this->get_vm_element(__("Comments Count", "e2pdf"), 'comment_count')}</div>";
        $vc .= "<div class='e2pdf-ib e2pdf-w50 e2pdf-pl10'>{$this->get_vm_element(__("Filter", "e2pdf"), 'filter')}</div>";
        $vc .= "</div>";
        return $vc;
    }

    private function get_vm_element($name, $id) {
        $element = "<div>";
        $element .= "<label>{$name}:</label>";
        $element .= "<input type='text' name='[{$id}]' value='[{$id}]'>";
        $element .= "</div>";
        return $element;
    }

    public function load_shortcodes() {
        add_shortcode('e2pdf-exclude', array($this, 'shortcode_e2pdf_exclude'));
    }

    function shortcode_e2pdf_exclude($atts = array(), $content = null) {
        if (is_array($atts) && array_key_exists('apply', $atts)) {
            return '';
        } else {
            return $content;
        }
    }

}

```
