PDF-Viever-Elementor/includes/widgets/pdf-viewer-widget.php
2025-05-03 04:32:37 +05:00

310 lines
10 KiB
PHP

<?php
class PDF_Viewer_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'pdf_viewer';
}
public function get_title() {
return 'PDF Viewer';
}
public function get_icon() {
return 'eicon-document-file';
}
public function get_categories() {
return ['general'];
}
protected function register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => 'Настройки PDF',
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'pdf_source',
[
'label' => 'Источник PDF',
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'media',
'options' => [
'media' => 'Медиафайл',
'dynamic' => 'Динамический',
],
]
);
$this->add_control(
'pdf_file',
[
'label' => 'PDF Файл',
'type' => \Elementor\Controls_Manager::MEDIA,
'media_types' => ['application/pdf'],
'default' => [
'url' => '',
'id' => null,
],
'condition' => [
'pdf_source' => 'media',
],
]
);
$this->add_control(
'pdf_dynamic',
[
'label' => 'Динамический PDF',
'type' => \Elementor\Controls_Manager::TEXT,
'dynamic' => [
'active' => true,
'categories' => [
\Elementor\Modules\DynamicTags\Module::URL_CATEGORY,
\Elementor\Modules\DynamicTags\Module::POST_META_CATEGORY,
\Elementor\Modules\DynamicTags\Module::MEDIA_CATEGORY,
],
],
'condition' => [
'pdf_source' => 'dynamic',
],
]
);
$this->add_control(
'show_all_pages',
[
'label' => 'Показать все страницы',
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => 'Да',
'label_off' => 'Нет',
'default' => '',
]
);
$this->add_control(
'pages_to_show',
[
'label' => 'Количество страниц для показа',
'type' => \Elementor\Controls_Manager::NUMBER,
'min' => 1,
'max' => 100,
'step' => 1,
'default' => 1,
'condition' => [
'show_all_pages' => '',
],
]
);
$this->add_control(
'pages_layout',
[
'label' => 'Расположение страниц',
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'horizontal',
'options' => [
'horizontal' => 'Горизонтально',
'vertical' => 'Вертикально',
],
'condition' => [
'pages_to_show!' => '1',
],
]
);
$this->add_control(
'enable_scrollbar',
[
'label' => 'Включить полосу прокрутки',
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => 'Да',
'label_off' => 'Нет',
'default' => '',
'condition' => [
'show_all_pages' => 'yes',
],
]
);
$this->add_control(
'pdf_file_style',
[
'label' => '',
'type' => \Elementor\Controls_Manager::HIDDEN,
'selectors' => [
'{{WRAPPER}} .elementor-control-media' => 'border: 1px solid #d5dadf; padding: 7px; border-radius: 3px;',
'{{WRAPPER}} .elementor-control-media__content' => 'display: flex; align-items: center;',
'{{WRAPPER}} .elementor-control-media__preview' => 'height: 40px; width: auto; max-width: 100%; margin-right: 10px;',
'{{WRAPPER}} .elementor-control-media-upload-button' => 'margin-right: 10px;',
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'style_section',
[
'label' => 'Стили',
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
]
);
$this->add_control(
'background_color',
[
'label' => 'Цвет фона',
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#525659',
'selectors' => [
'{{WRAPPER}} .pdf-viewer-container' => 'background-color: {{VALUE}}',
],
]
);
$this->add_responsive_control(
'viewer_width',
[
'label' => 'Ширина виджета',
'type' => \Elementor\Controls_Manager::SLIDER,
'size_units' => ['px', '%'],
'range' => [
'px' => [
'min' => 0,
'max' => 2000,
'step' => 1,
],
'%' => [
'min' => 0,
'max' => 100,
'step' => 1,
],
],
'default' => [
'unit' => '%',
'size' => 100,
],
'selectors' => [
'{{WRAPPER}} .pdf-viewer-widget' => 'width: {{SIZE}}{{UNIT}};',
],
]
);
$this->add_group_control(
\Elementor\Group_Control_Border::get_type(),
[
'name' => 'border',
'label' => 'Граница',
'selector' => '{{WRAPPER}} .pdf-viewer-container',
]
);
$this->add_responsive_control(
'border_radius',
[
'label' => 'Скругление углов',
'type' => \Elementor\Controls_Manager::DIMENSIONS,
'size_units' => ['px', '%'],
'default' => [
'top' => 4,
'right' => 4,
'bottom' => 4,
'left' => 4,
'unit' => 'px',
'isLinked' => true,
],
'selectors' => [
'{{WRAPPER}} .pdf-viewer-container' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
]
);
$this->add_group_control(
\Elementor\Group_Control_Box_Shadow::get_type(),
[
'name' => 'box_shadow',
'label' => 'Тень',
'selector' => '{{WRAPPER}} .pdf-viewer-container',
'fields_options' => [
'box_shadow_type' => [
'default' => 'yes',
],
'box_shadow' => [
'default' => [
'horizontal' => 0,
'vertical' => 2,
'blur' => 5,
'spread' => 0,
'color' => 'rgba(0,0,0,0.2)',
],
],
],
]
);
$this->add_responsive_control(
'padding',
[
'label' => 'Внутренний отступ',
'type' => \Elementor\Controls_Manager::DIMENSIONS,
'size_units' => ['px', 'em', '%'],
'selectors' => [
'{{WRAPPER}} .pdf-pages-container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
'default' => [
'top' => 20,
'right' => 20,
'bottom' => 20,
'left' => 20,
'unit' => 'px',
'isLinked' => true,
],
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$pdf_url = '';
if ($settings['pdf_source'] === 'media' && !empty($settings['pdf_file']['url'])) {
$pdf_url = $settings['pdf_file']['url'];
} elseif ($settings['pdf_source'] === 'dynamic' && !empty($settings['pdf_dynamic'])) {
$pdf_url = $settings['pdf_dynamic'];
}
if (empty($pdf_url)) {
echo 'Пожалуйста, выберите PDF файл или настройте динамический источник';
return;
}
$show_all_pages = $settings['show_all_pages'] === 'yes';
$pages_to_show = $show_all_pages ? 'all' : intval($settings['pages_to_show']);
$enable_scrollbar = $settings['enable_scrollbar'] === 'yes';
$pages_layout = $settings['pages_layout'];
?>
<div class="pdf-viewer-widget"
data-pdf-url="<?php echo esc_url($pdf_url); ?>"
data-pages="<?php echo esc_attr($pages_to_show); ?>"
data-show-all="<?php echo esc_attr($show_all_pages ? 'true' : 'false'); ?>"
data-scrollbar="<?php echo esc_attr($enable_scrollbar ? 'true' : 'false'); ?>"
data-layout="<?php echo esc_attr($pages_layout); ?>">
<div class="pdf-viewer-container">
<div class="pdf-pages-container">
</div>
</div>
</div>
<?php
wp_enqueue_script('pdfjs', 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js', [], '3.11.174', true);
wp_enqueue_script('pdf-viewer-widget');
wp_enqueue_style('pdf-viewer-widget');
}
}