| 1 |
<?php |
| 2 |
/** |
| 3 |
* Master Addons Popup Builder Elementor Integration |
| 4 |
* |
| 5 |
* @package MasterAddons |
| 6 |
* @subpackage PopupBuilder |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MasterAddons\Inc\Admin\PopupBuilder; |
| 10 |
|
| 11 |
use MasterAddons\Inc\Classes\Assets_Manager; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class Elementor_Integration { |
| 18 |
|
| 19 |
private static $instance = null; |
| 20 |
|
| 21 |
public static function get_instance() { |
| 22 |
if (self::$instance === null) { |
| 23 |
self::$instance = new self(); |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
if (!defined('ELEMENTOR_VERSION')) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$this->init_hooks(); |
| 34 |
} |
| 35 |
|
| 36 |
private function init_hooks() { |
| 37 |
// Document type registration - MUST be priority 10 or lower |
| 38 |
add_action('elementor/documents/register', [$this, 'register_popup_document_type'], 10, 1); |
| 39 |
|
| 40 |
// Elementor editor hooks |
| 41 |
add_action('elementor/editor/after_enqueue_scripts', [$this, 'editor_scripts']); |
| 42 |
add_action('elementor/editor/after_enqueue_styles', [$this, 'editor_styles']); |
| 43 |
|
| 44 |
// Editor customization for popups |
| 45 |
add_action('elementor/editor/init', [$this, 'editor_init']); |
| 46 |
|
| 47 |
// Override Elementor canvas template for popups |
| 48 |
add_filter('template_include', [$this, 'popup_canvas_template'], 999999); |
| 49 |
|
| 50 |
// Enqueue preview styles in iframe |
| 51 |
add_action('elementor/preview/enqueue_styles', [$this, 'preview_styles']); |
| 52 |
|
| 53 |
// Re-activate popup when disable_after date is extended to future |
| 54 |
add_action('elementor/document/after_save', [$this, 'maybe_reactivate_popup'], 10, 2); |
| 55 |
} |
| 56 |
|
| 57 |
public function editor_scripts() { |
| 58 |
if ($this->is_popup_editor()) { |
| 59 |
Assets_Manager::enqueue('popup-builder-elementor'); |
| 60 |
|
| 61 |
wp_localize_script('jltma-popup-builder-elementor', 'jltmaPopupElementor', [ |
| 62 |
'popup_id' => get_the_ID(), |
| 63 |
'popup_settings' => $this->get_popup_settings(get_the_ID()), |
| 64 |
'strings' => [ |
| 65 |
'popup_settings' => __('Popup Settings', 'master-addons'), |
| 66 |
'position' => __('Position', 'master-addons'), |
| 67 |
'animation' => __('Animation', 'master-addons'), |
| 68 |
] |
| 69 |
]); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function editor_styles() { |
| 74 |
// CSS is already enqueued by editor_scripts via Assets_Manager |
| 75 |
} |
| 76 |
|
| 77 |
public function preview_styles() { |
| 78 |
$post_id = get_the_ID(); |
| 79 |
if (get_post_type($post_id) === 'jltma_popup') { |
| 80 |
Assets_Manager::enqueue('popup-builder-elementor'); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function register_popup_document_type($documents_manager) { |
| 85 |
$documents_manager->register_document_type('jltma_popup', '\MasterAddons\Inc\Admin\PopupBuilder\Popup_Document'); |
| 86 |
} |
| 87 |
|
| 88 |
public function editor_init() { |
| 89 |
if ($this->is_popup_editor()) { |
| 90 |
// Add custom body class for popup editor |
| 91 |
add_filter('admin_body_class', function($classes) { |
| 92 |
return $classes . ' jltma-popup-editor elementor-editor-jltma_popup'; |
| 93 |
}); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function popup_canvas_template($template) { |
| 98 |
// Check if viewing a popup post type |
| 99 |
if (!is_singular('jltma_popup')) { |
| 100 |
return $template; |
| 101 |
} |
| 102 |
|
| 103 |
// Make sure Elementor is active |
| 104 |
if (!did_action('elementor/loaded')) { |
| 105 |
return $template; |
| 106 |
} |
| 107 |
|
| 108 |
// Load custom template for popup preview |
| 109 |
$editor_template = JLTMA_PATH . 'inc/admin/popup-builder/templates/editor.php'; |
| 110 |
|
| 111 |
if (file_exists($editor_template)) { |
| 112 |
return $editor_template; |
| 113 |
} |
| 114 |
|
| 115 |
return $template; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Re-activate popup when user extends the disable_after date to the future. |
| 120 |
*/ |
| 121 |
public function maybe_reactivate_popup($document, $data) { |
| 122 |
if ($document->get_name() !== 'jltma_popup') { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$popup_id = $document->get_main_id(); |
| 127 |
$settings = $data['settings'] ?? []; |
| 128 |
|
| 129 |
// Check if auto-disable is enabled with a future date |
| 130 |
$auto_disable = $settings['popup_disable_automatic'] ?? ''; |
| 131 |
$disable_after = $settings['popup_disable_after'] ?? ''; |
| 132 |
|
| 133 |
if ($auto_disable === 'yes' && !empty($disable_after)) { |
| 134 |
$expiration_date = strtotime($disable_after); |
| 135 |
$current_time = current_time('timestamp'); |
| 136 |
|
| 137 |
// If the date is in the future, re-activate the popup |
| 138 |
if ($expiration_date && $expiration_date > $current_time) { |
| 139 |
update_post_meta($popup_id, '_jltma_popup_activation', 'yes'); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private function is_popup_editor() { |
| 145 |
if (!is_admin() || !isset($_GET['action']) || $_GET['action'] !== 'elementor') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin context detection, no data processed |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
$post_id = isset($_GET['post']) ? intval($_GET['post']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin context detection, no data processed |
| 150 |
if (!$post_id) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
return get_post_type($post_id) === 'jltma_popup'; |
| 155 |
} |
| 156 |
|
| 157 |
private function get_popup_settings($popup_id) { |
| 158 |
return [ |
| 159 |
'position' => get_post_meta($popup_id, '_jltma_popup_position', true) ?: 'center', |
| 160 |
'animation' => get_post_meta($popup_id, '_jltma_popup_animation', true) ?: 'fade', |
| 161 |
'overlay' => get_post_meta($popup_id, '_jltma_popup_overlay', true) ?: 'yes', |
| 162 |
'close_button' => get_post_meta($popup_id, '_jltma_popup_close_button', true) ?: 'yes', |
| 163 |
]; |
| 164 |
} |
| 165 |
} |