true, 'raw' => true, 'add' => true, ); /** * Process wp-config.php updates from a settings save. * * @param array $post_array - The raw POST data. * @param array &$advanced_options - The sanitized options (may be modified for debug flags). * * @return void * * @since 3.4.0 */ public static function process( array $post_array, array &$advanced_options ): void { if ( is_a( Config_Transformer::init(), '\WP_Error' ) ) { return; } $config_errors = array(); $config_errors = self::update_debug_constants( $post_array, $config_errors ); $config_errors = self::update_environment_constants( $post_array, $config_errors ); $config_errors = self::update_debug_log( $post_array, $advanced_options, $config_errors ); $config_errors = self::update_cron_constants( $post_array, $config_errors ); $config_errors = self::update_external_requests( $post_array, $config_errors ); if ( ! empty( $config_errors ) ) { try { \update_option( 'advan_config_transformer_errors', $config_errors ); } catch ( \Throwable $e ) { // Ignore failures updating the option. } } } /** * Update WP_DEBUG and related display/script constants. * * @param array $post_array - The raw POST data. * @param array $errors - Accumulated errors. * * @return array * * @since 3.4.0 */ private static function update_debug_constants( array $post_array, array $errors ): array { $wp_debug_enable = ( array_key_exists( 'wp_debug_enable', $post_array ) ) ? filter_var( $post_array['wp_debug_enable'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'WP_DEBUG', $wp_debug_enable, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG', $wp_debug_enable ), 'message' => $e->getMessage(), ); } $wp_debug_display_enable = ( array_key_exists( 'wp_debug_display_enable', $post_array ) ) ? filter_var( $post_array['wp_debug_display_enable'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'WP_DEBUG_DISPLAY', $wp_debug_display_enable, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG_DISPLAY', $wp_debug_display_enable ), 'message' => $e->getMessage(), ); } $wp_script_debug = ( array_key_exists( 'wp_script_debug', $post_array ) ) ? filter_var( $post_array['wp_script_debug'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'SCRIPT_DEBUG', $wp_script_debug, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'SCRIPT_DEBUG', $wp_script_debug ), 'message' => $e->getMessage(), ); } $wp_save_queries = ( array_key_exists( 'wp_save_queries', $post_array ) ) ? filter_var( $post_array['wp_save_queries'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'SAVEQUERIES', $wp_save_queries, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'SAVEQUERIES', $wp_save_queries ), 'message' => $e->getMessage(), ); } return $errors; } /** * Update WP_ENVIRONMENT_TYPE and WP_DEVELOPMENT_MODE constants. * * @param array $post_array - The raw POST data. * @param array $errors - Accumulated errors. * * @return array * * @since 3.4.0 */ private static function update_environment_constants( array $post_array, array $errors ): array { $wp_environment_type = ( array_key_exists( 'wp_environment_type', $post_array ) ) ? \sanitize_text_field( \wp_unslash( $post_array['wp_environment_type'] ) ) : false; if ( false !== $wp_environment_type && ! in_array( $wp_environment_type, array( 'production', 'development', 'staging', 'local' ), true ) ) { $wp_environment_type = 'production'; } try { Config_Transformer::update( 'constant', 'WP_ENVIRONMENT_TYPE', $wp_environment_type, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_ENVIRONMENT_TYPE', $wp_environment_type ), 'message' => $e->getMessage(), ); } $wp_development_mode = ( array_key_exists( 'wp_development_mode', $post_array ) ) ? \sanitize_text_field( \wp_unslash( $post_array['wp_development_mode'] ) ) : false; if ( false !== $wp_development_mode && ! in_array( $wp_development_mode, array( '', 'all', 'core', 'plugin', 'theme' ), true ) ) { $wp_development_mode = ''; } try { Config_Transformer::update( 'constant', 'WP_DEVELOPMENT_MODE', $wp_development_mode, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEVELOPMENT_MODE', $wp_development_mode ), 'message' => $e->getMessage(), ); } return $errors; } /** * Update WP_DEBUG_LOG constant and handle file generation. * * @param array $post_array - The raw POST data. * @param array &$advanced_options - The sanitized options (modified for keep_reading flags). * @param array $errors - Accumulated errors. * * @return array * * @since 3.4.0 */ private static function update_debug_log( array $post_array, array &$advanced_options, array $errors ): array { $wp_debug_log_enable = ( array_key_exists( 'wp_debug_log_enable', $post_array ) ) ? filter_var( $post_array['wp_debug_log_enable'], FILTER_VALIDATE_BOOLEAN ) : false; if ( $wp_debug_log_enable ) { @clearstatcache( false, File_Helper::get_wp_config_file_path() ); $wp_debug_log_generate = ( array_key_exists( 'wp_debug_log_file_generate', $post_array ) ) ? filter_var( $post_array['wp_debug_log_file_generate'], FILTER_VALIDATE_BOOLEAN ) : false; $wp_debug_log_filename = ( array_key_exists( 'wp_debug_log_filename', $post_array ) ) ? \sanitize_text_field( $post_array['wp_debug_log_filename'] ) : ''; if ( ! empty( $wp_debug_log_filename ) && Error_Log::autodetect() !== $wp_debug_log_filename ) { $candidate = \wp_normalize_path( $wp_debug_log_filename ); $content_root = \wp_normalize_path( \WP_CONTENT_DIR ); // Allow only paths inside WP_CONTENT_DIR to mitigate arbitrary path writes. if ( 0 === strpos( $candidate, $content_root ) && \is_writable( \dirname( $candidate ) ) ) { try { Config_Transformer::update( 'constant', 'WP_DEBUG_LOG', $candidate, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG_LOG', $candidate ), 'message' => $e->getMessage(), ); } } } if ( $wp_debug_log_generate ) { $file_name = \WP_CONTENT_DIR . \DIRECTORY_SEPARATOR . 'debug_' . File_Helper::generate_random_file_name() . '.log'; try { Config_Transformer::update( 'constant', 'WP_DEBUG_LOG', $file_name, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG_LOG', $file_name ), 'message' => $e->getMessage(), ); } } // If at this point file is still not set, set to default. if ( false === $wp_debug_log_generate && '' === trim( $wp_debug_log_filename ) ) { try { Config_Transformer::update( 'constant', 'WP_DEBUG_LOG', $wp_debug_log_enable, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG_LOG', $wp_debug_log_enable ), 'message' => $e->getMessage(), ); } } // Clear the flag for keep reading the error log if WP settings are disabled (because at this point they are enabled). $advanced_options['keep_reading_error_log'] = false; $advanced_options['plugin_debug_enable'] = false; } else { try { Config_Transformer::update( 'constant', 'WP_DEBUG_LOG', $wp_debug_log_enable, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_DEBUG_LOG', $wp_debug_log_enable ), 'message' => $e->getMessage(), ); } } @clearstatcache( false, File_Helper::get_wp_config_file_path() ); return $errors; } /** * Update DISABLE_WP_CRON constant. * * @param array $post_array - The raw POST data. * @param array $errors - Accumulated errors. * * @return array * * @since 3.4.0 */ private static function update_cron_constants( array $post_array, array $errors ): array { $wp_cron_disable = ( array_key_exists( 'wp_cron_disable', $post_array ) ) ? filter_var( $post_array['wp_cron_disable'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'DISABLE_WP_CRON', $wp_cron_disable, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'DISABLE_WP_CRON', $wp_cron_disable ), 'message' => $e->getMessage(), ); } return $errors; } /** * Update WP_HTTP_BLOCK_EXTERNAL constant. * * @param array $post_array - The raw POST data. * @param array $errors - Accumulated errors. * * @return array * * @since 3.4.0 */ private static function update_external_requests( array $post_array, array $errors ): array { $block_external_requests = ( array_key_exists( 'block_external_requests', $post_array ) ) ? filter_var( $post_array['block_external_requests'], FILTER_VALIDATE_BOOLEAN ) : false; try { Config_Transformer::update( 'constant', 'WP_HTTP_BLOCK_EXTERNAL', $block_external_requests, self::$config_args ); } catch ( \Throwable $e ) { $errors[] = array( 'context' => self::build_error_context( 'update', 'WP_HTTP_BLOCK_EXTERNAL', $block_external_requests ), 'message' => $e->getMessage(), ); } return $errors; } /** * Show any stored wp-config transformer errors as admin notices. * * @return void * * @since 3.4.0 */ public static function show_errors() { if ( ! \is_admin() ) { return; } if ( ! \current_user_can( 'manage_options' ) ) { return; } $errors = \get_option( 'advan_config_transformer_errors', array() ); if ( empty( $errors ) || ! is_array( $errors ) ) { return; } \delete_option( 'advan_config_transformer_errors' ); echo '
' . \esc_html__( 'wp-config.php update warnings', '0-day-analytics' ) . '