<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function thim_hex2rgb( $hex ) {
	$hex = str_replace( "#", "", $hex );
	if ( strlen( $hex ) == 3 ) {
		$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
		$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
		$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
	} else {
		$r = hexdec( substr( $hex, 0, 2 ) );
		$g = hexdec( substr( $hex, 2, 2 ) );
		$b = hexdec( substr( $hex, 4, 2 ) );
	}
	$rgb = array( $r, $g, $b );

	return $rgb; // returns an array with the rgb values
}

function thim_getExtraClass( $el_class ) {
	$output = '';
	if ( $el_class != '' ) {
		$output = " " . str_replace( ".", "", $el_class );
	}

	return $output;
}

function thim_getCSSAnimation( $css_animation ) {
	$output = '';
	if ( $css_animation != '' ) {
		wp_enqueue_script( 'waypoints' );
		$output = ' wpb_animate_when_almost_visible wpb_' . $css_animation;
	}

	return $output;
}

function thim_excerpt( $limit ) {
	$content = get_the_excerpt();
	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]>', ']]&gt;', $content );
	$content = explode( ' ', $content, $limit );
	array_pop( $content );
	$content = implode( " ", $content );

	return $content;
}

// function convert type int variable to string variable
function thim_convertIntToString( $id ) {
	$src = '';
	if ( is_numeric( $id ) ) {
		$imageAttachment = wp_get_attachment_image_src( $id );
		$src             = $imageAttachment[0];
	}

	return $src;
}

/************ List Comment ***************/
if ( !function_exists( 'thim_comment' ) ) {
	function thim_comment( $comment, $args, $depth ) {
		$GLOBALS['comment'] = $comment;
		//extract( $args, EXTR_SKIP );
		if ( 'div' == $args['style'] ) {
			$tag       = 'div';
			$add_below = 'comment';
		} else {
			$tag       = 'li';
			$add_below = 'div-comment';
		}
		?>
		<<?php echo ent2ncr( $tag ) . ' '; ?><?php comment_class( 'description_comment' ) ?> id="comment-<?php comment_ID() ?>">
		<?php
		if ( $args['avatar_size'] != 0 ) {
			echo get_avatar( $comment, $args['avatar_size'] );
		}
		?>
		<div class="content-comment">
			<?php
			if ( is_plugin_active( 'comment-rating-stars/comment-star-reviews.php' ) ) {
				echo csr_get_rating_stars( get_comment_ID() );
			}
			?>
			<?php printf( wp_kses( __( '<span class="author-name">%s</span>', 'cakeart' ), array( 'span' => array() ) ), esc_html( get_comment_author_link() ) ) ?>
			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'cakeart' ) ?></em>
			<?php endif; ?>
			<div class="comment-extra-info">
				<div class="date"><?php printf( get_comment_date(), get_comment_time() ) ?></div>
				<?php edit_comment_link( esc_html__( 'Edit', 'cakeart' ), '', '' ); ?>
				<?php comment_reply_link( array_merge( $args, array(
					'add_below' => $add_below,
					'depth'     => $depth,
					'max_depth' => $args['max_depth']
				) ) ) ?>
			</div>

			<div class="message">
				<?php comment_text() ?>
			</div>
			<div class="clear"></div>
		</div>
		<?php
	}
}
/************end list comment************/

/* Import/Export Customizer Setting */
if ( !function_exists( 'thim_cusomizer_upload_settings' ) ) :
	function thim_cusomizer_upload_settings() {
		WP_Filesystem();
		global $wp_filesystem;
		$file_name = $_FILES['thim-customizer-settings-upload']['name'];
		$file_ext  = pathinfo( $file_name, PATHINFO_EXTENSION );
		if ( $file_ext == 'json' ) {
			$encode_options = $wp_filesystem->get_contents( $_FILES['thim-customizer-settings-upload']['tmp_name'] );
			if ( !empty( $encode_options ) ) {
				echo esc_html( $encode_options );
				exit();
			}
		}
		exit( '-1' );
	}
