Completed
Push — develop ( 789caa...a4a0e1 )
by David
03:01
created
src/includes/class-wordlift-entity-link-service.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -187,7 +187,7 @@
 block discarded – undo
187 187
 	 * @since 3.6.0
188 188
 	 *
189 189
 	 * @param string $slug       The slug.
190
-	 * @param array  $post_types An array of post types.
190
+	 * @param string[]  $post_types An array of post types.
191 191
 	 *
192 192
 	 * @return bool True if the slug exists, otherwise false.
193 193
 	 */
Please login to merge, or discard this patch.
Indentation   +173 added lines, -173 removed lines patch added patch discarded remove patch
@@ -26,178 +26,178 @@
 block discarded – undo
26 26
  */
27 27
 class Wordlift_Entity_Link_Service {
28 28
 
29
-	/**
30
-	 * The entity type service.
31
-	 *
32
-	 * @since  3.6.0
33
-	 * @access private
34
-	 * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
35
-	 */
36
-	private $entity_type_service;
37
-
38
-	/**
39
-	 * The entity post type slug.
40
-	 *
41
-	 * @since  3.6.0
42
-	 * @access private
43
-	 * @var string $slug The entity post type slug.
44
-	 */
45
-	private $slug;
46
-
47
-	/**
48
-	 * A logger instance.
49
-	 *
50
-	 * @since  3.6.0
51
-	 * @access private
52
-	 * @var Wordlift_Log_Service
53
-	 */
54
-	private $log;
55
-
56
-	/**
57
-	 * Wordlift_Entity_Link_Service constructor.
58
-	 *
59
-	 * @since 3.6.0
60
-	 *
61
-	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
62
-	 * @param string                            $slug The entity post type slug.
63
-	 */
64
-	public function __construct( $entity_type_service, $slug ) {
65
-
66
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
67
-
68
-		$this->entity_type_service = $entity_type_service;
69
-		$this->slug                = $slug;
70
-
71
-	}
72
-
73
-	/**
74
-	 * Intercept link generation to posts in order to customize links to entities.
75
-	 *
76
-	 * @since 3.6.0
77
-	 *
78
-	 * @param string  $post_link The post's permalink.
79
-	 * @param WP_Post $post      The post in question.
80
-	 * @param bool    $leavename Whether to keep the post name.
81
-	 * @param bool    $sample    Is it a sample permalink.
82
-	 *
83
-	 * @return string The link to the post.
84
-	 */
85
-	public function post_type_link( $post_link, $post, $leavename, $sample ) {
86
-
87
-		// Return the post link if this is not our post type.
88
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
89
-			return $post_link;
90
-		}
91
-
92
-		// Replace /slug/post_name/ with /post_name/
93
-		// The slug comes from the Entity Type Service since that service is responsible for registering the default
94
-		// slug.
95
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
96
-	}
97
-
98
-	/**
99
-	 * Alter the query to look for our own custom type.
100
-	 *
101
-	 * @since 3.6.0
102
-	 *
103
-	 * @param WP_Query $query
104
-	 */
105
-	public function pre_get_posts( $query ) {
106
-
107
-		// If a slug has been set, we don't need to alter the query.
108
-		if ( ! empty( $this->slug ) ) {
109
-			return;
110
-		}
111
-
112
-		// Check if it's a query we should extend with our own custom post type.
113
-		//
114
-		// The `$query->query` count could be > 2 if the preview parameter is passed too.
115
-		//
116
-		// See https://github.com/insideout10/wordlift-plugin/issues/439
117
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
118
-			return;
119
-		}
120
-
121
-		// Add our own post type to the query.
122
-		$post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array();
123
-		$query->set( 'post_type', array_merge( $post_type, array(
124
-			'post',
125
-			$this->entity_type_service->get_post_type(),
126
-			'page',
127
-		) ) );
128
-
129
-	}
130
-
131
-	/**
132
-	 * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
133
-	 *
134
-	 * @since 3.6.0
135
-	 *
136
-	 * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
137
-	 * @param string $slug      The post slug.
138
-	 * @param string $post_type Post type.
139
-	 *
140
-	 * @return bool Whether the slug is bad.
141
-	 */
142
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
143
-
144
-		// The list of post types that might have conflicting slugs.
145
-		$post_types = array( 'post', 'page', $this->entity_type_service->get_post_type() );
146
-
147
-		// Ignore post types different from the ones we need to check.
148
-		if ( ! in_array( $post_type, $post_types ) ) {
149
-			return $bad_slug;
150
-		}
151
-
152
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
153
-
154
-		$this->log->debug( "[ exists :: " . ( $exists ? "yes" : "no" ) . " ]" );
155
-
156
-		return $exists;
157
-	}
158
-
159
-	/**
160
-	 * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved.
161
-	 *
162
-	 * @since 3.6.0
163
-	 *
164
-	 * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
165
-	 * @param string $slug      The post slug.
166
-	 * @param string $post_type Post type.
167
-	 * @param int    $post_parent
168
-	 *
169
-	 * @return bool Whether the slug is bad.
170
-	 */
171
-	public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) {
172
-
173
-		// We only care about pages here.
174
-		if ( 'page' !== $post_type ) {
175
-			return $bad_slug;
176
-		}
177
-
178
-		// We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue
179
-		// about overlapping slugs among pages and posts:
180
-		//  https://core.trac.wordpress.org/ticket/13459
181
-		return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type );
182
-	}
183
-
184
-	/**
185
-	 * Check whether a slug exists already for the specified post types.
186
-	 *
187
-	 * @since 3.6.0
188
-	 *
189
-	 * @param string $slug       The slug.
190
-	 * @param array  $post_types An array of post types.
191
-	 *
192
-	 * @return bool True if the slug exists, otherwise false.
193
-	 */
194
-	private function slug_exists( $slug, $post_types ) {
195
-		global $wpdb;
196
-
197
-		// Post slugs must be unique across all posts.
198
-		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1";
199
-
200
-		return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) );
201
-	}
29
+    /**
30
+     * The entity type service.
31
+     *
32
+     * @since  3.6.0
33
+     * @access private
34
+     * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
35
+     */
36
+    private $entity_type_service;
37
+
38
+    /**
39
+     * The entity post type slug.
40
+     *
41
+     * @since  3.6.0
42
+     * @access private
43
+     * @var string $slug The entity post type slug.
44
+     */
45
+    private $slug;
46
+
47
+    /**
48
+     * A logger instance.
49
+     *
50
+     * @since  3.6.0
51
+     * @access private
52
+     * @var Wordlift_Log_Service
53
+     */
54
+    private $log;
55
+
56
+    /**
57
+     * Wordlift_Entity_Link_Service constructor.
58
+     *
59
+     * @since 3.6.0
60
+     *
61
+     * @param Wordlift_Entity_Post_Type_Service $entity_type_service
62
+     * @param string                            $slug The entity post type slug.
63
+     */
64
+    public function __construct( $entity_type_service, $slug ) {
65
+
66
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
67
+
68
+        $this->entity_type_service = $entity_type_service;
69
+        $this->slug                = $slug;
70
+
71
+    }
72
+
73
+    /**
74
+     * Intercept link generation to posts in order to customize links to entities.
75
+     *
76
+     * @since 3.6.0
77
+     *
78
+     * @param string  $post_link The post's permalink.
79
+     * @param WP_Post $post      The post in question.
80
+     * @param bool    $leavename Whether to keep the post name.
81
+     * @param bool    $sample    Is it a sample permalink.
82
+     *
83
+     * @return string The link to the post.
84
+     */
85
+    public function post_type_link( $post_link, $post, $leavename, $sample ) {
86
+
87
+        // Return the post link if this is not our post type.
88
+        if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
89
+            return $post_link;
90
+        }
91
+
92
+        // Replace /slug/post_name/ with /post_name/
93
+        // The slug comes from the Entity Type Service since that service is responsible for registering the default
94
+        // slug.
95
+        return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
96
+    }
97
+
98
+    /**
99
+     * Alter the query to look for our own custom type.
100
+     *
101
+     * @since 3.6.0
102
+     *
103
+     * @param WP_Query $query
104
+     */
105
+    public function pre_get_posts( $query ) {
106
+
107
+        // If a slug has been set, we don't need to alter the query.
108
+        if ( ! empty( $this->slug ) ) {
109
+            return;
110
+        }
111
+
112
+        // Check if it's a query we should extend with our own custom post type.
113
+        //
114
+        // The `$query->query` count could be > 2 if the preview parameter is passed too.
115
+        //
116
+        // See https://github.com/insideout10/wordlift-plugin/issues/439
117
+        if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
118
+            return;
119
+        }
120
+
121
+        // Add our own post type to the query.
122
+        $post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array();
123
+        $query->set( 'post_type', array_merge( $post_type, array(
124
+            'post',
125
+            $this->entity_type_service->get_post_type(),
126
+            'page',
127
+        ) ) );
128
+
129
+    }
130
+
131
+    /**
132
+     * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
133
+     *
134
+     * @since 3.6.0
135
+     *
136
+     * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
137
+     * @param string $slug      The post slug.
138
+     * @param string $post_type Post type.
139
+     *
140
+     * @return bool Whether the slug is bad.
141
+     */
142
+    public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
143
+
144
+        // The list of post types that might have conflicting slugs.
145
+        $post_types = array( 'post', 'page', $this->entity_type_service->get_post_type() );
146
+
147
+        // Ignore post types different from the ones we need to check.
148
+        if ( ! in_array( $post_type, $post_types ) ) {
149
+            return $bad_slug;
150
+        }
151
+
152
+        $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
153
+
154
+        $this->log->debug( "[ exists :: " . ( $exists ? "yes" : "no" ) . " ]" );
155
+
156
+        return $exists;
157
+    }
158
+
159
+    /**
160
+     * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved.
161
+     *
162
+     * @since 3.6.0
163
+     *
164
+     * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
165
+     * @param string $slug      The post slug.
166
+     * @param string $post_type Post type.
167
+     * @param int    $post_parent
168
+     *
169
+     * @return bool Whether the slug is bad.
170
+     */
171
+    public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) {
172
+
173
+        // We only care about pages here.
174
+        if ( 'page' !== $post_type ) {
175
+            return $bad_slug;
176
+        }
177
+
178
+        // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue
179
+        // about overlapping slugs among pages and posts:
180
+        //  https://core.trac.wordpress.org/ticket/13459
181
+        return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type );
182
+    }
183
+
184
+    /**
185
+     * Check whether a slug exists already for the specified post types.
186
+     *
187
+     * @since 3.6.0
188
+     *
189
+     * @param string $slug       The slug.
190
+     * @param array  $post_types An array of post types.
191
+     *
192
+     * @return bool True if the slug exists, otherwise false.
193
+     */
194
+    private function slug_exists( $slug, $post_types ) {
195
+        global $wpdb;
196
+
197
+        // Post slugs must be unique across all posts.
198
+        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1";
199
+
200
+        return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) );
201
+    }
202 202
 
