SHARE
TWEET

Untitled

a guest Nov 15th, 2017 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Auto Subpage Menu
  4. Plugin URI: https://wordpress.org/plugins/auto-subpage-menu/
  5. Description: Automatically add child page (subpage) into menus, also update hierarchy / remove / restore subpage on menu for you.
  6. Version: 1.1.5
  7. Author: Nathachai Thongniran
  8. Author URI: http://jojoee.com/
  9. Text Domain: asm
  10. License: GPLv2
  11. */
  12.  
  13. class Auto_Subpage_Menu {
  14.  
  15.   function __construct() {
  16.     add_action( 'post_updated', array( &$this, 'when_update_page' ), 10, 4 );
  17.     add_action( 'wp_trash_post', '_wp_delete_post_menu_item' );
  18.   }
  19.  
  20.   function dd( $var, $die = true ) {
  21.     echo '<pre>';
  22.     print_r( $var );
  23.     echo '</pre>';
  24.  
  25.     if ( $die ) { die(); }
  26.   }
  27.  
  28.   function da( $var ) {
  29.     $this->dd( $var, false );
  30.   }
  31.  
  32.   function is_restore( $page_after_status, $page_before_status ) {
  33.     if ( ( $page_after_status === 'publish' ) &&
  34.       ( $page_before_status === 'trash' ) ) {
  35.       return true;
  36.     }
  37.  
  38.     return false;
  39.   }
  40.  
  41.   function is_parent_change( $page_parent_after, $page_parent_before ) {
  42.     if ( $page_parent_after !== $page_parent_before ) {
  43.       return true;
  44.     }
  45.  
  46.     return false;
  47.   }
  48.    
  49.     function is_reorder( $page_order_after, $page_order_before ) {
  50.         if ( $page_order_after !== $page_order_before ) {
  51.           return true;
  52.         }
  53.  
  54.         return false;
  55.     }
  56.  
  57.   /**
  58.    * Check the theme, support menu or not
  59.    *
  60.    * @see https://codex.wordpress.org/Function_Reference/current_theme_supports
  61.    *
  62.    * @return boolean
  63.    */
  64.   function is_support_menus() {
  65.     if ( ! current_theme_supports( 'menus' ) ) {
  66.       return false;
  67.     }
  68.  
  69.     return true;
  70.   }
  71.  
  72.   /**
  73.    * Check the page, has page parent or not
  74.    *
  75.    * @param  integer $page_id
  76.    * @return boolean
  77.    */
  78.   function has_parent( $page_id ) {
  79.     $post = get_post( $page_id );
  80.     if ( ! $post->post_parent ) {
  81.       return false;
  82.     }
  83.  
  84.     return true;
  85.   }
  86.  
  87.   /**
  88.    * Check menus, set `Automatically add new top-level pages to this menu` (auto_add) or not
  89.    *
  90.    * @see https://codex.wordpress.org/Appearance_Menus_Screen
  91.    *
  92.    * @return boolean
  93.    */
  94.   function has_auto_add() {
  95.     $auto_add = get_option( 'nav_menu_options' );
  96.     if ( ! is_array( $auto_add ) || empty( $auto_add ) ||
  97.       ! isset( $auto_add['auto_add'] ) || ! is_array( $auto_add['auto_add'] ) || empty( $auto_add['auto_add'] ) ) {
  98.       return false;
  99.     }
  100.  
  101.     return true;
  102.   }
  103.  
  104.   function get_auto_add() {
  105.     $auto_add = get_option( 'nav_menu_options' );
  106.  
  107.     return $auto_add['auto_add'];
  108.   }
  109.  
  110.   function remove_page_from_menu( $object_id ) {
  111.     global $wpdb,
  112.       $table_prefix;
  113.  
  114.     $query = sprintf( 'DELETE FROM %s_term_relationships WHERE object_id = %d',
  115.       $table_prefix,
  116.       $object_id
  117.     );
  118.  
  119.     $wpdb->get_results( $query, OBJECT );
  120.   }
  121.  
  122.   function remove_page_from_menus( $menu_ids, $page ) {
  123.     foreach ( $menu_ids as $menu_id ) {
  124.       $menu_items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish' ) );
  125.  
  126.       if ( ! is_array( $menu_items ) ) {
  127.         continue;
  128.  
  129.       } else {
  130.         foreach ( $menu_items as $menu_item ) {
  131.           // if page's existed in menu, then remove it from menu
  132.           if ( $menu_item->object_id == $page->ID ) { $this->remove_page_from_menu( $menu_item->ID ); }
  133.         }
  134.       }
  135.     }
  136.   }
  137.    
  138.     function update_items_order( $menu_ids, $page ) {
  139.         foreach ( $menu_ids as $menu_id ) {
  140.           $menu_items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish' ) );
  141.  
  142.           if ( ! is_array( $menu_items ) ) {
  143.             continue;
  144.  
  145.           } else {
  146.             foreach ( $menu_items as $menu_item ) {
  147.               // if page's existed in menu, then remove it from menu
  148.               if ( $menu_item->object_id == $page->ID ) {
  149.                  
  150.                   wp_update_nav_menu_item( $menu_id, $menu_item->db_id, array(
  151.                       'menu-item-db-id'       => $menu_item->db_id,
  152.                       'menu-item-object-id'   => $menu_item->object_id,
  153.                       'menu-item-object'      => $menu_item->object,
  154.                       'menu-item-parent-id'   => $menu_item->menu_item_parent,
  155.                       'menu-item-position'    => $page->menu_order,
  156.                       'menu-item-type'        => $menu_item->type,
  157.                       'menu-item-title'       => $menu_item->title,
  158.                       'menu-item-url'         => $menu_item->url,
  159.                       'menu-item-description' => $menu_item->description,
  160.                       'menu-item-attr-title'  => $menu_item->attr_title,
  161.                       'menu-item-target'      => $menu_item->target,
  162.                       'menu-item-classes'     => implode($menu_item->classes, ' '),
  163.                       'menu-item-xfn'         => $menu_item->xfn,
  164.                       'menu-item-status'      => 'publish'
  165.                     ) );
  166.  
  167.               }
  168.             }
  169.           }
  170.         }
  171.       }
  172.  
  173.   function add_page_into_menu( $menu_id, $page_id, $post_type, $page_parent_id = 0, $menu_order ) {
  174.     wp_update_nav_menu_item( $menu_id, 0, array(
  175.       'menu-item-object-id'   => $page_id,
  176.       'menu-item-object'      => $post_type,
  177.       'menu-item-parent-id'   => $page_parent_id,
  178.       'menu-item-position'    => $menu_order,
  179.       'menu-item-type'        => 'post_type',
  180.       'menu-item-status'      => 'publish'
  181.     ) );
  182.   }
  183.  
  184.   function add_page_into_submenu( $menu_id, $page, $page_parent ) {
  185.     $this->add_page_into_menu( $menu_id, $page->ID, $page->post_type, $page_parent->ID, $page->menu_order );
  186.   }
  187.  
  188.   function add_page_into_topmenu( $menu_id, $page ) {
  189.     $this->add_page_into_menu( $menu_id, $page->ID, $page->post_type, 0, $page->menu_order );
  190.   }
  191.  
  192.   function update_submenu( $menu_ids, $page ) {
  193.     // loop through each menu
  194.     foreach ( $menu_ids as $menu_id ) {
  195.       $page_parent = null;
  196.       $menu_items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish' ) );
  197.  
  198.       if ( ! is_array( $menu_items ) ) {
  199.         continue;
  200.  
  201.       } else {
  202.         // loop through each menu item
  203.         foreach ( $menu_items as $menu_item ) {
  204.           // if page's existed in this menu, then ignore
  205.           if ( $menu_item->object_id == $page->ID ) {
  206.             continue;
  207.           }
  208.  
  209.           // if page parent's existed in this menu, then get this menu item
  210.           if ( $menu_item->object_id == $page->post_parent ) {
  211.             $page_parent = $menu_item;
  212.           }
  213.         }
  214.       }
  215.  
  216.       // if page has parent page, then add page into sub-menu under page parent
  217.       if ( ! is_null( $page_parent ) ) {
  218.         $this->add_page_into_submenu( $menu_id, $page, $page_parent );
  219.       }
  220.     }
  221.   }
  222.  
  223.   function update_topmenu( $menu_ids, $page ) {
  224.     foreach ( $menu_ids as $menu_id ) {
  225.       $this->add_page_into_topmenu( $menu_id, $page );
  226.     }
  227.   }
  228.  
  229.   function when_update_page( $page_id, $page_after, $page_before ) {
  230.     // if support menu and has auto_add
  231.     if ( $this->is_support_menus() && $this->has_auto_add() ) {
  232.       $page = $page_after;
  233.       $menu_ids = $this->get_auto_add();
  234.  
  235.       $is_restore = $this->is_restore( $page_after->post_status, $page_before->post_status );
  236.       $is_parent_change = $this->is_parent_change( $page_after->post_parent, $page_before->post_parent);
  237.         $is_reorder = $this->is_reorder( $page_after->menu_order, $page_before->menu_order );
  238.  
  239.       // if it's restored or its page parent's changed
  240.       if ( $is_restore || $is_parent_change ) {
  241.         $this->remove_page_from_menus( $menu_ids, $page );
  242.  
  243.         if ( $this->has_parent( $page_id ) ) {
  244.           $this->update_submenu( $menu_ids, $page );
  245.  
  246.         } else {
  247.           $this->update_topmenu( $menu_ids, $page );
  248.         }
  249.        
  250.       } elseif ( $is_reorder ) {
  251.           $this->update_items_order( $menu_ids, $page );
  252.       }
  253.     }
  254.   }
  255. }
  256.  
  257. $auto_subpage_menu = new Auto_Subpage_Menu();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top