SeoPreview.php
8.3 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace AIOSEO\Plugin\Common\Standalone;
use AIOSEO\Plugin\Common\Models;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the SEO Preview feature on the front-end.
*
* @since 4.2.8
*/
class SeoPreview {
/**
* Whether this feature is allowed on the current page or not.
*
* @since 4.2.8
*
* @var bool
*/
private $enable = false;
/**
* The relative JS filename for this standalone.
*
* @since 4.3.1
*
* @var string
*/
private $mainAssetRelativeFilename = 'src/vue/standalone/seo-preview/main.js';
/**
* Class constructor.
*
* @since 4.2.8
*/
public function __construct() {
// Hook into `wp` in order to have access to the WP queried object.
add_action( 'wp', [ $this, 'init' ] );
}
/**
* Initialize the feature.
* Hooked into `wp` action hook.
*
* @since 4.2.8
*
* @return void
*/
public function init() {
if (
is_admin() ||
! is_admin_bar_showing() ||
// If we're seeing the Divi theme Visual Builder.
function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled()
) {
return;
}
$allow = [
'archive',
'attachment',
'author',
'date',
'dynamic_home',
'page',
'search',
'single',
'taxonomy',
];
if ( ! in_array( aioseo()->helpers->getTemplateType(), $allow, true ) ) {
return;
}
$this->enable = true;
// As WordPress uses priority 10 to print footer scripts we use 9 to make sure our script still gets output.
add_action( 'wp_print_footer_scripts', [ $this, 'enqueueScript' ], 9 );
}
/**
* Hooked into `wp_print_footer_scripts` action hook.
* Enqueue the standalone JS the latest possible and prevent 3rd-party performance plugins from merging it.
*
* @since 4.3.1
*
* @return void
*/
public function enqueueScript() {
aioseo()->core->assets->load( $this->mainAssetRelativeFilename, [], $this->getVueData(), 'aioseoSeoPreview' );
aioseo()->main->enqueueTranslations();
}
/**
* Returns the data for Vue.
*
* @since 4.2.8
*
* @return array The data.
*/
private function getVueData() {
$queriedObject = get_queried_object();
$templateType = aioseo()->helpers->getTemplateType();
if (
'taxonomy' === $templateType ||
'single' === $templateType ||
'page' === $templateType ||
'attachment' === $templateType
) {
$labels = null;
if ( is_a( $queriedObject, 'WP_Term' ) ) {
$wpObject = $queriedObject;
$labels = get_taxonomy_labels( get_taxonomy( $queriedObject->taxonomy ) );
$editObjectUrl = get_edit_term_link( $queriedObject, $queriedObject->taxonomy );
} else {
$wpObject = aioseo()->helpers->getPost();
if ( is_a( $wpObject, 'WP_Post' ) ) {
$labels = get_post_type_labels( get_post_type_object( $wpObject->post_type ) );
$editObjectUrl = get_edit_post_link( $wpObject, 'url' );
if (
! aioseo()->helpers->isSpecialPage( $wpObject->ID ) &&
'attachment' !== $templateType
) {
$aioseoPost = Models\Post::getPost( $wpObject->ID );
$pageAnalysis = ! empty( $aioseoPost->page_analysis ) ? json_decode( $aioseoPost->page_analysis ) : [ 'analysis' => [] ];
$keyphrases = Models\Post::getKeyphrasesDefaults( $aioseoPost->keyphrases );
}
}
}
// At this point if `$wpObject` is not an instance of WP_Term nor WP_Post, then we can't have the URLs.
if (
is_object( $wpObject ) &&
is_object( $labels )
) {
// Translators: 1 - The singular label for the current post type.
$editObjectBtnText = sprintf( esc_html__( 'Edit %1$s', 'all-in-one-seo-pack' ), $labels->singular_name );
$editGoogleSnippetUrl = $this->getEditSnippetUrl( $templateType, 'google', $wpObject );
$editFacebookSnippetUrl = $this->getEditSnippetUrl( $templateType, 'facebook', $wpObject );
$editTwitterSnippetUrl = $this->getEditSnippetUrl( $templateType, 'twitter', $wpObject );
}
} elseif (
'archive' === $templateType ||
'author' === $templateType ||
'date' === $templateType ||
'search' === $templateType
) {
if ( is_a( $queriedObject, 'WP_User' ) ) {
$editObjectUrl = get_edit_user_link( $queriedObject->ID );
$editObjectBtnText = esc_html__( 'Edit User', 'all-in-one-seo-pack' );
}
$editGoogleSnippetUrl = $this->getEditSnippetUrl( $templateType, 'google' );
} elseif ( 'dynamic_home' === $templateType ) {
$editGoogleSnippetUrl = $this->getEditSnippetUrl( $templateType, 'google' );
$editFacebookSnippetUrl = $this->getEditSnippetUrl( $templateType, 'facebook' );
$editTwitterSnippetUrl = $this->getEditSnippetUrl( $templateType, 'twitter' );
}
return [
'editGoogleSnippetUrl' => isset( $editGoogleSnippetUrl ) ? $editGoogleSnippetUrl : '',
'editFacebookSnippetUrl' => isset( $editFacebookSnippetUrl ) ? $editFacebookSnippetUrl : '',
'editTwitterSnippetUrl' => isset( $editTwitterSnippetUrl ) ? $editTwitterSnippetUrl : '',
'editObjectBtnText' => isset( $editObjectBtnText ) ? $editObjectBtnText : '',
'editObjectUrl' => isset( $editObjectUrl ) ? $editObjectUrl : '',
'keyphrases' => isset( $keyphrases ) ? $keyphrases : '',
'page_analysis' => isset( $pageAnalysis ) ? $pageAnalysis : '',
'urls' => [
'domain' => aioseo()->helpers->getSiteDomain(),
'mainSiteUrl' => aioseo()->helpers->getSiteUrl(),
],
'mainAssetCssQueue' => aioseo()->core->assets->getJsAssetCssQueue( $this->mainAssetRelativeFilename ),
];
}
/**
* Get the URL to the place where the snippet details can be edited.
*
* @since 4.2.8
*
* @param string $templateType The WP template type {@see WpContext::getTemplateType}.
* @param string $snippet 'google', 'facebook' or 'twitter'.
* @param \WP_Post|\WP_Term|null $object Post or term object.
* @return string The URL. Returns an empty string if nothing matches.
*/
private function getEditSnippetUrl( $templateType, $snippet, $object = null ) {
$url = '';
// Bail if `$snippet` doesn't fit requirements.
if ( ! in_array( $snippet, [ 'google', 'facebook', 'twitter' ], true ) ) {
return $url;
}
// If we're in a post/page/term (not an attachment) we'll have a URL directly to the meta box.
if (
'single' === $templateType ||
'page' === $templateType ||
'taxonomy' === $templateType
) {
$url = in_array( $templateType, [ 'single', 'page' ], true )
? get_edit_post_link( $object, 'url' ) . '#aioseo-settings'
: get_edit_term_link( $object, $object->taxonomy ) . '#aioseo-term-settings-field';
// Default `$queryArgs` for 'google' snippet.
$queryArgs = [ 'aioseo-tab' => 'general' ];
if ( in_array( $snippet, [ 'facebook', 'twitter' ], true ) ) {
$queryArgs = [
'aioseo-tab' => 'social',
'social-tab' => $snippet
];
}
return add_query_arg( $queryArgs, $url );
}
// If we're in any sort of archive let's point to the global archive editing.
if (
'archive' === $templateType ||
'author' === $templateType ||
'date' === $templateType ||
'search' === $templateType
) {
return admin_url( 'admin.php?page=aioseo-search-appearance' ) . '#/archives';
}
// If homepage is set to show the latest posts let's point to the global home page editing.
if ( 'dynamic_home' === $templateType ) {
// Default `$url` for 'google' snippet.
$url = add_query_arg(
[ 'aioseo-scroll' => 'home-page-settings' ],
admin_url( 'admin.php?page=aioseo-search-appearance' ) . '#/global-settings'
);
if ( in_array( $snippet, [ 'facebook', 'twitter' ], true ) ) {
$url = admin_url( 'admin.php?page=aioseo-social-networks' ) . '#/' . $snippet;
}
return $url;
}
return $url;
}
/**
* Returns the "SEO Preview" submenu item data ("node" as WP calls it).
*
* @since 4.2.8
*
* @return array The admin bar menu item data or an empty array if this feature is disabled.
*/
public function getAdminBarMenuItemNode() {
if ( ! $this->enable ) {
return [];
}
$title = esc_html__( 'SEO Preview', 'all-in-one-seo-pack' );
// @TODO Remove 'NEW' after a couple months.
$title .= '<span class="aioseo-menu-new-indicator">';
$title .= esc_html__( 'NEW', 'all-in-one-seo-pack' ) . '!';
$title .= '</span>';
return [
'id' => 'aioseo-seo-preview',
'parent' => 'aioseo-main',
'title' => $title,
'href' => '#',
];
}
}