| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace PersianElementor; |
| 6 |
|
| 7 |
/** |
| 8 |
* Persian Elementor Core Class |
| 9 |
* Handles RTL styles, Persian fonts, and template library integration |
| 10 |
*/ |
| 11 |
class PersianElementorCore |
| 12 |
{ |
| 13 |
/** @var self Singleton instance */ |
| 14 |
private static $instance = null; |
| 15 |
|
| 16 |
/** @var string Plugin version */ |
| 17 |
private const VERSION = PERSIAN_ELEMENTOR_VERSION; |
| 18 |
|
| 19 |
/** @var array Default plugin options */ |
| 20 |
private const DEFAULT_OPTIONS = [ |
| 21 |
'efa-panel-font' => '1', |
| 22 |
'efa-iranian-icon' => '1', |
| 23 |
'efa-elementor-pro' => '1', |
| 24 |
'efa-elementor' => '1', |
| 25 |
'efa-all-font' => '1', |
| 26 |
'efa-zarinpal-button' => '1' |
| 27 |
]; |
| 28 |
|
| 29 |
/** @var array Plugin options */ |
| 30 |
private $options; |
| 31 |
|
| 32 |
/** @return self Get singleton instance */ |
| 33 |
public static function instance() |
| 34 |
{ |
| 35 |
if (self::$instance === null) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** Constructor */ |
| 42 |
private function __construct() |
| 43 |
{ |
| 44 |
$this->load_options(); |
| 45 |
$this->init_hooks(); |
| 46 |
} |
| 47 |
|
| 48 |
/** Load and initialize plugin options */ |
| 49 |
private function load_options() |
| 50 |
{ |
| 51 |
$saved_options = get_option('persian_elementor', []); |
| 52 |
$this->options = array_merge(self::DEFAULT_OPTIONS, $saved_options); |
| 53 |
|
| 54 |
// Only update if options are different from saved options |
| 55 |
if ($saved_options !== $this->options) { |
| 56 |
update_option('persian_elementor', $this->options); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** Prevent cloning of the instance */ |
| 61 |
private function __clone() {} |
| 62 |
|
| 63 |
/** Prevent unserializing of the instance */ |
| 64 |
public function __wakeup() {} |
| 65 |
|
| 66 |
/** Initialize hooks based on plugin options */ |
| 67 |
private function init_hooks() |
| 68 |
{ |
| 69 |
// RTL styles hooks |
| 70 |
if (!empty($this->options['efa-panel-font'])) { |
| 71 |
$this->register_rtl_style_hooks(); |
| 72 |
} |
| 73 |
|
| 74 |
// Persian font hooks |
| 75 |
if (!empty($this->options['efa-all-font'])) { |
| 76 |
add_action('elementor/frontend/after_enqueue_styles', [$this, 'enqueue_persian_font']); |
| 77 |
} |
| 78 |
|
| 79 |
// Iranian icons hooks |
| 80 |
if (!empty($this->options['efa-iranian-icon'])) { |
| 81 |
add_action('elementor/editor/before_enqueue_scripts', [$this, 'enqueue_icon_styles']); |
| 82 |
add_action('elementor/frontend/before_enqueue_styles', [$this, 'enqueue_icon_styles']); |
| 83 |
} |
| 84 |
|
| 85 |
// Template library integration |
| 86 |
$this->init_template_library(); |
| 87 |
|
| 88 |
// Dashboard widget version display |
| 89 |
add_action('elementor/admin/dashboard_overview_widget/after_version', [$this, 'add_version_to_dashboard_header']); |
| 90 |
} |
| 91 |
|
| 92 |
/** Register RTL style hooks for different contexts */ |
| 93 |
private function register_rtl_style_hooks() |
| 94 |
{ |
| 95 |
add_action('elementor/editor/before_enqueue_scripts', [$this, 'enqueue_rtl_styles']); |
| 96 |
add_action('elementor/preview/enqueue_styles', [$this, 'enqueue_rtl_styles']); |
| 97 |
add_action('elementor/app/init', [$this, 'enqueue_rtl_styles']); |
| 98 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_rtl_styles']); |
| 99 |
} |
| 100 |
|
| 101 |
// ASSETS ENQUEUING |
| 102 |
|
| 103 |
/** Enqueue RTL styles based on current context */ |
| 104 |
public function enqueue_rtl_styles() |
| 105 |
{ |
| 106 |
$current_hook = current_filter(); |
| 107 |
$style_suffix = 'common-rtl.css'; |
| 108 |
|
| 109 |
if ($current_hook === 'elementor/editor/before_enqueue_scripts') { |
| 110 |
$style_suffix = 'editor-rtl.min.css'; |
| 111 |
} elseif ($current_hook === 'elementor/preview/enqueue_styles') { |
| 112 |
$style_suffix = 'preview-rtl.css'; |
| 113 |
} |
| 114 |
|
| 115 |
wp_enqueue_style( |
| 116 |
'persian-elementor-rtl', |
| 117 |
plugins_url("assets/css/$style_suffix", __FILE__), |
| 118 |
[], |
| 119 |
self::VERSION |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** Enqueue Persian font styles */ |
| 124 |
public function enqueue_persian_font() |
| 125 |
{ |
| 126 |
wp_enqueue_style( |
| 127 |
'persian-elementor-font', |
| 128 |
plugins_url('assets/css/font.css', __FILE__), |
| 129 |
[], |
| 130 |
self::VERSION |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** Enqueue frontend styles */ |
| 135 |
public function enqueue_frontend_styles() |
| 136 |
{ |
| 137 |
wp_enqueue_style( |
| 138 |
'persian-elementor-front', |
| 139 |
plugins_url('assets/css/front-rtl.css', __FILE__), |
| 140 |
[], |
| 141 |
self::VERSION |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
/** Enqueue icon styles */ |
| 146 |
public function enqueue_icon_styles() |
| 147 |
{ |
| 148 |
wp_enqueue_style( |
| 149 |
'persian-elementor-icon', |
| 150 |
plugins_url('includes/icons/efaicons/style.css', __FILE__), |
| 151 |
[], |
| 152 |
self::VERSION |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
// TEMPLATE LIBRARY |
| 157 |
|
| 158 |
/** Initialize the template library functionality */ |
| 159 |
private function init_template_library() |
| 160 |
{ |
| 161 |
add_action('elementor/editor/after_enqueue_scripts', [$this, 'enqueue_template_editor_scripts']); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Enqueue editor scripts for template library |
| 166 |
* Injects JavaScript to add the Temply tab to Elementor's template library |
| 167 |
*/ |
| 168 |
public function enqueue_template_editor_scripts() |
| 169 |
{ |
| 170 |
wp_add_inline_script('elementor-editor', ' |
| 171 |
elementor.on("document:loaded", function() { |
| 172 |
if ($e.components.get("library").hasTab("templates/temply")) { |
| 173 |
return; |
| 174 |
} |
| 175 |
$e.components.get("library").addTab("templates/temply", { |
| 176 |
title: "<a href=\"https://temply.ir/\" target=\"_blank\" style=\"color: #0c0d0e; padding:17px 25px\">قالب های ایرانی</a>", |
| 177 |
}, 5); |
| 178 |
}); |
| 179 |
'); |
| 180 |
} |
| 181 |
|
| 182 |
/** @deprecated Use enqueue_template_editor_scripts() instead */ |
| 183 |
public function enqueue_template_script() |
| 184 |
{ |
| 185 |
$this->enqueue_template_editor_scripts(); |
| 186 |
} |
| 187 |
|
| 188 |
/** Add Persian Elementor version to Elementor dashboard widget header */ |
| 189 |
public function add_version_to_dashboard_header() |
| 190 |
{ |
| 191 |
echo '<span class="e-overview__version">ال� |
| 192 |
نتور فارسی v' . esc_html(self::VERSION) . '</span>'; |
| 193 |
} |
| 194 |
|
| 195 |
/** @return string The plugin version */ |
| 196 |
public function get_version() |
| 197 |
{ |
| 198 |
return self::VERSION; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Initialize plugin |
| 203 |
PersianElementorCore::instance(); |
| 204 |
|