endif;
add_action( 'wp_ajax_thim_cusomizer_upload_settings', 'thim_cusomizer_upload_settings' );
if ( !function_exists( 'thim_ajax_get_attachment_url' ) ) :
	function thim_ajax_get_attachment_url() {
		check_ajax_referer( 'thim_customize_attachment', 'nonce' );

		if ( !isset( $_POST['attachment_id'] ) ) {
			exit();
		}

		$attachment_id = $_POST['attachment_id'];

		echo wp_get_attachment_url( $attachment_id );
		exit();
	}
endif;

add_action( 'wp_ajax_thim_ajax_get_attachment_url', 'thim_ajax_get_attachment_url' );
add_action( 'wp_ajax_nopriv_thim_ajax_get_attachment_url', 'thim_ajax_get_attachment_url' );


/**
 * Display recipe video upload
 *
 * @param $id
 */
function thim_recipe_video_upload() {
	$data_video = get_post_meta( get_the_ID(), 'thim_custom_attachment', true );
	if ( $data_video ) {
		echo '<div class="video-container">';
		echo '<video width="100%" height="auto" controls>
  				<source src="' . $data_video['url'] . '" type="video/mp4">
  				<source src="' . $data_video['url'] . '" type="video/ogg">
  				<source src="' . $data_video['url'] . '" type="video/webm">';
		echo esc_html__( 'Your browser does not support the video tag.', 'cakeart' );
		echo '</video>';
		echo '</div>';
	}

}

/**
 * Display recipe video url
 *
 * @param $id
 */
function thim_recipe_video_url( $id = 0 ) {
	if ( !function_exists( 'thim_get_recipe_details' ) ) {
		return;
	}
	$details = thim_get_recipe_details( $id );
	if ( $details['video_url'] ) {
		echo '<div class="video-container">';
		echo wp_oembed_get( $details['video_url'] );
		echo '</div>';
	}

}

/**
 * Display recipe instructions
 *
 * @param $id
 */
function thim_recipe_instructions( $id = 0 ) {
	if ( !function_exists( 'thim_get_recipe_details' ) ) {
		return;
	}
	$details = thim_get_recipe_details( $id );
	if ( $details['instructions'] ) {

		$count = 1;
		foreach ( $details['instructions'] as $step ) {
			echo '<div class="step">
							<input type="checkbox" name="checkbox" id="step-checkbox' . $count . '">
							<label for="step-checkbox' . $count . '">' . esc_html__( 'Step', 'cakeart' ) . ' ' . esc_html( $count ) . '</label>
							<p>' . esc_html( $step ) . '</p>
							</div>';
			$count += 1;
		}
	}
}

/**
 * Display recipe ingredients
 *
 * @param $id
 */
function thim_recipe_ingredients( $id = 0 ) {
	if ( !function_exists( 'thim_get_recipe_details' ) ) {
		return;
	}
	$details = thim_get_recipe_details( $id );
	if ( $details['ingredients'] ) {
		$count = 1;
		foreach ( $details['ingredients'] as $ingredient ) {
			echo '<div class="ingredient">
							<input type="checkbox" name="checkbox" id="checkbox' . $count . '">
							<label for="checkbox' . $count . '">' . esc_html( $ingredient ) . '</label>
							</div>';
			$count += 1;
		}
	}
}

/**
 * Display recipe gallery
 *
 * @param $id
 */
