<?php

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER ['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest') {
	require( dirname(__FILE__) . '/../../../../wp-config.php' );
	if (!(is_user_logged_in() && current_user_can('edit_posts')))
		die("Athentication failed!");

	
	global $wpdb, $canvas;
	
	if(isset($_GET["delete"])) canvas_delete_block($_GET["delete"]);
	if(isset($_GET["duplicate"])) canvas_create_management_block(canvas_duplicate_block($_GET["duplicate"]),' style="display: none"');
	
	if($_GET["update"] == 'all') {
		$blocks = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE theme = '".get_option('template')."' ORDER BY module_title");
		$delete_button = get_option('canvas_allow_deletion');
		foreach($blocks as $block) {
			// Get module name
			include_once('RCCWP_CustomWriteModule.php');
			$customWriteModule = RCCWP_CustomWriteModule::Get($block->module_id);
			$block->module_name = $customWriteModule->name;

			canvas_create_management_block($block,'',$delete_button);
		}
	}
	if(isset($_POST["rename"]) && $_POST["rename"] == 'true') {
		$module_title = urldecode($_POST["value"]);
		$block_id = str_replace('title_','',$_POST["id"]);
		$wpdb->query("UPDATE ".$canvas->main." SET module_title='$module_title' WHERE block_id='$block_id'");
		echo $module_title;
	}
}
?>