-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautosave-interval.php
More file actions
72 lines (61 loc) · 1.73 KB
/
Copy pathautosave-interval.php
File metadata and controls
72 lines (61 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Module: Auto-save Interval
*
* Adjusts editor autosave timing via targeted hooks (does not redefine AUTOSAVE_INTERVAL).
*
* @package RefiTune
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Get configured autosave interval in seconds.
*
* @return int Seconds, or 0 when not configured.
*/
function refitune_get_autosave_interval(): int {
$settings = get_option( 'refitune_settings', array() );
if ( ! isset( $settings['autosave_interval'] ) || '' === $settings['autosave_interval'] ) {
return 0;
}
$seconds = (int) $settings['autosave_interval'];
return max( 10, $seconds );
}
/**
* Block editor autosave interval (seconds).
*
* @param array $editor_settings Editor settings.
* @param WP_Block_Editor_Context $editor_context Editor context.
* @return array
*/
function refitune_filter_block_editor_autosave_interval( $editor_settings, $editor_context ) {
$interval = refitune_get_autosave_interval();
if ( $interval > 0 ) {
$editor_settings['autosaveInterval'] = $interval;
}
return $editor_settings;
}
add_filter( 'block_editor_settings_all', 'refitune_filter_block_editor_autosave_interval', 10, 2 );
/**
* Classic editor autosave script interval.
*
* Runs after core wp_just_in_time_script_localization() on admin_print_scripts.
*
* @return void
*/
function refitune_localize_classic_autosave_interval(): void {
$interval = refitune_get_autosave_interval();
if ( $interval <= 0 || ! wp_script_is( 'autosave', 'registered' ) ) {
return;
}
wp_localize_script(
'autosave',
'autosaveL10n',
array(
'autosaveInterval' => $interval,
'blog_id' => get_current_blog_id(),
)
);
}
add_action( 'admin_print_scripts', 'refitune_localize_classic_autosave_interval', 100 );