Admin Menu Tree Page View - Version 2.1

Version Description

  • Fixed: forgot to remove console.log at some places. sorry!
  • Updated: Drag and drop now works better. Still not 100%, but I can't find the reason why I does order the pages a bit wrong sometimes. Any ideas?
Download this release

Release Info

Developer eskapism
Plugin Icon wp plugin Admin Menu Tree Page View
Version 2.1
Comparing to
See all releases

Code changes from version 2.0 to 2.1

Files changed (3) hide show
  1. index.php +87 -129
  2. js/scripts.js +4 -12
  3. readme.txt +8 -2
index.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Admin Menu Tree Page View
4
  Plugin URI: http://eskapism.se/code-playground/admin-menu-tree-page-view/
5
  Description: Get a tree view of all your pages directly in the admin menu. Search, edit, view and add pages - all with just one click away!
6
- Version: 2.0
7
  Author: Pär Thernström
8
  Author URI: http://eskapism.se/
9
  License: GPL2
@@ -33,7 +33,7 @@ add_action('wp_ajax_admin_menu_tree_page_view_move_page', 'admin_menu_tree_page_
33
 
34
  function admin_menu_tree_page_view_admin_init() {
35
 
36
- define( "admin_menu_tree_page_view_VERSION", "2.0" );
37
  define( "admin_menu_tree_page_view_URL", WP_PLUGIN_URL . '/admin-menu-tree-page-view/' );
38
  define( "admin_menu_tree_page_view_DIR", WP_PLUGIN_DIR . '/admin-menu-tree-page-view/' );
39
 
@@ -389,7 +389,7 @@ function admin_menu_tree_page_view_add_page() {
389
 
390
 
391
  // move a post up or down
392
- // code from our internal theme, "ma-theme"
393
  function admin_menu_tree_page_view_move_page() {
394
 
395
  /*
@@ -400,143 +400,101 @@ function admin_menu_tree_page_view_move_page() {
400
  $post_to_update_id = (int) $_POST["post_to_update_id"];
401
  $direction = $_POST["direction"];
402
  $post_to_update = get_post($post_to_update_id);
 
 
403
 
404
- // get all posts with the same parent as our article
405
- $args = array(
406
- "post_type" => $post_to_update->post_type,
407
- "orderby" => "menu_order",
408
- "order" => "ASC",
409
- "post_parent" => $post_to_update->post_parent,
410
- "post_status" => "any",
411
- "numberposts" => -1
412
- );
413
- $posts = get_posts($args);
414
-
415
- if ($direction == "down") {
416
 
417
- // let's move the article down
418
- // update menu order of all pages
419
- /*
420
-
421
- 1 Page A
422
- 2 Page B
423
- 5 Page C <- flytta
424
- 7 Page D <- menu_order inte alltid vårt menu_order +1, kan "glappa" liksom
425
- 8 Page E
426
- 9 Page F
427
-
428
- Ta reda på aktuellt menu_order
429
- Ta reda på efterföljande posts menu_order
430
- Byt plats på menu_order på vald artikel + efterföljande artikel
431
-
432
- */
433
 
434
- // loop until we find our post
435
- $did_find_post = false;
436
- foreach ($posts as $one_post) {
437
- if ($one_post->ID == $post_to_update->ID) {
438
- $did_find_post = true;
439
- break;
440
- }
441
- }
442
 
443
- if ($did_find_post) {
444
- // cool, we found our post
445
- // but do we have a next post too?
446
- $post_next = current($posts); // not next() as I thought first
447
- if ($post_next) {
448
- // yep, got the next one
449
-
450
- // there can be situations where both posts have the same menu_order
451
- // it that case, increase the menu_order of all posts above and including our next post
452
- // and then do the swap
453
- // clean_post_cache( $id ) ?
454
- if ($post_to_update->menu_order == $post_next->menu_order) {
455
- // echo "<p>Both posts have the same menu_order. Updating menu_order for all posts that are after post_next...</p>";
456
-
457
- // first update menu_order of the next post
458
- $post_next->menu_order++;
459
- wp_update_post(array(
460
- "ID" => $post_next->ID,
461
- "menu_order" => $post_next->menu_order
462
- ));
463
-
464
- // and then loop through the rest of the posts in $posts
465
- $one_post = null;
466
- while ($one_post = next($posts)) {
467
- wp_update_post(array(
468
- "ID" => $one_post->ID,
469
- "menu_order" => $one_post->menu_order + 1
470
- ));
471
- }
472
- }
473
-
474
- // now swap the order of our posts
475
- wp_update_post(array(
476
- "ID" => $post_to_update->ID,
477
- "menu_order" => $post_next->menu_order
478
- ));
479
- wp_update_post(array(
480
- "ID" => $post_next->ID,
481
- "menu_order" => $post_to_update->menu_order
482
- ));
483
-
484
- }
485
  }
486
 
487
- // echo "move down";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
 
489
- } elseif ($direction == "up") {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
 
491
- // echo "move up";
492
-
493
- /*
494
- Move article up
495
-
496
- 0 Page A
497
- 1 Page B
498
- 2 Page C
499
-
500
- 2 Page B
501
- 5 Page B <- kan ha samma menu_order..
502
- 5 Page C <- menu_order inte alltid vårt menu_order -1, kan "glappa"
503
- 7 Page D <- flytta
504
- 8 Page E
505
- 9 Page F
506
-
507
- Ta reda på aktuellt menu_order
508
- Ta reda på föregående posts menu_order
509
- Byt plats på menu_order på vald artikel + föregående artikel
510
-
511
- */
512
 
513
- // find our post and also find the previous post
514
- $prev_post = null;
515
- $found_post = false;
516
- foreach ($posts as $one_post) {
517
- if ($one_post->ID == $post_to_update->ID) {
518
- $found_post = true;
519
- break;
520
- }
521
- $prev_post = $one_post;
 
 
 
 
 
 
 
 
522
  }
523
 
524
- if ($found_post && $prev_post) {
525
- // swap the order of our posts
526
- wp_update_post(array(
527
- "ID" => $post_to_update->ID,
528
- "menu_order" => $prev_post->menu_order
529
- ));
530
- wp_update_post(array(
531
- "ID" => $prev_post->ID,
532
- "menu_order" => $post_to_update->menu_order
533
- ));
534
- } else {
535
- // nah, don't do it!
536
- }
537
- } // if direction
538
-
539
  echo 1;
540
  die();
541
 
542
- } // move post
 
 
3
  Plugin Name: Admin Menu Tree Page View
4
  Plugin URI: http://eskapism.se/code-playground/admin-menu-tree-page-view/
5
  Description: Get a tree view of all your pages directly in the admin menu. Search, edit, view and add pages - all with just one click away!
6
+ Version: 2.1
7
  Author: Pär Thernström
8
  Author URI: http://eskapism.se/
9
  License: GPL2
33
 
34
  function admin_menu_tree_page_view_admin_init() {
35
 
36
+ define( "admin_menu_tree_page_view_VERSION", "2.1" );
37
  define( "admin_menu_tree_page_view_URL", WP_PLUGIN_URL . '/admin-menu-tree-page-view/' );
38
  define( "admin_menu_tree_page_view_DIR", WP_PLUGIN_DIR . '/admin-menu-tree-page-view/' );
39
 
389
 
390
 
391
  // move a post up or down
392
+ // code from my other plugin cms tree page view
393
  function admin_menu_tree_page_view_move_page() {
394
 
395
  /*
400
  $post_to_update_id = (int) $_POST["post_to_update_id"];
401
  $direction = $_POST["direction"];
402
  $post_to_update = get_post($post_to_update_id);
403
+ $aboveOrNextPostID = $_POST["aboveOrNextPostID"];
404
+ $post_aboveOrNext = get_post($aboveOrNextPostID);
405
 
406
+ /*
407
+ the node that was moved,
408
+ the reference node in the move,
409
+ the new position relative to the reference node (one of "before", "after" or "inside"),
410
+ inside = man placerar den under en sida som inte har några barn?
411
+ */
 
 
 
 
 
 
412
 
413
+ global $wpdb;
414
+
415
+ $node_id = (int) $_POST["post_to_update_id"]; // the node that was moved
416
+ $ref_node_id = (int) $_POST["aboveOrNextPostID"];
417
+ $type = $_POST["direction"];
418
+
419
+ $_POST["skip_sitepress_actions"] = true; // sitepress.class.php->save_post_actions
420
+
421
+ if ($node_id && $ref_node_id) {
422
+ #echo "\nnode_id: $node_id";
423
+ #echo "\ntype: $type";
 
 
 
 
 
424
 
425
+ $post_node = get_post($node_id);
426
+ $post_ref_node = get_post($ref_node_id);
 
 
 
 
 
 
427
 
428
+ // first check that post_node (moved post) is not in trash. we do not move them
429
+ if ($post_node->post_status == "trash") {
430
+ exit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
  }
432
 
433
+ if ( "inside" == $type ) {
434
+ // note: inside does not exist for Admin Menu Tree Page View
435
+
436
+ // post_node is moved inside ref_post_node
437
+ // add ref_post_node as parent to post_node and set post_nodes menu_order to 0
438
+ // @todo: shouldn't menu order of existing items be changed?
439
+ $post_to_save = array(
440
+ "ID" => $post_node->ID,
441
+ "menu_order" => 0,
442
+ "post_parent" => $post_ref_node->ID
443
+ );
444
+ wp_update_post( $post_to_save );
445
+
446
+ echo "did inside";
447
+
448
+ } elseif ( "up" == $type ) {
449
 
450
+ // post_node is placed before ref_post_node
451
+ // 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
452
+ // we do this so there will be room for our page if it's the first page
453
+ // so: no move of individial posts yet
454
+ $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+1 WHERE post_parent = %d", $post_ref_node->post_parent ) );
455
+
456
+ // update menu order with +1 for all pages below ref_node, this should fix the problem with "unmovable" pages because of
457
+ // multiple pages with the same menu order (...which is not the fault of this plugin!)
458
+ $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = menu_order+1 WHERE menu_order >= %d", $post_ref_node->menu_order+1) );
459
+
460
+ $post_to_save = array(
461
+ "ID" => $post_node->ID,
462
+ "menu_order" => $post_ref_node->menu_order,
463
+ "post_parent" => $post_ref_node->post_parent
464
+ );
465
+ wp_update_post( $post_to_save );
466
 
467
+ echo "did before";
468
+
469
+ } elseif ( "down" == $type ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
 
471
+ // post_node is placed after ref_post_node
472
+
473
+ // 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
474
+ // +2 since multiple can have same menu order and we want our moved post to have a unique "spot"
475
+ $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 ) );
476
+
477
+ // update menu_order of post_node to the same that ref_post_node_had+1
478
+ #$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 ) );
479
+
480
+ $post_to_save = array(
481
+ "ID" => $post_node->ID,
482
+ "menu_order" => $post_ref_node->menu_order+1,
483
+ "post_parent" => $post_ref_node->post_parent
484
+ );
485
+ wp_update_post( $post_to_save );
486
+
487
+ echo "did after";
488
  }
489
 
490
+ #echo "ok"; // I'm done here!
491
+
492
+ } else {
493
+ // error
494
+ }
 
 
 
 
 
 
 
 
 
 
495
  echo 1;
496
  die();
497
 
498
+ } // move post
499
+
500
+
js/scripts.js CHANGED
@@ -359,12 +359,9 @@ jQuery(function($) {
359
  "revert": true,
360
  "start": function(event, ui) {
361
  var li = $(ui.item);
362
- //console.log("index start", li.index());
363
  li.data("startindex", li.index());
364
  },
365
  "update": function(event, ui) {
366
- console.log(event);
367
- console.log(ui);
368
  /*
369
  ui.item <- the post that was moved
370
  send post id to server, with info about post above or under, depending on if there is a post above/under
@@ -373,13 +370,11 @@ jQuery(function($) {
373
  var a = li.find("a:first");
374
  var post_id = a.data("post-id");
375
 
376
- //console.log("index update", li.index());
377
  // check if we have a post above
378
- /*
379
  var prev = li.prev();
380
  var aboveOrNextItem;
381
  var aboveOrNext;
382
- if (prev.length > 0) {
383
  aboveOrNextItem = prev;
384
  aboveOrNext = "above";
385
  } else {
@@ -388,12 +383,9 @@ jQuery(function($) {
388
  aboveOrNextItem = next;
389
  aboveOrNext = "below";
390
  }
391
-
392
  // get id of above or below post
393
  var aboveOrNextPostID = $(aboveOrNextItem).find("a:first").data("post-id");
394
- */
395
 
396
- // ok, i thought wrong. we "only" need to find the direction instead!
397
  // flytt upp = start > update
398
  // flytt ner = start < update
399
  var startindex = li.data("startindex");
@@ -404,15 +396,15 @@ jQuery(function($) {
404
  } else {
405
  direction = "down";
406
  }
407
- //console.log("direction", direction);
408
 
409
  // now we have all we need, tell the server to do the move
410
  $.post(ajaxurl, {
411
  "action": "admin_menu_tree_page_view_move_page",
412
  "post_to_update_id": post_id,
413
- "direction": direction
 
414
  }, function(data) {
415
- console.log(data);
416
  });
417
 
418
  }
359
  "revert": true,
360
  "start": function(event, ui) {
361
  var li = $(ui.item);
 
362
  li.data("startindex", li.index());
363
  },
364
  "update": function(event, ui) {
 
 
365
  /*
366
  ui.item <- the post that was moved
367
  send post id to server, with info about post above or under, depending on if there is a post above/under
370
  var a = li.find("a:first");
371
  var post_id = a.data("post-id");
372
 
 
373
  // check if we have a post above
 
374
  var prev = li.prev();
375
  var aboveOrNextItem;
376
  var aboveOrNext;
377
+ if (prev.length > 0 && !prev.hasClass("admin-menu-tree-page-filter")) {
378
  aboveOrNextItem = prev;
379
  aboveOrNext = "above";
380
  } else {
383
  aboveOrNextItem = next;
384
  aboveOrNext = "below";
385
  }
 
386
  // get id of above or below post
387
  var aboveOrNextPostID = $(aboveOrNextItem).find("a:first").data("post-id");
 
388
 
 
389
  // flytt upp = start > update
390
  // flytt ner = start < update
391
  var startindex = li.data("startindex");
396
  } else {
397
  direction = "down";
398
  }
 
399
 
400
  // now we have all we need, tell the server to do the move
401
  $.post(ajaxurl, {
402
  "action": "admin_menu_tree_page_view_move_page",
403
  "post_to_update_id": post_id,
404
+ "direction": direction,
405
+ "aboveOrNextPostID": aboveOrNextPostID
406
  }, function(data) {
407
+ // console.log(data);
408
  });
409
 
410
  }
readme.txt CHANGED
@@ -4,9 +4,9 @@ Donate link: http://eskapism.se/sida/donate/
4
  Tags: admin, page, pages, tree, view, admin menu, menu
5
  Requires at least: 3.0
6
  Tested up to: 3.0
7
- Stable tag: 2.0
8
 
9
- Get a tree view of all your pages directly in the admin menu. Search, edit, view and add pages - all with just one click away!
10
 
11
  == Description ==
12
 
@@ -14,6 +14,8 @@ This WordPress plugin adds all your pages to the admin menu, so all your pages w
14
  no matter where you are in the admin area. You can also add pages directly in the tree and you can
15
  quickly find your pages by using the search box.
16
 
 
 
17
  Works perfect in CMS-like WordPress installations with lots of pages in a tree hierarchy.
18
 
19
  If you want a tree with all your pages, but don't want it visible all the time, please check out my other plugin
@@ -40,6 +42,10 @@ Now the tree with the pages will be visible in the admin menu to the left.
40
 
41
  == Changelog ==
42
 
 
 
 
 
43
  = 2.0 =
44
  - Added:Now you can order posts with drag and drop. Just click and hold mouse button and move post up/down. But please note that you can only move posts that have the same level/depth in the tree.
45
 
4
  Tags: admin, page, pages, tree, view, admin menu, menu
5
  Requires at least: 3.0
6
  Tested up to: 3.0
7
+ Stable tag: 2.1
8
 
9
+ Get a tree view of all your pages directly in the admin menu. Search, edit, view, re-order/sort and add pages - all with just one click away!
10
 
11
  == Description ==
12
 
14
  no matter where you are in the admin area. You can also add pages directly in the tree and you can
15
  quickly find your pages by using the search box.
16
 
17
+ **Version 2.0 update:** Now you can order your pages with drag-and-drop!
18
+
19
  Works perfect in CMS-like WordPress installations with lots of pages in a tree hierarchy.
20
 
21
  If you want a tree with all your pages, but don't want it visible all the time, please check out my other plugin
42
 
43
  == Changelog ==
44
 
45
+ = 2.1 =
46
+ - Fixed: forgot to remove console.log at some places. sorry!
47
+ - Updated: Drag and drop now works better. Still not 100%, but I can't find the reason why I does order the pages a bit wrong sometimes. Any ideas?
48
+
49
  = 2.0 =
50
  - Added:Now you can order posts with drag and drop. Just click and hold mouse button and move post up/down. But please note that you can only move posts that have the same level/depth in the tree.
51