"post",
"parent" => "",
"view" => "all" // all | public | trash
);
$r = wp_parse_args( $args, $defaults );
$get_posts_args = array(
"numberposts" => "-1",
"orderby" => "menu_order",
"order" => "ASC",
"caller_get_posts" => 1, // get sticky posts in natural order (or so I understand it anyway)
// "post_type" => "any",
"post_type" => $r["post_type"],
"xsuppress_filters" => "0"
);
if ($r["parent"]) {
$get_posts_args["post_parent"] = $r["parent"];
} else {
$get_posts_args["post_parent"] = "0";
}
if ($r["view"] == "all") {
$get_posts_args["post_status"] = "any"; // "any" seems to get all but auto-drafts
} elseif ($r["view"] == "trash") {
$get_posts_args["post_status"] = "trash";
// if getting trash, just get all pages, don't care about parent?
// because otherwise we have to mix trashed pages and pages with other statuses. messy.
$get_posts_args["post_parent"] = null;
} else {
$get_posts_args["post_status"] = "publish";
}
// does not work with plugin role scoper. don't know why, but this should fix it
remove_action("get_pages", array('ScoperHardway', 'flt_get_pages'), 1, 2);
#do_action_ref_array('parse_query', array(&$this));
#print_r($get_posts_args);
$pages = get_posts($get_posts_args);
// filter out pages for wpml, by applying same filter as get_pages does
// only run if wpml is available or always?
$pages = apply_filters('get_pages', $pages, $get_posts_args);
return $pages;
}
function cms_tpv_parse_query($q) {
}
function cms_tpv_firedebug($var) {
global $firephp;
$firephp->log($var);
}
/**
* Output JSON for the children of a node
* $arrOpenChilds = array with id of pages to open children on
*/
function cms_tpv_print_childs($pageID, $view = "all", $arrOpenChilds = null, $post_type) {
$arrPages = cms_tpv_get_pages("parent=$pageID&view=$view&post_type=$post_type");
if ($arrPages) {
global $current_screen;
$screen = convert_to_screen("edit");
$screen->post_type = null;
ob_start(); // some plugins, for example magic fields, return javascript and things here. we're not campatible with that, so just swallow any output
$posts_columns = get_column_headers($screen);
ob_get_clean();
unset($posts_columns["cb"], $posts_columns["title"], $posts_columns["author"], $posts_columns["categories"], $posts_columns["tags"], $posts_columns["date"]);
global $post;
#cms_tpv_firedebug(timer_stop());
?>[ID;
$editLink = get_edit_post_link($onePage->ID, 'notDisplay');
$content = esc_html($onePage->post_content);
$content = str_replace(array("\n","\r"), "", $content);
$hasChildren = false;
// if viewing trash, don't get children. we watch them "flat" instead
if ($view == "trash") {
} else {
$arrChildPages = cms_tpv_get_pages("parent={$onePage->ID}&view=$view&post_type=$post_type");
}
if ($arrChildPages) {
$hasChildren = true;
}
// if no children, output no state
$strState = '"state": "closed",';
if (!$hasChildren) {
$strState = '';
}
// type of node
$rel = $onePage->post_status;
if ($onePage->post_password) {
$rel = "password";
}
// modified time
$post_modified_time = strtotime($onePage->post_modified);
$post_modified_time = date_i18n(get_option('date_format'), $post_modified_time, false);
// last edited by
setup_postdata($post);
$post_author = get_the_modified_author();
if (empty($post_author)) {
$post_author = __("Unknown user", 'cms-tree-page-view');
}
$title = get_the_title($onePage->ID); // so hooks and stuff will do their work
if (empty($title)) {
$title = __("", 'cms-tree-page-view');
}
$title = esc_html($title);
#$title = html_entity_decode($title, ENT_COMPAT, "UTF-8");
#$title = html_entity_decode($title, ENT_COMPAT);
// can edit?
if ( current_user_can( 'edit_page', $page_id ) ) {
$user_can_edit_page = true;
$user_can_edit_page_css = "cms_tpv_user_can_edit_page_yes";
} else {
$user_can_edit_page = false;
$user_can_edit_page_css = "cms_tpv_user_can_edit_page_no";
}
// fetch columns
$str_columns = "";
foreach ( $posts_columns as $column_name => $column_display_name ) {
$col_name = $column_display_name;
if ($column_name == "comments") {
$col_name = __("Comments");
}
$str_columns .= "
$col_name
";
$str_columns .= "
";
if ($column_name == "comments") {
$str_columns .= '
";
}
?>
{
"data": {
"title": "",
"attr": {
"href": "",
"xid": "cms-tpv-ID ?>"
},
"xicon": ""
},
"attr": {
"xhref": "",
"id": "cms-tpv-ID ?>",
"xtitle": "",
"class": ""
},
"metadata": {
"id": "cms-tpv-ID ?>",
"post_id": "ID ?>",
"post_type": "post_type ?>",
"post_status": "post_status ?>",
"rel": "",
"childCount": ,
"permalink": "ID)) ?>",
"editlink": "",
"modified_time": "",
"modified_author": "",
"columns": "",
"user_can_edit_page": ""
}
ID, $arrOpenChilds)) {
?>, "children": ID, $view, $arrOpenChilds, $post_type);
?>
}
,]prepare("SELECT id, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_title LIKE %s", $sqlsearch);
$hits = $wpdb->get_results($sql);
$arrNodesToOpen = array();
foreach ($hits as $oneHit) {
$arrNodesToOpen[] = $oneHit->post_parent;
}
$arrNodesToOpen = array_unique($arrNodesToOpen);
$arrNodesToOpen2 = array();
// find all parents to the arrnodestopen
foreach ($arrNodesToOpen as $oneNode) {
if ($oneNode > 0) {
// not at top so check it out
$parentNodeID = $oneNode;
while ($parentNodeID != 0) {
$hits = $wpdb->get_results($sql);
$sql = "SELECT id, post_parent FROM $wpdb->posts WHERE id = $parentNodeID";
$row = $wpdb->get_row($sql);
$parentNodeID = $row->post_parent;
$arrNodesToOpen2[] = $parentNodeID;
}
}
}
$arrNodesToOpen = array_merge($arrNodesToOpen, $arrNodesToOpen2);
$sReturn = "";
#foreach ($arrNodesToOpen as $oneNodeID) {
# $sReturn .= "cms-tpv-{$oneNodeID},";
#}
#$sReturn = preg_replace("/,$/", "", $sReturn);
foreach ($arrNodesToOpen as $oneNodeID) {
$sReturn .= "\"#cms-tpv-{$oneNodeID}\",";
}
$sReturn = preg_replace('/,$/', "", $sReturn);
if ($sReturn) {
$sReturn = "[" . $sReturn . "]";
}
if ($sReturn) {
echo $sReturn;
} else {
// if no hits
echo "[]";
}
exit;
} else {
// regular get
$id = (isset($_GET["id"])) ? $_GET["id"] : null;
$id = (int) str_replace("cms-tpv-", "", $id);
$jstree_open = array();
if ( isset( $_COOKIE["jstree_open"] ) ) {
$jstree_open = $_COOKIE["jstree_open"]; // like this: [jstree_open] => cms-tpv-1282,cms-tpv-1284,cms-tpv-3
#var_dump($jstree_open); string(22) "#cms-tpv-14,#cms-tpv-2"
$jstree_open = explode( ",", $jstree_open );
for( $i=0; $i cms_tpv_add_page
[pageID] => cms-tpv-1318
type
)
*/
$type = $_POST["type"];
$pageID = $_POST["pageID"];
$pageID = str_replace("cms-tpv-", "", $pageID);
$page_title = trim($_POST["page_title"]);
$post_type = $_POST["post_type"];
$wpml_lang = $_POST["wpml_lang"];
if (!$page_title) { $page_title = __("New page", 'cms-tree-page-view'); }
$ref_post = get_post($pageID);
if ("after" == $type) {
/*
add page under/below ref_post
*/
// update menu_order of all pages below our page
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+2 WHERE post_parent = %d AND menu_order >= %d AND id <> %d ", $ref_post->post_parent, $ref_post->menu_order, $ref_post->ID ) );
// create a new page and then goto it
$post_new = array();
$post_new["menu_order"] = $ref_post->menu_order+1;
$post_new["post_parent"] = $ref_post->post_parent;
$post_new["post_type"] = "page";
$post_new["post_status"] = "draft";
$post_new["post_title"] = $page_title;
$post_new["post_content"] = "";
$post_new["post_type"] = $post_type;
$newPostID = wp_insert_post($post_new);
} else if ( "inside" == $type ) {
/*
add page inside ref_post
*/
// update menu_order, so our new post is the only one with order 0
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+1 WHERE post_parent = %d", $ref_post->ID) );
$post_new = array();
$post_new["menu_order"] = 0;
$post_new["post_parent"] = $ref_post->ID;
$post_new["post_type"] = "page";
$post_new["post_status"] = "draft";
$post_new["post_title"] = $page_title;
$post_new["post_content"] = "";
$post_new["post_type"] = $post_type;
$newPostID = wp_insert_post($post_new);
}
if ($newPostID) {
// return editlink for the newly created page
$editLink = get_edit_post_link($newPostID, '');
if ($wpml_lang) {
$editLink = add_query_arg("lang", $wpml_lang, $editLink);
}
echo $editLink;
} else {
// fail, tell js
echo "0";
}
exit;
}
// AJAX: perform move of article
function cms_tpv_move_page() {
/*
the node that was moved,
the reference node in the move,
the new position relative to the reference node (one of "before", "after" or "inside"),
inside = man placerar den under en sida som inte har nĂ¥gra barn?
*/
global $wpdb;
$node_id = $_POST["node_id"]; // the node that was moved
$ref_node_id = $_POST["ref_node_id"];
$type = $_POST["type"];
$node_id = str_replace("cms-tpv-", "", $node_id);
$ref_node_id = str_replace("cms-tpv-", "", $ref_node_id);
if ($node_id && $ref_node_id) {
#echo "\nnode_id: $node_id";
#echo "\ntype: $type";
$post_node = get_post($node_id);
$post_ref_node = get_post($ref_node_id);
// first check that post_node (moved post) is not in trash. we do not move them
if ($post_node->post_status == "trash") {
exit;
}
if ( "inside" == $type ) {
// post_node is moved inside ref_post_node
// add ref_post_node as parent to post_node and set post_nodes menu_order to 0
// @todo: shouldn't menu order of existing items be changed?
$post_to_save = array(
"ID" => $post_node->ID,
"menu_order" => 0,
"post_parent" => $post_ref_node->ID
);
wp_update_post( $post_to_save );
echo "did inside";
} elseif ( "before" == $type ) {
// post_node is placed before ref_post_node
// update menu_order of all pages with a menu order more than or equal ref_node_post and with the same parent as ref_node_post
// we do this so there will be room for our page if it's the first page
// so: no move of individial posts yet
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+1 WHERE post_parent = %d", $post_ref_node->post_parent ) );
// update menu order with +1 for all pages below ref_node, this should fix the problem with "unmovable" pages because of
// multiple pages with the same menu order (...which is not the fault of this plugin!)
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+1 WHERE menu_order >= %d", $post_ref_node->menu_order+1) );
$post_to_save = array(
"ID" => $post_node->ID,
"menu_order" => $post_ref_node->menu_order,
"post_parent" => $post_ref_node->post_parent
);
wp_update_post( $post_to_save );
echo "did before";
} elseif ( "after" == $type ) {
// post_node is placed after ref_post_node
// update menu_order of all posts with the same parent ref_post_node and with a menu_order of the same as ref_post_node, but do not include ref_post_node
// +2 since multiple can have same menu order and we want our moved post to have a unique "spot"
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+2 WHERE post_parent = %d AND menu_order >= %d AND id <> %d ", $post_ref_node->post_parent, $post_ref_node->menu_order, $post_ref_node->ID ) );
// update menu_order of post_node to the same that ref_post_node_had+1
#$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = %d, post_parent = %d WHERE ID = %d", $post_ref_node->menu_order+1, $post_ref_node->post_parent, $post_node->ID ) );
$post_to_save = array(
"ID" => $post_node->ID,
"menu_order" => $post_ref_node->menu_order+1,
"post_parent" => $post_ref_node->post_parent
);
wp_update_post( $post_to_save );
echo "did after";
}
#echo "ok"; // I'm done here!
} else {
// error
}
// ok, we have updated the order of the pages
// but we must tell wordpress that we have done something
// other plugins (cache plugins) will not know to clear the cache otherwise
// edit_post seems like the most appropriate action to fire
// fire for the page that was moved? can not fire for all.. would be crazy, right?
#wp_update_post(array("ID" => $node_id));
#wp_update_post(array("ID" => $post_ref_node));
#clean_page_cache($node_id); clean_page_cache($post_ref_node); // hmpf.. db cache reloaded don't care
exit;
}
/**
* Show a box with some dontate-links and stuff
*/
function cms_tpv_show_annoying_box() {
#update_option('cms_tpv_show_annoying_little_box', 1); // enable this to show box
if ( isset($_GET["action"]) && "cms_tpv_remove_annoying_box" == $_GET["action"] ) {
$show_box = 0;
update_option('cms_tpv_show_annoying_little_box', $show_box);
} else {
$show_box = get_option('cms_tpv_show_annoying_little_box', 1);
}
if ($show_box) {
?>
Thank you for using this plugin! If you need help please check out the plugin homepage or the support forum.', 'cms-tree-page-view') ?>
support my work by donating - or at least say something nice about this plugin in a blog post or tweet.', 'cms-tree-page-view') ?>
";
print_r($var);
echo "";
}
}
function cms_tpv_install() {
// after upgrading/re-enabling the plugin, also re-enable the little please-donate-box
update_option('cms_tpv_show_annoying_little_box', 1);
// check and update version
$version = get_option('cms_tpv_version', 0);
if ($version <= 0) {
// first install or pre custom posts version:
// make sure pages are enabled by default
$options = array();
$options["dashboard"] = array("page");
$options["menu"] = array("page");
update_option('cms_tpv_options', $options);
}
// set to current version
update_option('cms_tpv_version', CMS_TPV_VERSION);
}
// when plugins are loaded, check if current plugin version is same as stored
// if not = it's an upgrade. right?
function cms_tpv_plugins_loaded($a) {
$installed_version = get_option('cms_tpv_version', 0);
#echo "installed_version: $installed_version";
#echo " " . CMS_TPV_VERSION;
if ($installed_version != CMS_TPV_VERSION) {
// new version!
// upgrade stored version to current version + show that annoying litte box again
update_option('cms_tpv_version', CMS_TPV_VERSION);
update_option('cms_tpv_show_annoying_little_box', 1);
}
}