Twitter.php
7.64 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
<?php
namespace AIOSEO\Plugin\Common\Social;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the Twitter meta.
*
* @since 4.0.0
*/
class Twitter {
/**
* Returns the Twitter URL for the site.
*
* @since 4.0.0
*
* @return string The Twitter URL.
*/
public function getTwitterUrl() {
if ( ! aioseo()->options->social->profiles->sameUsername->enable ) {
return aioseo()->options->social->profiles->urls->twitterUrl;
}
$userName = aioseo()->options->social->profiles->sameUsername->username;
return ( $userName && in_array( 'twitterUrl', aioseo()->options->social->profiles->sameUsername->included, true ) )
? 'https://twitter.com/' . $userName
: '';
}
/**
* Returns the Twitter card type.
*
* @since 4.0.0
*
* @return string $card The card type.
*/
public function getCardType() {
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
return aioseo()->options->social->twitter->homePage->cardType;
}
$metaData = aioseo()->meta->metaData->getMetaData();
return ! empty( $metaData->twitter_card ) && 'default' !== $metaData->twitter_card ? $metaData->twitter_card : aioseo()->options->social->twitter->general->defaultCardType;
}
/**
* Returns the Twitter creator.
*
* @since 4.0.0
*
* @return string The creator.
*/
public function getCreator() {
$author = '';
$post = aioseo()->helpers->getPost();
if ( $post && aioseo()->options->social->twitter->general->showAuthor ) {
$twitterUser = get_the_author_meta( 'aioseo_twitter', $post->post_author );
$author = $twitterUser ? $twitterUser : aioseo()->social->twitter->getTwitterUrl();
$author = aioseo()->social->twitter->prepareUsername( $author );
}
return $author;
}
/**
* Returns the Twitter image URL.
*
* @since 4.0.0
*
* @param int $postId The post ID (optional).
* @return string The image URL.
*/
public function getImage( $postId = null ) {
$post = aioseo()->helpers->getPost( $postId );
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
$image = aioseo()->options->social->twitter->homePage->image;
if ( empty( $image ) ) {
$image = aioseo()->options->social->facebook->homePage->image;
}
if ( empty( $image ) ) {
$image = aioseo()->social->image->getImage( 'twitter', aioseo()->options->social->twitter->general->defaultImageSourcePosts, $post );
}
return $image ? $image : aioseo()->social->facebook->getImage();
}
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData->twitter_use_og ) ) {
return aioseo()->social->facebook->getImage();
}
$image = '';
if ( ! empty( $metaData ) ) {
$imageSource = ! empty( $metaData->twitter_image_type ) && 'default' !== $metaData->twitter_image_type
? $metaData->twitter_image_type
: aioseo()->options->social->twitter->general->defaultImageSourcePosts;
$image = aioseo()->social->image->getImage( 'twitter', $imageSource, $post );
}
return $image ? $image : aioseo()->social->facebook->getImage();
}
/**
* Returns the Twitter title for the current page.
*
* @since 4.0.0
*
* @param WP_Post|integer $post The post object or ID (optional).
* @return string The Twitter title.
*/
public function getTitle( $post = null ) {
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
$title = aioseo()->meta->title->helpers->prepare( aioseo()->options->social->twitter->homePage->title );
return $title ? $title : aioseo()->social->facebook->getTitle( $post );
}
$post = aioseo()->helpers->getPost( $post );
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData->twitter_use_og ) ) {
return aioseo()->social->facebook->getTitle( $post );
}
$title = '';
if ( ! empty( $metaData->twitter_title ) ) {
$title = aioseo()->meta->title->helpers->prepare( $metaData->twitter_title );
}
return $title ? $title : aioseo()->social->facebook->getTitle( $post );
}
/**
* Returns the Twitter description for the current page.
*
* @since 4.0.0
*
* @param WP_Post|integer $post The post object or ID (optional).
* @return string The Twitter description.
*/
public function getDescription( $post = null ) {
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
$description = aioseo()->meta->description->helpers->prepare( aioseo()->options->social->twitter->homePage->description );
return $description ? $description : aioseo()->social->facebook->getDescription( $post );
}
$post = aioseo()->helpers->getPost( $post );
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData->twitter_use_og ) ) {
return aioseo()->social->facebook->getDescription( $post );
}
$description = '';
if ( ! empty( $metaData->twitter_description ) ) {
$description = aioseo()->meta->description->helpers->prepare( $metaData->twitter_description );
}
return $description ? $description : aioseo()->social->facebook->getDescription( $post );
}
/**
* Prepare twitter username for public display.
*
* We do things like strip out the URL, etc and return just (at)username.
* At the moment, we'll check for 1 of 3 things... (at)username, username, and https://twitter.com/username.
*
* @since 4.0.0
*
* @param string $profile Twitter username.
* @param boolean $includeAt Whether or not ot include the @ sign.
* @return string Full Twitter username.
*/
public function prepareUsername( $profile, $includeAt = true ) {
if ( ! $profile ) {
return $profile;
}
$profile = (string) $profile;
if ( preg_match( '/^(\@)?[A-Za-z0-9_]+$/', $profile ) ) {
if ( '@' !== $profile[0] && $includeAt ) {
$profile = '@' . $profile;
} elseif ( '@' === $profile[0] && ! $includeAt ) {
$profile = ltrim( $profile, '@' );
}
} elseif ( strpos( $profile, 'twitter.com' ) ) {
$profile = esc_url( $profile );
// extract the twitter username from the url.
$parsedTwitterProfile = wp_parse_url( $profile );
$path = $parsedTwitterProfile['path'];
$pathParts = explode( '/', $path );
$profile = $pathParts[1];
if ( $profile ) {
if ( '@' !== $profile[0] && $includeAt ) {
$profile = '@' . $profile;
} elseif ( '@' === $profile[0] && ! $includeAt ) {
$profile = ltrim( $profile, '@' );
}
}
}
return $profile;
}
/**
* Get additional twitter data.
*
* @since 4.0.0
*
* @return array An array of additional twitter data.
*/
public function getAdditionalData() {
if ( ! aioseo()->options->social->twitter->general->additionalData ) {
return [];
}
$data = [];
$post = aioseo()->helpers->getPost();
if ( ! is_a( $post, 'WP_Post' ) ) {
return $data;
}
if ( $post->post_author ) {
$data[] = [
'label' => __( 'Written by', 'all-in-one-seo-pack' ),
'value' => get_the_author_meta( 'display_name', $post->post_author )
];
}
if ( ! empty( $post->post_content ) ) {
$minutes = $this->getReadingTime( $post->post_content );
if ( ! empty( $minutes ) ) {
$data[] = [
'label' => __( 'Est. reading time', 'all-in-one-seo-pack' ),
// Translators: 1 - The estimated reading time.
'value' => sprintf( _n( '%1$s minute', '%1$s minutes', $minutes, 'all-in-one-seo-pack' ), $minutes )
];
}
}
return $data;
}
/**
* Returns the estimated reading time for a string.
*
* @since 4.0.0
*
* @param string $string The string to count.
* @return integer The estimated reading time as an integer.
*/
private function getReadingTime( $string ) {
$wpm = 200;
$word = str_word_count( wp_strip_all_tags( $string ) );
return round( $word / $wpm );
}
}