Divi.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace AIOSEO\Plugin\Common\Standalone\PageBuilders;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Integrate our SEO Panel with Divi Page Builder.
*
* @since 4.1.7
*/
class Divi extends Base {
/**
* The theme name.
*
* @since 4.1.7
*
* @var array
*/
public $themes = [ 'Divi' ];
/**
* The plugin files.
*
* @since 4.2.0
*
* @var array
*/
public $plugins = [
'divi-builder/divi-builder.php'
];
/**
* The integration slug.
*
* @since 4.1.7
*
* @var string
*/
public $integrationSlug = 'divi';
/**
* Init the integration.
*
* @since 4.1.7
*
* @return void
*/
public function init() {
add_action( 'wp', [ $this, 'maybeRun' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdmin' ] );
}
/**
* Check if we are in the Page Builder and run the integrations.
*
* @since 4.1.7
*
* @return void
*/
public function maybeRun() {
$postType = get_post_type( $this->getPostId() );
if (
! defined( 'ET_BUILDER_PRODUCT_VERSION' ) ||
! version_compare( '4.9.2', ET_BUILDER_PRODUCT_VERSION, '<=' ) ||
! ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ) ||
! aioseo()->postSettings->canAddPostSettingsMetabox( $postType )
) {
return;
}
add_action( 'wp_footer', [ $this, 'addContainers' ] );
add_action( 'wp_footer', [ $this, 'addIframeWatcher' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
add_filter( 'script_loader_tag', [ $this, 'addEtTag' ], 10, 2 );
}
/**
* Enqueue the required scripts for the admin screen.
*
* @since 4.1.7
*
* @return void
*/
public function enqueueAdmin() {
if ( ! aioseo()->helpers->isScreenBase( 'toplevel_page_et_divi_options' ) ) {
return;
}
aioseo()->core->assets->load( 'src/vue/standalone/divi-admin/main.js', [], aioseo()->helpers->getVueData() );
aioseo()->main->enqueueTranslations();
}
/**
* Add et attributes to script tags.
*
* @since 4.1.7
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
* @return string The tag.
*/
public function addEtTag( $tag, $handle ) {
$scriptHandles = [
'aioseo/js/src/vue/standalone/divi/main.js',
'aioseo/js/src/vue/standalone/app/main.js'
];
if ( in_array( $handle, $scriptHandles, true ) ) {
// These tags load in parent window only, not in Divi iframe.
return preg_replace( '/<script/', '<script class="et_fb_ignore_iframe"', $tag );
}
return $tag;
}
/**
* Add the Divi watcher.
*
* @since 4.1.7
*
* @return void
*/
public function addIframeWatcher() {
?>
<script type="text/javascript">
if (typeof jQuery === 'function') {
jQuery(window).on('et_builder_api_ready et_fb_section_content_change', function(event) {
window.parent.postMessage({ eventType : event.type })
})
}
</script>
<?php
}
/**
* Add the containers to mount our panel.
*
* @since 4.1.7
*
* @return void
*/
public function addContainers() {
echo '<div id="aioseo-app-modal" class="et_fb_ignore_iframe"><div class="et_fb_ignore_iframe"></div></div>';
echo '<div id="aioseo-settings" class="et_fb_ignore_iframe"></div>';
echo '<div id="aioseo-admin" class="et_fb_ignore_iframe"></div>';
echo '<div id="aioseo-modal-portal" class="et_fb_ignore_iframe"></div>';
}
/**
* Returns whether or not the given Post ID was built with Divi.
*
* @since 4.1.7
*
* @param int $postId The Post ID.
* @return boolean Whether or not the Post was built with Divi.
*/
public function isBuiltWith( $postId ) {
if ( ! function_exists( 'et_pb_is_pagebuilder_used' ) ) {
return false;
}
return et_pb_is_pagebuilder_used( $postId );
}
/**
* Returns the Divi edit url for the given Post ID.
*
* @since 4.3.1
*
* @param int $postId The Post ID.
* @return string The Edit URL.
*/
public function getEditUrl( $postId ) {
if ( ! function_exists( 'et_fb_get_vb_url' ) ) {
return '';
}
$isDiviLibrary = 'et_pb_layout' === get_post_type( $postId );
$editUrl = $isDiviLibrary ? get_edit_post_link( $postId, 'raw' ) : get_permalink( $postId );
if ( et_pb_is_pagebuilder_used( $postId ) ) {
$editUrl = et_fb_get_vb_url( $editUrl );
} else {
if ( ! et_pb_is_allowed( 'divi_builder_control' ) ) {
// Prevent link when user lacks `Toggle Divi Builder` capability.
return '';
}
$editUrl = add_query_arg(
[ 'et_fb_activation_nonce' => wp_create_nonce( 'et_fb_activation_nonce_' . $postId ) ],
$editUrl
);
}
return $editUrl;
}
}