wizard
1 month ago
class-dashboard-controller.php
1 month ago
class-license-resolve-controller.php
1 month ago
class-newsletter-signup-controller.php
1 month ago
class-notice-controller.php
1 month ago
class-plugin-reset-controller.php
1 month ago
class-rewrite-check-controller.php
1 month ago
class-settings-controller.php
1 month ago
class-troubleshooting-controller.php
1 month ago
class-rewrite-check-controller.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | class RewriteCheckController |
| 8 | { |
| 9 | const CHECK_NEEDED_TRANSIENT = 'spbaddons_rewrite_check_needed'; |
| 10 | const ISSUE_FOUND_TRANSIENT = 'spbaddons_rewrite_issue'; |
| 11 | |
| 12 | /** |
| 13 | * Schedule a rewrite rules check on the next admin page load. |
| 14 | * Called on plugin activation. |
| 15 | */ |
| 16 | public static function ScheduleCheck() |
| 17 | { |
| 18 | set_transient(self::CHECK_NEEDED_TRANSIENT, 1, HOUR_IN_SECONDS); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Run the actual check if the short-lived transient exists. |
| 23 | * Called on admin_init. Consumes the trigger transient and sets |
| 24 | * the long-lived issue transient if a problem is found. |
| 25 | */ |
| 26 | public static function MaybeRunCheck() |
| 27 | { |
| 28 | if (!get_transient(self::CHECK_NEEDED_TRANSIENT)) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | delete_transient(self::CHECK_NEEDED_TRANSIENT); |
| 33 | |
| 34 | if (self::HasRewriteIssue()) { |
| 35 | set_transient(self::ISSUE_FOUND_TRANSIENT, 1, MONTH_IN_SECONDS); |
| 36 | } else { |
| 37 | delete_transient(self::ISSUE_FOUND_TRANSIENT); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check whether the current WP install has a permalink/rewrite issue |
| 43 | * that would break the REST API. |
| 44 | */ |
| 45 | public static function HasRewriteIssue() |
| 46 | { |
| 47 | $permalink_structure = get_option('permalink_structure'); |
| 48 | if (empty($permalink_structure)) { |
| 49 | // Plain permalinks — REST API uses ?rest_route= which always works. |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | $rules = get_option('rewrite_rules'); |
| 54 | if (empty($rules) || !is_array($rules)) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | $rest_prefix = rest_get_url_prefix(); |
| 59 | foreach ($rules as $pattern => $query) { |
| 60 | if (strpos($pattern, $rest_prefix) !== false) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Whether an issue has been detected (long-lived transient). |
| 70 | */ |
| 71 | public static function HasDetectedIssue() |
| 72 | { |
| 73 | return (bool) get_transient(self::ISSUE_FOUND_TRANSIENT); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Clear the issue transient. Called after a successful fix |
| 78 | * or when the user saves permalink settings. |
| 79 | */ |
| 80 | public static function ClearIssue() |
| 81 | { |
| 82 | delete_transient(self::ISSUE_FOUND_TRANSIENT); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Hook into permalink settings save to clear the issue flag. |
| 87 | */ |
| 88 | public static function Initialize() |
| 89 | { |
| 90 | add_action('admin_init', array(__CLASS__, 'MaybeRunCheck')); |
| 91 | add_action('update_option_permalink_structure', array(__CLASS__, 'ClearIssue')); |
| 92 | } |
| 93 | } |
| 94 |