Description.php
7.47 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
<?php
namespace AIOSEO\Plugin\Common\Meta;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the (Open Graph) description.
*
* @since 4.0.0
*/
class Description {
/**
* Class constructor.
*
* @since 4.1.2
*/
public function __construct() {
$this->helpers = new Helpers( 'description' );
}
/**
* Returns the homepage description.
*
* @since 4.0.0
*
* @return string The homepage description.
*/
public function getHomePageDescription() {
if ( 'page' === get_option( 'show_on_front' ) ) {
$description = $this->getPostDescription( (int) get_option( 'page_on_front' ) );
return $description ? $description : aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'description' ) );
}
$description = $this->helpers->prepare( aioseo()->options->searchAppearance->global->metaDescription );
return $description ? $description : aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'description' ) );
}
/**
* Returns the description for the current page.
*
* @since 4.0.0
*
* @param WP_Post $post The post object (optional).
* @param boolean $default Whether we want the default value, not the post one.
* @return string The page description.
*/
public function getDescription( $post = null, $default = false ) {
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
return $this->getHomePageDescription();
}
if ( $post || is_singular() || aioseo()->helpers->isStaticPage() ) {
$description = $this->getPostDescription( $post, $default );
if ( $description ) {
return $description;
}
if ( is_attachment() ) {
$post = empty( $post ) ? aioseo()->helpers->getPost() : $post;
$caption = wp_get_attachment_caption( $post->ID );
return $caption ? $this->helpers->prepare( $caption ) : $this->helpers->prepare( $post->post_content );
}
}
if ( is_category() || is_tag() || is_tax() ) {
$term = $post ? $post : get_queried_object();
return $this->getTermDescription( $term, $default );
}
if ( is_author() ) {
$description = $this->helpers->prepare( aioseo()->options->searchAppearance->archives->author->metaDescription );
if ( $description ) {
return $description;
}
$author = get_queried_object();
return $author ? $this->helpers->prepare( get_the_author_meta( 'description', $author->ID ) ) : '';
}
if ( is_date() ) {
return $this->helpers->prepare( aioseo()->options->searchAppearance->archives->date->metaDescription );
}
if ( is_search() ) {
return $this->helpers->prepare( aioseo()->options->searchAppearance->archives->search->metaDescription );
}
if ( is_archive() ) {
$postType = get_queried_object();
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( $dynamicOptions->searchAppearance->archives->has( $postType->name ) ) {
return $this->helpers->prepare( aioseo()->dynamicOptions->searchAppearance->archives->{ $postType->name }->metaDescription );
}
}
return '';
}
/**
* Returns the description for a given post.
*
* @since 4.0.0
*
* @param WP_Post|int $post The post object or ID.
* @param boolean $default Whether we want the default value, not the post one.
* @return string The post description.
*/
public function getPostDescription( $post, $default = false ) {
$post = $post && is_object( $post ) ? $post : aioseo()->helpers->getPost( $post );
if ( ! is_a( $post, 'WP_Post' ) ) {
return '';
}
static $posts = [];
if ( isset( $posts[ $post->ID ] ) ) {
return $posts[ $post->ID ];
}
$description = '';
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData->description ) && ! $default ) {
$description = $this->helpers->prepare( $metaData->description, $post->ID, false, false );
}
if (
$description ||
(
in_array( 'autogenerateDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) &&
! aioseo()->options->deprecated->searchAppearance->advanced->autogenerateDescriptions
)
) {
$posts[ $post->ID ] = $description;
return $description;
}
$description = $this->helpers->sanitize( $this->getPostTypeDescription( $post->post_type ), $post->ID, $default );
$generateDescriptions = apply_filters( 'aioseo_generate_descriptions_from_content', true, [ $post ] );
if ( ! $description && ! post_password_required( $post ) ) {
$description = $post->post_excerpt;
if (
$generateDescriptions &&
in_array( 'useContentForAutogeneratedDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) &&
aioseo()->options->deprecated->searchAppearance->advanced->useContentForAutogeneratedDescriptions
) {
$description = aioseo()->helpers->getDescriptionFromContent( $post );
}
$description = $this->helpers->sanitize( $description, $post->ID, $default );
if ( ! $description && $generateDescriptions && $post->post_content ) {
$description = $this->helpers->sanitize( aioseo()->helpers->getDescriptionFromContent( $post ), $post->ID, $default, false );
}
}
if ( ! is_paged() ) {
if ( in_array( 'descriptionFormat', aioseo()->internalOptions->deprecatedOptions, true ) ) {
$descriptionFormat = aioseo()->options->deprecated->searchAppearance->global->descriptionFormat;
if ( $descriptionFormat ) {
$description = preg_replace( '/#description/', $description, $descriptionFormat );
}
}
}
$posts[ $post->ID ] = $description ? $this->helpers->prepare( $description, $post->ID, $default ) : $this->helpers->prepare( term_description( '' ), $post->ID, $default );
return $posts[ $post->ID ];
}
/**
* Retrieve the default description for the post type.
*
* @since 4.0.6
*
* @param string $postType The post type.
* @return string The description.
*/
public function getPostTypeDescription( $postType ) {
static $postTypeDescription = [];
if ( isset( $postTypeDescription[ $postType ] ) ) {
return $postTypeDescription[ $postType ];
}
if ( aioseo()->dynamicOptions->searchAppearance->postTypes->has( $postType ) ) {
$description = aioseo()->dynamicOptions->searchAppearance->postTypes->{$postType}->metaDescription;
}
$postTypeDescription[ $postType ] = empty( $description ) ? '' : $description;
return $postTypeDescription[ $postType ];
}
/**
* Returns the term description.
*
* @since 4.0.6
*
* @param WP_Term $term The term object.
* @param boolean $default Whether we want the default value, not the post one.
* @return string The term description.
*/
public function getTermDescription( $term, $default = false ) {
if ( ! is_a( $term, 'WP_Term' ) ) {
return '';
}
static $terms = [];
if ( isset( $terms[ $term->term_id ] ) ) {
return $terms[ $term->term_id ];
}
$description = '';
if (
in_array( 'autogenerateDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) &&
! aioseo()->options->deprecated->searchAppearance->advanced->autogenerateDescriptions
) {
$terms[ $term->term_id ] = $description;
return $description;
}
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( ! $description && $dynamicOptions->searchAppearance->taxonomies->has( $term->taxonomy ) ) {
$description = $this->helpers->prepare( aioseo()->dynamicOptions->searchAppearance->taxonomies->{$term->taxonomy}->metaDescription, false, $default );
}
$terms[ $term->term_id ] = $description ? $description : $this->helpers->prepare( term_description( $term->term_id ), false, $default );
return $terms[ $term->term_id ];
}
}