更新后包含元数据的任何更改?

时间:2017-01-01 14:27:26

标签: wordpress hook

下面的代码(在课堂上给出)

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或类似的。有人能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分: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;            
}