function thim_recipe_gallery( $id = 0 ) {
	if ( !function_exists( 'thim_get_recipe_details' ) ) {
		return;
	}
	$details = thim_get_recipe_details( $id );
	if ( $details['gallery'] ) {
		$i          = 0;
		$indicators = '';
		$images     = '';
		foreach ( $details['gallery'] as $image ) {
			if ( $i ) {
				$class = '';
			} else {
				$class = 'active';
			}
			$indicators .= '<li data-target="#recipe-gallery" data-slide-to="' . $i . '" class="' . $class . '"></li>';
			$images .= '<div class="item ' . $class . '">' . wp_get_attachment_image( $image, 'full' ) . '</div>';
			$i ++;

		}

		printf(
			'<div id="recipe-gallery" class="carousel slide" data-ride="carousel">
				<ol class="carousel-indicators">%s</ol>
				<div class="carousel-inner" role="listbox">%s</div>
				<a class="left carousel-control control-slide-recipe" href="#recipe-gallery" role="button" data-slide="prev">
					<span class="dashicons dashicons-arrow-left-alt2 icon-control-slide-recipe" aria-hidden="true"></span>
					<span class="sr-only">%s</span>
				</a>
				<a class="right carousel-control control-slide-recipe" href="#recipe-gallery" role="button" data-slide="next">
					<span class="dashicons dashicons-arrow-right-alt2 icon-control-slide-recipe icon-control-next-slide-recipe" aria-hidden="true"></span>
					<span class="sr-only">%s</span>
				</a>
			</div>',
			$indicators,
			$images,
			esc_html__( 'Previous', 'cakeart' ),
			esc_html__( 'Next', 'cakeart' )
		);
	}

}

/**
 * Display other recipe information
 *
 * @param $id
 */
function thim_recipe_other( $id = 0 ) {
	if ( !function_exists( 'thim_get_recipe_details' ) ) {
		return;
	}
	$details = thim_get_recipe_details( $id );
	$images  = array(
		'/images/admin/recipe-detail/prep-time.png',
		'/images/admin/recipe-detail/cook-time.png',
		'/images/admin/recipe-detail/yield.png',
		'/images/admin/recipe-detail/servings.png'
	);
	$title   = array(
		esc_html__( 'Prep Time', 'cakeart' ),
		esc_html__( 'Cook Time', 'cakeart' ),
		esc_html__( 'Yield', 'cakeart' ),
		esc_html__( 'Servings', 'cakeart' )
	);
	$data    = array(
		'prep_time',
		'cook_time',
		'yield',
		'servings'
	);

	echo '<div class="recipe-detail-other">';
	echo '<div class="row">';
	for ( $count = 0; $count < 4; $count ++ ) {
		if ( 0 == $count || 1 == $count ) {
			$info = thim_recipe_time( $details[$data[$count]] );
		} else {
			$info = $details[$data[$count]];
		}
		printf(
			'<div class="col-md-3 col-sm-3 col-xs-6 recipe-detail-component">
				<div class="col-md-4">
					<img src="%s" alt="recipe detail">
				</div>
				<div class="col-md-8">
					<em>%s</em>
					<p class="detail-info">%s</p>
				</div>
			</div>',
			THIM_URI . $images[$count],
			esc_html__( $title[$count], 'cakeart' ),
			esc_html( $info )
		);
	}
	echo '</div>';
	echo '</div>';
}

/**
 * Process recipe time
 *
 * @param $time
 *
 * @return string
 */
function thim_recipe_time( $time ) {
	$display_time = '';
	if ( !$time ) {
		return esc_html__( 'Unknown', 'cakeart' );
	}
	$hours   = floor( $time / 60 );
	$minutes = $time - $hours * 60;
	if ( $hours ) {
		$display_time .= $hours . ( $hours == 1 ? esc_html__( ' hour ', 'cakeart' ) : esc_html__( ' hours ', 'cakeart' ) );
	}
	if ( $minutes ) {
		$display_time .= $minutes . ( $minutes == 1 ? esc_html__( ' minute', 'cakeart' ) : esc_html__( ' minutes', 'cakeart' ) );
	}

	return $display_time;
}

