PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 4.3.1
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v4.3.1
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / EmbedPress / Includes / Classes / PermalinkHelper.php
embedpress / EmbedPress / Includes / Classes Last commit date
Elementor_Enhancer.php 1 year 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 Feature_Enhancer.php 1 year ago Helper.php 10 months ago PermalinkHelper.php 10 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