project_id = $project_id; $project = get_post( $project_id ); if ( ! empty( $project->post_title ) ) { $this->project_name = $project->post_title; } add_filter( 'posts_clauses', array( $this, 'filter_orderby_for_author_name' ), 10, 2 ); } /** * @todo Do this noscript logic and other $_REQUEST parsing earlier */ function display() { wp_enqueue_script( 'anthologize-sortlist-js' ); wp_enqueue_script( 'anthologize-project-organizer' ); 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() ?>
__( 'Author (A-Z)', 'anthologize' ), 'author_desc' => __( 'Author (Z-A)', 'anthologize' ), 'date_asc' => __( 'Date (oldest first)', 'anthologize' ), 'date_desc' => __( 'Date (newest first)', 'anthologize' ), 'title_asc' => __( 'Title (A-Z)', 'anthologize' ), 'title_desc' => __( 'Title (Z-A)', 'anthologize' ), ); $orderby = 'title_asc'; $corderby = isset( $_COOKIE['anth-orderby'] ) ? $_COOKIE['anth-orderby'] : ''; if ( $corderby && isset( $filters[ $corderby ] ) ) { $orderby = $corderby; } ?> __( 'Tag', 'anthologize' ), 'category' => __( 'Category', 'anthologize' ), 'date' => __( 'Date Range', 'anthologize' ), 'post_type' => __( 'Post Type', 'anthologize' ) ); $cfilter = isset( $_COOKIE['anth-filter'] ) ? $_COOKIE['anth-filter'] : ''; ?> available_post_types(); $terms = array(); foreach ( $types as $type_id => $type_label ) { $type_object = new stdClass; $type_object->term_id = $type_id; $type_object->name = $type_label; $terms[] = $type_object; } $nulltext = __( 'All post types', 'anthologize' ); break; default : $terms = array(); $nulltext = ' - '; break; } ?>

