| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Classes; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use MasterAddons\Inc\Classes\Helper; |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
}; |
| 12 |
|
| 13 |
if (!class_exists('MasterAddons\Inc\Classes\Extension_Prototype')) { |
| 14 |
class Extension_Prototype |
| 15 |
{ |
| 16 |
|
| 17 |
private static $instance = null; |
| 18 |
|
| 19 |
public $name = 'Extension'; |
| 20 |
|
| 21 |
public $has_controls = false; |
| 22 |
|
| 23 |
public $is_premium = false; |
| 24 |
|
| 25 |
private $is_common = true; |
| 26 |
|
| 27 |
private $depended_scripts = []; |
| 28 |
|
| 29 |
private $depended_styles = []; |
| 30 |
|
| 31 |
public static $depended_plugins = []; |
| 32 |
|
| 33 |
static public function is_enabled() |
| 34 |
{ |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
public $common_sections_actions = array( |
| 39 |
array( |
| 40 |
'element' => 'common', |
| 41 |
'action' => '_section_style', |
| 42 |
), |
| 43 |
array( |
| 44 |
'element' => 'container', |
| 45 |
'action' => '_section_style', |
| 46 |
), |
| 47 |
array( |
| 48 |
'element' => 'section', |
| 49 |
'action' => '_section_style', |
| 50 |
), |
| 51 |
array( |
| 52 |
'element' => 'column', |
| 53 |
'action' => '_section_style', |
| 54 |
) |
| 55 |
); |
| 56 |
|
| 57 |
public function __construct() |
| 58 |
{ |
| 59 |
$this->init(); |
| 60 |
} |
| 61 |
|
| 62 |
public function init($param = null) |
| 63 |
{ |
| 64 |
// Enqueue scripts |
| 65 |
add_action('elementor/frontend/after_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 66 |
|
| 67 |
// Enqueue styles |
| 68 |
add_action('elementor/frontend/after_enqueue_styles', [$this, 'enqueue_styles']); |
| 69 |
|
| 70 |
// Elementor hooks |
| 71 |
if ($this->is_common) { |
| 72 |
// Add the advanced section required to display controls |
| 73 |
$this->add_common_sections_actions(); |
| 74 |
} |
| 75 |
|
| 76 |
$this->add_actions(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function add_script_depends($handler) |
| 82 |
{ |
| 83 |
$this->depended_scripts[] = $handler; |
| 84 |
} |
| 85 |
|
| 86 |
public function add_style_depends($handler) |
| 87 |
{ |
| 88 |
$this->depended_styles[] = $handler; |
| 89 |
} |
| 90 |
|
| 91 |
public function get_script_depends() |
| 92 |
{ |
| 93 |
return $this->depended_scripts; |
| 94 |
} |
| 95 |
|
| 96 |
final public function enqueue_scripts() |
| 97 |
{ |
| 98 |
$this->_enqueue_scripts(); |
| 99 |
} |
| 100 |
|
| 101 |
public function _enqueue_scripts() |
| 102 |
{ |
| 103 |
$scripts = $this->get_script_depends(); |
| 104 |
if (!empty($scripts)) { |
| 105 |
foreach ($scripts as $script) { |
| 106 |
wp_enqueue_script($script); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
final public function get_style_depends() |
| 112 |
{ |
| 113 |
return $this->depended_styles; |
| 114 |
} |
| 115 |
|
| 116 |
final public function enqueue_styles() |
| 117 |
{ |
| 118 |
$this->_enqueue_styles(); |
| 119 |
} |
| 120 |
|
| 121 |
public function _enqueue_styles() |
| 122 |
{ |
| 123 |
$styles = $this->get_style_depends(); |
| 124 |
if (!empty($styles)) { |
| 125 |
foreach ($styles as $style) { |
| 126 |
wp_enqueue_style($style); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public function _enqueue_alles() |
| 132 |
{ |
| 133 |
$this->_enqueue_styles(); |
| 134 |
$this->_enqueue_scripts(); |
| 135 |
} |
| 136 |
|
| 137 |
public function get_low_name() |
| 138 |
{ |
| 139 |
$low_name = strtolower($this->name); |
| 140 |
$low_name = str_replace(' ', '_', $low_name); |
| 141 |
return $low_name; |
| 142 |
} |
| 143 |
|
| 144 |
public final function add_common_sections($element, $args) |
| 145 |
{ |
| 146 |
$low_name = $this->get_low_name(); |
| 147 |
$section_name = 'jltma_section_' . $low_name . '_advanced'; |
| 148 |
|
| 149 |
if (!$this->is_common()) { |
| 150 |
//return false; |
| 151 |
} |
| 152 |
|
| 153 |
if (!$this->has_controls) { |
| 154 |
// no need settings |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
// Check if this section exists |
| 159 |
$section_exists = \Elementor\Plugin::instance()->controls_manager->get_control_from_stack($element->get_unique_name(), $section_name); |
| 160 |
|
| 161 |
|
| 162 |
if (!is_wp_error($section_exists)) { |
| 163 |
// We can't and should try to add this section to the stack |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
$this->get_control_section($section_name, $element); |
| 168 |
} |
| 169 |
|
| 170 |
public function get_control_section($section_name, $element) |
| 171 |
{ |
| 172 |
$section_title = ''; |
| 173 |
// If the extension is premium, add a badge to the section title |
| 174 |
if (Helper::jltma_premium() && $this->is_premium) { |
| 175 |
$section_title = $this->name; |
| 176 |
} elseif( $this->is_premium ){ |
| 177 |
/* translators: 1: Extension name, 2: Pro badge label */ |
| 178 |
$section_title = sprintf(__('%1$s <span class="jltma_pro_text">%2$s<span>', 'master-addons'), $this->name, __('Pro', 'master-addons') ); |
| 179 |
} else { |
| 180 |
// If the extension is not premium, just show the name |
| 181 |
$section_title = $this->name; |
| 182 |
} |
| 183 |
|
| 184 |
$element->start_controls_section( |
| 185 |
$section_name, |
| 186 |
[ |
| 187 |
'tab' => Controls_Manager::TAB_ADVANCED, |
| 188 |
'label' => ( !empty($this->name) ? $section_title : __('Master Addons', 'master-addons') ) . ' ' . JLTMA_EXTENSION_BADGE, |
| 189 |
// Provide a fallback name if $this->name is empty |
| 190 |
] |
| 191 |
); |
| 192 |
$element->end_controls_section(); |
| 193 |
} |
| 194 |
|
| 195 |
public function add_common_sections_actions() |
| 196 |
{ |
| 197 |
|
| 198 |
//_section_style |
| 199 |
//section_advanced |
| 200 |
//section_custom_css |
| 201 |
//section_custom_css_pro |
| 202 |
|
| 203 |
foreach ($this->common_sections_actions as $action) { |
| 204 |
// Activate action for elements |
| 205 |
add_action('elementor/element/' . $action['element'] . '/' . $action['action'] . '/after_section_end', function ($element, $args) { |
| 206 |
$this->add_common_sections($element, $args); |
| 207 |
}, 10, 2); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
protected function add_actions() |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
public function is_common() |
| 216 |
{ |
| 217 |
return $this->is_common; |
| 218 |
} |
| 219 |
|
| 220 |
public static function get_instance() |
| 221 |
{ |
| 222 |
if (!self::$instance) { |
| 223 |
self::$instance = new self; |
| 224 |
} |
| 225 |
return self::$instance; |
| 226 |
} |
| 227 |
} // End class Extension_Prototype |
| 228 |
} // End class_exists check |
| 229 |
|
| 230 |
// Don't auto-instantiate - let the plugin initialization handle this |
| 231 |
|