ThirdParty.php
9.03 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
namespace AIOSEO\Plugin\Common\Traits\Helpers;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Contains all third-party related helper methods.
*
* @since 4.1.4
*/
trait ThirdParty {
/**
* Checks whether WooCommerce is active.
*
* @since 4.0.0
*
* @return boolean Whether WooCommerce is active.
*/
public function isWooCommerceActive() {
return class_exists( 'woocommerce' );
}
/**
* Checks if the current page is a special WooCommerce page (Cart, Checkout, ...).
*
* @since 4.0.0
*
* @param int $postId The post ID.
* @return string|bool The type of page or false if it isn't a WooCommerce page.
*/
public function isWooCommercePage( $postId = 0 ) {
if ( ! $this->isWooCommerceActive() ) {
return false;
}
$postId = $postId ? $postId : get_the_ID();
static $cartPageId;
if ( ! $cartPageId ) {
$cartPageId = (int) get_option( 'woocommerce_cart_page_id' );
}
static $checkoutPageId;
if ( ! $checkoutPageId ) {
$checkoutPageId = (int) get_option( 'woocommerce_checkout_page_id' );
}
static $myAccountPageId;
if ( ! $myAccountPageId ) {
$myAccountPageId = (int) get_option( 'woocommerce_myaccount_page_id' );
}
static $termsPageId;
if ( ! $termsPageId ) {
$termsPageId = (int) get_option( 'woocommerce_terms_page_id' );
}
switch ( $postId ) {
case $cartPageId:
return 'cart';
case $checkoutPageId:
return 'checkout';
case $myAccountPageId:
return 'myAccount';
case $termsPageId:
return 'terms';
default:
return false;
}
}
/**
* Checks whether the current page is a special WooCommerce page we shouldn't show our schema settings for.
*
* @since 4.1.6
*
* @param int $postId The post ID.
* @return bool Whether the current page is a disallowed WooCommerce page.
*/
public function isWooCommercePageWithoutSchema( $postId = 0 ) {
$page = $this->isWooCommercePage( $postId );
if ( ! $page ) {
return false;
}
$disallowedPages = [ 'cart', 'checkout', 'myAccount' ];
return in_array( $page, $disallowedPages, true );
}
/**
* Checks whether the queried object is the WooCommerce shop page.
*
* @since 4.0.0
*
* @param int $id The post ID to check against (optional).
* @return bool Whether the current page is the WooCommerce shop page.
*/
public function isWooCommerceShopPage( $id = 0 ) {
if ( ! $this->isWooCommerceActive() ) {
return false;
}
if ( ! is_admin() && ! aioseo()->helpers->isAjaxCronRest() && function_exists( 'is_shop' ) ) {
return is_shop();
}
$id = ! $id && ! empty( $_GET['post'] ) ? (int) wp_unslash( $_GET['post'] ) : (int) $id; // phpcs:ignore HM.Security.ValidatedSanitizedInput
return $id && wc_get_page_id( 'shop' ) === $id;
}
/**
* Checks whether the queried object is the WooCommerce cart page.
*
* @since 4.1.3
*
* @param int $id The post ID to check against (optional).
* @return bool Whether the current page is the WooCommerce cart page.
*/
public function isWooCommerceCartPage( $id = 0 ) {
if ( ! $this->isWooCommerceActive() ) {
return false;
}
if ( ! is_admin() && ! aioseo()->helpers->isAjaxCronRest() && function_exists( 'is_cart' ) ) {
return is_cart();
}
$id = ! $id && ! empty( $_GET['post'] ) ? (int) wp_unslash( $_GET['post'] ) : (int) $id; // phpcs:ignore HM.Security.ValidatedSanitizedInput
return $id && wc_get_page_id( 'cart' ) === $id;
}
/**
* Checks whether the queried object is the WooCommerce checkout page.
*
* @since 4.1.3
*
* @param int $id The post ID to check against (optional).
* @return bool Whether the current page is the WooCommerce checkout page.
*/
public function isWooCommerceCheckoutPage( $id = 0 ) {
if ( ! $this->isWooCommerceActive() ) {
return false;
}
if ( ! is_admin() && ! aioseo()->helpers->isAjaxCronRest() && function_exists( 'is_checkout' ) ) {
return is_checkout();
}
$id = ! $id && ! empty( $_GET['post'] ) ? (int) wp_unslash( $_GET['post'] ) : (int) $id; // phpcs:ignore HM.Security.ValidatedSanitizedInput
return $id && wc_get_page_id( 'checkout' ) === $id;
}
/**
* Checks whether the queried object is the WooCommerce account page.
*
* @since 4.1.3
*
* @param int $id The post ID to check against (optional).
* @return bool Whether the current page is the WooCommerce account page.
*/
public function isWooCommerceAccountPage( $id = 0 ) {
if ( ! $this->isWooCommerceActive() ) {
return false;
}
if ( ! is_admin() && ! aioseo()->helpers->isAjaxCronRest() && function_exists( 'is_account_page' ) ) {
return is_account_page();
}
$id = ! $id && ! empty( $_GET['post'] ) ? (int) wp_unslash( $_GET['post'] ) : (int) $id; // phpcs:ignore HM.Security.ValidatedSanitizedInput
return $id && wc_get_page_id( 'myaccount' ) === $id;
}
/**
* Internationalize.
*
* @since 4.0.0
*
* @param $in
* @return mixed|void
*/
public function internationalize( $in ) {
if ( function_exists( 'langswitch_filter_langs_with_message' ) ) {
$in = langswitch_filter_langs_with_message( $in );
}
if ( function_exists( 'polyglot_filter' ) ) {
$in = polyglot_filter( $in );
}
if ( function_exists( 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage' ) ) {
$in = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage( $in );
} elseif ( function_exists( 'ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage' ) ) {
$in = ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage( $in );
} elseif ( function_exists( 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' ) ) {
$in = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $in );
}
return apply_filters( 'localization', $in );
}
/**
* Checks if WPML is active.
*
* @since 4.0.0
*
* @return bool True if it is, false if not.
*/
public function isWpmlActive() {
return class_exists( 'SitePress' );
}
/**
* Localizes a given URL.
*
* This is required for compatibility with WPML.
*
* @since 4.0.0
*
* @param string $path The relative path of the URL.
* @return string $url The filtered URL.
*/
public function localizedUrl( $path ) {
$url = apply_filters( 'wpml_home_url', home_url( '/' ) );
// Remove URL parameters.
preg_match_all( '/\?[\s\S]+/', $url, $matches );
// Get the base URL.
$url = preg_replace( '/\?[\s\S]+/', '', $url );
$url = trailingslashit( $url );
$url .= preg_replace( '/\//', '', $path, 1 );
// Readd URL parameters.
if ( $matches && $matches[0] ) {
$url .= $matches[0][0];
}
return $url;
}
/**
* Checks whether BuddyPress is active.
*
* @since 4.0.0
*
* @return boolean
*/
public function isBuddyPressActive() {
return class_exists( 'BuddyPress' );
}
/**
* Checks whether the queried object is a buddy press user page.
*
* @since 4.0.0
*
* @return boolean
*/
public function isBuddyPressUser() {
return $this->isBuddyPressActive() && function_exists( 'bp_is_user' ) && bp_is_user();
}
/**
* Returns if the page is a BuddyPress page (Activity, Members, Groups).
*
* @since 4.0.0
*
* @param int $postId The post ID.
* @return boolean If the page is a BuddyPress page or not.
*/
public function isBuddyPressPage( $postId = false ) {
$bpPages = get_option( 'bp-pages' );
if ( empty( $bpPages ) ) {
return false;
}
foreach ( $bpPages as $page ) {
if ( (int) $page === (int) $postId ) {
return true;
}
}
return false;
}
/**
* Returns ACF fields as an array of meta keys and values.
*
* @since 4.0.6
*
* @param WP_Post|int $post The post.
* @param array $allowedTypes A whitelist of ACF field types.
* @return array An array of meta keys and values.
*/
public function getAcfContent( $post = null, $types = [] ) {
$post = ( $post && is_object( $post ) ) ? $post : $this->getPost( $post );
if ( ! class_exists( 'ACF' ) || ! function_exists( 'get_field_objects' ) ) {
return [];
}
if ( defined( 'ACF_VERSION' ) && version_compare( ACF_VERSION, '5.7.0', '<' ) ) {
return [];
}
// Set defaults.
$allowedTypes = [
'text',
'textarea',
'email',
'url',
'wysiwyg',
'image',
'gallery',
// 'link',
// 'taxonomy',
];
$types = wp_parse_args( $types, $allowedTypes );
$acfFields = [];
$fieldObjects = get_field_objects( $post->ID );
if ( ! empty( $fieldObjects ) ) {
foreach ( $fieldObjects as $field ) {
if ( empty( $field['value'] ) ) {
continue;
}
if ( ! in_array( $field['type'], $types, true ) ) {
continue;
}
if ( 'url' === $field['type'] ) {
// Url field
$value = "<a href='{$field['value']}'>{$field['value']}</a>";
} elseif ( 'image' === $field['type'] ) {
// Image field
$value = "<img src='{$field['value']['url']}'>";
} elseif ( 'gallery' === $field['type'] ) {
// Image field
$value = "<img src='{$field['value'][0]['url']}'>";
} else {
// Other fields
$value = $field['value'];
}
if ( $value ) {
$acfFields[ $field['name'] ] = $value;
}
}
}
return $acfFields;
}
}