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;
}
// 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 = [];
// LiteSpeed-specific directives (CRITICAL for LiteSpeed servers)
$directives[] = '# LiteSpeed Timeout Configuration';
$directives[] = '';
$directives[] = ' php_value max_execution_time ' . $max_time;
$directives[] = ' php_value max_input_time ' . $max_time;
$directives[] = ' # LiteSpeed connection timeout';
$directives[] = ' RewriteEngine On';
$directives[] = ' RewriteRule .* - [E=noabort:1,E=noconntimeout:1]';
$directives[] = '';
$directives[] = '';
// Try setting via RUIDn (if available on LiteSpeed)
$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[] = '';
$directives[] = '';
// Apache Timeout directive (in case of Apache compatibility mode)
$directives[] = '';
$directives[] = ' # Disable mod_reqtimeout to prevent premature timeouts';
$directives[] = ' RequestReadTimeout handshake=0 header=0 body=0';
$directives[] = '';
$directives[] = '';
// FastCGI timeout settings (mod_fcgid)
$directives[] = '';
$directives[] = ' FcgidIOTimeout ' . $max_time;
$directives[] = ' FcgidIdleTimeout ' . $max_time;
$directives[] = ' FcgidBusyTimeout ' . $max_time;
$directives[] = ' FcgidConnectTimeout ' . $max_time;
$directives[] = ' FcgidMaxRequestLen 1073741824';
$directives[] = '';
$directives[] = '';
// Proxy timeout settings (mod_proxy_fcgi for PHP-FPM)
$directives[] = '';
$directives[] = ' ProxyTimeout ' . $max_time;
$directives[] = '';
$directives[] = '';
// Proxy HTTP module
$directives[] = '';
$directives[] = ' ProxyTimeout ' . $max_time;
$directives[] = '';
$directives[] = '';
// CGI timeout (mod_cgi/mod_cgid)
$directives[] = '';
$directives[] = ' TimeOut ' . $max_time;
$directives[] = '';
$directives[] = '';
$directives[] = '';
$directives[] = ' TimeOut ' . $max_time;
$directives[] = '';
$directives[] = '';
// Try to set via php_value if mod_php is loaded
$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[] = '';
$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 );
}
}