| 1 |
<?php |
| 2 |
|
| 3 |
namespace LPagery\controller; |
| 4 |
|
| 5 |
use LPagery\utils\Utils; |
| 6 |
|
| 7 |
/** |
| 8 |
* Controller for handling slug-related operations |
| 9 |
*/ |
| 10 |
class SlugController |
| 11 |
{ |
| 12 |
private static $instance; |
| 13 |
|
| 14 |
/** |
| 15 |
* Singleton pattern implementation |
| 16 |
*/ |
| 17 |
public static function get_instance(): self |
| 18 |
{ |
| 19 |
if (null === self::$instance) { |
| 20 |
self::$instance = new self(); |
| 21 |
} |
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Sanitizes a slug and returns it with the full URL |
| 27 |
* |
| 28 |
* @param string $slug The slug to sanitize |
| 29 |
* @param int $parent_id The parent post ID |
| 30 |
* @param int $template_id The template post ID |
| 31 |
* @return array Array containing the URL |
| 32 |
*/ |
| 33 |
public function sanitizeSlug(string $slug, int $parent_id = 0, int $template_id = 0): array |
| 34 |
{ |
| 35 |
if (empty($slug)) { |
| 36 |
return ["url" => ""]; |
| 37 |
} |
| 38 |
|
| 39 |
$slug = strtolower(Utils::lpagery_sanitize_title_with_dashes($slug)); |
| 40 |
$base_url = ''; |
| 41 |
|
| 42 |
// Get post type slug if template exists |
| 43 |
if ($template_id > 0) { |
| 44 |
$post_type = get_post_type($template_id); |
| 45 |
$post_type_obj = get_post_type_object($post_type); |
| 46 |
if ($post_type_obj) { |
| 47 |
// First try to get the rewrite slug if set |
| 48 |
if (isset($post_type_obj->rewrite['slug'])) { |
| 49 |
$base_url .= $post_type_obj->rewrite['slug'] . '/'; |
| 50 |
} // If no rewrite slug and it's not 'post' or 'page', use the post type name |
| 51 |
else if (!in_array($post_type, ['post', 'page'])) { |
| 52 |
$base_url .= $post_type . '/'; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Add parent path if parent exists |
| 58 |
if ($parent_id) { |
| 59 |
$parent_path = trim(get_page_uri($parent_id), '/'); |
| 60 |
if ($parent_path) { |
| 61 |
$base_url .= $parent_path . '/'; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Combine everything for the final URL |
| 66 |
$final_url = site_url($base_url . $slug); |
| 67 |
|
| 68 |
return ["url" => $final_url]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Custom sanitizes a title |
| 73 |
* |
| 74 |
* @param string $slug The slug to sanitize |
| 75 |
* @return string The sanitized slug |
| 76 |
*/ |
| 77 |
public function customSanitizeTitle(string $slug): string |
| 78 |
{ |
| 79 |
if (empty($slug)) { |
| 80 |
return ''; |
| 81 |
} |
| 82 |
|
| 83 |
return strtolower(urldecode(Utils::lpagery_sanitize_title_with_dashes($slug))); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets a post title as a slug |
| 88 |
* |
| 89 |
* @param int $post_id The post ID |
| 90 |
* @return array Array containing the slug |
| 91 |
*/ |
| 92 |
public function getPostTitleAsSlug(int $post_id): array |
| 93 |
{ |
| 94 |
$post = get_post($post_id); |
| 95 |
$slug = strtolower(Utils::lpagery_sanitize_title_with_dashes($post->post_title)); |
| 96 |
|
| 97 |
return ["slug" => $slug]; |
| 98 |
} |
| 99 |
} |