203 203
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -61,9 +61,9 @@  discard block
 block discarded – undo
61 61
 	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
62 62
 	 * @param string                            $slug The entity post type slug.
63 63
 	 */
64
-	public function __construct( $entity_type_service, $slug ) {
64
+	public function __construct($entity_type_service, $slug) {
65 65
 
66
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
66
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Link_Service');
67 67
 
68 68
 		$this->entity_type_service = $entity_type_service;
69 69
 		$this->slug                = $slug;
@@ -82,17 +82,17 @@  discard block
 block discarded – undo
82 82
 	 *
83 83
 	 * @return string The link to the post.
84 84
 	 */
85
-	public function post_type_link( $post_link, $post, $leavename, $sample ) {
85
+	public function post_type_link($post_link, $post, $leavename, $sample) {
86 86
 
87 87
 		// Return the post link if this is not our post type.
88
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
88
+		if ( ! empty($this->slug) || $this->entity_type_service->get_post_type() !== get_post_type($post)) {
89 89
 			return $post_link;
90 90
 		}
91 91
 
92 92
 		// Replace /slug/post_name/ with /post_name/
93 93
 		// The slug comes from the Entity Type Service since that service is responsible for registering the default
94 94
 		// slug.
95
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
95
+		return str_replace("/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link);
96 96
 	}
97 97
 
98 98
 	/**
@@ -102,10 +102,10 @@  discard block
 block discarded – undo
102 102
 	 *
103 103
 	 * @param WP_Query $query
104 104
 	 */
105
-	public function pre_get_posts( $query ) {
105
+	public function pre_get_posts($query) {
106 106
 
107 107
 		// If a slug has been set, we don't need to alter the query.
108
-		if ( ! empty( $this->slug ) ) {
108
+		if ( ! empty($this->slug)) {
109 109
 			return;
110 110
 		}
111 111
 
@@ -114,17 +114,17 @@  discard block
 block discarded – undo
114 114
 		// The `$query->query` count could be > 2 if the preview parameter is passed too.
115 115
 		//
116 116
 		// See https://github.com/insideout10/wordlift-plugin/issues/439
117
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
117
+		if ( ! $query->is_main_query() || 2 > count($query->query) || ! isset($query->query['page']) || empty($query->query['name'])) {
118 118
 			return;
119 119
 		}
120 120
 
121 121
 		// Add our own post type to the query.
122
-		$post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array();
123
-		$query->set( 'post_type', array_merge( $post_type, array(
122
+		$post_type = is_array($query->get('post_type')) ? $query->get('post_type') : array();
123
+		$query->set('post_type', array_merge($post_type, array(
124 124
 			'post',
125 125
 			$this->entity_type_service->get_post_type(),
126 126
 			'page',
127
-		) ) );
127
+		)));
128 128
 
129 129
 	}
130 130
 
@@ -139,19 +139,19 @@  discard block
 block discarded – undo
139 139
 	 *
140 140
 	 * @return bool Whether the slug is bad.
141 141
 	 */
142
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
142
+	public function wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type) {
143 143
 
144 144
 		// The list of post types that might have conflicting slugs.
145
-		$post_types = array( 'post', 'page', $this->entity_type_service->get_post_type() );
145
+		$post_types = array('post', 'page', $this->entity_type_service->get_post_type());
146 146
 
147 147
 		// Ignore post types different from the ones we need to check.
148
-		if ( ! in_array( $post_type, $post_types ) ) {
148
+		if ( ! in_array($post_type, $post_types)) {
149 149
 			return $bad_slug;
150 150
 		}
151 151
 
152
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
152
+		$exists = $this->slug_exists($slug, array_diff($post_types, array($post_type)));
153 153
 
154
-		$this->log->debug( "[ exists :: " . ( $exists ? "yes" : "no" ) . " ]" );
154
+		$this->log->debug("[ exists :: ".($exists ? "yes" : "no")." ]");
155 155
 
156 156
 		return $exists;
157 157
 	}
@@ -168,17 +168,17 @@  discard block
 block discarded – undo
168 168
 	 *
169 169
 	 * @return bool Whether the slug is bad.
170 170
 	 */
171
-	public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) {
171
+	public function wp_unique_post_slug_is_bad_hierarchical_slug($bad_slug, $slug, $post_type, $post_parent) {
172 172
 
173 173
 		// We only care about pages here.
174
-		if ( 'page' !== $post_type ) {
174
+		if ('page' !== $post_type) {
175 175
 			return $bad_slug;
176 176
 		}
177 177
 
178 178
 		// We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue
179 179
 		// about overlapping slugs among pages and posts:
180 180
 		//  https://core.trac.wordpress.org/ticket/13459
181
-		return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type );
181
+		return $this->wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type);
182 182
 	}
183 183
 
184 184
 	/**
@@ -191,13 +191,13 @@  discard block
 block discarded – undo
191 191
 	 *
192 192
 	 * @return bool True if the slug exists, otherwise false.
193 193
 	 */
194
-	private function slug_exists( $slug, $post_types ) {
194
+	private function slug_exists($slug, $post_types) {
195 195
 		global $wpdb;
196 196
 
197 197
 		// Post slugs must be unique across all posts.
198
-		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1";
198
+		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('".implode("', '", array_map('esc_sql', $post_types))."') LIMIT 1";
199 199
 
200
-		return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) );
200
+		return null !== $wpdb->get_var($wpdb->prepare($check_sql, $slug));
201 201
 	}
202 202
 
203 203
 }
Please login to merge, or discard this patch.