Helpers.php
3.13 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
<?php
namespace AIOSEO\Plugin\Common\Meta;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Contains helper methods for the title/description classes.
*
* @since 4.1.2
*/
class Helpers {
/**
* The name of the class where this instance is constructed.
*
* @since 4.1.2
*
* @param string $name The name of the class. Either "title" or "description".
*/
private $name;
/**
* Supported filters we can run after preparing the value.
*
* @since 4.1.2
*
* @var array
*/
private $supportedFilters = [
'title' => 'aioseo_title',
'description' => 'aioseo_description'
];
/**
* Class constructor.
*
* @since 4.1.2
*
* @param string $name The name of the class where this instance is constructed.
*/
public function __construct( $name ) {
$this->name = $name;
}
/**
* Sanitizes the title/description.
*
* @since 4.1.2
*
* @param string $value The value.
* @param int|bool $objectId The post/term ID.
* @param bool $replaceTags Whether the smart tags should be replaced.
* @return string The sanitized value.
*/
public function sanitize( $value, $objectId = false, $replaceTags = false ) {
$value = $replaceTags ? $value : aioseo()->tags->replaceTags( $value, $objectId );
$value = aioseo()->helpers->doShortcodes( $value );
$value = aioseo()->helpers->decodeHtmlEntities( $value );
$value = $this->encodeExceptions( $value );
$value = wp_strip_all_tags( strip_shortcodes( $value ) );
// Because we encoded the exceptions, we need to decode them again first to prevent double encoding later down the line.
$value = aioseo()->helpers->decodeHtmlEntities( $value );
// Trim internal and external whitespace.
$value = preg_replace( '/[\s]+/u', ' ', trim( $value ) );
return aioseo()->helpers->internationalize( $value );
}
/**
* Prepares the title/description before returning it.
*
* @since 4.1.2
*
* @param string $value The value.
* @param int|bool $objectId The post/term ID.
* @param bool $replaceTags Whether the smart tags should be replaced.
* @return string The sanitized value.
*/
public function prepare( $value, $objectId = false, $replaceTags = false ) {
if (
! empty( $value ) &&
! is_admin() &&
1 < aioseo()->helpers->getPageNumber()
) {
$value .= ' ' . trim( aioseo()->options->searchAppearance->advanced->pagedFormat );
}
$value = $replaceTags ? $value : aioseo()->tags->replaceTags( $value, $objectId );
$value = apply_filters( $this->supportedFilters[ $this->name ], $value );
return $this->sanitize( $value, $objectId, $replaceTags );
}
/**
* Encodes a number of exceptions before we strip tags.
* We need this function to allow certain character (combinations) in the title/description.
*
* @since 4.1.1
*
* @param string $string The string.
* @return string $string The string with exceptions encoded.
*/
public function encodeExceptions( $string ) {
$exceptions = [ '<3' ];
foreach ( $exceptions as $exception ) {
$string = preg_replace( "/$exception/", aioseo()->helpers->encodeOutputHtml( $exception ), $string );
}
return $string;
}
}