settings = Plugin::get_settings( 'performance' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
add_action( 'adminease_settings_saved', [ $this, 'adminease_settings_saved' ] );
add_action( 'init', [ $this, 'apply_max_execution_time' ], 1 );
}
/**
* Apply max execution time setting at runtime for all requests
* This ensures AJAX calls and all other requests respect the configured timeout
* @return void
*/
public function apply_max_execution_time(): void {
$max_time = $this->settings['max_execution_time'] ?? null;
// Only apply if a value is set and not empty
if( !empty( $max_time ) && $max_time > 0 ) {
@ini_set( 'max_execution_time', $max_time );
@set_time_limit( $max_time );
}
}
/**
* Modifies and returns the admin settings fields for the plugin.
*
* @param array $fields An array containing the existing fields configuration, organized by categories such as 'security'.
*
* @return array The updated fields array with additional configuration settings, including performance-related options like 'Max Execution Time'.
*/
public function adminease_settings_fields( array $fields ): array {
$fields['performance']['fields'][] = [
'type' => 'number',
'id' => 'max-execution-time',
'name' => 'adminease[performance][max_execution_time]',
'value' => $this->settings['max_execution_time'] ?? '',
'label_class' => 'adminease-label',
'input_class' => 'form-control',
'label' => __( 'Max Execution Time', 'adminease' ),
'description' => __( "Sets the maximum time (in seconds) a PHP script can run before timing out. This is essential for resource-intensive operations like imports, exports, and database migrations. Default is typically 30 seconds, which may be insufficient for complex tasks.
⚠️ Shared Hosting Limitation: This setting configures PHP-level timeouts via .user.ini, wp-config.php, and .htaccess. However, many shared hosting providers enforce server-level timeout limits (commonly 120 seconds) that cannot be overridden without upgrading to VPS or dedicated hosting. If long-running operations still timeout after configuring this setting, contact your hosting provider to increase server-level timeout limits (ExtAppTimeout, ConnectionTimeout on LiteSpeed/Nginx) or consider upgrading your hosting plan.", 'adminease' ),
'attributes' => [
'min' => -1,
'max' => 3600,
'step' => 1,
],
];
return $fields;
}
/**
* Handles the saving and application of settings related to admin ease functionality.
*
* @param array $sanitized_settings An array containing configuration settings, including performance-related options such as 'max_execution_time'.
*
* @return void
*/
public function adminease_settings_saved( array $sanitized_settings ): void {
$max_time = (int) $sanitized_settings['performance']['max_execution_time'] ?? 30;
// Apply to .user.ini / wp-config.php (handled by FileHandler)
Plugin::$FileHandler->apply_php_configuration( 'max_execution_time', $max_time );
// Apply FastCGI/Proxy timeout directives to .htaccess
$this->apply_htaccess_timeout_directives( $max_time );
}
/**
* Apply web server timeout directives to .htaccess
* This handles FastCGI, PHP-FPM, and proxy timeouts that often cause 120-second limits
*
* @param int $max_time Maximum execution time in seconds
*
* @return void
*/
private function apply_htaccess_timeout_directives( int $max_time ): void {
// If max_time is empty or invalid, remove the rule
if( empty( $max_time ) || $max_time <= 0 ) {
Plugin::$FileHandler->stack_htaccess_rule( 'TIMEOUT_DIRECTIVES', null, Plugin::$FileHandler::STACK_MODE_REMOVE );
return;
}
// Skip .htaccess modifications on known incompatible environments
if( $this->is_incompatible_environment() ) {
error_log( 'AdminEase: Skipping .htaccess timeout directives - incompatible server environment detected' );
return;
}
// Generate timeout directives for various server configurations
$timeout_rules = $this->generate_timeout_directives( $max_time );
// Stack the rule for execution
Plugin::$FileHandler->stack_htaccess_rule( 'TIMEOUT_DIRECTIVES', $timeout_rules, Plugin::$FileHandler::STACK_MODE_REPLACE );
}
/**
* Generate comprehensive timeout directives for .htaccess
*
* @param int $max_time Maximum execution time in seconds
*
* @return string Formatted .htaccess directives
*/
private function generate_timeout_directives( int $max_time ): string {
$directives = [];
// Check server environment to avoid incompatible directives
$server_software = $_SERVER['SERVER_SOFTWARE'] ?? '';
$is_litespeed = stripos( $server_software, 'litespeed' ) !== false;
// Only add LiteSpeed directives if actually running LiteSpeed
if( $is_litespeed ) {
$directives[] = '# LiteSpeed Timeout Configuration';
$directives[] = '';
$directives[] = ' RewriteEngine On';
$directives[] = ' RewriteRule .* - [E=noabort:1,E=noconntimeout:1]';
$directives[] = '';
$directives[] = '';
$directives[] = '';
$directives[] = ' php_value max_execution_time ' . $max_time;
$directives[] = ' php_value max_input_time ' . $max_time;
$directives[] = ' php_value default_socket_timeout ' . $max_time;
$directives[] = '';
}
return implode( "\n", $directives );
}
/**
* Check if the current environment is incompatible with .htaccess timeout directives
* @return bool True if environment is incompatible
*/
private function is_incompatible_environment(): bool {
// Check PHP SAPI - CGI/FPM modes don't support php_value in .htaccess
$sapi = php_sapi_name();
if( in_array( $sapi, [ 'fpm-fcgi', 'cgi-fcgi' ], true ) ) {
return true;
}
// Check if running under Nginx (doesn't use .htaccess)
$server_software = $_SERVER['SERVER_SOFTWARE'] ?? '';
if( stripos( $server_software, 'nginx' ) !== false ) {
return true;
}
// Check if .htaccess modification is actually possible
$htaccess_path = ABSPATH . '.htaccess';
if( file_exists( $htaccess_path ) && !is_writable( $htaccess_path ) ) {
return true;
}
// Check if AllowOverride is disabled by attempting to detect it
// If we can't set ini values, .htaccess likely won't work either
if( !function_exists( 'ini_set' ) || ini_get( 'safe_mode' ) ) {
return true;
}
return false;
}
}