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