|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Define the {@link Wordlift_Uri_Service} responsible for managing entity URIs |
|
4
|
|
|
* (for posts, entities, authors, ...). |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The {@link Wordlift_Uri_Service} class. |
|
9
|
|
|
* |
|
10
|
|
|
* @since 3.7.1 |
|
11
|
|
|
*/ |
|
12
|
|
|
class Wordlift_Uri_Service { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The title regex to sanitize titles in paths. |
|
16
|
|
|
* |
|
17
|
|
|
* According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved: |
|
18
|
|
|
* ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
|
19
|
|
|
* "$" | "," |
|
20
|
|
|
* |
|
21
|
|
|
* We also remove the space and the UTF-8 BOM sequence. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 3.7.1 |
|
24
|
|
|
*/ |
|
25
|
|
|
const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/"; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @since 3.6.0 |
|
31
|
|
|
* @access private |
|
32
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
33
|
|
|
*/ |
|
34
|
|
|
private $log; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The global WordPress database connection. |
|
38
|
|
|
* |
|
39
|
|
|
* @since 3.6.0 |
|
40
|
|
|
* @access private |
|
41
|
|
|
* @var \wpdb $wpdb The global WordPress database connection. |
|
42
|
|
|
*/ |
|
43
|
|
|
private $wpdb; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The {@link Wordlift_Uri_Service} singleton instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @since 3.7.2 |
|
49
|
|
|
* @access private |
|
50
|
|
|
* @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
51
|
|
|
*/ |
|
52
|
|
|
private static $instance; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create an instance of Wordlift_Uri_Service. |
|
56
|
|
|
* |
|
57
|
|
|
* @since 3.6.0 |
|
58
|
|
|
* |
|
59
|
|
|
* @param \wpdb $wpdb The global WordPress database connection. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct( $wpdb ) { |
|
62
|
|
|
|
|
63
|
|
|
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' ); |
|
64
|
|
|
|
|
65
|
|
|
$this->wpdb = $wpdb; |
|
66
|
|
|
|
|
67
|
|
|
self::$instance = $this; |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the {@link Wordlift_Uri_Service} singleton instance. |
|
73
|
|
|
* |
|
74
|
|
|
* @since 3.7.2 |
|
75
|
|
|
* @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function get_instance() { |
|
78
|
|
|
|
|
79
|
|
|
return self::$instance; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Delete all generated URIs from the database. |
|
84
|
|
|
* |
|
85
|
|
|
* @since 3.6.0 |
|
86
|
|
|
*/ |
|
87
|
|
|
public function delete_all() { |
|
88
|
|
|
|
|
89
|
|
|
// Delete URIs associated with posts/entities. |
|
90
|
|
|
$this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) ); |
|
91
|
|
|
|
|
92
|
|
|
// Delete URIs associated with authors. |
|
93
|
|
|
$this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) ); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Sanitizes an URI path by replacing the non allowed characters with an underscore. |
|
99
|
|
|
* |
|
100
|
|
|
* @since 3.7.2 |
|
101
|
|
|
* @uses sanitize_title() to manage not ASCII chars |
|
102
|
|
|
* |
|
103
|
|
|
* @see https://codex.wordpress.org/Function_Reference/sanitize_title |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $path The path to sanitize. |
|
106
|
|
|
* @param string $char The replacement character (by default an underscore). |
|
107
|
|
|
* |
|
108
|
|
|
* @return string The sanitized path. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function sanitize_path( $path, $char = '_' ) { |
|
111
|
|
|
|
|
112
|
|
|
// Ensure the path is ASCII. |
|
113
|
|
|
// see https://github.com/insideout10/wordlift-plugin/issues/386 |
|
114
|
|
|
// $path_ascii = mb_convert_encoding( $path, 'ASCII' ); |
|
115
|
|
|
|
|
116
|
|
|
return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|