View difference between Paste ID: xBF9NgY8 and 8J1kiz3T
SHOW: | | - or go back to the newest paste.
1
<?php
2-
 * Detect product permalinks
2+
3
/**
4-
function pm_detect_post_slugs($query) {
4+
 * Use only for specific CPT posts
5-
	global $wpdb, $pm_query;
5+
6
define('PM_CPT_NAME', 'courses');
7-
	if(!empty($pm_query['uri'])) {
7+
8-
		$post_name = basename($pm_query['uri']);
8+
9
 * The function that detects the CPT posts permalinks
10-
		$sql_query = $wpdb->prepare(
10+
11-
			"SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = 'product'",
11+
function pm_detect_cpt_slugs($query) {
12-
			array($post_name)
12+
	global $wpdb, $pm_query, $wp, $wp_rewrite;
13-
		);
13+
	
14-
		$uri = $wpdb->get_var($sql_query);
14+
	// Do not run when Elementor is opened
15
	if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
16-
		if(!empty($uri)) {
16+
		return $query;
17-
			$page = (!empty($query['page'])) ? $query['page'] : 1;
17+
18
19-
				'product' => $uri,
19+
	// 1. Get the slug
20-
				'post_type' => 'product',
20+
	preg_match("/([^\/]+)(?:\/{$wp_rewrite->pagination_base}\/([\d]+))?$/", $wp->request, $parts);
21-
				'name' => $uri,
21+
22-
				'page' => $page,
22+
	if(!empty($parts[1])) {
23
		$post_name = basename($parts[1]);
24
		$post_type = PM_CPT_NAME;
25
26-
			// Disable canonical redirect
26+
		// 2. Check if the slug is assigned to any post item
27
		$sql_query = "SELECT ID FROM $wpdb->posts WHERE 1=1 AND post_name = %s AND post_type = %s";
28
		$post_id = $wpdb->get_var($wpdb->prepare($sql_query, array($post_name, $post_type)));
29
30
		// 3. Filter the query if post was found
31
		if(!empty($post_id)) {
32-
			return $new_query;
32+
			$post = get_post($post_id);
33
			
34
			if(empty($post->post_type)) { return $query; }
35
36
			$post_type_object = get_post_type_object($post->post_type);
37
			$final_uri = (!empty($post->post_name)) ? $post->post_name : false;
38-
add_filter('request', 'pm_detect_post_slugs');
38+
39
			// Fix for hierarchical CPT & pages
40
			if(!(empty($post->ancestors)) && !empty($post_type_object->hierarchical)) {
41-
* Filter product permalinks
41+
				foreach($post->ancestors as $parent) {
42-
*/
42+
					$parent = get_post($parent);
43-
function bis_filter_product_permalinks($url, $element) {
43+
44-
	if(!empty($element->post_type) && ($element->post_type == 'product')) {
44+
					if($parent && $parent->post_name) {
45-
		$home_base = trim(get_option('home'), '/');
45+
						$final_uri = $parent->post_name . '/' . $final_uri;
46-
		
46+
					}
47-
		// $url = preg_replace('/(http(?:s)?:\/\/(?:www\.)?[^\/]+\/)([^\/]+)\/([^\/]+)/', '$1$3', $url);
47+
				}
48-
		$url = "{$home_base}/{$element->post_name}";
48+
			}
49
50
			$new_query = array(
51-
	return user_trailingslashit($url);
51+
				'post_type' => $post->post_type,
52
				$post->post_type => $final_uri,
53-
add_filter('post_type_link', 'bis_filter_product_permalinks', 999, 2);
53+
				'name' => $final_uri,
54
				'do_not_redirect' => 1,
55
			);
56
57
			// 4. Disable canonical redirect
58
			remove_action('template_redirect', 'wp_old_slug_redirect');
59
			remove_action('template_redirect', 'redirect_canonical');
60
			add_filter('wpml_is_redirected', '__return_false', 99, 2);
61
			add_filter('pll_check_canonical_url', '__return_false', 99, 2);
62
63
			$query = $new_query;
64
		}
65
	}
66
	
67
	if(isset($_GET['debug_query'])) {
68
		echo sprintf('<pre>Old query: %s</pre>', print_r($query, true));
69
		echo (!empty($new_query)) ? sprintf('<pre>New query: %s</pre>', print_r($new_query, true)) : '';
70
		echo (!empty($parts)) ? sprintf('<pre>Parts: %s</pre>', print_r($parts, true)) : '';
71
		die();
72
	}
73
74
	return $query;
75
}
76
add_filter('request', 'pm_detect_cpt_slugs', 9999);
77
78
function bis_filter_cpt_permalinks($url, $element) {
79
	if(!empty($element->post_type) && ($element->post_type == PM_CPT_NAME)) {
80
		$full_uri = get_page_uri($element->ID);
81
		$url = trim(get_option('home'), '/') . "/{$full_uri}";
82
	}
83
84
	return $url;
85
}
86
add_filter('post_type_link', 'bis_filter_cpt_permalinks', 999, 2);
87
88
function pm_redirect_native_cpt_permalinks() {
89
	global $wp;
90
91
	if(!empty($wp->request) && is_singular(PM_CPT_NAME)) {
92
		$cpt_data = get_post_type_object(PM_CPT_NAME);
93
		$cpt_slug = preg_quote($cpt_data->rewrite['slug'], '/');
94
95
		if(!empty($cpt_slug) && preg_match("/^({$cpt_slug})/", $wp->request)) {
96
			$url = get_permalink();
97
			wp_safe_redirect($url, 301);
98
			exit();
99
		}
100
	}
101
}
102
add_action('template_redirect', 'pm_redirect_native_cpt_permalinks', 1);