duplicate-post-common.php 4.79 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
<?php

/**
 * Test if the user is allowed to copy posts
 */
function duplicate_post_is_current_user_allowed_to_copy() {
	return current_user_can('copy_posts');
}

/**
 * Test if post type is enable to be copied
 */
function duplicate_post_is_post_type_enabled($post_type){
	$duplicate_post_types_enabled = get_option('duplicate_post_types_enabled', array ('post', 'page'));
	if(!is_array($duplicate_post_types_enabled)) $duplicate_post_types_enabled = array($duplicate_post_types_enabled);
	return in_array($post_type, $duplicate_post_types_enabled);
}

/**
 * Wrapper for the option 'duplicate_post_create_user_level'
 */
function duplicate_post_get_copy_user_level() {
	return get_option( 'duplicate_post_copy_user_level' );
}

// Template tag
/**
 * Retrieve duplicate post link for post.
 *
 *
 * @param int $id Optional. Post ID.
 * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
 * @param string $draft Optional, default to true
 * @return string
 */
function duplicate_post_get_clone_post_link( $id = 0, $context = 'display', $draft = true ) {
	if ( !duplicate_post_is_current_user_allowed_to_copy() )
		return;

	if ( !$post = get_post( $id ) )
		return;
	
	if(!duplicate_post_is_post_type_enabled($post->post_type))
		return;

	if ($draft)
	$action_name = "duplicate_post_save_as_new_post_draft";
	else
	$action_name = "duplicate_post_save_as_new_post";

	if ( 'display' == $context )
	$action = '?action='.$action_name.'&amp;post='.$post->ID;
	else
	$action = '?action='.$action_name.'&post='.$post->ID;

	$post_type_object = get_post_type_object( $post->post_type );
	if ( !$post_type_object )
	return;

	return wp_nonce_url(apply_filters( 'duplicate_post_get_clone_post_link', admin_url( "admin.php". $action ), $post->ID, $context ), 'duplicate-post_' . $post->ID);
}
/**
 * Display duplicate post link for post.
 *
 * @param string $link Optional. Anchor text.
 * @param string $before Optional. Display before edit link.
 * @param string $after Optional. Display after edit link.
 * @param int $id Optional. Post ID.
 */
function duplicate_post_clone_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
	if ( !$post = get_post( $id ) )
	return;

	if ( !$url = duplicate_post_get_clone_post_link( $post->ID ) )
	return;

	if ( null === $link )
	$link = esc_html__('Copy to a new draft', 'duplicate-post');

	$link = '<a class="post-clone-link" href="' . $url . '" title="'
	. esc_attr__("Copy to a new draft", 'duplicate-post')
	.'">' . $link . '</a>';
	echo $before . apply_filters( 'duplicate_post_clone_post_link', $link, $post->ID ) . $after;
}
/**
 * Get original post .
 *
 * @param int $post Optional. Post ID or Post object.
 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
 * @return mixed Post data
 */
function duplicate_post_get_original($post = null , $output = OBJECT){
	if ( !$post = get_post( $post ) )
		return;
	$original_ID = get_post_meta( $post->ID, '_dp_original');
	if (empty($original_ID)) return null;
	$original_post = get_post($original_ID[0],  $output);
	return $original_post;
}

// Admin bar
function duplicate_post_admin_bar_render() {
	if(!is_admin_bar_showing()) return;
	global $wp_admin_bar;
	$current_object = get_queried_object();
	if ( empty($current_object) )
		return;
	if ( ! empty( $current_object->post_type )
		&& ( $post_type_object = get_post_type_object( $current_object->post_type ) )
		&& duplicate_post_is_current_user_allowed_to_copy()
		&& ( $post_type_object->show_ui || 'attachment' == $current_object->post_type )
		&& (duplicate_post_is_post_type_enabled($current_object->post_type) ) )
	{
		$wp_admin_bar->add_menu( array(
        'id' => 'new_draft',
        'title' => esc_attr__("Copy to a new draft", 'duplicate-post'),
        'href' => duplicate_post_get_clone_post_link( $current_object->ID )
		) );	
	}
}

function duplicate_post_add_css() {
	if(!is_admin_bar_showing()) return;
	$current_object = get_queried_object();
	if ( empty($current_object) )
		return;
	if ( ! empty( $current_object->post_type )
		&& ( $post_type_object = get_post_type_object( $current_object->post_type ) )
		&& duplicate_post_is_current_user_allowed_to_copy()
		&& ( $post_type_object->show_ui || 'attachment' == $current_object->post_type )
		&& (duplicate_post_is_post_type_enabled($current_object->post_type) ) )
	{
		wp_enqueue_style ( 'duplicate-post', plugins_url('/duplicate-post.css', __FILE__));
	}
}


add_action('init', 'duplicate_post_init');

function duplicate_post_init(){
	if (get_option ( 'duplicate_post_show_adminbar' ) == 1) {
		add_action ( 'wp_before_admin_bar_render', 'duplicate_post_admin_bar_render' );
		add_action ( 'wp_enqueue_scripts', 'duplicate_post_add_css' );	
	}
}

/**
 * Sort taxonomy objects: first public, then private
 */
function duplicate_post_tax_obj_cmp($a, $b) {
	return ($a->public < $b->public);
}