function thim_related_recipe() {
	global $post;
	$cat = wp_get_post_terms( $post->ID, 'recipe_category' );
	if ( $cat ) {
		$cat_ids = array();

		foreach ( $cat as $individual_cat ) {
			$cat_ids[] = $individual_cat->term_id;
		}
		$args                       = array(
			'post_type'           => 'recipe',
			'tax_query'           => array(
				array(
					'taxonomy' => 'recipe_category',
					'terms'    => $cat_ids,
					'operator' => 'IN'
				)
			),
			'post__not_in'        => array( $post->ID ),
			'posts_per_page'      => 3,
			'ignore_sticky_posts' => 1
		);
		$recipes_query              = new wp_query( $args );
		$recipes_query->in_the_loop = true;

		if ( $recipes_query->have_posts() ) {
			echo '<div class="relate-recipe">';
			echo '<h3>' . esc_html__( 'YOU MAY ALSO LIKE', 'cakeart' ) . '</h3>';
			echo '<div class="wrap-recipes"><div class="row">';

			while ( $recipes_query->have_posts() ): $recipes_query->the_post();
				get_template_part( 'content', 'recipe' );
			endwhile;

			echo '</div></div></div>';
		}
		wp_reset_postdata();
	}
}


//thim_excerpt_length
function thim_excerpt_length() {
	$theme_options_data = theme_options_data();
	if ( isset( $theme_options_data['thim_archive_excerpt_length'] ) ) {
		$length = $theme_options_data['thim_archive_excerpt_length'];
	} else {
		$length = 50;
	}

	return $length;
}

add_filter( 'excerpt_length', 'thim_excerpt_length', 999 );
function thim_wp_new_excerpt( $text ) {
	if ( $text == '' ) {
		$text           = get_the_content( '' );
		$text           = strip_shortcodes( $text );
		$text           = apply_filters( 'the_content', $text );
		$text           = str_replace( ']]>', ']]>', $text );
		$text           = strip_tags( $text );
		$text           = nl2br( $text );
		$excerpt_length = apply_filters( 'excerpt_length', 55 );
		$words          = explode( ' ', $text, $excerpt_length + 1 );
		if ( count( $words ) > $excerpt_length ) {
			array_pop( $words );
			array_push( $words, '' );
			$text = implode( ' ', $words );
			$text .= '...';
		}
	}

	return $text;
}

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'thim_wp_new_excerpt' );


function thim_post_share() {
	$theme_options_data = theme_options_data();
	if ( ( isset( $theme_options_data['thim_sharing_facebook'] ) && $theme_options_data['thim_sharing_facebook'] == 1 ) ||
		( isset( $theme_options_data['thim_sharing_twitter'] ) && $theme_options_data['thim_sharing_twitter'] == 1 ) ||
		( isset( $theme_options_data['thim_sharing_pinterest'] ) && $theme_options_data['thim_sharing_pinterest'] ) == 1 ||
		( isset( $theme_options_data['thim_sharing_google'] ) && $theme_options_data['thim_sharing_google'] ) == 1
	) {
		echo '<ul class="social-share">';
		if ( $theme_options_data['thim_sharing_facebook'] == 1 ) {
			echo '<li><div class="facebook-social"><a target="_blank" class="facebook"  href="https://www.facebook.com/sharer.php?u=' . urlencode( get_permalink() ) . '" title="' . esc_attr__( 'Facebook', 'cakeart' ) . '"><i class="fa fa-facebook"></i></a></div></li>';
		}
		if ( $theme_options_data['thim_sharing_twitter'] == 1 ) {
			echo '<li><div class="twitter-social"><a target="_blank" class="twitter" href="https://twitter.com/share?url=' . urlencode( get_permalink() ) . '&amp;text=' . rawurlencode( esc_attr( get_the_title() ) ) . '" title="' . esc_attr__( 'Twitter', 'cakeart' ) . '"><i class="fa fa-twitter"></i></a></div></li>';
		}
		if ( $theme_options_data['thim_sharing_google'] == 1 ) {
			echo '<li><div class="googleplus-social"><a target="_blank" class="googleplus" href="https://plus.google.com/share?url=' . urlencode( get_permalink() ) . '&amp;title=' . rawurlencode( esc_attr( get_the_title() ) ) . '" title="' . esc_attr__( 'Google Plus', 'cakeart' ) . '" onclick=\'javascript:window.open(this.href, "", "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600");return false;\'><i class="fa fa-google-plus"></i></a></div></li>';
		}
		if ( $theme_options_data['thim_sharing_pinterest'] == 1 ) {
			echo '<li><div class="pinterest-social"><a target="_blank" class="pinterest"  href="http://pinterest.com/pin/create/button/?url=' . urlencode( get_permalink() ) . '&amp;description=' . rawurlencode( esc_attr( get_the_excerpt() ) ) . '&amp;media=' . urlencode( wp_get_attachment_url( get_post_thumbnail_id() ) ) . '" onclick="window.open(this.href); return false;" title="' . esc_attr__( 'Pinterest', 'cakeart' ) . '"><i class="fa fa-pinterest"></i></a></div></li>';
		}
		echo '</ul>';
	}

}

