| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/FileManager/wp-content/plugins/quick-embed-pdf/includes/ |
Upload File : |
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class QEPW_Viewer {
public static function init() {
add_shortcode( 'qepw_pdf_viewer', [ self::class, 'render_pdf_viewer' ] );
add_action( 'wp_enqueue_scripts', [ self::class, 'conditionally_enqueue_assets' ] );
add_filter( 'media_send_to_editor', [ self::class, 'replace_pdf_with_shortcode' ], 10, 3 );
add_action( 'enqueue_block_editor_assets', [ self::class, 'enqueue_block_editor_assets' ] );
}
public static function enqueue_block_editor_assets() {
wp_enqueue_script(
'qepw-gutenberg-block',
QEPW_PLUGIN_URL . 'assets/js/block.js',
[ 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ],
QEPW_VERSION,
true
);
wp_enqueue_style(
'qepw-gutenberg-block-style',
QEPW_PLUGIN_URL . 'assets/css/block.css',
[],
QEPW_VERSION
);
}
public static function conditionally_enqueue_assets() {
if ( ! is_singular() ) {
return;
}
global $post;
if ( has_shortcode( $post->post_content, 'qepw_pdf_viewer' ) ) {
wp_register_style(
'qepw-viewer-css',
QEPW_PLUGIN_URL . 'assets/css/viewer.css',
[],
QEPW_VERSION
);
wp_enqueue_style( 'qepw-viewer-css' );
wp_register_script(
'qepw-pdfjs-lib',
QEPW_PLUGIN_URL . 'assets/js/pdfjs/pdf.min.js',
[],
QEPW_VERSION,
true
);
wp_enqueue_script( 'qepw-pdfjs-lib' );
wp_register_script(
'qepw-viewer-js',
QEPW_PLUGIN_URL . 'assets/js/viewer.jquery.js',
[ 'jquery', 'qepw-pdfjs-lib' ],
QEPW_VERSION,
true
);
wp_localize_script(
'qepw-viewer-js',
'qepwViewerConfig',
[
'workerSrc' => esc_url( QEPW_PLUGIN_URL . 'assets/js/pdfjs/pdf.worker.min.js' ),
]
);
wp_enqueue_script( 'qepw-viewer-js' );
$pdf_width = intval( get_option( 'qepw_width', 800 ) );
add_action( 'wp_head', function () use ( $pdf_width ) {
echo '<style>.pdf-viewer { max-width: ' . esc_attr( $pdf_width ) . 'px; margin: 0 auto; }</style>';
});
}
}
public static function render_pdf_viewer( $atts ) {
$atts = shortcode_atts(
[ 'file' => '' ],
$atts,
'qepw_pdf_viewer'
);
$file_url = esc_url( $atts['file'] );
$show_download_button = get_option( 'qepw_download_button', true );
if ( empty( $file_url ) ) {
return '<p style="color: red;">No PDF file provided.</p>';
}
$file_size = '';
if ( filter_var( $file_url, FILTER_VALIDATE_URL ) ) {
$file_headers = @get_headers( $file_url, 1 );
if ( isset( $file_headers['Content-Length'] ) ) {
$file_size = size_format( $file_headers['Content-Length'], 2 );
}
}
ob_start(); ?>
<div class="pdf-viewer" data-pdf-url="<?php echo esc_url( $file_url ); ?>">
<canvas class="pdf-canvas"></canvas>
<div class="pdf-controls">
<button class="prev-page">Previous Page</button>
<span class="page-info"></span>
<button class="next-page">Next Page</button>
<?php if ( $show_download_button ) : ?>
<button class="download-pdf">
<a href="<?php echo esc_url( $file_url ); ?>" download>
<?php
echo esc_html( sprintf(
'Download (%s)',
$file_size ? esc_html( $file_size ) : 'Unknown size'
) );
?>
</a>
</button>
<?php endif; ?>
</div>
</div>
<?php
return ob_get_clean();
}
public static function replace_pdf_with_shortcode( $html, $id, $attachment ) {
$mime = get_post_mime_type( $id );
if ( $mime === 'application/pdf' ) {
$file_url = wp_get_attachment_url( $id );
$html = sprintf( '[qepw_pdf_viewer file="%s"]', esc_url( $file_url ) );
}
return $html;
}
}