permalink-manager-actions.php
3 years ago
permalink-manager-admin-functions.php
3 years ago
permalink-manager-core-functions.php
3 years ago
permalink-manager-debug.php
3 years ago
permalink-manager-gutenberg.php
3 years ago
permalink-manager-helper-functions.php
3 years ago
permalink-manager-language-plugins.php
3 years ago
permalink-manager-third-parties.php
3 years ago
permalink-manager-uri-functions-post.php
3 years ago
permalink-manager-uri-functions.php
3 years ago
permalink-manager-debug.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Additional debug functions for "Permalink Manager Pro" |
| 5 | */ |
| 6 | class Permalink_Manager_Debug_Functions extends Permalink_Manager_Class { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action('init', array($this, 'debug_data'), 99); |
| 10 | } |
| 11 | |
| 12 | public function debug_data() { |
| 13 | add_filter('permalink_manager_filter_query', array($this, 'debug_query'), 9, 5); |
| 14 | add_filter('permalink_manager_filter_redirect', array($this, 'debug_redirect'), 9, 3); |
| 15 | add_filter('wp_redirect', array($this, 'debug_wp_redirect'), 9, 2); |
| 16 | |
| 17 | self::debug_custom_redirects(); |
| 18 | self::debug_custom_fields(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * 1. Used in Permalink_Manager_Core_Functions::detect_post(); |
| 23 | */ |
| 24 | public function debug_query($query, $old_query = null, $uri_parts = null, $pm_query = null, $content_type = null) { |
| 25 | global $permalink_manager; |
| 26 | |
| 27 | if(isset($_REQUEST['debug_url'])) { |
| 28 | $debug_info['uri_parts'] = $uri_parts; |
| 29 | $debug_info['old_query_vars'] = $old_query; |
| 30 | $debug_info['new_query_vars'] = $query; |
| 31 | $debug_info['pm_query'] = (!empty($pm_query['id'])) ? $pm_query['id'] : "-"; |
| 32 | $debug_info['content_type'] = (!empty($content_type)) ? $content_type : "-"; |
| 33 | |
| 34 | // License key info |
| 35 | if(class_exists('Permalink_Manager_Pro_Functions')) { |
| 36 | $license_key = $permalink_manager->functions['pro-functions']->get_license_key(); |
| 37 | |
| 38 | // Mask the license key |
| 39 | $debug_info['license_key'] = preg_replace('/([^-]+)-([^-]+)-([^-]+)-([^-]+)$/', '***-***-$3', $license_key); |
| 40 | } |
| 41 | |
| 42 | // Plugin version |
| 43 | $debug_info['version'] = PERMALINK_MANAGER_VERSION; |
| 44 | |
| 45 | self::display_debug_data($debug_info); |
| 46 | } |
| 47 | |
| 48 | return $query; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * 2. Used in Permalink_Manager_Core_Functions::new_uri_redirect_and_404(); |
| 53 | */ |
| 54 | public function debug_redirect($correct_permalink, $redirect_type, $queried_object) { |
| 55 | global $wp_query; |
| 56 | |
| 57 | if(isset($_REQUEST['debug_redirect'])) { |
| 58 | $debug_info['query_vars'] = $wp_query->query_vars; |
| 59 | $debug_info['redirect_url'] = (!empty($correct_permalink)) ? $correct_permalink : '-'; |
| 60 | $debug_info['redirect_type'] = (!empty($redirect_type)) ? $redirect_type : "-"; |
| 61 | $debug_info['queried_object'] = (!empty($queried_object)) ? $queried_object : "-"; |
| 62 | |
| 63 | self::display_debug_data($debug_info); |
| 64 | } |
| 65 | |
| 66 | return $correct_permalink; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * 3. Used to debug wp_redirect() function used in 3rd party plugins |
| 71 | */ |
| 72 | public function debug_wp_redirect($url, $status) { |
| 73 | if(isset($_GET['debug_wp_redirect'])) { |
| 74 | $debug_info['url'] = $url; |
| 75 | $debug_info['backtrace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10); |
| 76 | |
| 77 | self::display_debug_data($debug_info); |
| 78 | } |
| 79 | |
| 80 | return $url; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * 4. Outputs a list of native & custom redirects |
| 85 | */ |
| 86 | public function debug_custom_redirects() { |
| 87 | global $permalink_manager, $permalink_manager_uris, $permalink_manager_redirects; |
| 88 | |
| 89 | if(isset($_GET['debug_custom_redirects']) && current_user_can('manage_options')) { |
| 90 | if(!empty($permalink_manager_uris)) { |
| 91 | $uri_post_class = $permalink_manager->functions['uri-functions-post']; |
| 92 | $uri_tax_class = $permalink_manager->functions['uri-functions-tax']; |
| 93 | $home_url = Permalink_Manager_Helper_Functions::get_permalink_base(); |
| 94 | |
| 95 | remove_filter('_get_page_link', array($uri_post_class, 'custom_post_permalinks'), 99, 2); |
| 96 | remove_filter('page_link', array($uri_post_class, 'custom_post_permalinks'), 99, 2); |
| 97 | remove_filter('post_link', array($uri_post_class, 'custom_post_permalinks'), 99, 2); |
| 98 | remove_filter('post_type_link', array($uri_post_class, 'custom_post_permalinks'), 99, 2); |
| 99 | remove_filter('attachment_link', array($uri_post_class, 'custom_post_permalinks'), 99, 2); |
| 100 | remove_filter('term_link', array($uri_tax_class, 'custom_tax_permalinks'), 999, 2 ); |
| 101 | // remove_filter('category_link', array($uri_tax_class, 'custom_tax_permalinks'), 999, 2 ); |
| 102 | // remove_filter('tag_link', array($uri_tax_class, 'custom_tax_permalinks'), 999, 2 ); |
| 103 | |
| 104 | $csv = array(); |
| 105 | |
| 106 | // Native redirects |
| 107 | foreach($permalink_manager_uris as $element_id => $uri) { |
| 108 | if(is_numeric($element_id)) { |
| 109 | $original_permalink = user_trailingslashit(get_permalink($element_id)); |
| 110 | $custom_permalink = user_trailingslashit($home_url . "/" . $uri); |
| 111 | } else { |
| 112 | $term_id = preg_replace("/[^0-9]/", "", $element_id); |
| 113 | $term = get_term($term_id); |
| 114 | |
| 115 | if(empty($term->taxonomy)) { continue; } |
| 116 | |
| 117 | $original_permalink = user_trailingslashit(get_term_link($term->term_id, $term->taxonomy)); |
| 118 | $custom_permalink = user_trailingslashit($home_url . "/" . $uri); |
| 119 | } |
| 120 | |
| 121 | if($original_permalink == $custom_permalink && $original_permalink !== '/') { continue; } |
| 122 | |
| 123 | $csv[$element_id] = array( |
| 124 | 'type' => 'native_redirect', |
| 125 | 'from' => $original_permalink, |
| 126 | 'to' => $custom_permalink |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Custom redirects |
| 132 | if($permalink_manager_redirects) { |
| 133 | foreach($permalink_manager_redirects as $element_id => $redirects) { |
| 134 | if(empty($permalink_manager_uris[$element_id])) { continue; } |
| 135 | $custom_permalink = user_trailingslashit($home_url . "/" . $permalink_manager_uris[$element_id]); |
| 136 | |
| 137 | if(is_array($redirects)) { |
| 138 | $redirects = array_values($redirects); |
| 139 | $redirects_count = count($redirects); |
| 140 | |
| 141 | foreach($redirects as $index => $redirect) { |
| 142 | $redirect_url = user_trailingslashit($home_url . "/" . $redirect); |
| 143 | |
| 144 | $csv["extra-redirect-{$index}-{$element_id}"] = array( |
| 145 | 'type' => 'extra_redirect', |
| 146 | 'from' => $redirect_url, |
| 147 | 'to' => $custom_permalink |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | echo self::output_csv($csv); |
| 155 | die(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public static function debug_custom_fields() { |
| 160 | global $pagenow, $post; |
| 161 | |
| 162 | if($pagenow == 'post.php' && isset($_GET['debug_custom_fields']) && isset($_GET['post'])) { |
| 163 | $post_id = intval($_GET['post']); |
| 164 | $custom_fields = get_post_meta($post_id); |
| 165 | |
| 166 | self::display_debug_data($custom_fields); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * A function used to display the debug data in various functions |
| 172 | */ |
| 173 | public static function display_debug_data($debug_info) { |
| 174 | $debug_txt = print_r($debug_info, true); |
| 175 | $debug_txt = sprintf("<pre style=\"display:block;\">%s</pre>", esc_html($debug_txt)); |
| 176 | |
| 177 | wp_die($debug_txt); |
| 178 | } |
| 179 | |
| 180 | public static function output_csv($array, $filename = 'debug.csv') { |
| 181 | if(count($array) == 0) { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | // Disable caching |
| 186 | $now = gmdate("D, d M Y H:i:s"); |
| 187 | header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); |
| 188 | header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); |
| 189 | header("Last-Modified: {$now} GMT"); |
| 190 | |
| 191 | // Force download |
| 192 | header("Content-Type: application/force-download"); |
| 193 | header("Content-Type: application/octet-stream"); |
| 194 | header("Content-Type: application/download"); |
| 195 | header('Content-Type: text/csv'); |
| 196 | |
| 197 | // Disposition / encoding on response body |
| 198 | header("Content-Disposition: attachment;filename={$filename}"); |
| 199 | header("Content-Transfer-Encoding: binary"); |
| 200 | |
| 201 | ob_start(); |
| 202 | |
| 203 | $df = fopen("php://output", 'w'); |
| 204 | |
| 205 | fputcsv($df, array_keys(reset($array))); |
| 206 | foreach ($array as $row) { |
| 207 | fputcsv($df, $row); |
| 208 | } |
| 209 | fclose($df); |
| 210 | |
| 211 | echo ob_get_clean(); |
| 212 | die(); |
| 213 | } |
| 214 | |
| 215 | } |
| 216 | |
| 217 | ?> |
| 218 |