Created
May 25, 2017 01:18
-
-
Save imvaskii/5925a5e6bd543a7823d1b5a3fb3375dd to your computer and use it in GitHub Desktop.
Post duplicator component
This file contains hidden or 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 | |
/** | |
* Post duplicator component. | |
* | |
* @author Bhaskar K C | |
* @since 24/05/2017 | |
*/ | |
class Component_post_duplicator extends Component { | |
/** | |
* duplicator version | |
*/ | |
const VERSION = '1.0'; | |
/** | |
* Singleton instance. This is compulsory. | |
* | |
* @var Component | |
*/ | |
protected static $instance = null; | |
/** | |
* comma separated list of post types to which duplicator is applied | |
* @var array | |
*/ | |
private $post_types; | |
/** | |
* whether to skip duplicating taxonomies from original post | |
* @var boolean | |
*/ | |
private $skip_taxonomies; | |
/** | |
* whether to skip duplicating postmeta from original post | |
* @var boolean | |
*/ | |
private $skip_postmeta; | |
/** | |
* clone nonce string | |
*/ | |
const CLONE_NONCE = 'dm_form_clone_nonce'; | |
/** | |
* clone nonce action | |
*/ | |
const CLONE_ACTION = 'dm_form_clone_action'; | |
/** | |
* duplicate post status | |
*/ | |
const POST_STATUS = 'draft'; | |
function __construct() { | |
if ( defined( 'DM_DUPLICATOR_POST_TYPES' ) ) { | |
$this->post_types = DM_DUPLICATOR_POST_TYPES; | |
} | |
if ( defined( 'DM_DUPLICATOR_SKIP_TAXONOMIES' ) ) { | |
$this->skip_taxonomies = DM_DUPLICATOR_SKIP_TAXONOMIES; | |
} | |
if ( defined( 'DM_DUPLICATOR_SKIP_POSTMETA' ) ) { | |
$this->skip_postmeta = DM_DUPLICATOR_SKIP_POSTMETA; | |
} | |
} | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self; | |
self::$instance->init(); | |
} | |
return self::$instance; | |
} | |
private function init() { | |
if ( $this->add_post_types() ) { | |
add_filter( 'post_row_actions', array( $this, 'update_post_row_actions' ), 10, 2 ); | |
add_action( 'admin_action_' . $this::CLONE_ACTION, array( $this, 'clone_the_form_post' ) ) ; | |
} | |
} | |
/** | |
* Post types to have duplicate functionality. | |
* @param string $post_type | |
*/ | |
protected function add_post_types() { | |
$post_types = array_map( 'trim', explode( ',', $this->post_types ) ); | |
foreach( $post_types as $post_type ) { | |
if ( post_type_exists( $post_type ) ) { | |
$this->post_type[] = $post_type; | |
} | |
} | |
return ( ! empty( $this->post_type ) ? true : false ); | |
} | |
/** | |
* Updates post quick edit row options | |
* @param array $actions list of actions | |
* @param WP_Post $post post obj. | |
* @return array | |
*/ | |
function update_post_row_actions( $actions, $post ) { | |
// Check for your post type. | |
if ( in_array( $post->post_type, $this->post_type ) ) { | |
$clone_nonce_url = wp_nonce_url( | |
admin_url( 'edit.php?action=' . $this::CLONE_ACTION . '&post='.$post->ID), | |
basename( __FILE__ ), | |
$this::CLONE_NONCE | |
); | |
$actions['clone'] = "<a href='{$clone_nonce_url}'>Duplicate</a>"; | |
} | |
return $actions; | |
} | |
/** | |
* Clones form post id | |
*/ | |
function clone_the_form_post() { | |
if ( ! ( isset( $_REQUEST['post'] ) || ( isset( $_REQUEST['action'] ) && $this::CLONE_ACTION == $_REQUEST['action'] ) ) ) { | |
wp_die( 'Form post is not supplied.' ); | |
} | |
// nonce verification. | |
if ( ! isset( $_REQUEST[ $this::CLONE_NONCE ] ) || ! wp_verify_nonce( $_REQUEST[ $this::CLONE_NONCE ], basename( __FILE__ ) ) ) { | |
wp_die( 'nonce failed'); | |
return; | |
} | |
$post_id = absint( $_REQUEST['post'] ); | |
$post = get_post( $post_id ); | |
if ( ! $post instanceof WP_Post ) { | |
wp_die( 'Post does not exist.' ); | |
} | |
$args = array( | |
'post_type' => $post->post_type, | |
'post_author' => $post->post_author, | |
'post_title' => $post->post_title, | |
'post_name' => $post->post_name, | |
'post_content' => $post->post_content, | |
'post_excerpt' => $post->post_excerpt, | |
'post_parent' => $post->post_parent, | |
'post_status' => $this::POST_STATUS, | |
'comment_status' => $post->comment_status, | |
'ping_status' => $post->ping_status, | |
'post_password' => $post->post_password, | |
'to_ping' => $post->to_ping, | |
'menu_order' => $post->menu_order | |
); | |
$cloned_post_id = wp_insert_post( $args ); | |
if ( false == $cloned_post_id ) { | |
wp_die( 'Duplicate post was not created. Try again.' ); | |
} | |
/** | |
* Import taxonomies | |
*/ | |
if ( false == $this->skip_taxonomies ) { | |
$taxonomies = get_object_taxonomies( $post->post_type ); | |
foreach ( $taxonomies as $taxonomy ) { | |
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array('fields' => 'slugs') ); | |
wp_set_object_terms( $cloned_post_id, $post_terms, $taxonomy, false ); | |
} | |
} | |
/** | |
* Import postmeta | |
*/ | |
if ( false == $this->skip_postmeta ) { | |
$postmeta = get_post_custom( $post_id ); | |
// $skip_meta = array( 'form_fields', 'form_settings' ); | |
foreach( $postmeta as $meta_key=>$value ) { | |
// if ( in_array( $meta_key, $skip_meta ) ) { | |
// continue; | |
// } | |
update_post_meta( $cloned_post_id, $meta_key, maybe_unserialize( $value[0] ) ); | |
} | |
} | |
wp_redirect( admin_url( 'edit.php?post_type=' . $post->post_type ) ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment