Created
November 15, 2018 16:34
-
-
Save timwhitlock/0b5a83c1b632713713c6c5395318bded to your computer and use it in GitHub Desktop.
Loco Translate Webhook example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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