add_action( 'thim_social_share', 'thim_post_share' );

add_filter( 'thim_mtb_setting_after_created', 'thim_mtb_setting_after_created', 10, 2 );
function thim_mtb_setting_after_created( $mtb_setting ) {
	$mtb_setting->removeOption( array( 6, 11 ) );

	$settings = array(
		'name' => esc_html__( 'No Padding Content', 'cakeart' ),
		'id'   => 'mtb_no_padding',
		'type' => 'checkbox',
		'desc' => ' ',
	);

	$mtb_setting->insertOptionBefore( $settings, 15 );

	return $mtb_setting;
}


/**
 * Add upload-file metabox for recipe
 *
 */
function thim_add_custom_meta_boxes() {

	// Define the custom attachment for posts
	add_meta_box(
		'thim_custom_attachment',
		'Upload Video',
		'thim_custom_attachment',
		'recipe',
		'normal',
		'high'
	);

} // end add_custom_meta_boxes
add_action( 'add_meta_boxes', 'thim_add_custom_meta_boxes' );

function thim_custom_attachment() {

	wp_nonce_field( plugin_basename( __FILE__ ), 'thim_custom_attachment_nonce' );
	echo '<p class="description">';
	echo esc_html__( 'Upload your video here.', 'cakeart' );
	echo '</p>';
	echo '<input type="file" id="thim_custom_attachment" name="thim_custom_attachment" value="" size="25" />';
	echo '<p>' . esc_html__( 'Maximum upload file size: ', 'cakeart' ) . esc_html( size_format( wp_max_upload_size() ) ) . '</p>';
	// Grab the array of file information currently associated with the post
	$doc = get_post_meta( get_the_ID(), 'thim_custom_attachment', true );

	if ( $doc ) {
		// Create the input box and set the file's URL as the text element's value
		echo '<input type="text" id="thim_custom_attachment_url" name="thim_custom_attachment_url" value=" ' . esc_attr( $doc['url'] ) . '" size="30" />';

		// Display the 'Delete' option if a URL to a file exists
		if ( strlen( trim( $doc['url'] ) ) > 0 ) {
			echo '<a href="javascript:;" id="thim_custom_attachment_delete">' . esc_html__( 'Delete File', 'cakeart' ) . '</a>';
		} // end if
	}

} // end thim_custom_attachment

