permalink-manager-actions.php
1 month ago
permalink-manager-admin-functions.php
1 month ago
permalink-manager-core-functions.php
1 month ago
permalink-manager-debug.php
1 month ago
permalink-manager-gutenberg.php
1 month ago
permalink-manager-helper-functions.php
1 month ago
permalink-manager-permastructures-functions.php
1 month ago
permalink-manager-uri-functions-post.php
1 month ago
permalink-manager-uri-functions.php
1 month ago
permalink-manager-debug.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | /** |
| 6 | * Additional debug functions for "Permalink Manager Pro" |
| 7 | */ |
| 8 | class Permalink_Manager_Debug_Functions { |
| 9 | |
| 10 | public function __construct() { |
| 11 | add_action( 'init', array( $this, 'debug_data' ), 99 ); |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Map the debug functions to specific hooks |
| 16 | */ |
| 17 | public function debug_data() { |
| 18 | global $permalink_manager_options; |
| 19 | |
| 20 | if ( ! empty( $permalink_manager_options['general']['debug_mode'] ) || current_user_can( 'manage_options' ) ) { |
| 21 | add_filter( 'permalink_manager_filter_query', array( $this, 'debug_query' ), 9, 5 ); |
| 22 | add_filter( 'permalink_manager_filter_redirect', array( $this, 'debug_redirect' ), 9, 3 ); |
| 23 | add_filter( 'wp_redirect', array( $this, 'debug_wp_redirect' ), 9, 2 ); |
| 24 | |
| 25 | if ( current_user_can( 'manage_options' ) ) { |
| 26 | self::debug_custom_fields(); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Debug the WordPress query filtered in the Permalink_Manager_Core_Functions::detect_post(); function |
| 33 | * |
| 34 | * @param array $query |
| 35 | * @param array $old_query |
| 36 | * @param array $uri_parts |
| 37 | * @param array $pm_query |
| 38 | * @param string $content_type |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function debug_query( $query, $old_query = null, $uri_parts = null, $pm_query = null, $content_type = null ) { |
| 43 | global $permalink_manager; |
| 44 | |
| 45 | if ( isset( $_REQUEST['debug_url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No data is saved here |
| 46 | $debug_info['uri_parts'] = $uri_parts; |
| 47 | $debug_info['old_query_vars'] = $old_query; |
| 48 | $debug_info['new_query_vars'] = $query; |
| 49 | $debug_info['pm_query'] = ( ! empty( $pm_query['id'] ) ) ? $pm_query['id'] : "-"; |
| 50 | $debug_info['content_type'] = ( ! empty( $content_type ) ) ? $content_type : "-"; |
| 51 | |
| 52 | // License key info |
| 53 | if ( class_exists( 'Permalink_Manager_Pro_License' ) ) { |
| 54 | $license_key = $permalink_manager->functions['pro-license']->get_license_key(); |
| 55 | |
| 56 | // Mask the license key |
| 57 | $debug_info['license_key'] = preg_replace( '/([^-]+)-([^-]+)-([^-]+)-([^-]+)$/', '***-***-$3', $license_key ); |
| 58 | } |
| 59 | |
| 60 | // Plugin version |
| 61 | $debug_info['version'] = PERMALINK_MANAGER_VERSION; |
| 62 | |
| 63 | self::display_debug_data( $debug_info ); |
| 64 | } |
| 65 | |
| 66 | return $query; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Debug the redirect controlled by Permalink_Manager_Core_Functions::new_uri_redirect_and_404(); |
| 71 | * |
| 72 | * @param string $correct_permalink |
| 73 | * @param string $redirect_type |
| 74 | * @param mixed $queried_object |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function debug_redirect( $correct_permalink, $redirect_type, $queried_object ) { |
| 79 | if ( isset( $_REQUEST['debug_redirect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No data is saved here |
| 80 | $debug_info['redirect_url'] = ( ! empty( $correct_permalink ) ) ? $correct_permalink : '-'; |
| 81 | $debug_info['redirect_type'] = ( ! empty( $redirect_type ) ) ? $redirect_type : "-"; |
| 82 | |
| 83 | self::display_debug_data( $debug_info ); |
| 84 | } |
| 85 | |
| 86 | return $correct_permalink; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Debug wp_redirect() function used in 3rd party plugins |
| 91 | * |
| 92 | * @param string $url |
| 93 | * @param string $status |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | public function debug_wp_redirect( $url, $status ) { |
| 98 | if ( isset( $_GET['debug_wp_redirect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No data is saved here |
| 99 | $debug_info['url'] = $url; |
| 100 | $debug_info['status'] = $status; |
| 101 | $debug_info['backtrace'] = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 10 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Backtrace is a part of debug tools |
| 102 | |
| 103 | self::display_debug_data( $debug_info ); |
| 104 | } |
| 105 | |
| 106 | return $url; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Display the list of all custom fields assigned to specific post |
| 111 | */ |
| 112 | public static function debug_custom_fields() { |
| 113 | global $pagenow; |
| 114 | |
| 115 | // phpcs:disable WordPress.Security.NonceVerification.Recommended -- No data is saved here |
| 116 | if ( ! isset( $_GET['debug_custom_fields'] ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if ( $pagenow == 'post.php' && isset( $_GET['post'] ) ) { |
| 121 | $post_id = intval( $_GET['post'] ); |
| 122 | $custom_fields = get_post_meta( $post_id ); |
| 123 | } |
| 124 | |
| 125 | if ( $pagenow == 'term.php' && isset( $_GET['tag_ID'] ) ) { |
| 126 | $term_id = intval( $_GET['tag_ID'] ); |
| 127 | |
| 128 | $custom_fields = get_term_meta( $term_id ); |
| 129 | } |
| 130 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 131 | |
| 132 | if ( isset ( $custom_fields ) ) { |
| 133 | self::display_debug_data( $custom_fields ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * A helper function used to display the debug data in various functions |
| 139 | * |
| 140 | * @param mixed $debug_info |
| 141 | */ |
| 142 | public static function display_debug_data( $debug_info ) { |
| 143 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- print_r() is a part of debug tool |
| 144 | $debug_txt = print_r( $debug_info, true ); |
| 145 | $debug_txt = sprintf( "<pre style=\"display:block;\">%s</pre>", esc_html( $debug_txt ) ); |
| 146 | |
| 147 | wp_die( wp_kses_post( $debug_txt ) ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Generate a CSV file from array |
| 152 | * |
| 153 | * @param array $array |
| 154 | * @param string $filename |
| 155 | */ |
| 156 | public static function output_csv( $array, $filename = 'debug.csv', $delimiter = ',' ) { |
| 157 | if ( count( $array ) == 0 ) { |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | // Disable caching |
| 162 | $now = gmdate( "D, d M Y H:i:s" ); |
| 163 | header( "Expires: Tue, 03 Jul 2001 06:00:00 GMT" ); |
| 164 | header( "Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate" ); |
| 165 | header( "Last-Modified: {$now} GMT" ); |
| 166 | |
| 167 | // Force download |
| 168 | header( "Content-Type: application/force-download" ); |
| 169 | header( "Content-Type: application/octet-stream" ); |
| 170 | header( "Content-Type: application/download" ); |
| 171 | header( 'Content-Type: text/csv; charset=utf-8' ); |
| 172 | |
| 173 | // Disposition / encoding on response body |
| 174 | header( "Content-Disposition: attachment;filename={$filename}" ); |
| 175 | header( "Content-Transfer-Encoding: binary" ); |
| 176 | |
| 177 | $df = fopen( "php://output", 'w' ); |
| 178 | |
| 179 | // Use the same delimiter for headers and data |
| 180 | fputcsv( $df, array_keys( reset( $array ) ), $delimiter ); |
| 181 | foreach ( $array as $row ) { |
| 182 | fputcsv( $df, $row, $delimiter ); |
| 183 | } |
| 184 | |
| 185 | fclose( $df ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 186 | |
| 187 | die(); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 |