<?php 
/**
 * Trending news meta-fields
**/ 
?>
<?php

//============Video link meta field=================

add_action ( 'load-post.php', 'trendingnewsmetabox' );
add_action ( 'load-post-new.php', 'trendingnewsmetabox' );

// Meta box setup function.

function trendingnewsmetabox() {
	add_action ( 'add_meta_boxes', 'trendingnews_add_post_meta' );
	add_action ( 'save_post', 'trendingnews_save_post_meta', 10, 2 );
}

//Display meta box.

function trendingnews_add_post_meta() {
	add_meta_box ( 'trendingnews-video-link', 'Featured Video', 'trendingnews_video_link_box', 'post', 'normal', 'high' );
}

function trendingnews_video_link_box($object, $box) {wp_nonce_field( basename( __FILE__ ), 'trendingnews_video_link_nonce' ); ?>

<p>
	<label for="trendingnews-video-link">
		<?php echo "Paste a link from Vimeo or Youtube, it will be embeded in the post."; ?>
	</label>
	<input class="widefat" type="text" name="trendingnews-video-link" id="trendingnews-video-link" value="<?php echo get_post_meta( $object->ID, 'trendingnews_video_link', true ); ?>"size="30" />
</p>

<?php
}

//Save the metabox value.

function trendingnews_save_post_meta($post_id, $post) {
	if (! isset ( $_POST ['trendingnews_video_link_nonce'] ) || ! wp_verify_nonce ( $_POST ['trendingnews_video_link_nonce'], basename ( __FILE__ ) ))
		return $post_id;
	$post_type = get_post_type_object ( $post->post_type );
	if (! current_user_can ( $post_type->cap->edit_post, $post_id ))
		return $post_id;
	$new_meta_value = (isset ( $_POST ['trendingnews-video-link'] ) ? balanceTags ( $_POST ['trendingnews-video-link'] ) : '');
	$meta_key = 'trendingnews_video_link';
	$meta_value = get_post_meta ( $post_id, $meta_key, true );
	if ($new_meta_value && '' == $meta_value)
		add_post_meta ( $post_id, $meta_key, $new_meta_value, true );
	elseif ($new_meta_value && $new_meta_value != $meta_value)
		update_post_meta ( $post_id, $meta_key, $new_meta_value );
	elseif ('' == $new_meta_value && $meta_value)
		delete_post_meta ( $post_id, $meta_key, $meta_value );
}


function user_social_profile_fields($user) {
	
//================Author social links=================
	
	?>
<h3>
	<?php 'Social Profiles'; ?>
</h3>
<table class="form-table">
	<tr>
		<th><label for="twitter">
				<?php echo 'Twitter'; ?>
			</label></th>
		<td><input type="text" name="twitter" id="twitter"value="<?php echo get_the_author_meta( 'twitter', $user->ID ); ?>"class="regular-text" /></td>
	</tr>
	<tr>
		<th><label for="facebook">
				<?php echo 'Facebook'; ?>
			</label></th>
		<td><input type="text" name="facebook" id="facebook"value="<?php echo get_the_author_meta( 'facebook', $user->ID  ); ?>"class="regular-text" /></td>
	</tr>
	<tr>
		<th><label for="google">
				<?php echo 'Google+'; ?>
			</label></th>
		<td><input type="text" name="google" id="google"value="<?php echo get_the_author_meta( 'google', $user->ID  ); ?>"class="regular-text" /></td>
	</tr>
	<tr>
		<th><label for="pinterest">
				<?php echo 'Pinterest'; ?>
			</label></th>
		<td><input type="text" name="pinterest" id="pinterest"value="<?php echo get_the_author_meta( 'pinterest', $user->ID ); ?>"class="regular-text" /></td>
	</tr>
	<tr>
		<th><label for="linkedin">
				<?php echo 'Linkedin'; ?>
			</label></th>
		<td><input type="text" name="linkedin" id="linkedin" value="<?php echo get_the_author_meta( 'linkedin', $user->ID  ); ?>"class="regular-text" /></td>
	</tr>
</table>
<?php
}
function save_user_social_profile_fields($user_id) {
	
//Save the metabox value.

	if (! current_user_can ( 'edit_user', $user_id ))
		return false;
	
	update_user_meta ( $user_id, 'twitter', $_POST ['twitter'] );
	update_user_meta ( $user_id, 'facebook', $_POST ['facebook'] );
	update_user_meta ( $user_id, 'google', $_POST ['google'] );
	update_user_meta ( $user_id, 'pinterest', $_POST ['pinterest'] );
	update_user_meta ( $user_id, 'linkedin', $_POST ['linkedin'] );
}
add_action ( 'show_user_profile', 'user_social_profile_fields' );
add_action ( 'edit_user_profile', 'user_social_profile_fields' );

