project_id = $project_id; $project = get_post( $project_id ); $this->project_name = $project->post_title; } function load_scripts() { } function display() { if ( isset( $_POST['new_item'] ) ) $this->add_item_to_part( $_POST['item_id'], $_POST['part_id'] ); if ( isset( $_POST['new_part'] ) ) $this->add_new_part( $_POST['new_part_name'] ); if ( isset( $_GET['move_up'] ) ) $this->move_up( $_GET['move_up'] ); if ( isset( $_GET['move_down'] ) ) $this->move_down( $_GET['move_down'] ); if ( isset( $_GET['remove'] ) ) $this->remove_item( $_GET['remove'] ); if ( isset( $_POST['append_children'] ) ) { $this->append_children( $_POST['append_parent'], $_POST['append_children'] ); } ?>

project_name ?>


    list_existing_parts() ?>
__( 'Tag', 'anthologize' ), 'category' => __( 'Category', 'anthologize' ) ); ?> Filter by $last_item, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'pinged' => $post->pinged, 'post_author' => $post->post_author, 'post_content' => $post->post_content, 'post_date' => $post->post_date, 'post_date_gmt' => $post->post_date_gmt, 'post_excerpt' => $post->post_excerpt, 'post_parent' => $part_id, 'post_password' => $post->post_password, 'post_status' => 'publish', 'post_title' => $post->post_title, 'post_type' => 'library_items', 'to_ping' => $post->to_ping, // todo: tags and categories ); if ( !$imported_item_id = wp_insert_post( $args ) ) return false; // Author data $user = get_userdata( $post->post_author ); if ( !$author_name = get_post_meta( $item_id, 'author_name', true ) ) $author_name = $user->display_name; $author_name_array = array( $author_name ); update_post_meta( $imported_item_id, 'author_name', $author_name ); update_post_meta( $imported_item_id, 'author_name_array', $author_name_array ); return $imported_item_id; } function add_new_part( $part_name ) { if ( !(int)$last_item = get_post_meta( $this->project_id, 'last_item', true ) ) $last_item = 0; $last_item++; $args = array( 'post_title' => $part_name, 'post_type' => 'parts', 'post_status' => 'publish', 'post_parent' => $this->project_id ); if ( !$part_id = wp_insert_post( $args ) ) return false; // Store the menu order of the last item to enable easy moving later on update_post_meta( $this->project, 'last_item', $last_item ); return true; } function list_existing_parts() { query_posts( 'post_type=parts&order=ASC&orderby=menu_order&post_parent=' . $this->project_id ); if ( have_posts() ) { while ( have_posts() ) { the_post(); $part_id = get_the_ID(); ?>
  • |

  • "New Part" to get started.', 'anthologize' ), 'post-new.php?post_type=parts&project_id=' . $this->project_id . '&new_part=1' ) ?>

    array('post', 'page', 'imported_items' ), 'posts_per_page' => -1 ); $big_posts = new WP_Query( $args ); if ( $big_posts->have_posts() ) { ?> query()); $sql = "SELECT id, post_title FROM wp_posts WHERE post_type = 'page' OR post_type = 'post' OR post_type = 'imported_items'"; $ids = $wpdb->get_results($sql); $counter = 0; foreach( $ids as $id ) { if ( in_array( $id->id, $items ) || array_key_exists( $id->id, $items ) ) // Todo: adjust so that it references parent stuff continue; echo ''; $counter++; } if ( !$counter ) echo ''; } function get_part_items( $part_id ) { if ( isset( $_GET['append_parent'] ) ) $append_parent = $_GET['append_parent']; $items = get_post_meta( $part_id, 'items', true ); //echo "
    ";
    		//print_r($items); die();
    		//if ( empty( $items ) )
    		//	return;
    
    		$args = array(
    			'post_parent' => $part_id,
    			'post_type' => 'library_items',
    			'posts_per_page' => -1,
    			'orderby' => 'menu_order',
    			'order' => ASC
    		);
    
    		$items_query = new WP_Query( $args );
    
    		if ( $items_query->have_posts() ) {
    
    			while ( $items_query->have_posts() ) : $items_query->the_post();
    
    				$this->display_item( $append_parent );
    
    			endwhile;
    
    		}
    	}
    
    	function move_up( $id ) {
    		global $wpdb;
    
    		$post = get_post( $id );
    		$my_menu_order = $post->menu_order;
    
    		$little_brother = 0;
    		$minus = 0;
    
    		while ( !$big_brother ) {
    			$minus++;
    
    			// Find the big brother
    			$big_brother_q = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND menu_order = %d LIMIT 1", $post->post_parent, $my_menu_order-$minus );
    
    			$bb = $wpdb->get_results( $big_brother_q, ARRAY_N );
    			$big_brother = $bb[0][0];
    		}
    
    		// Downgrade the big brother
    		$big_brother_q = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = %d WHERE ID = %d", $my_menu_order, $big_brother ) );
    
    		// Upgrade self
    		$little_brother_q = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = %d WHERE ID = %d", $my_menu_order-$minus, $id ) );
    
    		return true;
    	}
    
    	function move_down( $id ) {
    		global $wpdb;
    
    		$post = get_post( $id );
    		$my_menu_order = $post->menu_order;
    
    		$little_brother = 0;
    		$plus = 0;
    
    		while ( !$little_brother ) {
    			$plus++;
    
    			// Find the little brother
    			$little_brother_q = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND menu_order = %d LIMIT 1", $post->post_parent, $my_menu_order+$plus );
    
    			$lb = $wpdb->get_results( $little_brother_q, ARRAY_N );
    			$little_brother = $lb[0][0];
    		}
    
    		// Upgrade the little brother
    		$little_brother_q = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = %d WHERE ID = %d", $my_menu_order, $little_brother ) );
    
    		// Downgrade self
    		$big_brother_q = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET menu_order = %d WHERE ID = %d", $my_menu_order+$plus, $id ) );
    
    		return true;
    	}
    
    
    
    	function insert_item( $project_id, $post_id, $new_post, $dest_id, $source_id, $dest_seq, $source_seq ) {
    		global $wpdb;
    		if ( !isset( $project_id ) || !isset( $post_id ) || !isset( $dest_id ) || !isset( $dest_seq ) )
    			return false;
    
    		if ( !$new_post ) {
    			if ( !isset( $source_id ) || !isset( $source_seq ) )
    				return false;
    		}
    
    		if ( true === $new_post ) {
                $add_item_result = $this->add_item_to_part( $post_id, $dest_id );
    			if (false === $add_item_result)
    				return false;
                $post_id = $add_item_result;
                $dest_seq[$post_id] = $dest_seq['new_new_new'];
                unset($dest_seq['new_new_new']);
            } else {
                $post_params = Array('ID' => $post_id,
                                     'post_parent' => $dest_id);
                $update_item_result = wp_update_post($post_params);
    			if (0 === $update_item_result) {
    				return false;
                }
                $post_id = $update_item_result;
                $this->rearrange_items( $source_seq );
            }
    
            // not really any point in checking for errors at this point
            // Since the insert succeeded
            // We should use more detailed Exceptions eventually
            //
    		// All items require the destination siblings to be reordered
    /*		if ( !$this->rearrange_items( $dest_seq ) )
        return false;*/
            $this->rearrange_items( $dest_seq );
    
    		return $post_id;
    	}
    
    	function rearrange_items( $seq ) {
            global $wpdb;
    		foreach ( $seq as $item_id => $pos ) {
    			$q = "UPDATE $wpdb->posts SET menu_order = %d WHERE ID = %d";
    			$post_up_query = $wpdb->query( $wpdb->prepare( $q, $pos, $item_id ) );
    		}
    
    		return true;
    	}
    
    	function remove_item( $id ) {
    		// Git ridda the post
    		if ( !wp_delete_post( $id ) )
    			return false;
    
    		return true;
    	}
    
    	function append_children( $append_parent, $append_children ) {
    
    		$parent_post = get_post( $append_parent );
    		$pp_content = $parent_post->post_content;
    
    		if ( !$author_name = get_post_meta( $append_parent, 'author_name', true ) )
    			$author_name = '';
    
    		if ( !$author_name_array = get_post_meta( $append_parent, 'author_name_array', true ) )
    			$author_name_array = array();
    
    		foreach( $append_children as $append_child ) {
    			$child_post = get_post( $append_child );
    
    			$cp_title = '

    ' . $child_post->post_title . '

    '; $cp_content = $child_post->post_content; $pp_content .= $cp_title . $cp_content . ' '; if ( $author_name != '' ) $author_name .= ', '; $cp_author_name = get_post_meta( $append_child, 'author_name', true ); $author_name .= $cp_author_name; $author_name_array[] = $cp_author_name; wp_delete_post( $append_child ); } $args = array( 'ID' => $append_parent, 'post_content' => $pp_content, ); if ( !wp_update_post( $args ) ) return false; update_post_meta( $append_parent, 'author_name', $author_name ); update_post_meta( $append_parent, 'author_name_array', $author_name_array ); return true; } function display_item( $append_parent ) { global $post; ?>
  • ID ) echo 'checked="checked" disabled=disabled'; ?>/> ID . " " . $append_parent ?>

    | | project_id&append_parent= the_ID() ?>