<?php

if (!defined( 'ABSPATH' )) exit;

add_action( 'wp_ajax_delete_actionbar','delete_actionbar' );
function delete_actionbar(){
	$id = ($_GET['post_id']);
	if ( wp_delete_post( $id ) ) {
		echo 1;
		return;
	}
	echo 0;
	return;
}

add_action( 'wp_ajax_add_slider_hami_shop','add_slider_hami_shop' );

function add_slider_hami_shop(){

	$title = ($_REQUEST["title"]);

	$pic = esc_url_raw($_REQUEST["pic"]);

	$type = ($_REQUEST["type"]);

	$value = ($_REQUEST["value"]);

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_slider";

	$r = $wpdb->query( $wpdb->prepare("INSERT INTO $table_name 

		( sl_title , sl_type , sl_value , sl_pic ) 

		VALUES ( %s, %d, %s, %s )", $title,$type,$value,$pic) );

	if($r){

		echo 1;

	}else{

		echo 2;

	}

}

//-------------------------------------------------------

add_action( 'wp_ajax_load_list_slider_hami','load_list_slider_hami' );

function load_list_slider_hami(){

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_slider";

	$sliders = $wpdb->get_results("select * from $table_name");

	$exp = get_option('exp_time_WOO2APP');

	$i = -1;

	$f = 0;



	foreach ($sliders as $slider) {

		$i++;

		?>

        <tr id="row_slider_<?= $slider->sl_id; ?>" class="<?= ($f == 1 && $i >= 2) ? 'opacity-0-4' : ''; ?>">

            <td><?= esc_html($slider->sl_title);  ?></td>

            <td>
                <img height="30px;" src="<?= $slider->sl_pic;  ?>">
            </td>

            <td>

                <button onclick="delete_slider_hami(<?= $slider->sl_id; ?>)" type="button"
                        class="btn btn-danger btn-xs"><?php _e('Delete', 'woo2app'); ?></button>

            </td>

        </tr>

		<?php

	}

}

//-----------------------------------------------------------------------

add_action( 'wp_ajax_delete_item_slider_hami','delete_item_slider_hami' );

function delete_item_slider_hami(){

	$id = ($_REQUEST["id"]);

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_slider";

	$r = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE sl_id = %d" , $id));

	if($r){

		echo "1";

	}else{

		echo "2";

	}

}
add_action( 'wp_ajax_delete_item_slider_woo2app_vendor','delete_item_slider_woo2app_vendor' );

function delete_item_slider_woo2app_vendor(){
	echo 222;
}

//---------------------------------------------------------------

add_action( 'wp_ajax_add_item_hami_woo','add_item_hami_woo' );

function add_item_hami_woo(){
	$title = ($_REQUEST["title"]);
	$type = ($_REQUEST["type"]);
	$sort = ($_REQUEST["sort"]);
	$value = ($_REQUEST["value"]);
	$showtype = ($_REQUEST["showtype"]);
	$pic = esc_url_raw($_REQUEST["pic"]);
	global $wpdb;
	if($showtype == 4){
		$value = 4;
	}
    elseif($showtype == 1003){
	    $type = time() + $type * (60*60);
		$results = get_option("$value");
		$results = json_decode($results);
		foreach ($results as $key => $value) {
			if($key == 'title') continue;
			else{
				$value = json_encode($value) ;
			}
		}
	}
	if($type == 4){
		$value = stripslashes($value) ;
    }

	$table_name = $wpdb->prefix . "woo2app_mainpage";
	$res = $wpdb->get_results("select MAX(mp_order) AS max_order from $table_name");

	if(!is_null($res[0]->max_order)){
		foreach ($res as $key) {
			$max = $key->max_order + 1;
		}
	}else{
		$max = 1;
	}
	$r = $wpdb->query( $wpdb->prepare("INSERT INTO $table_name 

		( mp_title , mp_type , mp_value , mp_showtype , mp_pic , mp_order ,mp_sort) 

		VALUES ( %s, %d, %s, %s, %s, %d, %s )", $title,$type,$value,$showtype,$pic,$max, $sort) );
	if($r){
		echo 1;
	}
}

//-------------------------------------------------------------------

add_action( 'wp_ajax_load_item_hami_woo','load_item_hami_woo' );

function load_item_hami_woo(){

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_mainpage";

	$res = $wpdb->get_results("select * from $table_name order by mp_order");

	$exp = get_option('exp_time_WOO2APP');

	$i = -1;

	$f = 0;



	foreach ($res as $key) {

		$i++;

		?>

        <li id="record<?= $key->mp_id; ?>">

            <div class="alert alert-info class_menu_appost_hover <?= ($f == 1 && $i >= 2) ? 'opacity-0-4' : ''; ?>padding-right-2px height-40px padding-top-5px background-color-9C5D90 border-color-9C5D90">

                <div class="col-md-8 pull-right text-right white">

					<?php

					if($key->mp_showtype == 1) echo 'Single Banner :';

                    elseif($key->mp_showtype == 2) echo 'Horizontal list :';

                    elseif($key->mp_showtype == 3) echo 'Vertical list :';

                    elseif($key->mp_showtype == 5) echo 'Special Offer :';

					//elseif($key->mp_showtype == 10) echo ' Slider List :';

                    elseif($key->mp_showtype == 9) echo ' Custom list :';

                    elseif($key->mp_showtype == 10) echo ' Vertical list [posts]  :';

                    elseif($key->mp_showtype == 11) echo ' Horizontal list [posts]  :';

                    elseif($key->mp_showtype == 12) echo ' Slider List  :';

					if($key->mp_title == "") echo '[Untitled]';

					?>

					<?= esc_html($key->mp_title); ?>

                    <input type="hidden" name="id[]" value="<?php echo $key->mp_id; ?>">

                </div>

                <div class="col-md-4 pull-right text-left">

                    <button onclick="delete_item_mainepage_woo(<?php echo $key->mp_id; ?>)" type="button"
                            class="btn btn-danger btn-xs"><?php _e('Delete', 'woo2app'); ?></button>

                </div>

            </div>

        </li>

		<?php

	}

}

//------------------------------------------------------------------

add_action( 'wp_ajax_save_order_woo_item','save_order_woo_item' );

function save_order_woo_item() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'woo2app_mainpage';

    $r = 0; // Initialize $r as an integer

    $data = explode("_", $_POST["id"]);

    $i = 1;

    foreach ($data as $id) {
        $id = (int)$id; // Ensure $id is an integer

        $result = $wpdb->update(
            $table_name,
            array('mp_order' => $i),
            array('mp_id' => $id),
            array('%d'),
            array('%d')
        );

        if ($result === false) {
            error_log('Database update failed for ID: ' . $id);
        } else {
            $r += $result;
        }

        $i++;
    }

    if ($r > 0) {
        echo 1;
    } else {
        echo 0; // Indicate failure if no rows were updated
    }

    wp_die(); // Always call wp_die() at the end of Ajax functions
}


//------------------------------------------------------------------

add_action( 'wp_ajax_delete_item_mainepage_woo','delete_item_mainepage_woo' );

function delete_item_mainepage_woo(){

	$id = ($_REQUEST['id']);

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_mainpage";

	$r = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE mp_id = %d" , $id));

	if($r){

		echo "1";

	}else{

		echo "2";

	}

}

//----------------------------------------------------------------------------------------

add_action( 'wp_ajax_change_show_btn_catlst','change_show_btn_catlst' );

function change_show_btn_catlst(){
	$id = ($_REQUEST['id']);
	update_option("SHOW_BTN_CATLST",$id);
}

add_action( 'wp_ajax_change_show_map','change_show_map' );
function change_show_map() {
	$map = ($_REQUEST['id']);
	update_option( "SHOW_MAP", $map );
}

//-------------------------------------------------------------------------------

add_action( 'wp_ajax_woo2app_setsplash','woo2app_setsplash' );

function woo2app_setsplash(){

	$delay = ($_REQUEST['delay_splash']);

	$url = esc_url_raw($_REQUEST['url_splash']);

	update_option("URL_SPLASH_PIC" , "");

	update_option("NUM_SPLASH_DELAY" , "");

	if( update_option("URL_SPLASH_PIC",$url) && update_option("NUM_SPLASH_DELAY",$delay)	){

		echo 1;

	}else{

		echo 0;

	}

}

//------------------------------------------------------------------------------

add_action( 'wp_ajax_set_position_menu','set_position_menu' );

function set_position_menu(){

	$pos = ($_REQUEST["pos"]);

	$menu = ($_REQUEST["menu"]);

	update_option("NUM_MENU".$menu."_POS",$pos);

}



add_action( 'wp_ajax_set_position_mode_menu','set_position_mode_menu' );

function set_position_mode_menu(){

	$val = ($_REQUEST["val"]);
	$menu = ($_REQUEST["menu"]);
	update_option("App_display_menu_mode_".$menu,$val);

}
add_action( 'wp_ajax_set_account_menu1','set_account_menu1' );

function set_account_menu1(){

	$pos = ($_REQUEST["pos"]);

	$menu = ($_REQUEST["menu"]);

	update_option("NUM_MENU".$menu."_account",$pos);

}

add_action( 'wp_ajax_set_account_menu3','set_account_menu3' );

function set_account_menu3(){
	$pos = ($_REQUEST["pos"]);
	$menu = ($_REQUEST["menu"]);
	update_option("NUM_MENU".$menu."_account",$pos);
}

add_action( 'wp_ajax_set_bg_menu','set_bg_menu' );
function set_bg_menu(){
	$bg = ($_REQUEST["bg"]);
	$menu = ($_REQUEST["menu"]);
	update_option("bg_menu".$menu,$bg);
}


//-----------------------------------------------------------------------------

add_action( 'wp_ajax_set_menu1_item','set_menu1_item' );

function set_menu1_item(){

	$action = ($_REQUEST["action_t"]);

	$title = ($_REQUEST["title"]);

	$pic = esc_url_raw($_REQUEST["pic"]);

	$menu = ($_REQUEST["menu"]);

	$action_type = ($_REQUEST["action_type"]);

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_menu";



	$max = $wpdb->get_results("SELECT MAX(	menu_order) as max FROM $table_name where menu_menu = 1");

	$m = 1 ;

	if(!is_null($max[0]->max)){

		$m = $max[0]->max + 1;

	}

	$r = $wpdb->query( $wpdb->prepare("INSERT INTO $table_name 

		( menu_title , menu_action , menu_value , menu_pic , menu_menu , menu_order ) 

		VALUES ( %s , %d , %s , %s , %d , %d )", $title,$action,$action_type,$pic,$menu,$m) );



	if($r){

		echo 1;

	}else{

		echo 2;

	}

}

add_action( 'wp_ajax_set_menu2_item','set_menu2_item' );

function set_menu2_item(){

	$action = ($_REQUEST["action_t"]);

	$title = ($_REQUEST["title"]);

	$pic = (esc_url_raw($_REQUEST["pic"]));

	$action_type = $_REQUEST["action_type"];

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_menu";



	$max = $wpdb->get_results("SELECT MAX(	menu_order) as max FROM $table_name where menu_menu = 2");

	$m = 1 ;

	if(!is_null($max[0]->max)){

		$m = $max[0]->max + 1;

	}

	$r = $wpdb->query( $wpdb->prepare("INSERT INTO $table_name 

		( menu_title , menu_action , menu_value , menu_pic , menu_menu , menu_order ) 

		VALUES ( %s , %d , %s , %s , %d , %d )", $title,$action,$action_type,$pic,2,$m) );



	if($r){

		echo 1;

	}else{

		echo 2;

	}

}

//--------------------------------------------------------------------------

add_action( 'wp_ajax_list_menu1_item','list_menu1_item' );

function list_menu1_item(){

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_menu";

	$menu = ($_REQUEST["menu"]);

	$res = $wpdb->get_results("select * from $table_name where menu_menu = $menu order by menu_order");

	$exp = get_option('exp_time_WOO2APP');

	$i = -1;

	$f = 0;



	foreach ($res as $key) {

		$i++;

		?>

        <tr style="position: relative" id="1menu<?= $key->menu_id; ?>">

            <td style="width: 60%">
				<?php
				if($key->menu_action != 0){
					if($key->menu_pic != ""){
						?>
                        <img src="<?= $key->menu_pic ?>" title="Click to edit Picture."
                             onclick="change_img_menu('<?= $key->menu_id; ?>')" id="1menu_img_<?= $key->menu_id ?>"
                             class="cursor-pointer margin-2-8 width-height-32px"/>
						<?php
					}
					else{
						?>
                        <img src="<?= WOO2APP_URL . '/files/img/demo-icon.png' ?>" title="Click to edit Picture."
                             onclick="change_img_menu('<?= $key->menu_id; ?>')" id="1menu_img_<?= $key->menu_id ?>"
                             class="cursor-pointer margin-2-8 width-height-32px"/>
						<?php
					}
				}
				?>
                <label class="margin-bottom-25px color-000"
                       id="1menu_label_<?= $key->menu_id; ?>"><?= esc_html($key->menu_title); ?></label>
                <input type="text" id="1menu_txt_<?= $key->menu_id ?>" class="regular-text hide config-ajax-input"
                       value="<?= esc_html($key->menu_title); ?>">
                <input type="hidden"   name="id[]" value="<?php echo $key->menu_id; ?>">
            </td>

            <td>
                <div id="1menu_del_edit_<?= $key->menu_id?>">
                    <button onclick="delete_item_menu1_woo('<?php echo $key->menu_id; ?>')" type="button"
                            class="button button-primary"><?php _e('Delete', 'woo2app'); ?></button>
					<?php
					if($key->menu_action != 0){
						?>
                        <button onclick="show_update_item_menu1_woo('<?php echo $key->menu_id; ?>')" type="button"
                                class="button button-cancel"> <?php _e('Edit', 'woo2app'); ?> </button>
						<?php
					}
					?>
                </div>
                <div id="1menu_save_cancel_<?= $key->menu_id?>" class="hide">
                    <button onclick="update_item_menu1_woo(<?php echo $key->menu_id; ?>)" type="button"
                            class="button button-primary"> <?php _e('Save', 'woo2app'); ?></button>
                    <button onclick="unshow_update_item_menu1_woo(<?php echo $key->menu_id; ?>)" type="button"
                            class="button button-cancel"> <?php _e('Cancel', 'woo2app'); ?> </button>
                </div>

            </td>

        </tr>

		<?php

	}

}

add_action( 'wp_ajax_list_menu2_item','list_menu2_item' );

function list_menu2_item(){

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_menu";

	$res = $wpdb->get_results("select * from $table_name where menu_menu = 2 order by menu_order");

	$exp = get_option('exp_time_WOO2APP');

	$i = -1;

	$f = 0;



	foreach ($res as $key) {

		$i++;

		?>

        <tr style="position: relative" id="2menu<?= $key->menu_id; ?>">

            <td style="width: 60%">
				<?php
				if($key->menu_action != 0){
					if($key->menu_pic != ""){
						?>
                        <img src="<?= $key->menu_pic ?>" title="Click to edit the photo."
                             onclick="change_img_menu('<?= $key->menu_id; ?>')" id="1menu_img_<?= $key->menu_id ?>"
                             class="cursor-pointer margin-2-8 width-height-32px"/>
						<?php
					}
					else{
						?>
                        <img src="<?= WOO2APP_URL . '/files/img/demo-icon.png' ?>" title="Click to edit the photo."
                             onclick="change_img_menu('<?= $key->menu_id; ?>')" id="1menu_img_<?= $key->menu_id ?>"
                             class="cursor-pointer margin-2-8 width-height-32px"/>
						<?php
					}
				}
				?>
                <label class="margin-bottom-25px color-000"
                       id="1menu_label_<?= $key->menu_id; ?>"><?= esc_html($key->menu_title); ?></label>
                <input type="text" id="1menu_txt_<?= $key->menu_id ?>" class="regular-text hide"
                       value="<?= esc_html($key->menu_title); ?>">
                <input type="hidden"   name="id[]" value="<?php echo $key->menu_id; ?>">
            </td>

            <td>
                <div id="1menu_del_edit_<?= $key->menu_id?>">
                    <button onclick="delete_item_menu1_woo('<?php echo $key->menu_id; ?>')" type="button"
                            class="button button-primary"><?php _e('Delete', 'woo2app'); ?></button>
					<?php
					if($key->menu_action != 0){
						?>
                        <button onclick="show_update_item_menu1_woo('<?php echo $key->menu_id; ?>')" type="button"
                                class="button button-cancel"> <?php _e('Edit', 'woo2app'); ?> </button>
						<?php
					}
					?>
                </div>
                <div id="1menu_save_cancel_<?= $key->menu_id?>" class="hide">
                    <button onclick="update_item_menu1_woo(<?php echo $key->menu_id; ?>)" type="button"
                            class="button button-primary"> <?php _e('Save', 'woo2app'); ?> </button>
                    <button onclick="unshow_update_item_menu1_woo(<?php echo $key->menu_id; ?>)" type="button"
                            class="button button-cancel"> <?php _e('Cancel', 'woo2app'); ?> </button>
                </div>

            </td>


        </tr>

		<?php

	}

}

//--------------------------------------------------------------------------------------

add_action( 'wp_ajax_save_order_woo_menu1','save_order_woo_menu1' );

function save_order_woo_menu1() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'woo2app_menu';

    $r = 0;  // مقدار اولیه به عنوان عدد صحیح برای شمارش آپدیت‌های موفق

    $data = explode("_", $_POST["id"]);

    $menu = $_POST["menu"];

    $i = 1;

    foreach ($data as $id) {
        $updated_rows = $wpdb->update(
            $table_name,
            array('menu_order' => $i),
            array('menu_id' => $id),
            array('%d'),
            array('%d')
        );

        if ($updated_rows !== false) {
            $r += $updated_rows;  // اضافه کردن تعداد ردیف‌های به‌روزرسانی‌شده
        }

        $i++;
    }

    if ($r > 0) {
        echo 1;
    } else {
        echo 0;  // یا یک پیام خطای مناسب
    }
}


add_action( 'wp_ajax_save_order_woo_menu2','save_order_woo_menu2' );

function save_order_woo_menu2(){

	global $wpdb;

	$table_name = $wpdb->prefix . 'woo2app_menu';

	$r = "";

	$data = (explode("_",($_POST["id"]))) ;

	$i = 1;

	foreach($data as $id){

		$r += $wpdb->update( $table_name,

			array( 'menu_order' => $i),

			array( 'menu_id' => $id ), array( '%d' ), array( '%d' ) );

		$i++;

	}

	if($r) echo 1;

}

//-------------------------------------------------------------------------------

add_action( 'wp_ajax_delete_item_menu1_woo','delete_item_menu1_woo' );

function delete_item_menu1_woo(){
	$id = ($_REQUEST['id']);
	global $wpdb;
	$table_name = $wpdb->prefix . "woo2app_menu";
	$r = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE menu_id = %d" , $id));
	if($r){
		echo "1";
	}else{
		echo "2";
	}
}

add_action( 'wp_ajax_update_item_menu1_woo','update_item_menu1_woo' );

function update_item_menu1_woo(){
	$id = ($_REQUEST['id']);
	$title = ($_REQUEST['title']);
	$img = esc_url_raw($_REQUEST['img']);
	if($img == WOO2APP_URL.'/files/img/demo-icon.png'){
		$img = "";
	}
	global $wpdb;
	$table_name = $wpdb->prefix . "woo2app_menu";
	echo $r = $wpdb->query($wpdb->prepare("UPDATE $table_name SET menu_title = %s , menu_pic = %s WHERE menu_id = %d" , $title , $img , $id));
}

add_action( 'wp_ajax_delete_item_menu2_woo','delete_item_menu2_woo' );

function delete_item_menu2_woo(){

	$id = ($_REQUEST['id']);

	global $wpdb;

	$table_name = $wpdb->prefix . "woo2app_menu";

	$r = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE menu_id = %d" , $id));

	if($r){

		echo "1";

	}else{

		echo "2";

	}

}

//---------------------------------------------------------------------------------------------------

add_action( 'wp_ajax_load_all_product','load_all_product' );

function load_all_product(){

	$s = ($_REQUEST["search_product"]);

	$args = array(

		'numberposts'       => -1,

		'orderby'          => 'ID',

		'order'            => 'DESC',

		'post_type'        => 'product',

		'post_status'      => 'publish',

		'suppress_filters' => true ,

		's' => $s

	);

	$posts = get_posts($args);

	foreach ($posts as $post) {

		?>

        <option value="<?= $post->ID; ?>"><?= $post->post_title; ?></option>

		<?php

	}

}



add_action( 'wp_ajax_woo2app_posts', 'rudr_get_posts_ajax_callback2' ); // wp_ajax_{action}

function rudr_get_posts_ajax_callback2(){



	// we will pass post IDs and titles to this array



	$return = array();



	// you can use WP_Query, query_posts() or get_posts() here - it doesn't matter

	$post_type = array('post');

	$search_results = new WP_Query( array(

		's'=> $_GET['q'], // the search query

		'post_status' => 'publish', // if you don't want drafts to be returned

		'ignore_sticky_posts' => 1,

		'posts_per_page' => -1 ,// how much to show at once

		'post_type' =>  $post_type,

	));



	if( $search_results->have_posts() ) :

		while( $search_results->have_posts() ) : $search_results->the_post();

			// shorten the title a little

			$title = ( mb_strlen( $search_results->post->post_title ) > 50 ) ? mb_substr( $search_results->post->post_title, 0, 49 ) . '...' : $search_results->post->post_title;

			$return[] = array( $search_results->post->ID, $title ); // array( Post ID, Post Title )

		endwhile;

	endif;

	echo  json_encode($return) ;

	die;

}



add_action( 'wp_ajax_woo2app_product', 'rudr_get_posts_ajax_callback3' ); // wp_ajax_{action}

function rudr_get_posts_ajax_callback3(){
	// we will pass post IDs and titles to this array
	$return = array();
	// you can use WP_Query, query_posts() or get_posts() here - it doesn't matter
	$post_type = array('product');

	$search_results = new WP_Query( array(

		's'=> $_GET['q'], // the search query

		'post_status' => 'publish', // if you don't want drafts to be returned

		'ignore_sticky_posts' => 1,

		'posts_per_page' => -1 ,// how much to show at once

		'post_type' =>  $post_type,

	));

	if( $search_results->have_posts() ) :

		while( $search_results->have_posts() ) : $search_results->the_post();

			// shorten the title a little

			$title = ( mb_strlen( $search_results->post->post_title ) > 50 ) ? mb_substr( $search_results->post->post_title, 0, 49 ) . '...' : $search_results->post->post_title;

			$return[] = array( $search_results->post->ID, $title ); // array( Post ID, Post Title )

		endwhile;

	endif;

	echo  json_encode($return) ;

	die;
}

add_action( 'wp_ajax_woo2app_tag', 'rudr_get_posts_ajax_callback4' ); // wp_ajax_{action}

function rudr_get_posts_ajax_callback4(){
	// we will pass post IDs and titles to this array
	$return = array();
	$args = array(
		'hide_empty' => 0 ,
		'taxonomy' => 'product_tag',
		'search' => ($_GET['q'])
	);
	$product_tags = get_categories($args);
	foreach ($product_tags as $tag) {
		# code...
		$return[] = array( $tag->slug, $tag->name ); // array( Post ID, Post Title )
	}
	echo  json_encode($return) ;

	die;
}

add_action( 'wp_ajax_woo2app_cat', 'rudr_get_posts_ajax_callback5' ); // wp_ajax_{action}

function rudr_get_posts_ajax_callback5(){
	// we will pass post IDs and titles to this array
	$return = array();
	$args = array(
		'hide_empty' => 0 ,
		'taxonomy' => 'product_cat',
		'search' => ($_GET['q'])
	);
	$product_tags = get_categories($args);
	$return[] = array( 'all', 'all categories' );
	foreach ($product_tags as $tag) {
		# code...
		$return[] = array( $tag->slug, $tag->name ); // array( Post ID, Post Title )
	}
	echo  json_encode($return) ;

	die;
}

add_action( 'wp_ajax_mr2app_update_post_meta', 'mr2app_update_post_meta' ); // wp_ajax_{action}
function mr2app_update_post_meta(){
	$post_id = ($_REQUEST['post_id']);
	$meta_key = ($_REQUEST['meta_key']);
	$active = ($_REQUEST['active']);
	echo update_post_meta($post_id,$meta_key,$active);
}


add_action( 'wp_ajax_woo2app_send_feedback', 'woo2app_send_feedback' ); // wp_ajax_{action}
function woo2app_send_feedback(){
	$array = array(
		'site' => esc_url_raw($_SERVER['HTTP_HOST']),
		'plugin' => 'woo2app',
		'select' => ($_REQUEST['reason']),
		'memo' => ($_REQUEST['detail'])
	);
	//url-ify the data for the POST
	$fields_string = http_build_query($array);
	$url = 'https://mr2app.com/feedback';
	echo file_get_contents($url.'?'.$fields_string);

//close connection
	exit();
}


add_action( 'wp_ajax_set_default_setting', 'set_default_setting' ); // wp_ajax_{action}
function set_default_setting(){
	$param = ($_REQUEST['param']);
	if($param == 'add_default_menu'){
		add_default_menu();
	}
    elseif ($param == 'add_default_slider'){
		add_default_slider();
	}
    elseif($param == 'add_default_mainpage'){
		add_default_mainpage();
	}
	echo 1;
//close connection
	exit();
}


add_action( 'wp_ajax_change_color', 'change_color' ); // wp_ajax_{action}
function change_color(){
	$sec = ($_REQUEST['sec']);
	$index = ($_REQUEST['index']);
	$value = ($_REQUEST['value']);
	$color = get_option('woo2app_color_setting');
	if(!is_array($color)){
		$color = array();
	}
	$color[$sec][$index]['value'] = $value;
	update_option('woo2app_color_setting' , $color);
	exit();
}