function thim_save_custom_meta_data( $id ) {

	/* --- security verification --- */
	// Check if our nonce is set.
	if ( !isset( $_POST['thim_custom_attachment_nonce'] ) ) {
		return;
	}

	if ( !wp_verify_nonce( $_POST['thim_custom_attachment_nonce'], plugin_basename( __FILE__ ) ) ) {
		return;
	} // end if

	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	} // end if

	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $id ) ) {
			return;
		} // end if
	} else {
		if ( !current_user_can( 'edit_page', $id ) ) {
			return;
		} // end if
	} // end if
	/* - end security verification - */

	// Make sure the file array isn't empty
	if ( !empty( $_FILES['thim_custom_attachment']['name'] ) ) {

		// Setup the array of supported file types.
		$supported_types = array( 'video/mp4', 'video/webm', 'video/ogg' );

		// Get the file type of the upload
		$arr_file_type = wp_check_filetype( basename( $_FILES['thim_custom_attachment']['name'] ) );
		$uploaded_type = $arr_file_type['type'];

		// Check if the type is supported. If not, throw an error.
		if ( in_array( $uploaded_type, $supported_types ) ) {

			// Use the WordPress API to upload the file
			$upload = wp_upload_bits( $_FILES['thim_custom_attachment']['name'], null, $wp_filesystem->get_contents( $_FILES['thim_custom_attachment']['tmp_name'] ) );

			if ( isset( $upload['error'] ) && $upload['error'] != 0 ) {
				wp_die( esc_html__( 'There was an error uploading your file. The error is: ', 'cakeart' ) . $upload['error'] );
			} else {
				add_post_meta( $id, 'thim_custom_attachment', $upload );
				update_post_meta( $id, 'thim_custom_attachment', $upload );
			} // end if/else
		} else {
			wp_die( esc_html__( "The file type that you've uploaded is not a video.", "cakeart" ) );
		} // end if/else

	} else {
		// Grab a reference to the file associated with this post
		$doc = get_post_meta( $id, 'thim_custom_attachment', true );

		// Grab the value for the URL to the file stored in the text element
		$delete_flag = get_post_meta( $id, 'thim_custom_attachment_url', true );

		// Determine if a file is associated with this post and if the delete flag has been set (by clearing out the input box)
		if ( (isset( $doc['url'] ) && strlen( trim( $doc['url'] ) ) > 0 ) && strlen( trim( $delete_flag ) ) == 0 ) {

			// Attempt to remove the file. If deleting it fails, print a WordPress error.
			if ( $doc['file'] ) {

				// Delete succeeded so reset the WordPress meta data
				update_post_meta( $id, 'thim_custom_attachment', null );
				update_post_meta( $id, 'thim_custom_attachment_url', '' );

			} else {
				wp_die( esc_html__( 'There was an error trying to delete your video.', 'cakeart' ) );
			} // end if/el;se

		} // end if

	} // end if/else

} // end save_custom_meta_data
add_action( 'save_post', 'thim_save_custom_meta_data' );

function thim_update_edit_form() {
	echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action( 'post_edit_form_tag', 'thim_update_edit_form' );

//End of adding upload-file metabox for recipe

function thim_ob_ajax_url() {
	echo '<script type="text/javascript">
        var thim_ob_ajax_url ="' . esc_url( get_site_url() ) . '/wp-admin/admin-ajax.php";
        var export_url = "' . esc_url( get_site_url() ) . '/wp-admin/options.php?page=export_settings";
        </script>';
}

add_action( 'wp_print_scripts', 'thim_ob_ajax_url' );

/* The function that handles the AJAX request */
function thim_substr_words( $str, $txt_len, $end_txt = '...' ) {
	$words   = explode( ' ', $str );
	$new_str = '';
	foreach ( $words as $k => $val ) {
		if ( $k < $txt_len ) {
			$new_str .= $val . ' ';
		}
	}
	$new_str = rtrim( $new_str, ' ,.;:' );
	$new_str .= $end_txt;

	return $new_str;
}

function thim_result_search_callback() {
	ob_start();
	function thim_search_title_filter( $where, &$wp_query ) {
		global $wpdb;
		if ( $keyword = $wp_query->get( 'search_prod_title' ) ) {
			$where .= ' AND ' . esc_sql( $wpdb->posts ) . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $keyword ) ) . '%\'';
		}

		return $where;
	}

	$keyword = $_REQUEST['keyword'];

	if ( $keyword ) {
		$search_query = array(
			'search_prod_title' => $keyword,
			'order'             => 'DESC',
			'orderby'           => 'date',
			'post_status'       => 'publish',
			'posts_per_page'    => 3,
			'post_type'         => array(
				'post',
				'page',
				'recipe',
				'product'
			),
		);
		add_filter( 'posts_where', 'thim_search_title_filter', 10, 2 );
		$search                    = new WP_Query( $search_query );
		$search_query->in_the_loop = true;

		remove_filter( 'posts_where', 'thim_search_title_filter', 10, 2 );

		$newdata = array();
		if ( $search ) {
			foreach ( $search->posts as $post ) {
				$newdata[] = array(
					'id'        => esc_attr( $post->ID ),
					'title'     => esc_attr( $post->post_title ),
					'guid'      => esc_url( get_permalink( $post->ID ) ),
					'date'      => mysql2date( 'M d Y', $post->post_date ),
					'thumbnail' => get_the_post_thumbnail( $post->ID, 'thumbnail' ),
					'shortdesc' => $post->post_content ? thim_substr_words( strip_shortcodes( $post->post_content ), 9, '...' ) : ''
				);
			}
		}
		ob_end_clean();
		echo json_encode( $newdata );
	}
	die(); // this is required to return a proper result
}

