OrderLogsCleanupHelper.php
2 months ago
OrderLogsDeletionProcessor.php
2 months ago
RemoteLogger.php
9 months ago
SafeGlobalFunctionProxy.php
1 year ago
SafeGlobalFunctionProxy.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Logging; |
| 6 | |
| 7 | /** |
| 8 | * SafeGlobalFunctionProxy Class |
| 9 | * |
| 10 | * This class creates a wrapper for non-built-in functions for safety. |
| 11 | * |
| 12 | * @since 9.4.0 |
| 13 | * @package Automattic\WooCommerce\Internal\Logging |
| 14 | */ |
| 15 | class SafeGlobalFunctionProxy { |
| 16 | |
| 17 | /** |
| 18 | * Load missing function if we know where to find it. |
| 19 | * Modify this file to add more functions to the map. |
| 20 | * |
| 21 | * @param string $name The name of the function to load. |
| 22 | * @return void |
| 23 | * @throws \Exception If the function is missing and could not be loaded. |
| 24 | */ |
| 25 | private static function maybe_load_missing_function( $name ) { |
| 26 | $function_map = array( |
| 27 | 'wp_parse_url' => ABSPATH . WPINC . '/http.php', |
| 28 | 'home_url' => ABSPATH . WPINC . '/link-template.php', |
| 29 | 'get_bloginfo' => ABSPATH . WPINC . '/general-template.php', |
| 30 | 'get_option' => ABSPATH . WPINC . '/option.php', |
| 31 | 'get_site_transient' => ABSPATH . WPINC . '/option.php', |
| 32 | 'set_site_transient' => ABSPATH . WPINC . '/option.php', |
| 33 | 'wp_safe_remote_post' => ABSPATH . WPINC . '/http.php', |
| 34 | 'is_wp_error' => ABSPATH . WPINC . '/load.php', |
| 35 | 'get_plugin_updates' => array( ABSPATH . 'wp-admin/includes/update.php', ABSPATH . 'wp-admin/includes/plugin.php' ), |
| 36 | 'wp_get_environment_type' => ABSPATH . WPINC . '/load.php', |
| 37 | 'wp_json_encode' => ABSPATH . WPINC . '/functions.php', |
| 38 | 'wc_get_logger' => WC_ABSPATH . 'includes/class-wc-logger.php', |
| 39 | 'wc_print_r' => WC_ABSPATH . 'includes/wc-core-functions.php', |
| 40 | ); |
| 41 | |
| 42 | if ( ! function_exists( $name ) ) { |
| 43 | if ( isset( $function_map[ $name ] ) ) { |
| 44 | $files = (array) $function_map[ $name ]; |
| 45 | foreach ( $files as $file ) { |
| 46 | require_once $file; |
| 47 | } |
| 48 | } else { |
| 49 | throw new \Exception( sprintf( 'Function %s does not exist and could not be loaded.', esc_html( $name ) ) ); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Proxy for trapping all calls on SafeGlobalFunctionProxy. |
| 56 | * Use this for calling WP and WC global functions safely. |
| 57 | * Example usage: |
| 58 | * |
| 59 | * SafeGlobalFunctionProxy::wp_parse_url('https://example.com', PHP_URL_PATH); |
| 60 | * |
| 61 | * @since 9.4.0 |
| 62 | * @param string $name The name of the function to call. |
| 63 | * @param array $arguments The arguments to pass to the function. |
| 64 | * @return mixed The result of the function call, or null if an error occurs. |
| 65 | */ |
| 66 | public static function __callStatic( $name, $arguments ) { |
| 67 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Custom error handler is necessary to convert errors to exceptions |
| 68 | set_error_handler( |
| 69 | static function ( int $type, string $message, string $file, int $line ) { |
| 70 | if ( __FILE__ === $file ) { |
| 71 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Used to adjust file and line number for accurate error reporting |
| 72 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3 ); |
| 73 | $file = $trace[2]['file'] ?? $file; |
| 74 | $line = $trace[2]['line'] ?? $line; |
| 75 | } |
| 76 | $sanitized_message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 77 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- $message sanitised above. we don't want to rely on esc_html since it's not a PHP built-in |
| 78 | throw new \ErrorException( $sanitized_message, 0, $type, $file, $line ); |
| 79 | } |
| 80 | ); |
| 81 | |
| 82 | try { |
| 83 | self::maybe_load_missing_function( $name ); |
| 84 | $results = call_user_func_array( $name, $arguments ); |
| 85 | } catch ( \Throwable $e ) { |
| 86 | self::log_wrapper_error( $name, $e->getMessage(), $arguments ); |
| 87 | $results = null; |
| 88 | } finally { |
| 89 | restore_error_handler(); |
| 90 | } |
| 91 | |
| 92 | return $results; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Log wrapper function errors to "local logging" for debugging. |
| 97 | * |
| 98 | * @param string $function_name The name of the wrapped function. |
| 99 | * @param string $error_message The error message. |
| 100 | * @param array $context Additional context for the error. |
| 101 | */ |
| 102 | protected static function log_wrapper_error( $function_name, $error_message, $context = array() ) { |
| 103 | self::maybe_load_missing_function( 'wc_get_logger' ); |
| 104 | |
| 105 | wc_get_logger()->error( |
| 106 | '[Wrapper function error] ' . sprintf( 'Error in %s: %s', $function_name, $error_message ), |
| 107 | array_merge( |
| 108 | array( |
| 109 | 'function' => $function_name, |
| 110 | 'source' => 'remote-logging', |
| 111 | ), |
| 112 | $context |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 |