이는 가지 까다로운, 그래서 내가 여기에 몇 가지 코드를 공유하고 나는 아래의 코드에서 가장 중요한 부분이 :-) 희망 누군가로부터 도움을 갖도록 다른 사람이 같은 문제가있을 수 있습니다 같아요
를
public function __construct()
{
add_action('post_updated', array($this, 'datachangecheck'), 10, 3);
}
/*
* datachangecheck
*
* This function is called upon hook post_updated.
* The function checks if data for updated post has been changed,
* and saves a file when data has been changed
*
* @param string $post_id id of post affected
* @param WP_Post $post_after WP_Post after post has been updated
* @param WP_Post $post_before WP_Post before post has been updated
* @return N/A
*
*/
function datachangecheck($post_id, $post_after, $post_before)
{
//Cast postobjects into arrays, so comparision is possible with builtin-php functions
$spf = (array)$post_before;
$spa = (array)$post_after;
//These would differ every update. so remove them for possible comparision
unset ($spf['post_modified']);
unset ($spf['post_modified_gmt']);
unset ($spa['post_modified']);
unset ($spa['post_modified_gmt']);
//Check if any difference between arrays (if empty no difference)
//If not empty, save file that tells plugin that data has been changed
$ard = array_diff ($spf, $spa);
if (!empty ($ard))
{
$this->datahaschanged_save();
}
else
{
//No change of post native data, check if any metapost data has been changed
//Remove edit_last and edit_lock because they could differ without data being changed
$this->metadata_before = get_post_meta($post_id);
unset ($this->metadata_before['_edit_last']);
unset ($this->metadata_before['_edit_lock']);
add_action('updated_post_meta', array($this, 'checkmetadata_after'), 10, 2);
}
return;
}
/*
* checkmetadata_after
*
* This function is called upon hook updated_post_meta when data has been update, but no change in native post data
* has been made and saves a file when data has been changed
*
* @param string $post_id id of post affected
* @param WP_Post $post_after WP_Post after post has been updated
* @param WP_Post $post_before WP_Post before post has been updated
* @return N/A
*
*/
function checkmetadata_after($meta_id, $post_id)
{
//Because updated_post_meta is used, now we can grab the actual updated values
//Remove edit_last and edit_lock because they could differ without data being changed
$this->metadata_after = get_post_meta($post_id);
unset ($this->metadata_after['_edit_last']);
unset ($this->metadata_after['_edit_lock']);
//Make one-level index arrays of metadata
//so array_diff works correctly down below
$arr_mdb = $this->onelevel_array($this->metadata_before);
$arr_mda = $this->onelevel_array($this->metadata_after);
//Compare array with metapost values before and after
//If not empty, save file that tells plugin that data has been changed
$ard_metadata = array_diff ($arr_mdb, $arr_mda);
if (!empty ($ard_metadata))
{
$this->datahaschanged_save();
}
return;
}