Completed
Push — develop ( 723be8...e4b26f )
by David
04:03
created
src/includes/class-wordlift-uri-service.php 2 patches
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -17,188 +17,188 @@
 block discarded – undo
17 17
  */
18 18
 class Wordlift_Uri_Service {
19 19
 
20
-	/**
21
-	 * The title regex to sanitize titles in paths.
22
-	 *
23
-	 * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved:
24
-	 * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
25
-	 * "$" | ","
26
-	 *
27
-	 * We also remove the space and the UTF-8 BOM sequence.
28
-	 *
29
-	 * @since 3.7.1
30
-	 */
31
-	const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/";
32
-
33
-	/**
34
-	 * A {@link Wordlift_Log_Service} instance.
35
-	 *
36
-	 * @since  3.6.0
37
-	 * @access private
38
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
39
-	 */
40
-	private $log;
41
-
42
-	/**
43
-	 * The global WordPress database connection.
44
-	 *
45
-	 * @since  3.6.0
46
-	 * @access private
47
-	 * @var \wpdb $wpdb The global WordPress database connection.
48
-	 */
49
-	private $wpdb;
50
-
51
-	/**
52
-	 * The {@link Wordlift_Uri_Service} singleton instance.
53
-	 *
54
-	 * @since  3.7.2
55
-	 * @access private
56
-	 * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
57
-	 */
58
-	private static $instance;
59
-
60
-	/**
61
-	 * Create an instance of Wordlift_Uri_Service.
62
-	 *
63
-	 * @since 3.6.0
64
-	 *
65
-	 * @param \wpdb $wpdb The global WordPress database connection.
66
-	 */
67
-	public function __construct( $wpdb ) {
68
-
69
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
70
-
71
-		$this->wpdb = $wpdb;
72
-
73
-		self::$instance = $this;
74
-
75
-	}
76
-
77
-	/**
78
-	 * Get the {@link Wordlift_Uri_Service} singleton instance.
79
-	 *
80
-	 * @since 3.7.2
81
-	 * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
82
-	 */
83
-	public static function get_instance() {
84
-
85
-		return self::$instance;
86
-	}
87
-
88
-	/**
89
-	 * Delete all generated URIs from the database.
90
-	 *
91
-	 * @since 3.6.0
92
-	 */
93
-	public function delete_all() {
94
-
95
-		$this->log->trace( 'Going to delete all the `entity_url` post metas...' );
96
-
97
-		// Delete URIs associated with posts/entities.
98
-		$this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) );
99
-
100
-		$this->log->trace( 'Going to delete all the `_wl_uri` user metas...' );
101
-
102
-		// Delete URIs associated with authors.
103
-		$this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) );
104
-
105
-		$this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' );
106
-
107
-	}
108
-
109
-	/**
110
-	 * Sanitizes an URI path by replacing the non allowed characters with an underscore.
111
-	 *
112
-	 * @since 3.7.2
113
-	 * @uses  sanitize_title() to manage not ASCII chars
114
-	 *
115
-	 * @see   https://codex.wordpress.org/Function_Reference/sanitize_title
116
-	 *
117
-	 * @param string $path The path to sanitize.
118
-	 * @param string $char The replacement character (by default an underscore).
119
-	 *
120
-	 * @return string The sanitized path.
121
-	 */
122
-	public function sanitize_path( $path, $char = '_' ) {
123
-
124
-		// Ensure the path is ASCII.
125
-		// see https://github.com/insideout10/wordlift-plugin/issues/386
20
+    /**
21
+     * The title regex to sanitize titles in paths.
22
+     *
23
+     * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved:
24
+     * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
25
+     * "$" | ","
26
+     *
27
+     * We also remove the space and the UTF-8 BOM sequence.
28
+     *
29
+     * @since 3.7.1
30
+     */
31
+    const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/";
32
+
33
+    /**
34
+     * A {@link Wordlift_Log_Service} instance.
35
+     *
36
+     * @since  3.6.0
37
+     * @access private
38
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
39
+     */
40
+    private $log;
41
+
42
+    /**
43
+     * The global WordPress database connection.
44
+     *
45
+     * @since  3.6.0
46
+     * @access private
47
+     * @var \wpdb $wpdb The global WordPress database connection.
48
+     */
49
+    private $wpdb;
50
+
51
+    /**
52
+     * The {@link Wordlift_Uri_Service} singleton instance.
53
+     *
54
+     * @since  3.7.2
55
+     * @access private
56
+     * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
57
+     */
58
+    private static $instance;
59
+
60
+    /**
61
+     * Create an instance of Wordlift_Uri_Service.
62
+     *
63
+     * @since 3.6.0
64
+     *
65
+     * @param \wpdb $wpdb The global WordPress database connection.
66
+     */
67
+    public function __construct( $wpdb ) {
68
+
69
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
70
+
71
+        $this->wpdb = $wpdb;
72
+
73
+        self::$instance = $this;
74
+
75
+    }
76
+
77
+    /**
78
+     * Get the {@link Wordlift_Uri_Service} singleton instance.
79
+     *
80
+     * @since 3.7.2
81
+     * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
82
+     */
83
+    public static function get_instance() {
84
+
85
+        return self::$instance;
86
+    }
87
+
88
+    /**
89
+     * Delete all generated URIs from the database.
90
+     *
91
+     * @since 3.6.0
92
+     */
93
+    public function delete_all() {
94
+
95
+        $this->log->trace( 'Going to delete all the `entity_url` post metas...' );
96
+
97
+        // Delete URIs associated with posts/entities.
98
+        $this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) );
99
+
100
+        $this->log->trace( 'Going to delete all the `_wl_uri` user metas...' );
101
+
102
+        // Delete URIs associated with authors.
103
+        $this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) );
104
+
105
+        $this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' );
106
+
107
+    }
108
+
109
+    /**
110
+     * Sanitizes an URI path by replacing the non allowed characters with an underscore.
111
+     *
112
+     * @since 3.7.2
113
+     * @uses  sanitize_title() to manage not ASCII chars
114
+     *
115
+     * @see   https://codex.wordpress.org/Function_Reference/sanitize_title
116
+     *
117
+     * @param string $path The path to sanitize.
118
+     * @param string $char The replacement character (by default an underscore).
119
+     *
120
+     * @return string The sanitized path.
121
+     */
122
+    public function sanitize_path( $path, $char = '_' ) {
123
+
124
+        // Ensure the path is ASCII.
125
+        // see https://github.com/insideout10/wordlift-plugin/issues/386
126 126
 //		$path_ascii = mb_convert_encoding( $path, 'ASCII' );
127 127
 
128
-		return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
129
-	}
130
-
131
-	/**
132
-	 * Build an entity uri for a given title. The uri is composed using a given
133
-	 * post_type and a title. If already exists an entity e2 with a given uri a
134
-	 * numeric suffix is added. If a schema type is given entities with same label
135
-	 * and same type are overridden.
136
-	 *
137
-	 * @since 3.5.0
138
-	 *
139
-	 * @param string  $title A post title.
140
-	 * @param string  $post_type A post type. Default value is 'entity'
141
-	 * @param string  $schema_type A schema org type.
142
-	 * @param integer $increment_digit A digit used to call recursively the same function.
143
-	 *
144
-	 * @return string Returns an uri.
145
-	 */
146
-	public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
147
-
148
-		// Get the entity slug suffix digit
149
-		$suffix_digit = $increment_digit + 1;
150
-
151
-		// Get a sanitized uri for a given title.
152
-		/*
128
+        return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
129
+    }
130
+
131
+    /**
132
+     * Build an entity uri for a given title. The uri is composed using a given
133
+     * post_type and a title. If already exists an entity e2 with a given uri a
134
+     * numeric suffix is added. If a schema type is given entities with same label
135
+     * and same type are overridden.
136
+     *
137
+     * @since 3.5.0
138
+     *
139
+     * @param string  $title A post title.
140
+     * @param string  $post_type A post type. Default value is 'entity'
141
+     * @param string  $schema_type A schema org type.
142
+     * @param integer $increment_digit A digit used to call recursively the same function.
143
+     *
144
+     * @return string Returns an uri.
145
+     */
146
+    public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
147
+
148
+        // Get the entity slug suffix digit
149
+        $suffix_digit = $increment_digit + 1;
150
+
151
+        // Get a sanitized uri for a given title.
152
+        /*
153 153
 		 * The call takes into consideration URL encoding.
154 154
 		 *
155 155
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/885
156 156
 		 *
157 157
 		 * @since 3.20.0
158 158
 		 */
