Action
uncanny-automator
automator_settings_advanced_tab_view_automator_cache
Fires when the Automator plugin's advanced tab displays the cache settings, allowing for customization.
add_action( 'automator_settings_advanced_tab_view_automator_cache', $callback, 10, 1 );
Description
Fires within the Advanced settings tab to render custom content related to Automator's cache management. Developers can use this hook to add their own UI elements, controls, or information concerning cache status and actions. The `self::SETTINGSGROUP` parameter provides access to the settings group object.
Usage
add_action( 'automator_settings_advanced_tab_view_automator_cache', 'your_function_name', 10, 1 );
Examples
<?php
/**
* Example callback function for the 'automator_settings_advanced_tab_view_automator_cache' action hook.
*
* This function demonstrates how to add custom content or logic to the Automator cache settings tab.
* In this example, we'll add a button to clear the cache and display a message indicating the last clear time.
*
* @param string $settings_group The settings group identifier.
*/
function my_uncanny_automator_custom_cache_settings( $settings_group ) {
// Retrieve the last cache clear time from WordPress options.
// In a real scenario, you would likely have a more robust way to store and retrieve this.
$last_cache_clear = get_option( 'my_automator_cache_last_cleared_time', 'Never' );
?>
<div class="my-custom-automator-cache-settings">
<h4><?php esc_html_e( 'Custom Cache Controls', 'my-text-domain' ); ?></h4>
<p>
<?php
printf(
/* translators: %s: The date and time the cache was last cleared. */
esc_html__( 'Cache last cleared on: %s', 'my-text-domain' ),
esc_html( $last_cache_clear )
);
?>
</p>
<button id="my-clear-automator-cache-button" class="button button-secondary">
<?php esc_html_e( 'Clear Automator Cache', 'my-text-domain' ); ?>
</button>
<p id="my-cache-clear-status" style="margin-top: 10px; color: green;"></p>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#my-clear-automator-cache-button').on('click', function() {
var button = $(this);
button.text('<?php esc_html_e( 'Clearing...', 'my-text-domain' ); ?>');
button.prop('disabled', true);
$.ajax({
url: ajaxurl, // WordPress AJAX URL
type: 'POST',
data: {
action: 'my_clear_automator_cache', // AJAX action hook
security: '<?php echo wp_create_nonce( 'my_clear_automator_cache_nonce' ); ?>', // Nonce for security
settings_group: '<?php echo esc_attr( $settings_group ); ?>' // Pass the settings group
},
success: function(response) {
if (response.success) {
$('#my-cache-clear-status').text('<?php esc_html_e( 'Cache cleared successfully!', 'my-text-domain' ); ?>');
// Update the displayed time if the server sends it back (optional)
if (response.data && response.data.last_cleared) {
// You might want to format this date/time appropriately
// $('#your-last-cleared-time-element').text(response.data.last_cleared);
}
} else {
$('#my-cache-clear-status').text('<?php esc_html_e( 'Error clearing cache.', 'my-text-domain' ); ?>').css('color', 'red');
}
},
complete: function() {
button.text('<?php esc_html_e( 'Clear Automator Cache', 'my-text-domain' ); ?>');
button.prop('disabled', false);
}
});
});
});
</script>
<?php
}
// Add the callback function to the action hook.
// We specify 10 as the priority and 1 as the number of accepted arguments.
add_action( 'automator_settings_advanced_tab_view_automator_cache', 'my_uncanny_automator_custom_cache_settings', 10, 1 );
/**
* Handle the AJAX request to clear the Automator cache.
* This function should be registered with WordPress's AJAX system.
*/
function my_handle_clear_automator_cache() {
// Verify the nonce for security.
check_ajax_referer( 'my_clear_automator_cache_nonce', 'security' );
// In a real application, you would implement the actual cache clearing logic here.
// This might involve flushing WordPress object cache, clearing transient data,
// or custom cache mechanisms used by Uncanny Automator.
// For demonstration, we'll just simulate clearing and update an option.
// Example: Clear Uncanny Automator's specific cache if possible (this is hypothetical)
if ( function_exists( 'uncanny_automator_get_helper' ) ) {
// Assume Uncanny Automator has a function to clear its cache.
// Replace 'uncanny_automator_get_helper()->clear_cache()' with the actual function call.
// For now, we'll just log that we attempted it.
error_log('Attempted to clear Uncanny Automator cache via AJAX.');
}
// Update the last cleared time option.
$current_time = current_time( 'mysql' );
update_option( 'my_automator_cache_last_cleared_time', $current_time );
// Send a success response back to the JavaScript.
wp_send_json_success( array( 'last_cleared' => $current_time ) );
}
// Hook the AJAX handler to WordPress.
add_action( 'wp_ajax_my_clear_automator_cache', 'my_handle_clear_automator_cache' );
// If you need this to work for logged-out users, you'd also add:
// add_action( 'wp_ajax_nopriv_my_clear_automator_cache', 'my_handle_clear_automator_cache' );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/views/admin-settings/tab/advanced/automator-cache.php:31
<div class="uap-settings-panel-title">
<?php esc_html_e( 'Automator cache', 'uncanny-automator' ); ?>
</div>
<div class="uap-settings-panel-content">
<?php do_action( 'automator_settings_advanced_tab_view_automator_cache', self::SETTINGSGROUP ); ?>
</div>
</div>
<div class="uap-settings-panel-bottom">
<div class="uap-settings-panel-bottom-left">