function queryByAction($obj) { global $wpdb; $table_name = $wpdb->prefix . "vlib_templates"; $action = htmlspecialchars(trim($obj['action'])); switch ($action) { /* ************************** */ /* PLOTS */ case 'save': $name = $obj['name']; $descr = $obj['description']; $graph = $obj['sceneGraph']; $placeholders = $obj['placeholders']; $thumbnail = $obj['thumbnail']; $tId = createTemplateQuery($name, $descr, $graph, $thumbnail); foreach ($placeholders as $pattern) { $pId = createPlaceholderQuery($pattern); createTemplateHasPlaceholderQuery($tId, $pId); } echo $tId; break; case 'update_t': $tId = $obj['id']; $name = $obj['name']; $descr = $obj['description']; $graph = $obj['sceneGraph']; $placeholders = $obj['placeholders']; $thumbnail = $obj['thumbnail']; /* delete patterns */ $patterns = getPlaceholdersByPlotIdQuery($tId); if ($patterns) { foreach ($patterns as $pattern) { deletePlaceholderByIdQuery($pattern->id); } /* delete plot-pattern mappings */ deleteTemplateHasPlaceholderQuery($tId); } foreach ($placeholders as $pattern) { $pId = createPlaceholderQuery($pattern); createTemplateHasPlaceholderQuery($tId, $pId); } updateTemplateQuery($tId, $name, $descr, $graph, $thumbnail); print "Template <b>{$name}</b> has been updated."; break; case 'delete_t': $name = $obj['name']; $tId = $obj["id"]; /* delete patterns */ $patterns = getPlaceholdersByPlotIdQuery($tId); if ($patterns) { foreach ($patterns as $pattern) { deletePlaceholderByIdQuery($pattern->id); } /* delete plot-pattern mappings */ deleteTemplateHasPlaceholderQuery($tId); } /* delete plot*/ $results = deleteTemplateByIdQuery($tId); if ($results === 1) { print $results; } else { print "There has been an error deleting the template!"; } break; case 'get_t': $results = getTemplatesQuery(); if (count($results) > 0) { print json_encode($results); } break; case 'get_t_id': $id = $obj['id']; $results = getTemplateByIdQuery($id); if (count($results) > 0) { print json_encode($results); } break; /* ************************** */ /* GROUPS */ /* ************************** */ /* GROUPS */ case 'save-group': $name = $obj['group']['config']['name']; $descr = $obj['group']['config']['description']; $group = $obj['group']; print createGroupQuery($name, $descr, $group); break; case 'load-group': $id = $obj['id']; $result = getGroupByIdQuery($id); $response = array(id => $result[0]->id, name => $result[0]->name, description => $result[0]->description, group => json_decode($result[0]->group_json)); print json_encode($response); break; case 'load-all-groups': $result = getAllGroups(); print json_encode($result); break; case 'load-all-group-definitions': $result = getAllGroupDefinitions(); print json_encode($result); break; case 'delete-group': $id = $obj["id"]; return deleteGroupQuery($id); break; case 'update-group': $id = $obj["id"]; $name = $obj['group']['config']['name']; $description = $obj['group']['config']['description']; $group = $obj['group']; return updateGroupQuery($id, $name, $description, $group); break; /* ************************** */ /* FILES */ /* ************************** */ /* FILES */ case 'get_file_by_id': $id = $obj['id']; $results = getFileByIdQuery($id); echo json_encode($results); break; case 'get_files': $results = getFilesQuery(); echo json_encode($results); break; case 'update-file-visibility': $id = $obj["id"]; $isVisible = $obj['isVisible']; echo updateFileVisibilityQuery($id, $isVisible); break; case 'get_visible_files': $results = getVisibleFilesQuery(); if (count($results) > 0) { print json_encode($results); } break; case 'delete_files': $files_ids = implode(",", $obj['files']); $dataFolder = "data/"; /* unlink */ foreach ($obj['files'] as $fileId) { $file = getFileByIdQuery($fileId); echo "try to unlink " . $dataFolder . $file[0]->file_path; if ($file[0]->file_path !== "") { echo "try to unlink " . $dataFolder . $file[0]->file_path; if (!unlink($dataFolder . $file[0]->file_path)) { echo "unlink( " . $file[0]->file_path . " ) failed."; } else { echo "unlink " . $dataFolder . $file[0]->file_path; } } } $results = deleteFilesQuery($files_ids); if (count($results) > 0) { print $results; } else { print "There has been an error deleting the file! "; } break; default: } }
$obj = json_decode($json, TRUE); queryByAction($obj); } if (isset($_POST["cmd"])) { $json = stripslashes($_POST["cmd"]); $obj = json_decode($json, TRUE); $action = $obj["action"]; $id = $obj["id"]; if ($action === "post_plot") { $plot = getTemplateByIdQuery($id); $title = $plot[0]->template_name; $description = $plot[0]->template_description; $sc = "vPlot"; } else { if ($action === "post_group") { $group = getGroupByIdQuery($id); $title = rawurldecode($group[0]->name); $description = rawurldecode($group[0]->description); $sc = "vGroup"; } } echo createPostWithShortcode($sc, $title, $description, $id); } function createPostWithShortcode($shortcode, $title, $description, $id) { require_once ABSPATH . 'wp-includes/post.php'; // Create post object $post = array('post_title' => $title, 'post_content' => '[' . $shortcode . ' id=' . intval($id) . '] <br />' . $description, 'post_status' => 'pending', 'post_author' => 1, 'post_category' => array(8, 39)); // Insert the post into the database and return last_insert_id return wp_insert_post($post); }