<?php

 

add_filter( 'px_process_convert_post_old_version', 'px_process_convert_post_old_version_callback', 10, 2 );

function px_process_convert_post_old_version_callback( $post_id, $return = '' ) {
	global $wpdb;

		$post 				= get_post( $post_id );
		$current_user 		= wp_get_current_user();
		$new_post_author 	= $current_user->ID;
	 
		if (isset( $post ) && $post != null) {
	 
		$info 		= get_post_meta( $post->ID, 'wp_apk_details', true );
		$cb 		= get_post_meta( $post->ID, 'custom_boxes', true );
		$post_title = $post->post_title;
			
		if(get_post_meta( $post->ID, 'wp_version_GP', true )){
			$version		= wp_strip_all_tags(get_post_meta( $post->ID, 'wp_version_GP', true ));	
		} else {
			$version		= wp_strip_all_tags(get_post_meta( $post->ID, 'wp_version', true ));	
		}
			
		if( !empty( get_post_meta( $post->ID, 'wp_version_GP', true ) ) ) {
			$post_title .= ' '.$version;
		}
		$args = array(
			'comment_status' => $post->comment_status,
			'ping_status'    => $post->ping_status,
			'post_author'    => $new_post_author,
			'post_content'   => $post->post_content,
			'post_excerpt'   => $post->post_excerpt,
			'post_name'      => $post->post_name,
			'post_parent'    => $post->post_parent,
			'post_password'  => $post->post_password,
			'post_status'    => 'draft',
			'post_title'     => $post_title,
			'post_type'      => $post->post_type,
			'to_ping'        => $post->to_ping,
			'menu_order'     => $post->menu_order,
			'post_parent'	 => $post_id,
		);

		$new_post_id 	= wp_insert_post( $args );
		
		$p_name 		= wp_unique_post_slug( sanitize_title( $post_title ), $new_post_id, 'draft', 'post', $post->post_parent );

		wp_update_post( array(
			'ID' 			=> $new_post_id,
			'post_date' 	=> $post->post_date,
			'post_date_gmt' => $post->post_date_gmt,
			'post_name' 	=> $p_name,
		));
		wp_update_post( array(
			'ID' 			=> $post->ID,
			'post_date' 	=> the_date('', '', '', FALSE),
			'post_date_gmt' => the_date('', '', '', FALSE),
		));
	
		update_post_meta( $post->ID, "custom_boxes", $cb );
		update_post_meta( $new_post_id, "custom_boxes", $cb );
 
		$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($new_post_id, $post_terms, $taxonomy, false);
		}
 
		$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
		if (count($post_meta_infos)!=0) {
			$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
			foreach ($post_meta_infos as $meta_info) {
				$meta_key = $meta_info->meta_key;
				if( $meta_key == '_wp_old_slug' ) continue;
				$meta_value = addslashes($meta_info->meta_value);
				$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
			}
			$sql_query.= implode(" UNION ALL ", $sql_query_sel);
			$wpdb->query($sql_query);
		}
 
		if( $return == 'redirect' ) {
			wp_redirect( admin_url( 'post.php?action=edit&post=' . $post_id ) );
			exit;
		} else {
			return $post_id;
		}
	} else {
		wp_die('Post creation failed, could not find original post: ' . $post_id);
	}
}

add_action( 'admin_action_px_convert_post_old_version', 'px_convert_post_old_version' );

function px_convert_post_old_version(){
	global $wpdb;
	
	if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'px_convert_post_old_version' == $_REQUEST['action'] ) ) ) {
		wp_die('No post to duplicate has been supplied!');
	}
 
	if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
		return;
		
	$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );

	apply_filters( 'px_process_convert_post_old_version', $post_id, 'redirect' );
}

add_filter( 'post_row_actions', 'px_duplicate_post_link', 25, 2 );
add_filter( 'page_row_actions', 'px_duplicate_post_link', 10, 2 );

function px_duplicate_post_link( $actions, $post ) {
	if( current_user_can('edit_posts') && $post->post_parent == 0 && $post->post_status == "publish" && $post->post_type == "post" ) {

		$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=px_convert_post_old_version&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="'.__( 'Convert to previous version', THEMES_NAMES ).'" rel="permalink">'.__( 'Convert to previous version', THEMES_NAMES ).'</a>';
	}
	return $actions;
}


# Verify content duplicate
if(!function_exists('exthemes_duplicate_titles_enqueue_scripts')){
	function exthemes_duplicate_titles_enqueue_scripts( $hook ) {
		if( !in_array( $hook, array( 'post.php', 'post-new.php' ) ) )
			return;

		wp_enqueue_script( 'duptitles',wp_enqueue_script( 'duptitles',get_template_directory_uri().'/assets/js/duptitles.js',array( 'jquery' )), array( 'jquery' )  );
	}
	add_action( 'admin_enqueue_scripts', 'exthemes_duplicate_titles_enqueue_scripts', 2000 );
	add_action( 'wp_ajax_title_check', 'exthemes_duplicate_title_check_callback');
}

# callback ajax  duplicate content
if(!function_exists('exthemes_duplicate_title_check_callback')){
	function exthemes_duplicate_title_check_callback() {

		function title_check() {

			$title 		= $_POST['post_title'];
			$post_id 	= $_POST['post_id'];
			$post_type 	= $_POST['post_type'];

			global $wpdb;

			$sim_titles = "SELECT post_title,ID FROM $wpdb->posts WHERE post_type = '{$post_type}' AND post_title LIKE '%{$title}%' AND ID != {$post_id} ";

			$sim_results = $wpdb->get_results($sim_titles);

			if($sim_results) {
				$titles = '<ul>';
				foreach ( $sim_results as $the_title )
				{
					$titles .= '<li><a href="'. get_edit_post_link($the_title->ID) .'" target="_blank">'.$the_title->post_title.'</a></li>';
				}
				$titles .= '</ul>';

				return 'Post title seems to exist already: '.$titles;
			}
			  else {
				return 'Post title seems to be unique';
			 }
		}
		echo title_check();
		die();
	}
}

function disable_autosave() {
	wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'disable_autosave' );

