PluginProbe
BeyondWords – AI audio for publishers / 4.6.1
BeyondWords – AI audio for publishers v4.6.1
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / src / Compatibility / Elementor / Elementor.php

Elementor.php in BeyondWords – AI audio for publishers 4.6.1, at src/Compatibility/Elementor/Elementor.php

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