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