Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
Introduce DuplicatePostFilter #49
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaber-de committed Feb 7, 2016
1 parent 9afef7f commit 73725ed
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions inc/Import/Filter/DuplicatePostFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php # -*- coding: utf-8 -*-

namespace W2M\Import\Filter;

use
W2M\Import\Common,
W2M\Import\Data,
W2M\Import\Type,
WP_Post;

/**
* Class DuplicatePostFilter
*
* Todo: Write tests for
*
* @package W2M\Import\Filter
*/
class DuplicatePostFilter implements PostFilterInterface, Data\PostImportListenerInterface {

/**
* @var string
*/
private $meta_key = '_w2m_remote_guid';

/**
* @var Common\WpFactoryInterface
*/
private $wp_factory;

/**
* @param Common\WpFactoryInterface $wp_factory (Optional)
*/
public function __construct( Common\WpFactoryInterface $wp_factory = NULL ) {

$this->wp_factory = $wp_factory
? $wp_factory
: new Common\WpFactory;
}

/**
* Checks if a post should be imported or not
*
* @param Type\ImportPostInterface $import_post
*
* @return bool
*/
public function post_to_import( Type\ImportPostInterface $import_post ) {

$query = $this->wp_factory->wp_query(
[
'posts_per_page' => 1,
'post_status' => 'any',
'post_type' => 'any',
'meta_key' => $this->meta_key,
'meta_value' => $import_post->guid(),
'fields' => 'ids',
'update_post_meta_cache' => FALSE,
'update_post_term_cache' => FALSE
]
);

return ! $query->have_posts();
}

/**
* @wp-hook w2m_post_imported
*
* @param WP_Post $wp_post
* @param Type\ImportPostInterface $import_post
*/
public function record_post( WP_Post $wp_post, Type\ImportPostInterface $import_post ) {

update_post_meta(
$wp_post->ID,
$this->meta_key,
$import_post->guid()
);
}
}

0 comments on commit 73725ed

Please sign in to comment.