Analytics
7 months ago
Database
5 months ago
Elementor_Enhancer.php
6 months ago
EmbedPress_Core_Installer.php
6 years ago
EmbedPress_Notice.php
2 years ago
EmbedPress_Plugin_Usage_Tracker.php
1 year ago
Extend_CustomPlayer_Controls.php
1 year ago
Extend_Elementor_Controls.php
1 year ago
FeatureNoticeManager.php
8 months ago
FeatureNotices.php
8 months ago
Feature_Enhancer.php
6 months ago
Helper.php
5 months ago
PermalinkHelper.php
10 months ago
README_FEATURE_NOTICES.md
8 months ago
PermalinkHelper.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Includes\Classes; |
| 4 | |
| 5 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 6 | |
| 7 | /** |
| 8 | * Helper class for handling WordPress permalink structures and REST API URLs |
| 9 | * |
| 10 | * This class provides compatibility for EmbedPress with different WordPress permalink structures, |
| 11 | * particularly fixing issues with Plain Permalinks (?p=123) where REST API URLs need to use |
| 12 | * query parameters instead of pretty URLs. |
| 13 | * |
| 14 | * @package EmbedPress |
| 15 | * @author EmbedPress <help@embedpress.com> |
| 16 | * @copyright Copyright (C) 2023 WPDeveloper. All rights reserved. |
| 17 | * @license GPLv3 or later |
| 18 | * @since 4.3.1 |
| 19 | */ |
| 20 | class PermalinkHelper |
| 21 | { |
| 22 | /** |
| 23 | * Get REST API URL with proper permalink structure support |
| 24 | * |
| 25 | * This method ensures that REST API URLs work correctly regardless of the WordPress |
| 26 | * permalink structure setting. It handles both pretty permalinks and plain permalinks. |
| 27 | * |
| 28 | * @param string $path The REST API path (e.g., 'embedpress/v1/oembed/youtube') |
| 29 | * @param int|null $blog_id Optional. Blog ID. Default null for current blog. |
| 30 | * @param string $scheme Optional. URL scheme. Default 'rest'. |
| 31 | * @return string The properly formatted REST API URL |
| 32 | * @since 4.3.1 |
| 33 | */ |
| 34 | public static function get_rest_url($path = '/', $blog_id = null, $scheme = 'rest') |
| 35 | { |
| 36 | // Ensure path starts with forward slash |
| 37 | if (!empty($path) && $path[0] !== '/') { |
| 38 | $path = '/' . $path; |
| 39 | } |
| 40 | |
| 41 | // Use WordPress core function which handles permalink structure automatically |
| 42 | return get_rest_url($blog_id, $path, $scheme); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Check if WordPress is using pretty permalinks |
| 47 | * |
| 48 | * @return bool True if using pretty permalinks, false for plain permalinks |
| 49 | * @since 4.3.1 |
| 50 | */ |
| 51 | public static function is_using_pretty_permalinks() |
| 52 | { |
| 53 | $permalink_structure = get_option('permalink_structure'); |
| 54 | return !empty($permalink_structure); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the permalink structure type |
| 59 | * |
| 60 | * @return string The permalink structure type ('plain', 'pretty', or 'custom') |
| 61 | * @since 4.3.1 |
| 62 | */ |
| 63 | public static function get_permalink_structure_type() |
| 64 | { |
| 65 | $permalink_structure = get_option('permalink_structure'); |
| 66 | |
| 67 | if (empty($permalink_structure)) { |
| 68 | return 'plain'; |
| 69 | } |
| 70 | |
| 71 | // Check for common pretty permalink structures |
| 72 | $common_structures = [ |
| 73 | '/%year%/%monthnum%/%day%/%postname%/', |
| 74 | '/%year%/%monthnum%/%postname%/', |
| 75 | '/%postname%/', |
| 76 | '/archives/%post_id%', |
| 77 | ]; |
| 78 | |
| 79 | if (in_array($permalink_structure, $common_structures)) { |
| 80 | return 'pretty'; |
| 81 | } |
| 82 | |
| 83 | return 'custom'; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Generate a REST API URL that's compatible with the current permalink structure |
| 88 | * |
| 89 | * This method provides additional compatibility checks and fallbacks for edge cases |
| 90 | * where the standard get_rest_url() might not work as expected. |
| 91 | * |
| 92 | * @param string $namespace The REST API namespace (e.g., 'embedpress/v1') |
| 93 | * @param string $route The REST API route (e.g., 'oembed/youtube') |
| 94 | * @param array $query_args Optional query arguments to append |
| 95 | * @return string The complete REST API URL |
| 96 | * @since 4.3.1 |
| 97 | */ |
| 98 | public static function build_rest_url($namespace, $route = '', $query_args = []) |
| 99 | { |
| 100 | // Construct the full path |
| 101 | $path = '/' . trim($namespace, '/'); |
| 102 | if (!empty($route)) { |
| 103 | $path .= '/' . trim($route, '/'); |
| 104 | } |
| 105 | |
| 106 | // Get the base REST URL |
| 107 | $url = self::get_rest_url($path); |
| 108 | |
| 109 | // Add query arguments if provided |
| 110 | if (!empty($query_args)) { |
| 111 | $url = add_query_arg($query_args, $url); |
| 112 | } |
| 113 | |
| 114 | return $url; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Validate that a REST API URL is accessible |
| 119 | * |
| 120 | * This method can be used to test if REST API endpoints are working correctly |
| 121 | * with the current permalink structure. |
| 122 | * |
| 123 | * @param string $url The REST API URL to test |
| 124 | * @return bool True if the URL is accessible, false otherwise |
| 125 | * @since 4.3.1 |
| 126 | */ |
| 127 | public static function validate_rest_url($url) |
| 128 | { |
| 129 | // Basic URL validation |
| 130 | if (!filter_var($url, FILTER_VALIDATE_URL)) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Check if it's a REST API URL |
| 135 | $rest_prefix = rest_get_url_prefix(); |
| 136 | if (strpos($url, $rest_prefix) === false && strpos($url, 'rest_route=') === false) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 |