149 lines
5.0 KiB
PHP
149 lines
5.0 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: PDF Viewer for Elementor
|
||
* Description: Добавляет виджет PDF Viewer для Elementor с возможностью просмотра PDF файлов
|
||
* Plugin URI: https://tooweb.ru
|
||
* Version: 1.0.0
|
||
* Author: Александр Головин
|
||
* Author URI: https://tooweb.ru
|
||
* Text Domain: pdf-viewer-elementor
|
||
* Domain Path: /languages
|
||
* Requires at least: 5.0
|
||
* Requires PHP: 7.0
|
||
* Elementor tested up to: 3.20
|
||
* Elementor Pro tested up to: 3.20
|
||
*/
|
||
|
||
if (!defined('ABSPATH')) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* Основной класс плагина
|
||
*/
|
||
final class PDF_Viewer_Elementor {
|
||
const VERSION = '1.0.0';
|
||
const MINIMUM_ELEMENTOR_VERSION = '3.0.0';
|
||
const MINIMUM_PHP_VERSION = '7.0';
|
||
|
||
private static $_instance = null;
|
||
|
||
public static function instance() {
|
||
if (is_null(self::$_instance)) {
|
||
self::$_instance = new self();
|
||
}
|
||
return self::$_instance;
|
||
}
|
||
|
||
public function __construct() {
|
||
add_action('init', [$this, 'i18n']);
|
||
add_action('plugins_loaded', [$this, 'init']);
|
||
|
||
// Добавляем поддержку PDF в медиабиблиотеку
|
||
add_filter('upload_mimes', [$this, 'add_pdf_mime_type']);
|
||
add_filter('wp_prepare_attachment_for_js', [$this, 'update_pdf_attachment_details'], 10, 2);
|
||
}
|
||
|
||
public function i18n() {
|
||
load_plugin_textdomain('pdf-viewer-elementor', false, dirname(plugin_basename(__FILE__)) . '/languages');
|
||
}
|
||
|
||
public function init() {
|
||
// Проверка наличия Elementor
|
||
if (!did_action('elementor/loaded')) {
|
||
add_action('admin_notices', [$this, 'admin_notice_missing_main_plugin']);
|
||
return;
|
||
}
|
||
|
||
// Проверка версии Elementor
|
||
if (!version_compare(ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=')) {
|
||
add_action('admin_notices', [$this, 'admin_notice_minimum_elementor_version']);
|
||
return;
|
||
}
|
||
|
||
// Проверка версии PHP
|
||
if (version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '<')) {
|
||
add_action('admin_notices', [$this, 'admin_notice_minimum_php_version']);
|
||
return;
|
||
}
|
||
|
||
// Добавляем поддержку динамических тегов
|
||
add_action('elementor/dynamic_tags/register', [$this, 'register_dynamic_tags']);
|
||
|
||
// Регистрируем виджет
|
||
add_action('elementor/widgets/register', [$this, 'register_widgets']);
|
||
|
||
// Регистрируем скрипты и стили
|
||
add_action('elementor/frontend/after_register_scripts', [$this, 'register_scripts']);
|
||
add_action('elementor/frontend/after_register_styles', [$this, 'register_styles']);
|
||
}
|
||
|
||
public function register_widgets($widgets_manager) {
|
||
require_once(__DIR__ . '/includes/widgets/pdf-viewer-widget.php');
|
||
$widgets_manager->register(new \PDF_Viewer_Widget());
|
||
}
|
||
|
||
public function register_scripts() {
|
||
wp_register_script(
|
||
'pdf-viewer-widget',
|
||
plugins_url('assets/js/pdf-viewer-widget.js', __FILE__),
|
||
['jquery', 'pdfjs'],
|
||
self::VERSION,
|
||
true
|
||
);
|
||
}
|
||
|
||
public function register_styles() {
|
||
wp_register_style(
|
||
'pdf-viewer-widget',
|
||
plugins_url('assets/css/pdf-viewer-widget.css', __FILE__),
|
||
[],
|
||
self::VERSION
|
||
);
|
||
}
|
||
|
||
public function register_dynamic_tags($dynamic_tags_manager) {
|
||
// Регистрируем категорию для PDF
|
||
\Elementor\Plugin::$instance->dynamic_tags->register_group(
|
||
'pdf',
|
||
[
|
||
'title' => 'PDF'
|
||
]
|
||
);
|
||
|
||
// Подключаем и регистрируем наш тег
|
||
require_once(__DIR__ . '/includes/dynamic-tags/pdf-url.php');
|
||
$dynamic_tags_manager->register(new \PDF_Viewer_URL_Tag());
|
||
}
|
||
|
||
public function admin_notice_missing_main_plugin() {
|
||
if (isset($_GET['activate'])) unset($_GET['activate']);
|
||
|
||
$message = sprintf(
|
||
esc_html__('"%1$s" requires "%2$s" to be installed and activated.', 'pdf-viewer-elementor'),
|
||
'<strong>' . esc_html__('PDF Viewer for Elementor', 'pdf-viewer-elementor') . '</strong>',
|
||
'<strong>' . esc_html__('Elementor', 'pdf-viewer-elementor') . '</strong>'
|
||
);
|
||
|
||
printf('<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message);
|
||
}
|
||
|
||
public function add_pdf_mime_type($mimes) {
|
||
$mimes['pdf'] = 'application/pdf';
|
||
return $mimes;
|
||
}
|
||
|
||
public function update_pdf_attachment_details($response, $attachment) {
|
||
if ($response['mime'] === 'application/pdf') {
|
||
$response['icon'] = includes_url('images/media/document.png');
|
||
$response['sizes'] = [
|
||
'full' => [
|
||
'url' => $response['url'],
|
||
],
|
||
];
|
||
}
|
||
return $response;
|
||
}
|
||
}
|
||
|
||
PDF_Viewer_Elementor::instance(); |