Automate translation workflows using hooks and scheduled events.
Auto-Create Translation Stubs
php
/**
* Automatically create empty translation stubs when a post is published.
* Useful when you want translators to always have a draft ready.
*/
add_action('transition_post_status', function ($new_status, $old_status, $post) {
if ($new_status !== 'publish' || $old_status === 'publish') return;
$translatable = get_option('lf_translatable_post_types', []);
if (!in_array($post->post_type, $translatable)) return;
$post_lang = lf_get_post_language($post->ID);
$languages = lf_get_languages();
$existing = lf_get_post_translations($post->ID);
foreach ($languages as $code => $lang) {
if ($code === $post_lang) continue;
if (isset($existing[$code])) continue;
// Create a draft stub
$stub_id = wp_insert_post([
'post_type' => $post->post_type,
'post_title' => $post->post_title . ' [' . strtoupper($code) . ']',
'post_status' => 'draft',
'post_author' => $post->post_author,
]);
if ($stub_id && !is_wp_error($stub_id)) {
LF_Post_Translation::instance()->set_post_language($stub_id, $code);
LF_Post_Translation::instance()->link_translations($post->ID, $stub_id);
}
}
}, 10, 3);Translation Deadline Tracking
php
/**
* Set a translation deadline meta when a translation is created,
* and send reminders for overdue translations.
*/
add_action('lf_translation_created', function ($original_id, $new_id, $lang) {
// Set a 7-day deadline
$deadline = date('Y-m-d H:i:s', strtotime('+7 days'));
update_post_meta($new_id, '_lf_translation_deadline', $deadline);
update_post_meta($new_id, '_lf_translation_status', 'pending');
}, 10, 3);
// Scheduled event to check for overdue translations
add_action('lf_check_overdue_translations', function () {
$overdue = new WP_Query([
'post_type' => 'any',
'post_status' => 'draft',
'meta_query' => [
'relation' => 'AND',
[
'key' => '_lf_translation_deadline',
'value' => current_time('mysql'),
'compare' => '<',
'type' => 'DATETIME',
],
[
'key' => '_lf_translation_status',
'value' => 'pending',
],
],
]);
foreach ($overdue->posts as $post) {
$deadline = get_post_meta($post->ID, '_lf_translation_deadline', true);
$lang = lf_get_post_language($post->ID);
// Send reminder email or Slack notification
do_action('lf_translation_overdue', $post->ID, $lang, $deadline);
}
});
// Schedule the check if not already scheduled
if (!wp_next_scheduled('lf_check_overdue_translations')) {
wp_schedule_event(time(), 'daily', 'lf_check_overdue_translations');
}—