false, 'html' => '
'. esc_html__('Have error when load html from URL', 'wpmf') .'
')); } $width = (!empty($_REQUEST['width'])) ? (int)$_REQUEST['width'] : ''; $height = (!empty($_REQUEST['height'])) ? (int)$_REQUEST['height'] : ''; $html = do_shortcode('[wpmfpdf id="'. (int)$_REQUEST['id'] .'" width="'. $width .'" height="'. $height .'" embed="'. $_REQUEST['embed'] .'" target="'. $_REQUEST['target'] .'"]'); wp_send_json(array('status' => true, 'html' => $html)); } /** * Load file design html * * @return void */ public function loadPdfEmbedHtml() { if (empty($_REQUEST['et_admin_load_nonce']) || !wp_verify_nonce($_REQUEST['et_admin_load_nonce'], 'et_admin_load_nonce')) { wp_send_json(array('status' => false, 'html' => ''. esc_html__('Have error when load html from URL', 'wpmf') .'
')); } if (empty($_REQUEST['url'])) { wp_send_json(array('status' => false, 'html' => ''. esc_html__('Have error when load html from URL', 'wpmf') .'
')); } $url = str_replace(array('-pdf.jpg', '-pdf.jpeg', '-pdf.png'), '.pdf', $_REQUEST['url']); if (strpos($url, 'admin-ajax.php?action=wpmf') !== false) { if (!empty($_REQUEST['id'])) { $url .= '&id=' . $_REQUEST['id'] . '&dl=0'; } } $width = (!empty($_REQUEST['width']) && $_REQUEST['width'] !== 'auto') ? $_REQUEST['width'] : '100%'; $height = (!empty($_REQUEST['height']) && $_REQUEST['height'] !== 'auto') ? $_REQUEST['height'] : '800'; $html = do_shortcode('[wpmfpdf is_divi="1" url="'. $url .'" width="'. $width .'" height="'. $height .'" embed="'. $_REQUEST['embed'] .'" target="'. $_REQUEST['target'] .'"]'); wp_send_json(array('status' => true, 'html' => $html)); } /** * Load elementor widget * * @return void */ public function loadElementorWidget() { require_once(WP_MEDIA_FOLDER_PLUGIN_DIR . 'class/elementor-widgets/class-pdf-embed-elementor-widget.php'); if (defined('ELEMENTOR_VERSION') && version_compare(ELEMENTOR_VERSION, '3.5', '<')) { \Elementor\Plugin::instance()->widgets_manager->register_widget_type(new \WpmfPdfEmbedElementorWidget()); } else { \Elementor\Plugin::instance()->widgets_manager->register(new \WpmfPdfEmbedElementorWidget()); } } /** * Enqueue styles and scripts for gutenberg * * @return void */ public function addEditorAssets() { wp_enqueue_script( 'wpmf_pdf_blocks', WPMF_PLUGIN_URL . 'assets/js/blocks/pdfembed/block.js', array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-data', 'wp-block-editor'), WPMF_VERSION ); $this->registerScript(); $this->enqueue(); $params = array( 'l18n' => array( 'block_pdf_title' => __('WP Media Folder PDF Embed', 'wpmf'), ), 'vars' => array( 'wpmf_nonce' => wp_create_nonce('wpmf_nonce'), 'ajaxurl' => admin_url('admin-ajax.php') ) ); wp_localize_script('wpmf_pdf_blocks', 'wpmf_pdf_blocks', $params); } /** * Load PDF script * * @param string $content Content * * @return string */ public function theContent($content) { if (strpos($content, 'wpmf-pdfemb-viewer') || strpos($content, 'data-wpmf_pdf_embed="embed"') || strpos($content, 'wpmfpdf')) { $this->enqueue(); } return $content; } /** * Render PDF embed * * @param array $attrs Shortcode params * * @return string */ public function wpmfPdf($attrs) { if (!empty($attrs['url'])) { $url = $attrs['url']; $title = basename($attrs['url']); } else { if (empty($attrs['id'])) { return ''; } $url = wp_get_attachment_url($attrs['id']); $pdf = get_post($attrs['id']); if (empty($pdf)) { return ''; } $cloud_type = get_post_meta((int)$attrs['id'], 'wpmf_drive_type', true); $drive_id = get_post_meta((int)$attrs['id'], 'wpmf_drive_id', true); $baseUrl = admin_url('admin-ajax.php'); switch ($cloud_type) { case 'dropbox': $action = '?action=wpmf-dbxdownload-file&id=' . urlencode($drive_id) . '&link=true&dl=0'; break; case 'onedrive': $action = '?action=wpmf_onedrive_download&id=' . urlencode($drive_id) . '&link=true&dl=0'; break; case 'onedrive_business': $action = '?action=wpmf_onedrive_business_download&id=' . urlencode($drive_id) . '&link=true&dl=0'; break; case 'google_drive': $action = '?action=wpmf-download-file&id=' . urlencode($drive_id) . '&link=true&dl=0'; break; case 'nextcloud': $action = '?action=wpmf_nextcloud_get_content&url=' . urlencode($url) . '/download'; break; default: $action = ''; $offload_infos = get_post_meta($attrs['id'], 'wpmf_awsS3_info', true); if (!empty($offload_infos)) { $action = '?action=wpmf_offload_get_content&url=' . urlencode($url); } break; } if ($action) { $url = $baseUrl . $action; } $title = $pdf->post_title; } if (!empty($attrs['embed']) && $attrs['embed'] !== 'off') { if (empty($attrs['is_divi'])) { $this->enqueue(); } $width = (!empty($attrs['width']) && strpos($attrs['width'], '%') === false) ? $attrs['width'] : '100%'; $height = (!empty($attrs['height']) && strpos($attrs['height'], '%') === false) ? $attrs['height'] : '800'; $download = (!empty($attrs['download'])) ? $attrs['download'] : 'off'; $plugin_dir_url = plugin_dir_url(__DIR__); $viewer_base_url = $plugin_dir_url . 'class/templates/pdf-embed.php'; $response = wp_remote_head($url); if (is_wp_error($response)) { $url = $plugin_dir_url . 'assets/pdf-loading-error.pdf'; } $status_code = wp_remote_retrieve_response_code($response); if ($status_code === 404) { $url = $plugin_dir_url . 'assets/pdf-loading-error.pdf'; } $attachment_info = '?file=' . urlencode($url); $final_url = $viewer_base_url . $attachment_info . '&plugins_url=' . urlencode($plugin_dir_url); $return = ''; } else { $return = ''.esc_html($title).''; } return $return; } /** * Load styles and scripts * * @return void */ public function loadStyleScript() { global $post; if (!empty($post)) { $is_elementor = get_post_meta($post->ID, '_elementor_edit_mode', true); if ($is_elementor === 'builder') { $_elementor_data = get_post_meta($post->ID, '_elementor_data', true); if (!is_string($_elementor_data)) { $_elementor_data = json_encode($_elementor_data); } if (strpos($_elementor_data, 'wpmf-pdfemb-viewer') || strpos($_elementor_data, 'data-wpmf_pdf_embed="embed"') || strpos($_elementor_data, 'wpmfpdf')) { $this->enqueue(); } } else { if (strpos($post->post_content, 'wpmf-pdfemb-viewer') || strpos($post->post_content, 'data-wpmf_pdf_embed="embed"') || strpos($post->post_content, 'wpmfpdf')) { $this->enqueue(); } } } } /** * Load script and style * * @return void */ public function enqueue() { wp_enqueue_script('wpmf_compat_js'); } /** * Localize a script. * Works only if the script has already been added. * * @return array */ public function getTranslation() { $array = array( // 'worker_src' => plugins_url('assets/js/pdf-embed/pdf.worker.min.js', dirname(__FILE__)), 'worker_src' => plugins_url('assets/js/pdf-embed/pdf.worker.js', dirname(__FILE__)), 'cmap_url' => plugins_url('assets/js/pdf-embed/cmaps/', dirname(__FILE__)), 'pdf_sandbox'=> plugins_url('assets/js/pdf-embed/pdf.sandbox.js', dirname(__FILE__)), 'objectL10n' => array( 'loading' => __('Loading...', 'wpmf'), 'page' => __('Page', 'wpmf'), 'zoom' => __('Zoom', 'wpmf'), 'prev' => __('Previous page', 'wpmf'), 'next' => __('Next page', 'wpmf'), 'zoomin' => __('Zoom In', 'wpmf'), 'zoomout' => __('Zoom Out', 'wpmf'), 'secure' => __('Secure', 'wpmf'), 'download' => __('Download PDF', 'wpmf'), 'fullscreen' => __('Full Screen', 'wpmf'), 'domainerror' => __('Error: URL to the PDF file must be on exactly the same domain as the current web page.', 'wpmf'), 'clickhereinfo' => __('Click here for more info', 'wpmf'), 'widthheightinvalid' => __('PDF page width or height are invalid', 'wpmf'), 'viewinfullscreen' => __('View in Full Screen', 'wpmf'), 'poweredby' => 1 ) ); return $array; } /** * Add pdf embed html to editor * * @param string $html HTML markup for a media item sent to the editor. * @param integer $id The first key from the $_POST['send'] data. * @param array $attachment Array of attachment metadata. * * @return string $html */ public function addImageFiles($html, $id, $attachment) { $post = get_post($id); $mimetype = explode('/', $post->post_mime_type); $pdf_embed = get_post_meta($id, 'wpmf_pdf_embed', true); $target = get_post_meta($id, '_gallery_link_target', true); if ($mimetype[1] === 'pdf') { if (isset($pdf_embed) && $pdf_embed === 'embed') { $doc = new DOMDocument(); libxml_use_internal_errors(true); $sousce = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); $doc->loadHTML($sousce); $tags = $doc->getElementsByTagName('a'); if ($tags->length > 0) { if (!empty($tags)) { $cloud_type = get_post_meta((int)$id, 'wpmf_drive_type', true); if ($cloud_type === 'dropbox') { $drive_id = get_post_meta((int)$id, 'wpmf_drive_id', true); $url = admin_url('admin-ajax.php') . '?action=wpmf-dbxdownload-file&id=' . urlencode($drive_id) . '&link=true&dl=0'; $tags->item(0)->setAttribute('href', $url); } $class = $tags->item(0)->getAttribute('class'); if (!empty($class)) { $newclass = $class . ' wpmf-pdfemb-viewer'; } else { $newclass = 'wpmf-pdfemb-viewer'; } $tags->item(0)->setAttribute('target', $target); $tags->item(0)->setAttribute('class', $newclass); $html = $doc->saveHTML(); } } } else { $singlefile = get_option('wpmf_option_singlefile'); if (isset($singlefile) && (int) $singlefile === 1) { $meta = get_post_meta($id, '_wp_attached_file'); $upload_dir = wp_upload_dir(); $url_attachment = $upload_dir['basedir'] . '/' . $meta[0]; if (file_exists($url_attachment)) { $size = filesize($url_attachment); if ($size < 1024 * 1024) { $size = round($size / 1024, 1) . ' kB'; } elseif ($size > 1024 * 1024) { $size = round($size / (1024 * 1024), 1) . ' MB'; } } else { $size = 0; } $type = wp_check_filetype($post->guid); $ext = $type['ext']; $html = ''; } else { $html = preg_replace('/(<]*)>/i', '$1 target="' . $target . '">', $html); } } } return $html; } /** * Add footer * * @return void */ public function adminFooterPdfEmbed() { ?> '; } /** * Add footer script * * @return void */ public function loadScript() { add_action('admin_footer', array($this, 'adminFooterPdfEmbed'), 11); add_action('wp_footer', array($this, 'adminFooterPdfEmbed'), 11); } /** * Create enable PDF embed field * Based on /wp-admin/includes/media.php * * @param array $form_fields An array of attachment form fields. * @param WP_Post $post The WP_Post attachment object. * * @return mixed */ public function attachmentFieldsToEdit($form_fields, $post) { if ($post->post_mime_type === 'application/pdf') { $value = get_post_meta($post->ID, 'wpmf_pdf_embed', true); if (empty($value)) { $value = 'large'; } $embed = array( 'link' => __('Off', 'wpmf'), 'embed' => __('On', 'wpmf'), ); $option = ''; foreach ($embed as $k => $v) { if ($value === $k) { $option .= ''; } else { $option .= ''; } } $form_fields['wpmf_pdf_embed'] = array( 'label' => __('PDF Embed', 'wpmf'), 'input' => 'html', 'html' => ' ' ); } return $form_fields; } /** * Save enable PDF embed option * Based on /wp-admin/includes/media.php * * @param array $post An array of post data. * @param array $attachment An array of attachment metadata. * * @return mixed $post */ public function attachmentFieldsToSave($post, $attachment) { if (isset($attachment['wpmf_pdf_embed'])) { update_post_meta($post['ID'], 'wpmf_pdf_embed', $attachment['wpmf_pdf_embed']); } return $post; } }