2017-01-01 3 views
0

갖는 코드메타 데이터를 포함하는 사후 업데이트의 변경 사항은 무엇입니까? 제목이 변경된 경우 검사 할 때 아래 (클래스에 주어진)

class X 
{ 

    public function __construct() 
    {      
     add_action('post_updated', array($this, 'datachangecheck'), 10, 3);   
    } 

    function datachangecheck($post_id, $post_after, $post_before) 
    { 
     if ($post_before->post_title != $post_after->post_title) 
     { 
     //do whatever 
     } 
     return; 
    } 

} 

위는 잘 작동하지만, 관련 메타 데이터를 가진 경우 같은 일을 관리의 거리 방법은 무엇입니까?

데이터가 변경된 경우에만 알 수 있습니다 (반환 값이 필요 없음). 나는 내가 meta_post_before와 meta_post_after 또는 유사하게 볼 수있는 고리를 찾을 수 없다. 누군가가 올바른 방향으로 나를 가리킬 수 있습니까? 고맙습니다!

답변

0

이는 가지 까다로운, 그래서 내가 여기에 몇 가지 코드를 공유하고 나는 아래의 코드에서 가장 중요한 부분이 :-) 희망 누군가로부터 도움을 갖도록 다른 사람이 같은 문제가있을 수 있습니다 같아요

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;    
}