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 = (int) ( $this->settings['max_execution_time'] ?? 0 );
// An empty field casts to 0, and 0 means "not managed": PHP keeps whatever
// the server configured. -1 is the documented "no limit" value and has to
// be passed through, which the previous $max_time > 0 test silently
// dropped, so the field accepted -1 but never applied it.
if( 0 === $max_time ) {
return;
}
ini_set( 'max_execution_time', $max_time ); // phpcs:ignore WordPress.PHP.IniSet.Risky,Squiz.PHP.DiscouragedFunctions.Discouraged
set_time_limit( $max_time ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
}
/**
* 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' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'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' ),
'field_description' => sprintf(
/* translators: %d: highest number of seconds the field accepts. */
__( 'Leave empty (or enter 0) to let the server decide and remove any value AdminEase wrote before. Enter -1 for no limit, or a number of seconds up to %d.', 'adminease' ),
self::MAX_SECONDS
),
'attributes' => [
'min' => self::MIN_SECONDS,
'max' => self::MAX_SECONDS,
'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 {
// (int) binds tighter than ??, so the previous
// (int) $sanitized_settings['performance']['max_execution_time'] ?? 30
// cast first and coalesced second: the ?? 30 fallback could never run, and
// an absent key raised an undefined-key warning straight into the AJAX
// response body.
$max_time = (int) ( $sanitized_settings['performance']['max_execution_time'] ?? 0 );
$FileHandler = FileHandler::get_instance();
// An empty field casts to 0, and 0 means the admin does not want this
// managed. Passing an empty value makes FileHandler remove the block it
// previously wrote to .user.ini, wp-config.php and .htaccess rather than
// write a new one, so the old setting really does go away.
$value = ( 0 === $max_time ) ? '' : $max_time;
// Apply to .user.ini / wp-config.php (handled by FileHandler)
$FileHandler->apply_php_configuration( 'max_execution_time', $value );
$FileHandler->stack_wp_config_ini_directive( 'max_execution_time', $value );
// 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 {
// 0 means the setting is not managed, and -1 (no limit) has no meaningful
// value to write into php_value max_input_time / default_socket_timeout.
// Both remove the block, so nothing AdminEase wrote earlier is left behind.
if( 0 >= $max_time ) {
FileHandler::get_instance()->stack_htaccess_rule( 'MAX_EXECUTION_TIME', null, FileHandler::get_instance()::STACK_MODE_REMOVE );
return;
}
// Generate timeout directives for various server configurations
$timeout_rules = $this->generate_timeout_directives( $max_time );
// Only add rules if content was generated
if( !empty( $timeout_rules ) ) {
FileHandler::get_instance()->stack_htaccess_rule( 'MAX_EXECUTION_TIME', $timeout_rules, FileHandler::get_instance()::STACK_MODE_REPLACE );
} else {
// Remove rule if no content generated (incompatible environment)
FileHandler::get_instance()->stack_htaccess_rule( 'MAX_EXECUTION_TIME', null, FileHandler::get_instance()::STACK_MODE_REMOVE );
}
}
/**
* 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 {
// Check if running LiteSpeed
if( !$this->is_litespeed_server() ) {
return '';
}
$directives = [];
$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 server is running LiteSpeed
*
* @return bool True if LiteSpeed, false otherwise
*/
private function is_litespeed_server(): bool {
if( !isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
return false;
}
$server_software = sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) );
return stripos( $server_software, 'litespeed' ) !== false;
}
}