add_action( 'wp_ajax_nopriv_result_search', 'thim_result_search_callback' );
add_action( 'wp_ajax_result_search', 'thim_result_search_callback' );

//show_author_recipe
function thim_show_author_recipe( $query_string ) {
	if ( isset( $query_string['author_name'] ) ) {
		$query_string['post_type'] = array( 'post', 'recipe' );
	}

	return $query_string;
}

add_filter( 'request', 'thim_show_author_recipe' );

/**
 * Display feature image
 *
 * @param $attachment_id
 * @param $width
 * @param $height
 * @param $alt
 * @param $title
 *
 * @return string
 */
function thim_get_feature_image( $attachment_id, $size = null, $width = null, $height = null, $alt = null, $title = null ) {
	if ( !$size ) {
		$size = 'full';
	}
	$src = wp_get_attachment_image_src( $attachment_id, $size );
	if ( !$src ) {
		// Get demo image
		global $wpdb;
		$attachment_id = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT p.ID FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
				WHERE 	pm.meta_key = %s
				AND 	pm.meta_value LIKE %s",
				'_wp_attached_file',
				'%demo_image.jpg'
			)
		);
		if ( empty( $attachment_id[0] ) ) {
			return;
		}
		$attachment_id = $attachment_id[0];
		$src           = wp_get_attachment_image_src( $attachment_id, 'full' );
	}
	if ( $width && $height ) {
		if ( $src[1] >= $width || $src[2] >= $height ) {
			$crop   = ( $src[2] >= $height ) ? true : false;
			$src[0] = aq_resize( $src[0], $width, $height, $crop );
		}
	}
	if ( !$alt ) {
		$alt = get_the_title( $attachment_id );
	}
	if ( !$title ) {
		$title = get_the_title( $attachment_id );
	}
	return '<img src="' . esc_url( $src[0] ) . '" alt="' . esc_attr( $alt ) . '" title="' . esc_attr( $title ) . '">';
}

// get data customizer
function theme_options_data() {
	global $theme_options_data;
	$theme_options_data = get_theme_mods();
	return $theme_options_data;
}

add_action( 'wp_head', 'thim_favicon' );

if ( !function_exists( 'thim_favicon' ) ) {
	function thim_favicon() {
		if ( !function_exists( 'wp_site_icon' ) ) {
			$theme_options_data = theme_options_data();
			if ( isset( $theme_options_data['thim_favicon'] ) ) {
				$thim_favicon       = $theme_options_data['thim_favicon'];
				$favicon_attachment = wp_get_attachment_image_src( $thim_favicon, 'full' );
				$thim_favicon_src   = $favicon_attachment[0];
			} else {
				$thim_favicon_src = THIM_URI . "images/favicon.png";
			}
			echo '<link rel="shortcut icon" href="' . esc_url( $thim_favicon_src ) . '" type="image/x-icon" />';

		} else if ( !has_site_icon() ) {
			$thim_favicon_src = THIM_URI . "images/favicon.png";
			echo '<link rel="shortcut icon" href="' . esc_url( $thim_favicon_src ) . '" type="image/x-icon" />';
		}
	}
}


if ( get_theme_mod( 'thim_enable_gutenberg_widget_editor', 0 ) == '0' ) {
	// Disables the block editor from managing widgets in the Gutenberg plugin.
	add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
	// Disables the block editor from managing widgets.
	add_filter( 'use_widgets_block_editor', '__return_false' );
}