159
-		$entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
160
-		               . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
159
+        $entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
160
+                        . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
161 161
 
162
-		// Compose a candidate uri.
163
-		$new_entity_uri = sprintf( '%s/%s/%s',
164
-			wl_configuration_get_redlink_dataset_uri(),
165
-			$post_type,
166
-			$entity_slug
167
-		);
162
+        // Compose a candidate uri.
163
+        $new_entity_uri = sprintf( '%s/%s/%s',
164
+            wl_configuration_get_redlink_dataset_uri(),
165
+            $post_type,
166
+            $entity_slug
167
+        );
168 168
 
169
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
169
+        $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
170 170
 
171
-		global $wpdb;
171
+        global $wpdb;
172 172
 
173
-		// Check if the candidated uri already is used
174
-		$stmt = $wpdb->prepare(
175
-			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1",
176
-			WL_ENTITY_URL_META_NAME,
177
-			$new_entity_uri
178
-		);
173
+        // Check if the candidated uri already is used
174
+        $stmt = $wpdb->prepare(
175
+            "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1",
176
+            WL_ENTITY_URL_META_NAME,
177
+            $new_entity_uri
178
+        );
179 179
 
180
-		// Perform the query
181
-		$post_id = $wpdb->get_var( $stmt );
180
+        // Perform the query
181
+        $post_id = $wpdb->get_var( $stmt );
182 182
 
183
-		// If the post does not exist, then the new uri is returned
184
-		if ( ! is_numeric( $post_id ) ) {
185
-			$this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
183
+        // If the post does not exist, then the new uri is returned
184
+        if ( ! is_numeric( $post_id ) ) {
185
+            $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
186 186
 
187
-			return $new_entity_uri;
188
-		}
187
+            return $new_entity_uri;
188
+        }
189 189
 
190
-		// If schema_type is equal to schema org type of post x, then the new uri is returned
191
-		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
190
+        // If schema_type is equal to schema org type of post x, then the new uri is returned
191
+        $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
192 192
 
193
-		// @todo: we shouldn't rely on css classes to take such decisions.
194
-		if ( $schema_type === $schema_post_type['css_class'] ) {
195
-			$this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
193
+        // @todo: we shouldn't rely on css classes to take such decisions.
194
+        if ( $schema_type === $schema_post_type['css_class'] ) {
195
+            $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
196 196
 
197
-			return $new_entity_uri;
198
-		}
197
+            return $new_entity_uri;
198
+        }
199 199
 
200
-		// Otherwise the same function is called recursively
201
-		return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
202
-	}
200
+        // Otherwise the same function is called recursively
201
+        return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
202
+    }
203 203
 
204 204
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -64,9 +64,9 @@  discard block
 block discarded – undo
64 64
 	 *
65 65
 	 * @param \wpdb $wpdb The global WordPress database connection.
66 66
 	 */
67
-	public function __construct( $wpdb ) {
67
+	public function __construct($wpdb) {
68 68
 
69
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
69
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Uri_Service');
70 70
 
71 71
 		$this->wpdb = $wpdb;
72 72
 
@@ -92,17 +92,17 @@  discard block
 block discarded – undo
92 92
 	 */
93 93
 	public function delete_all() {
94 94
 
95
-		$this->log->trace( 'Going to delete all the `entity_url` post metas...' );
95
+		$this->log->trace('Going to delete all the `entity_url` post metas...');
96 96
 
97 97
 		// Delete URIs associated with posts/entities.
98
-		$this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) );
98
+		$this->wpdb->delete($this->wpdb->postmeta, array('meta_key' => 'entity_url'));
99 99
 
100
-		$this->log->trace( 'Going to delete all the `_wl_uri` user metas...' );
100
+		$this->log->trace('Going to delete all the `_wl_uri` user metas...');
101 101
 
102 102
 		// Delete URIs associated with authors.
103
-		$this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) );
103
+		$this->wpdb->delete($this->wpdb->usermeta, array('meta_key' => '_wl_uri'));
104 104
 
105
-		$this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' );
105
+		$this->log->debug('`entity_url` post metas and `_wl_uri` user metas deleted.');
106 106
 
107 107
 	}
108 108
 
@@ -119,13 +119,13 @@  discard block
 block discarded – undo
119 119
 	 *
120 120
 	 * @return string The sanitized path.
121 121
 	 */
122
-	public function sanitize_path( $path, $char = '_' ) {
122
+	public function sanitize_path($path, $char = '_') {
123 123
 
124 124
 		// Ensure the path is ASCII.
125 125
 		// see https://github.com/insideout10/wordlift-plugin/issues/386
126 126
 //		$path_ascii = mb_convert_encoding( $path, 'ASCII' );
127 127
 
128
-		return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
128
+		return sanitize_title(preg_replace(self::INVALID_CHARACTERS, $char, stripslashes($path)));
129 129
 	}
130 130
 
131 131
 	/**
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
 	 *
144 144
 	 * @return string Returns an uri.
145 145
 	 */
146
-	public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
146
+	public function build_uri($title, $post_type, $schema_type = null, $increment_digit = 0) {
147 147
 
148 148
 		// Get the entity slug suffix digit
149 149
 		$suffix_digit = $increment_digit + 1;
@@ -156,17 +156,17 @@  discard block
 block discarded – undo
156 156
 		 *
157 157
 		 * @since 3.20.0
158 158
 		 */
159
-		$entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
160
-		               . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
159
+		$entity_slug = urldecode(wl_sanitize_uri_path($title))
160
+		               . (0 === $increment_digit ? '' : '_'.$suffix_digit);
161 161
 
162 162
 		// Compose a candidate uri.
163
-		$new_entity_uri = sprintf( '%s/%s/%s',
163
+		$new_entity_uri = sprintf('%s/%s/%s',
164 164
 			wl_configuration_get_redlink_dataset_uri(),
165 165
 			$post_type,
166 166
 			$entity_slug
167 167
 		);
168 168
 
169
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
169
+		$this->log->trace("Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]");
170 170
 
171 171
 		global $wpdb;
172 172
 
@@ -178,27 +178,27 @@  discard block
 block discarded – undo
178 178
 		);
179 179
 
180 180
 		// Perform the query
181
-		$post_id = $wpdb->get_var( $stmt );
181
+		$post_id = $wpdb->get_var($stmt);
182 182
 
183 183
 		// If the post does not exist, then the new uri is returned
184
-		if ( ! is_numeric( $post_id ) ) {
185
-			$this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
184
+		if ( ! is_numeric($post_id)) {
185
+			$this->log->trace("Going to return uri [ new_entity_uri :: $new_entity_uri ]");
186 186
 
187 187
 			return $new_entity_uri;
188 188
 		}
189 189
 
190 190
 		// If schema_type is equal to schema org type of post x, then the new uri is returned
191
-		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
191
+		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
192 192
 
193 193
 		// @todo: we shouldn't rely on css classes to take such decisions.
194
-		if ( $schema_type === $schema_post_type['css_class'] ) {
195
-			$this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
194
+		if ($schema_type === $schema_post_type['css_class']) {
195
+			$this->log->trace("An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]");
196 196
 
197 197
 			return $new_entity_uri;
198 198
 		}
199 199
 
200 200
 		// Otherwise the same function is called recursively
201
-		return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
201
+		return $this->build_uri($title, $post_type, $schema_type, ++ $increment_digit);
202 202
 	}
203 203
 
204 204
 }
Please login to merge, or discard this patch.