| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Beyondwords\Wordpress\Compatibility\Elementor; |
| 6 |
|
| 7 |
use Elementor\Controls_Manager; |
| 8 |
use Elementor\Utils as ElementorUtils; |
| 9 |
use Elementor\Core\DocumentTypes\PageBase as PageBase; |
| 10 |
use Elementor\Modules\Library\Documents\Page as LibraryPageDocument; |
| 11 |
use WP_Post; |
| 12 |
use WP_Screen; |
| 13 |
use Beyondwords\Wordpress\Compatibility\Elementor\Controls\InspectText; |
| 14 |
use Beyondwords\Wordpress\Compatibility\Elementor\Controls\InspectTextarea; |
| 15 |
use Beyondwords\Wordpress\Compatibility\Elementor\Controls\Player; |
| 16 |
use Beyondwords\Wordpress\Compatibility\Elementor\ControlsSections\Beyondwords as BeyondwordsControlsSection; |
| 17 |
use Beyondwords\Wordpress\Compatibility\Elementor\ControlsSections\Help as HelpControlsSection; |
| 18 |
use Beyondwords\Wordpress\Compatibility\Elementor\ControlsSections\Inspect as InspectControlsSection; |
| 19 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 20 |
use Beyondwords\Wordpress\Core\Environment; |
| 21 |
|
| 22 |
/** |
| 23 |
* Integrates the BeyondWords Sidebar in the Elementor editor. |
| 24 |
* |
| 25 |
* @codeCoverageIgnore |
| 26 |
* |
| 27 |
* @since 3.10.0 |
| 28 |
*/ |
| 29 |
class Elementor |
| 30 |
{ |
| 31 |
/** |
| 32 |
* The identifier for the Elementor tab. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public const BEYONDWORDS_TAB = 'beyondwords-tab'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Represents the post. |
| 40 |
* |
| 41 |
* @var WP_Post|null |
| 42 |
*/ |
| 43 |
protected $post; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
*/ |
| 48 |
public function __construct() |
| 49 |
{ |
| 50 |
// @todo Exit if Elementor plugin is not activated in WordPress |
| 51 |
|
| 52 |
$this->registerHooks(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Initializes the integration. |
| 57 |
* |
| 58 |
* This is the place to register hooks and filters. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function registerHooks() |
| 63 |
{ |
| 64 |
if (\did_action('elementor/init')) { |
| 65 |
$this->addTab(); |
| 66 |
} else { |
| 67 |
\add_action('elementor/init', [ $this, 'addTab' ]); |
| 68 |
} |
| 69 |
|
| 70 |
\add_action('elementor/ajax/register_actions', [$this, 'registerAjaxActions']); |
| 71 |
|
| 72 |
\add_action('elementor/controls/register', [$this, 'registerControls']); |
| 73 |
\add_action('elementor/documents/register_controls', [ $this, 'registerDocumentControls' ]); |
| 74 |
\add_action('elementor/document/before_save', [$this, 'beforeDocumentSave'], 10, 2); |
| 75 |
|
| 76 |
\add_action('elementor/editor/before_enqueue_styles', [$this, 'beforeEnqueueStyles']); |
| 77 |
\add_action('elementor/editor/after_enqueue_styles', [$this, 'afterEnqueueStyles']); |
| 78 |
|
| 79 |
\add_action('elementor/editor/before_enqueue_scripts', [$this, 'beforeEnqueueScripts' ]); |
| 80 |
\add_action('elementor/editor/after_enqueue_scripts', [$this, 'afterEnqueueScripts' ]); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register ajax actions. |
| 85 |
* |
| 86 |
* Add new actions to handle data after an ajax requests returned. |
| 87 |
* |
| 88 |
* Fired by `elementor/ajax/register_actions` action. |
| 89 |
* |
| 90 |
* @since 3.10.0 |
| 91 |
* |
| 92 |
* @param Ajax $ajax_manager |
| 93 |
*/ |
| 94 |
public function registerAjaxActions($ajaxManager) |
| 95 |
{ |
| 96 |
$ajaxManager->register_ajax_action('get_beyondwords_data', [$this, 'ajaxGetBeyondwordsData']); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Ajax request to get BeyondWords data from post metadata. |
| 101 |
* |
| 102 |
* Get BeyondWords data using an ajax request. |
| 103 |
* |
| 104 |
* @since 3.10.0 |
| 105 |
* |
| 106 |
* @param array $request Ajax request. |
| 107 |
* |
| 108 |
* @return array Ajax response data. |
| 109 |
*/ |
| 110 |
public function ajaxGetBeyondwordsData($request) |
| 111 |
{ |
| 112 |
if (empty($request) || empty($request['editor_post_id'])) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$editor_post_id = absint($request['editor_post_id']); |
| 117 |
|
| 118 |
if (! get_post($editor_post_id)) { |
| 119 |
throw new \Exception(esc_html__('Post not found.', 'speechkit')); |
| 120 |
} |
| 121 |
|
| 122 |
return [ |
| 123 |
'post_id' => $editor_post_id, |
| 124 |
'beyondwords_project_id' => PostMetaUtils::getProjectId($editor_post_id), |
| 125 |
'beyondwords_content_id' => PostMetaUtils::getContentId($editor_post_id), |
| 126 |
'beyondwords_disabled' => PostMetaUtils::getDisabled($editor_post_id), |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Registers our Elementor controls. |
| 132 |
*/ |
| 133 |
public function registerControls($controlsManager) |
| 134 |
{ |
| 135 |
$controlsManager->register(new Player()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Before enqueue scripts. |
| 140 |
* |
| 141 |
* @since 3.10.0 |
| 142 |
* @access public |
| 143 |
*/ |
| 144 |
public function beforeEnqueueScripts() |
| 145 |
{ |
| 146 |
$assetFile = include BEYONDWORDS__PLUGIN_DIR . 'build/elementor.asset.php'; |
| 147 |
|
| 148 |
wp_register_script( |
| 149 |
'beyondwords-elementor', |
| 150 |
BEYONDWORDS__PLUGIN_URI . 'build/elementor.js', |
| 151 |
$assetFile['dependencies'], |
| 152 |
$assetFile['version'], |
| 153 |
true |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* After enqueue scripts. |
| 159 |
* |
| 160 |
* @since 3.10.0 |
| 161 |
* @access public |
| 162 |
*/ |
| 163 |
public function afterEnqueueScripts() |
| 164 |
{ |
| 165 |
wp_enqueue_script('beyondwords-elementor'); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Before enqueue styles. |
| 170 |
*/ |
| 171 |
public function beforeEnqueueStyles() |
| 172 |
{ |
| 173 |
wp_register_style( |
| 174 |
'beyondwords-elementor', |
| 175 |
plugins_url('css/beyondwords-tab.css', __FILE__), |
| 176 |
array(), |
| 177 |
BEYONDWORDS__PLUGIN_VERSION |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* After enqueue styles. |
| 183 |
*/ |
| 184 |
public function afterEnqueueStyles() |
| 185 |
{ |
| 186 |
wp_enqueue_style('beyondwords-elementor'); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Register a panel tab slug, in order to allow adding controls to this tab. |
| 191 |
*/ |
| 192 |
public function addTab() |
| 193 |
{ |
| 194 |
Controls_Manager::add_tab($this::BEYONDWORDS_TAB, __('BeyondWords', 'speechkit')); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Register additional document controls. |
| 199 |
* |
| 200 |
* @param PageBase $document The PageBase document. |
| 201 |
*/ |
| 202 |
public function registerDocumentControls($document) |
| 203 |
{ |
| 204 |
BeyondwordsControlsSection::registerControls($document, self::BEYONDWORDS_TAB); |
| 205 |
HelpControlsSection::registerControls($document, self::BEYONDWORDS_TAB); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Before document save. |
| 210 |
* |
| 211 |
* elementor/document/after_save was not working as expected, so we use the |
| 212 |
* Elementors elementor/document/before_save hook for our checks. |
| 213 |
* |
| 214 |
* Fires when document save starts on Elementor. |
| 215 |
* |
| 216 |
* @param \Elementor\Core\Base\Document $document The current document. |
| 217 |
* @param array $data. |
| 218 |
**/ |
| 219 |
public function beforeDocumentSave($document, $data) |
| 220 |
{ |
| 221 |
$postId = $document->get_post()->ID; |
| 222 |
|
| 223 |
if (! empty($data['settings'])) { |
| 224 |
$postId = $document->get_post()->ID; |
| 225 |
$settings = $data['settings']; |
| 226 |
|
| 227 |
if (isset($settings['control_beyondwords_generate_audio'])) { |
| 228 |
switch ($settings['control_beyondwords_generate_audio']) { |
| 229 |
case 'yes': |
| 230 |
update_post_meta($postId, 'beyondwords_generate_audio', '1'); |
| 231 |
break; |
| 232 |
case '': |
| 233 |
update_post_meta($postId, 'beyondwords_generate_audio', '0'); |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (isset($settings['control_beyondwords_display_player'])) { |
| 239 |
switch ($settings['control_beyondwords_display_player']) { |
| 240 |
case 'yes': |
| 241 |
delete_post_meta($postId, 'beyondwords_disabled'); |
| 242 |
break; |
| 243 |
case '': |
| 244 |
update_post_meta($postId, 'beyondwords_disabled', '1'); |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Check whether the Elementor plugin has been installed & activated. |
| 253 |
* |
| 254 |
* If it has then we impose certain conditions to ensure compatibility. |
| 255 |
* |
| 256 |
* @since 4.0.0 |
| 257 |
* |
| 258 |
* @param int|WP_Post $post The WordPress post ID, or post object. |
| 259 |
* |
| 260 |
* @return boolean |
| 261 |
**/ |
| 262 |
public static function isElementorActivated() |
| 263 |
{ |
| 264 |
return is_admin() && ( |
| 265 |
in_array( |
| 266 |
'elementor/elementor.php', |
| 267 |
apply_filters('active_plugins', get_option('active_plugins')) |
| 268 |
) |
| 269 |
); |
| 270 |
} |
| 271 |
} |
| 272 |
|