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 );
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 {
$max_time = (int) ( $sanitized_settings['performance']['max_execution_time'] ?? 0 );
$FileHandler = FileHandler::get_instance();
$value = ( 0 === $max_time ) ? '' : $max_time;
$FileHandler->apply_php_configuration( 'max_execution_time', $value );
$FileHandler->stack_wp_config_ini_directive( 'max_execution_time', $value );
$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( 0 >= $max_time ) {
FileHandler::get_instance()->stack_htaccess_rule( 'MAX_EXECUTION_TIME', null, FileHandler::get_instance()::STACK_MODE_REMOVE );
return;
}
$timeout_rules = $this->generate_timeout_directives( $max_time );
if( !empty( $timeout_rules ) ) {
FileHandler::get_instance()->stack_htaccess_rule( 'MAX_EXECUTION_TIME', $timeout_rules, FileHandler::get_instance()::STACK_MODE_REPLACE );
} else {
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 {
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;
}
}