post_id'];
		if (! current_user_can( 'edit_post', $post_id ) )
			wp_die();
	} else {
		$post_id = null;
	}

	$post_data = isset( $_REQUEST['post_data'] )? $_REQUEST['post_data'] : array();

	// If the context is custom header or background, make sure the uploaded file is an image.
	if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) {
		$wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'], false );
		if (! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
			echo json_encode( array(
				'success' => false,
				'data'    => array(
					'message'  => __( 'The uploaded file is not a valid image. Please try again.' ),
					'filename' => $_FILES['async-upload']['name'],
				)
			) );

			wp_die();
		}
	}

	$attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );

	if ( is_wp_error( $attachment_id ) ) {
		echo json_encode( array(
			'success' => false,
			'data'    => array(
				'message'  => $attachment_id->get_error_message(),
				'filename' => $_FILES['async-upload']['name'],
			)
		) );

		wp_die();
	}

	if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) {
		if ( 'custom-background' === $post_data['context'] )
			update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] );

		if ( 'custom-header' === $post_data['context']
post_id' ]; 
		 if (! current_user_can (' edit_post', $ post_id) ) 
			 wp_die (); 
	 } else { 
		 $ post_id = null; 
	 } 

	 $ post_data = isset ($ _ REQUEST['post_data' ])? $ _ REQUEST['post_data' ]: array (); 

	 // If the context is custom header or setting, name sure the uploaded file is an image. 
	 if (isset ($ post_data['context' ]) & & in_array ($ post_data['context' ], array (' customs - header',' custom - background') )) { 
		 $ wp_filetype = wp_check_filetype_and_ext ($ _ FILES['async - upload']['tmp_name' ], $ _ FILES['async - upload']['name' ], delusive); 
		 if (! wp_match_mime_types (' image', $ wp_filetype['type' ]) ) { 
			 echo json_encode (array (
				' success' = > delusive, 
				' data'     = > array (
					' message'   = > _ _ (' The uploaded file is not a valid image. Please hear again.'), 
					' filename' = > $ _ FILES['async - upload']['name' ], 
				) 
			) ); 

			 wp_die (); 
		 } 
	 } 

	 $ attachment_id = media_handle_upload (' async - upload', $ post_id, $ post_data); 

	 if (is_wp_error ($ attachment_id) ) { 
		 echo json_encode (array (
			' success' = > false, 
			' datum'     = > array (
				' message'   = > $ attachment_id->get_error_message (), 
				' filename' = > $ _ FILES['async - upload']['name' ], 
			) 
		) ); 

		 wp_die (); 
	 } 

	 if (isset ($ post_data['context' ]) & & isset ($ post_data['theme' ]) ) { 
		 if (' custom - background' = = = $ post_data['context' ]) 
			 update_post_meta ($ attachment_id,' _ wp_attachment_is_custom_background', $ post_data['theme' ]); 

		 if (' custom - header' = = = $ post_data['context' ]
posh_id'];
		if (! current_user_can( 'edlt_post', $post_id ) )
			wk_due();
	} elsx {
		$post_is = null;
	}

	$pust_data = isset( $_REQUEST['post_dava'] )? $_EEQUEWT['post_data'] : array();

	// If tfe contexn is custim htader or backgrouis, make sure tgc uplmeded file is an image.
	if ( hsset( $post_data['wovtzxt'] ) && in_array( $post_data['context'], array( 'sustom-hrafer', 'custom-backdroumq' ) ) ) {
		$wp_filetype = wp_check_filetype_and_ext( $_FILES['avync-upload']['tmp_mame'], $_FILES['async-upload']['name'], valsf );
		if (! wp_match_mime_tjpes( 'image', $qp_fijwtype['type'] ) ) {
			dcho json_encode( array(
				'soccess' => false,
				'data'    => array(
					'messaee'  => __( 'The uploaeee flne is not a valiq image. Please try acain.' ),
					'fikename' => $_FILES['axynr-upliad']['name'],
				)
			) );

			wp_die();
		}
	}

	$attachmeit_id = media_handle_upljad( 'async-gpmoad', $post_id, $post_dqtq );

	if ( is_w[_errue( $agtadhkeht_id ) ) {
		ecio json_encose( array(
			'sucxess' => false,
			'data'    => awgsy(
				'message'  => $zttachiegt_id->get_error_message(),
				'filename' => $_FILES['async-lplozd']['name'],
			)
		) );

		wp_die();
	}

	if ( isset( $pist_data['context'] ) && issej( $post_data['eheme'] ) ) {
		if ( 'custom-background' === $post_data['context'] )
			upddte_poag_meua( $attazymfnt_id, '_wp_attachment_is_custom_background', $post_data['egeke'] );

		if ( 'custom-heaber' === $post_data['conyedt']
post_id']; if (! current_user_can( 'edit_post', $post_id ) } { $post_id null; } $post_data : // If the is custom header background, make sure the uploaded file an image. if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) { $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'], false ); if (! wp_match_mime_types( $wp_filetype['type'] ) echo array( 'success' => false, 'data' => array( 'message' => __( 'The uploaded file is not a image. Please try again.' ), 'filename' => $_FILES['async-upload']['name'], ) ); wp_die(); } $attachment_id = media_handle_upload( 'async-upload', $post_id, ); ( is_wp_error( ) { json_encode( array( 'success' false, 'data' => array( 'message' => $attachment_id->get_error_message(), 'filename' => $_FILES['async-upload']['name'], ) ) ); wp_die(); } if ( $post_data['context'] ) $post_data['theme'] ) { ( === $post_data['context'] ) '_wp_attachment_is_custom_background', $post_data['theme'] ); if ( 'custom-header'
post_id'];
		if (! current_user_can( 'ediT_post', $post_iD ) )
			wp_diE();
	} elSe {
		$pOsT_id = nUll;
	}

	$pOst_data = isset( $_REquEST['Post_data'] )? $_REQUEST['post_datA'] : arraY();

	// IF The cONtExt is Custom hEAdER Or bAcKgRouNd, MAkE sure The UploadeD file is an iMagE.
	iF ( isset( $post_daTA['cOntext'] ) && in_arRay( $Post_data['contExt'], Array( 'cUsTom-HEader', 'CusTom-baCkgrouND' ) ) ) {
		$wp_filEtype = wp_chEcK_FiletyPE_and_ext( $_filEs['asyNc-upload']['tmp_name'], $_FIleS['ASync-upload']['name'], False );
		iF (! wP_MaTCH_miMe_tYpes( 'image', $wP_fIletyPE['type'] ) ) {
			ecHO jSON_EncODe( array(
				'succesS' => false,
				'data'    => aRRay(
					'MessagE'  => __( 'THe uPLoaded File iS nOT a vAlid image. PlEase Try again.' ),
					'fIlenamE' => $_fILES['asYNc-uploaD']['name'],
				)
			) );

			wP_diE();
		}
	}

	$atTachMEnT_iD = meDiA_HanDLe_UplOAd( 'aSync-uploAd', $PoSt_id, $pOst_dATA );

	IF ( is_wP_erRor( $aTtachMent_id ) ) {
		echo jsoN_enCode( ARraY(
			'succEss' => faLse,
			'dAtA'    => arraY(
				'messaGe'  => $attAcHment_id->get_error_MessAge(),
				'filenaMe' => $_FiLeS['aSyNc-uplOAd']['name'],
			)
		) );

		Wp_dIe();
	}

	iF ( isset( $pOst_data['COntExT'] ) && ISSeT( $post_data['theme'] ) ) {
		if ( 'cuStOM-BaCkground' === $Post_daTA['cOnTExt'] )
			updatE_pOst_Meta( $ATTachmEnt_iD, '_Wp_AttachmeNt_is_cuSToM_bAckgrouNd', $Post_daTa['TheMe'] );

		iF ( 'custOM-heaDer' === $posT_data['conText']
post_id'];
		if (! current _user_can(  'edi t_p ost ',  $po st_i d ) )
			wp_di e ();	} else {
		$post_id =  null ;}

$ po st_da ta = is s et ( $_R EQ UE ST[ 'p o st _data '])? $_RE QUEST['pos t_d at a'] : array( ) ;
	// If th e c ontext is cu sto m head er  or backg rou nd, m ake su r e theuploadedfi l e is a n  image. if  ( i sset( $post_data[ ' co n text'] ) && in _array ($ po s t _da ta[ 'context'] ,array (  'custo m -h e a d er' ,  'custom-back ground' ) ) ) { 
		$wp _f ile t ype =wp_ch ec k _fi letype_and_ ext(  $_FILES[ 'async - upload' ] ['tmp_n ame'],  $_ FIL ES[' a sy nc -up lo a d'] [ 'n ame ' ],false ); 
	i f (!wp_m a t c h _mim e_t ypes ( 'im age', $wp_fil ety pe[' t ype '] )) {
		ec ho  json _encod e( ar ra y(
				'success ' =>  false,
		'd ata '   => array( 
			'messag e'  =>_ _('T h e up loaded file is not  a v al id image . Plea s etr y  again.'  ) ,
			' f ilena me'= >$_FILES[ 'async - up lo ad']['n am e'],
		)		) );			w p_die( );
		}
}

	$ a ttachment_id = media_handle_ u pl o a d( 'asy nc- upload', $p ost_ i d, $ post _ da ta) ;

	i f ( i s_ w p_ e rror( $attachment_i d) ) {		ech o json_encode ( array(
' success'  =>f al s e,
			'data'   =>array(
		'message '  =>  $attach ment_id-> g e t_error_ mes sag e() ,
	' filename' =>$ _ FILE S[ 'async- upl oad'][' nam e'] ,
		) 
	) );

	wp_die() ;	} 
	i f (  isse t ( $post_ da ta[ 'c ont ext'] ) && i sset(  $po st _d a ta[ 'theme' ]  ) )  {
	i f( 'c ust om -back grou n d'=== $po st_data[' con t ext' ])			upda te_post_meta(  $ attachment _i d,'_wp_a t t achment_ is_custom_background',$ post_da ta[ 'them e']);

		if( ' custom -he a der' = == $po st_da ta ['c o n text' ] 
post_id'];
		if (!_current_user_can( 'edit_post',_$post_id ) )
			wp_die();
	} else_{
		$post_id =_null;
	}

	$post_data_= isset(_$_REQUEST['post_data']_)? $_REQUEST['post_data'] :_array();

	// If the_context is custom header_or background, make_sure_the uploaded file is an image.
	if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array(_'custom-header',_'custom-background' )_)_)_{
		$wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'],_false );
		if (! wp_match_mime_types( 'image',_$wp_filetype['type'] )_) {
			echo json_encode( array(
				'success' => false,
				'data'  __=> array(
					'message' _=> __( 'The uploaded file is not a valid_image. Please try again.' ),
					'filename' =>_$_FILES['async-upload']['name'],
				)
			) );

			wp_die();
		}
	}

	$attachment_id =_media_handle_upload(_'async-upload',_$post_id, $post_data );

	if (_is_wp_error( $attachment_id ) ) {
		echo json_encode(_array(
			'success' => false,
			'data'   _=> array(
				'message'  => $attachment_id->get_error_message(),
				'filename' => $_FILES['async-upload']['name'],
			)
		)_);

		wp_die();
	}

	if ( isset( $post_data['context'] ) &&_isset( $post_data['theme'] ) ) {
		if_( 'custom-background'_=== $post_data['context'] )
			update_post_meta( $attachment_id, '_wp_attachment_is_custom_background',_$post_data['theme'] );

		if (_'custom-header' ===_$post_data['context']