true ), false ); $excluded_post_types = apply_filters( 'anth_excluded_post_types', array( 'anth_library_item', 'anth_part', 'anth_project', 'attachment', 'revision', 'nav_menu_item' ) ); $types = array(); foreach ( $all_post_types as $name => $post_type ) { if ( ! in_array( $name, $excluded_post_types ) ) { $types[ $name ] = isset( $post_type->labels->name ) ? $post_type->labels->name : $name; } } return apply_filters( 'anth_available_post_types', $types ); } function add_item_to_part( $item_id, $part_id ) { global $wpdb, $current_user; if ( ! (int) $last_item = get_post_meta( $part_id, 'last_item', true ) ) { $last_item = 0; } $last_item++; $the_item = get_post( $item_id ); $part = get_post( $part_id ); $args = array( 'menu_order' => $last_item, 'comment_status' => $the_item->comment_status, 'ping_status' => $the_item->ping_status, 'pinged' => $the_item->pinged, 'post_author' => $current_user->ID, 'post_content' => $the_item->post_content, 'post_date' => $the_item->post_date, 'post_date_gmt' => $the_item->post_date_gmt, 'post_excerpt' => $the_item->post_excerpt, 'post_parent' => $part_id, 'post_password' => $the_item->post_password, 'post_status' => $part->post_status, // post_status is set to the post_status of the parent part 'post_title' => $the_item->post_title, 'post_type' => 'anth_library_item', 'to_ping' => $the_item->to_ping, // todo: tags and categories ); // WordPress will strip these slashes off in wp_insert_post $args = add_magic_quotes($args); if ( ! $imported_item_id = wp_insert_post( $args ) ) { return false; } // Update the parent project's Date Modified field to right now $this->update_project_modified_date(); // Author data $user = get_userdata( $the_item->post_author ); if ( ! $author_name = get_post_meta( $item_id, 'author_name', true ) && $user ) { $author_name = $user->display_name; } $author_name_array = array( $author_name ); $anthologize_meta = apply_filters( 'anth_add_item_postmeta', array( 'author_name' => $author_name, 'author_name_array' => $author_name_array, 'author_id' => $the_item->post_author, 'original_post_id' => $item_id ) ); update_post_meta( $imported_item_id, 'anthologize_meta', $anthologize_meta ); update_post_meta( $imported_item_id, 'author_name', $author_name ); // Deprecated - please use anthologize_meta update_post_meta( $imported_item_id, 'author_name_array', $author_name_array ); // Deprecated - please use anthologize_meta return $imported_item_id; } function update_project_modified_date() { $project_post = get_post( $this->project_id ); $project_args = array( 'ID' => $this->project_id, 'post_modified' => date( "Y-m-d G:H:i" ), 'post_modified_gmt' => gmdate( "Y-m-d G:H:i" ) ); wp_update_post( $project_args ); } function add_new_part( $part_name ) { if ( ! (int) $last_item = get_post_meta( $this->project_id, 'last_item', true ) ) { $last_item = 0; } $last_item++; $project = get_post( $this->project_id ); $args = array( 'post_title' => $part_name, 'post_type' => 'anth_part', 'post_status' => $project->post_status, '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 ); $this->update_project_modified_date(); return true; } function list_existing_parts() { $args = array( 'post_type' => 'anth_part', 'order' => 'ASC', 'orderby' => 'menu_order', 'post_per_page' => -1, 'showposts' => -1, 'post_parent' => $this->project_id ); // @todo - no query_posts( $args ); if ( have_posts() ) { while ( have_posts() ) { the_post(); $part_id = get_the_ID(); ?>
  • | | | -
  • "New Part" to get started.', 'anthologize' ), esc_url( admin_url( 'post-new.php?post_type=anth_part&project_id=' . $this->project_id . '&new_part=1' ) ) ) ?>

    array_keys( $this->available_post_types() ), 'posts_per_page' => -1, 'post_status' => $this->source_item_post_statuses(), 'is_anthologize_query' => true, ); $cfilter = isset( $_COOKIE['anth-filter'] ) ? $_COOKIE['anth-filter'] : false; if ( $cfilter == 'date' ) { $startdate = wp_unslash( $_COOKIE['anth-startdate'] ); $enddate = wp_unslash( $_COOKIE['anth-enddate'] ); $date_range_where = ''; if ( strlen( $startdate ) > 0 ) { $date_range_where .= $wpdb->prepare( " AND post_date >= %s", $startdate ); } if ( strlen( $enddate ) > 0 ) { $date_range_where .= $wpdb->prepare( " AND post_date <= %s,", $enddate ); } $where_func = '$where .= "' . $date_range_where . '"; return $where;'; $filter_where = create_function( '$where', $where_func ); add_filter( 'posts_where', $filter_where ); } else { $cterm = isset( $_COOKIE['anth-term'] ) ? $_COOKIE['anth-term'] : false; if ( $cterm ) { if ( $cfilter ) { switch( $cfilter ) { case 'tag' : $filtertype = 'tag'; break; case 'category' : $filtertype = 'cat'; break; case 'post_type' : $filtertype = 'post_type'; break; } $args[$filtertype] = $cterm; } } } $corderby = isset( $_COOKIE['anth-orderby'] ) ? $_COOKIE['anth-orderby'] : 'title_asc'; $orderby_settings = Anthologize_Project_Organizer::get_orderby_settings( $corderby ); $args['orderby'] = $orderby_settings['orderby']; $args['order'] = $orderby_settings['order']; $big_posts = new WP_Query( $args ); if ( $big_posts->have_posts() ) { ?> 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 ''; } } /** * Get source item metadata for a post. * * @since 0.8.0 * * @param int $item_id ID of the item. * @return array */ public static function get_item_metadata( $item_id ) { $item_post = get_post( $item_id ); $item_metadata = array( 'link' => sprintf( '%s', esc_attr( get_permalink( $item_post ) ), esc_html__( 'View post', 'anthologize' ) ), ); $item_author = get_userdata( $item_post->post_author ); $item_tags = get_the_term_list( $item_id, 'post_tag', '', ', ' ); $item_cats = get_the_term_list( $item_id, 'category', '', ', ' ); if ( $item_author ) { $item_metadata['author'] = sprintf( __( 'Author: %s', 'anthologize' ), esc_html( sprintf( '%s (%s)', $item_author->display_name, $item_author->user_login ) ) ); } if ( $item_tags ) { $item_metadata['tags'] = sprintf( __( 'Tags: %s', 'anthologize' ), $item_tags ); } if ( $item_cats ) { $item_metadata['cats'] = sprintf( __( 'Categories: %s', 'anthologize' ), $item_cats ); } return $item_metadata; } /** * Get order values from stored setting. */ public static function get_orderby_settings( $orderby ) { $orderby_values = array( 'date_asc' => array( 'orderby' => 'date', 'order' => 'ASC', ), 'date_desc' => array( 'orderby' => 'date', 'order' => 'DESC', ), 'title_asc' => array( 'orderby' => 'title', 'order' => 'ASC', ), 'title_desc' => array( 'orderby' => 'title', 'order' => 'DESC', ), 'author_desc' => array( 'orderby' => 'author_name', 'order' => 'DESC', ), 'author_asc' => array( 'orderby' => 'author_name', 'order' => 'asc', ), ); if ( ! isset( $orderby_values[ $orderby ] ) ) { $orderby_values = 'title_asc'; } return array( 'orderby' => $orderby_values[ $orderby ]['orderby'], 'order' => $orderby_values[ $orderby ]['order'], ); } function get_part_items( $part_id ) { $append_parent = !empty( $_GET['append_parent'] ) ? $_GET['append_parent'] : false; $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'      => 'anth_library_item',
    			'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 );
    
    			//clone over the attachments to the original post and associate them with the new
    			//library item. That should make things like the [gallery] shortcode work
    			$attArgs = array( 'post_parent'=> $post_id, 'post_type' => 'attachment' );
    			$attachments = get_children( $attArgs );
    			foreach ( $attachments as $attachment ) {
    				$attPostArgs = array(
    					'post_parent'    => $add_item_result,
    					'post_type'      => 'attachment',
    					'guid'           => $attachment->guid,
    					'post_title'     => $attachment->post_title,
    					'post_status'    => $attachment->post_status,
    					'post_name'      => $attachment->post_name,
    					'post_mime_type' => $attachment->post_mime_type
    				);
    				wp_insert_post( $attPostArgs );
    			}
    
    			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 ) );
    		}
    
    		$this->update_project_modified_date();
    
    		return true;
    	}
    
    	function remove_item( $id ) {
    		// Git ridda the post
    		if ( ! wp_delete_post( $id ) ) {
    		    return false;
    		}
    
    		$this->update_project_modified_date();
    
    		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 ); $this->update_project_modified_date(); return true; } function display_item( $append_parent ) { global $post; /** * Pull up some comment data to be used in the Comments (x/y) area. * Comments themselves are fetched with AJAX as needed. */ // First, the original post $anth_meta = get_post_meta( get_the_ID(), 'anthologize_meta', true ); $original_comment_count = 0; if ( ! empty( $anth_meta['original_post_id'] ) ) { $original_post = get_post( $anth_meta['original_post_id'] ); if ( $original_post ) { $original_comment_count = (int) $original_post->comment_count; } } // Then, see how many comments are being brought along to the export $included_comment_count = 0; if ( ! empty( $anth_meta['included_comments'] ) ) { $included_comment_count = count( $anth_meta['included_comments'] ); } ?>
  • ID ) echo 'checked="checked" disabled=disabled'; ?>/> ID ) . " " . esc_html( $append_parent ) ?>

    | %1$d/%2$d)', 'anthologize' ), $included_comment_count, $original_comment_count ) ?> | */ ?> | |

  • 'anthologize', 'anth_preview' => '1', 'post_id' => $post_id, 'post_type' => $post_type ); $url = add_query_arg( $query_args, admin_url( 'admin.php' ) ); return $url; } /** * Gets the post statuses of source items to show in the project organizer. * * @package Anthologize * @since 0.8.0 * * @return array */ function source_item_post_statuses() { /** * Status of posts to include in the project organizer. * Defaults to just published, pending, future and private. * * @since 0.8.0 * * @param array $statuses statuses of posts/pages to include in the project organizer */ return apply_filters( 'anthologize_source_item_post_statuses', array( 'publish', 'pending', 'future', 'private' ) ); } public function filter_orderby_for_author_name( $clauses, $q ) { global $wpdb; if ( ! $q->get( 'is_anthologize_query' ) ) { return $clauses; } $orderby_param = $q->get( 'orderby' ); if ( 'author_name' !== $orderby_param ) { return $clauses; } // Don't double-add. if ( false === strpos( $clauses['join'], 'anthologize_author' ) ) { $clauses['join'] .= " LEFT JOIN {$wpdb->users} AS anthologize_author ON ({$wpdb->posts}.post_author = anthologize_author.ID) "; $clauses['orderby'] = 'anthologize_author.user_nicename ' . $q->get( 'order' ); } return $clauses; } } endif;