add_action ( 'personal_options_update', 'save_user_social_profile_fields' );
add_action ( 'edit_user_profile_update', 'save_user_social_profile_fields' );

//==================Subtitle====================

    add_action( 'edit_form_after_title', 'tn_subtitle_meta_box' );
    add_action( 'save_post', 'tn_save_subtitle_meta_box', 10, 2 );

function tn_subtitle_meta_box( $object ) { ?>
    <div id="tn_subtitle">
    <p>
    
        <label>Please enter sub title or favorite quote from the text:</label>
        <input name="tn_sub_title" id="sw_title" style="width: 100%;" value="<?php echo esc_html( get_post_meta( $object->ID, 'tn_sub_title', true ), 1 ); ?>" />
        <input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
    </p>
    </div>
<?php }

function tn_save_subtitle_meta_box( $post_id, $post ) {

    if (! isset ( $_POST ['my_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['my_meta_box_nonce'] , basename( __FILE__ ) ) )
        return $post_id;

    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    //Saving 1st Data
    
    $meta_value = get_post_meta( $post_id, 'tn_sub_title', true );
    $new_meta_value = stripslashes( $_POST['tn_sub_title'] );

    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, 'tn_sub_title', $new_meta_value, true );

    elseif ( $new_meta_value != $meta_value )
        update_post_meta( $post_id, 'tn_sub_title', $new_meta_value );

    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, 'tn_sub_title', $meta_value ); 
}
function tn_subtitle() {
		global $post;
		echo get_post_meta($post->ID, 'tn_sub_title', true);
}



//=======Review metabox===========

// Adds a box to the Posts edit screens. 
add_action( 'add_meta_boxes', 'tn_review_add_meta_boxes' );

// Saves the meta box custom data. 
add_action( 'save_post', 'tn_review_save_postdata', 10, 2 );

//Adds a box to the Post edit screens.
function tn_review_add_meta_boxes() {
    $post_types = get_post_types( array('public' => true), 'names' );
    $excluded_post_types = array('attachment');
    
    foreach ($post_types as $post_type) {
        if (!in_array($post_type, $excluded_post_types)) {
		
        	add_meta_box(
        		'tn-review-metabox',
        		'Review Item',
        		'tn_review_render_meta_box',
        		$post_type,
        		'normal',
        		'high'
        	);
        }
    }
}


//Render the meta box.

 
function tn_review_render_meta_box( $post ) {
	
// Retrieve an existing value from the database.
	$heading = get_post_meta( $post->ID, 'tn_review_heading', true );
	$image = get_post_meta( $post->ID, 'tn_review_image', true );
	
	
	
	
	
	$good = get_post_meta( $post->ID, 'tn_review_good', true );
	
	$goodCriteria = apply_filters('tn_review_good_criteria', array());
    $goodItems = array();
    foreach ($goodCriteria as $item) {
        $goodItems[] = array( 'tn_review_good' => $item);
    }
		
	$bad = get_post_meta( $post->ID, 'tn_review_bad', true );

	$badCriteria = apply_filters('tn_review_bad_criteria', array());
    $badItems = array();
    foreach ($badCriteria as $item) {
        $badItems[] = array( 'tn_review_bad' => $item);
    }

	$defaultCriteria = apply_filters('tn_review_default_criteria', array());
    $defaultItems = array();
    foreach ($defaultCriteria as $item) {
        $defaultItems[] = array( 'tn_review_item_title' => $item, 'tn_review_item_score' => '');
    }
	$items     = get_post_meta( $post->ID, 'tn_review_item', true );
	
	
	
	
	if ( $items == '' ) $items = $defaultItems; 
    
// Add an nonce field so we can check for it later.
	wp_nonce_field( basename( __FILE__ ), 'tn-review-item-nonce' );
	wp_nonce_field( basename( __FILE__ ), 'tn-review-heading-nonce' );
	wp_nonce_field( basename( __FILE__ ), 'tn-review-image-nonce' );
	wp_nonce_field( basename( __FILE__ ), 'tn-review-bad-nonce' );
	wp_nonce_field( basename( __FILE__ ), 'tn-review-good-nonce' );
	
?>
<p class="tn-review-field">
    <div id="post-review-title"><?php echo 'Review Item Title'; ?></div>
    <input type="text" name="tn_review_heading" id="tn_review_heading" value="<?php echo $heading; ?>" />
</p>
<p class="tn-image-field">
<div id="tn-image-preview-title"><?php echo 'Image Selection'; ?></div>
<input type="text" name="tn_review_image" id="tn_review_image" value="<?php echo $image; ?>"/>
<input type= "button" class="button" name="image_button" id="image_button" value="Choose Image" />
</p>


<div class="tn-image-preview-wrapper">
    <div id="tn-image-preview-title"><?php echo 'Preview Image'; ?></div>
	<img class="tn-image-preview" src="<?php echo $image; ?>" width="100%">
</div>



<div class="tn-good-wrapper">
<!-- Start repeater field good -->
<table id="tn-review-good" class="tn-review-good" width="100%">
  <thead>
    <tr>
      <th width="100%"><?php echo 'Good'; ?></th>
    </tr>
  </thead>
  <tbody>
    <?php if ( !empty($good) ) : ?>
    <?php foreach ( $good as $item ) { ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_good[]" value="<?php if( !empty( $item['tn_review_good'] ) ) echo $item['tn_review_good'] ; ?>" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php } ?>
    <?php else : ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_good[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php endif; ?>
    
    <!-- empty hidden -->
    <tr class="empty-row screen-reader-text good">
      <td><input type="text" class="widefat" name="tn_review_good[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
  </tbody>
</table>
<table width="40%">
  <tr>
    <td width="80%"><a id="add-row-good" class="button" href="#">
      <?php echo 'Add another'; ?>
      </a></td>
  </tr>
</table>
</div>

<div class="tn-bad-wrapper">
<!-- Start repeater field bad -->
<table id="tn-review-bad" class="tn-review-bad" width="100%">
  <thead>
    <tr>
      <th width="100%"><?php echo 'bad'; ?></th>
    </tr>
  </thead>
  <tbody>
    <?php if ( !empty($bad) ) : ?>
    <?php foreach ( $bad as $item ) { ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_bad[]" value="<?php if( !empty( $item['tn_review_bad'] ) ) echo $item['tn_review_bad'] ; ?>" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php } ?>
    <?php else : ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_bad[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php endif; ?>
    
    <!-- empty hidden -->
    <tr class="empty-row screen-reader-text bad">
      <td><input type="text" class="widefat" name="tn_review_bad[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
  </tbody>
</table>
<table width="100%">
  <tr>
    <td width="80%"><a id="add-row-bad" class="button" href="#">
      <?php echo 'Add another'; ?>
      </a></td>
  </tr>
</table>
</div>

<!-- Start repeater field -->
<table id="tn-review-item" class="tn-review-item" width="100%">
  <thead>
    <tr>
      <th width="80%"><?php echo 'Feature Name'; ?></th>
      <th width="10%" class="dynamic-text"><?php echo 'Score (1-10)'; ?></th>
      <th width="10%"></th>
    </tr>
  </thead>
  <tbody>
    <?php if ( !empty($items) ) : ?>
    <?php foreach ( $items as $item ) { ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_item_title[]" value="<?php if( !empty( $item['tn_review_item_title'] ) ) echo $item['tn_review_item_title'] ; ?>" /></td>
      <td><input type="text" min="1" step="1" autocomplete="off" class="widefat review-score" name="tn_review_item_score[]" value="<?php if ( !empty ($item['tn_review_item_score'] ) ) echo $item['tn_review_item_score']; ?>" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php } ?>
    <?php else : ?>
    <tr>
      <td><input type="text" class="widefat" name="tn_review_item_title[]" /></td>
      <td><input type="text" min="1" step="1" autocomplete="off" class="widefat review-score" name="tn_review_item_score[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
    <?php endif; ?>
    
    <!-- empty hidden -->
    <tr class="empty-row screen-reader-text scores">
      <td><input type="text" class="widefat" name="tn_review_item_title[]" /></td>
      <td><input type="text" min="1" step="1" autocomplete="off" class="widefat" name="tn_review_item_score[]" /></td>
      <td><a class="button remove-row" href="#">
        <?php echo 'Delete'; ?>
        </a></td>
    </tr>
  </tbody>
</table>
<table width="100%">
  <tr>
    <td width="80%"><a id="add-row" class="button" href="#">
      <?php echo 'Add another'; ?>
      </a></td>
    <td width="10%"><input type="text" class="widefat tn-review-total" name="tn_review_total" value="<?php echo get_post_meta( $post->ID, 'tn_review_total', true ); ?>" readonly /></td>
    <td width="10%"><?php echo'Total'; ?></td>
  </tr>
</table>

<?php
}


	// Saves the meta box.

function tn_review_save_postdata( $post_id, $post ) {

	if ( !isset( $_POST['tn-review-item-nonce'] ) || !wp_verify_nonce( $_POST['tn-review-item-nonce'], basename( __FILE__ ) ) )
		return;

	if ( !isset( $_POST['tn-review-heading-nonce'] ) || !wp_verify_nonce( $_POST['tn-review-heading-nonce'], basename( __FILE__ ) ) )
		return;
		
	if ( !isset( $_POST['tn-review-image-nonce'] ) || !wp_verify_nonce( $_POST['tn-review-image-nonce'], basename( __FILE__ ) ) )
		return;
		
	if ( !isset( $_POST['tn-review-good-nonce'] ) || !wp_verify_nonce( $_POST['tn-review-good-nonce'], basename( __FILE__ ) ) )
		return;
		
	if ( !isset( $_POST['tn-review-bad-nonce'] ) || !wp_verify_nonce( $_POST['tn-review-bad-nonce'], basename( __FILE__ ) ) )
		return;

	// If this is an autosave, our form has not been submitted, so we don't want to do anything. 
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
		return $post_id;

	// Check the user's permissions.
	if ( 'page' == $_POST['post_type'] ) {
		if ( ! current_user_can( 'edit_page', $post_id ) )
			return $post_id;
	} else {
		if ( ! current_user_can( 'edit_post', $post_id ) )
			return $post_id;
	}

	$meta = array(

		'tn_review_total'    => $_POST['tn_review_total'],
		'tn_review_heading'     => $_POST['tn_review_heading'],
		'tn_review_image'     => $_POST['tn_review_image'],
	);

	foreach ( $meta as $meta_key => $new_meta_value ) {

		// Get the meta value of the custom field key. 4
		$meta_value = get_post_meta( $post_id, $meta_key, true );

		//If there is no new meta value but an old value exists, delete it.
		if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
			delete_post_meta( $post_id, $meta_key, $meta_value );

		// If a new meta value was added and there was no previous value, add it. 
		elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
			add_post_meta( $post_id, $meta_key, $new_meta_value, true );

		// If the new meta value does not match the old value, update it. 
		elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $new_meta_value && $new_meta_value != $meta_value )
			update_post_meta( $post_id, $meta_key, $new_meta_value );
	}

	// Repeatable update and delete meta fields method.
	$title = $_POST['tn_review_item_title'];
	$score  = $_POST['tn_review_item_score'];

	$old   = get_post_meta( $post_id, 'tn_review_item', true );
	$new   = array();

	$count = count( $title );
	
	for ( $i = 0; $i < $count; $i++ ) {
		if ( $title[$i] != '' )
			$new[$i]['tn_review_item_title'] = sanitize_text_field( $title[$i] );
		if ( $score[$i] != '' )
			$new[$i]['tn_review_item_score'] = sanitize_text_field( $score[$i] );
	}

	if ( !empty( $new ) && $new != $old )
		update_post_meta( $post_id, 'tn_review_item', $new );
	elseif ( empty($new) && $old )
		delete_post_meta( $post_id, 'tn_review_item', $old );
		
		
	// Repeatable field Good.
	$good = $_POST['tn_review_good'];
	$old   = get_post_meta( $post_id, 'tn_review_good', true );
	$new   = array();

	$count = count( $good );
	
	for ( $i = 0; $i < $count; $i++ ) {
		if ( $good[$i] != '' )
			$new[$i]['tn_review_good'] = sanitize_text_field( $good[$i] );
	}

	if ( !empty( $new ) && $new != $old )
		update_post_meta( $post_id, 'tn_review_good', $new );
	elseif ( empty($new) && $old )
		delete_post_meta( $post_id, 'tn_review_good', $old );
		
		
	//Repeatable field Bad.
	$bad = $_POST['tn_review_bad'];
	$old   = get_post_meta( $post_id, 'tn_review_bad', true );
	$new   = array();

	$count = count( $bad );
	
	for ( $i = 0; $i < $count; $i++ ) {
		if ( $bad[$i] != '' )
			$new[$i]['tn_review_bad'] = sanitize_text_field( $bad[$i] );
	}

	if ( !empty( $new ) && $new != $old )
		update_post_meta( $post_id, 'tn_review_bad', $new );
	elseif ( empty($new) && $old )
		delete_post_meta( $post_id, 'tn_review_bad', $old );

}

function review_scripts($hook) {
	if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
			wp_enqueue_script('tnreview', get_template_directory_uri() . '/js/tn-post.js', array('jquery'));
			wp_enqueue_style( 'tn-review-style', get_template_directory_uri() . '/inc/tn-post-style.css' );
	}
}
add_action( 'admin_enqueue_scripts', 'review_scripts' );