Пример #1
0
 public static function get_lost_pages($allow_autodrafts = false)
 {
     global $wpdb;
     $lost_pages = array();
     $tree = ApmTreeDb::get_last_tree();
     if (!empty($tree)) {
         $tree = new ApmTree($tree);
         $tree_apm_ids = $tree->get_nodes_flat();
         $tree_wp_ids = ApmNodeDataIntern::get_wp_ids($tree_apm_ids);
         $tree_wp_ids = array_diff($tree_wp_ids, array(ApmTreeData::root_id));
         //Remove the zeroes (for root).
         $allowed_post_status = ApmConfig::$allowed_post_status;
         if ($allow_autodrafts) {
             $allowed_post_status[] = 'auto-draft';
         }
         $allowed_post_status = apply_filters('apm_allowed_post_status', $allowed_post_status, 'get_lost_pages');
         $allowed_post_status = array_map("addslashes", $allowed_post_status);
         $sql_status = " AND post_status IN ('" . implode("','", $allowed_post_status) . "') ";
         $sql = "SELECT * FROM {$wpdb->posts} AS p \n\t\t\t\t\t\t\t WHERE p.post_type = 'page' {$sql_status} AND p.ID NOT IN ('" . implode("','", $tree_wp_ids) . "')";
         $lost_pages_raw = $wpdb->get_results($sql);
         if (!empty($lost_pages_raw)) {
             foreach ($lost_pages_raw as $page) {
                 $page->post_title = _draft_or_post_title($page->ID);
                 $lost_pages[$page->ID] = $page;
             }
         }
     }
     return $lost_pages;
 }
Пример #2
0
 /**
  * Synchronizes our separate arbo with WP "real" pages
  * !!Careful!! $this->nodes_data must be loaded before calling this!
  */
 public function synchronize_tree_with_wp_entities($synchronisation_data = array(), $synchronize_only_my_children = array())
 {
     global $wpdb;
     if (empty($synchronisation_data) && empty($synchronize_only_my_children)) {
         //Synchronize all tree!
         $tree = $this->apm_tree->get_tree();
         //To avoid calling $this->nodes_data->get([node])->wp_id each time:
         $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids($this->apm_tree->get_nodes_flat(self::root_id));
         foreach ($tree as $parent => $children) {
             $parent_wp_id = $wp_ids[$parent];
             $children_wp_ids = array();
             foreach ($children as $child) {
                 $children_wp_ids[] = $wp_ids[$child];
             }
             if (!empty($children_wp_ids)) {
                 //We want to set order too... so we have to make one request per child!!
                 //$sql = "UPDATE ". $wpdb->posts ." SET post_parent='$parent_wp_id' WHERE ID IN ('". implode("','",$children_wp_ids) ."')";
                 //$results = $wpdb->query($sql);
                 $cpt = 0;
                 foreach ($children_wp_ids as $child_wp_id) {
                     $sql = "UPDATE " . $wpdb->posts . " SET post_parent='{$parent_wp_id}', menu_order='{$cpt}' WHERE ID='{$child_wp_id}'";
                     $results = $wpdb->query($sql);
                     $cpt++;
                 }
             }
         }
     } else {
         if (!empty($synchronisation_data)) {
             //Optimized synchronisation :
             $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids($synchronisation_data['all_apm_ids']);
             foreach ($synchronisation_data['actions'] as $apm_id => $actions) {
                 $set = array();
                 $ok_to_update = true;
                 foreach ($actions as $action => $value) {
                     switch ($action) {
                         case 'order':
                             if ($value === '--') {
                                 $set[] = "menu_order=menu_order-1";
                             } elseif ($value === '++') {
                                 $set[] = "menu_order=menu_order+1";
                             } else {
                                 $set[] = "menu_order='{$value}'";
                             }
                             break;
                         case 'parent':
                             if (array_key_exists($value, $wp_ids)) {
                                 $set[] = "post_parent='" . $wp_ids[$value] . "'";
                             } else {
                                 $ok_to_update = false;
                             }
                             break;
                     }
                 }
                 $sql_set = implode(", ", $set);
                 if ($ok_to_update && array_key_exists($apm_id, $wp_ids)) {
                     $sql = "UPDATE " . $wpdb->posts . " SET {$sql_set} WHERE ID='" . $wp_ids[$apm_id] . "'";
                     $results = $wpdb->query($sql);
                 }
             }
         } else {
             //Synchronise only direct children of the fathers nodes passed in $synchronize_only_my_children:
             //When comming from an editing action, $synchronize_only_my_children contains 1 or 2 items.
             foreach ($synchronize_only_my_children as $father) {
                 $children = $this->apm_tree->get_children($father);
                 //To avoid calling $this->nodes_data->get([node])->wp_id each time:
                 $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids(array_merge($children, array($father)));
                 $cpt = 0;
                 foreach ($children as $child) {
                     $sql = "UPDATE " . $wpdb->posts . " SET post_parent='" . $wp_ids[$father] . "', menu_order='{$cpt}' WHERE ID='" . $wp_ids[$child] . "'";
                     $results = $wpdb->query($sql);
                     $cpt++;
                 }
             }
         }
     }
 }