Skip to content

Instantly share code, notes, and snippets.

@timwhitlock
Created November 15, 2018 16:34
Show Gist options
  • Save timwhitlock/0b5a83c1b632713713c6c5395318bded to your computer and use it in GitHub Desktop.
Save timwhitlock/0b5a83c1b632713713c6c5395318bded to your computer and use it in GitHub Desktop.
Loco Translate Webhook example
<?php
/*
Plugin Name: Loco Webhook
Description: Monitors changes to PO files during save operations
Author: Tim Whitlock
*/
class LocoWebhook {
private $path;
private $hash;
public function __construct( $path ){
$this->path = $path;
if( file_exists($path) ){
$this->hash = md5_file($path);
}
}
public function ping(){
if( file_exists($this->path) ){
$hash = md5_file($this->path);
if( $hash !== $this->hash ){
$this->hash = $hash;
// --------------------------------------
FILE HAS CHANGED. PING YOUR WEB HOOK HERE
// --------------------------------------
}
}
}
public static function init(){
if( 'save' === $_POST['route'] ) {
$path = WP_CONTENT_DIR.'/'.$_POST['path'];
$inst = new LocoWebhook( $path );
add_action('loco_admin_shutdown',[$inst,'ping']);
}
}
}
add_action('wp_ajax_loco_json', ['LocoWebhook','init'] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment