diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 483db20..64ae51b 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -3,6 +3,7 @@ namespace WordPressDotOrg\FiveForTheFuture\Contributor; use WordPressDotOrg\FiveForTheFuture; use WordPressDotOrg\FiveForTheFuture\Pledge; +use WP_Error, WP_Post; defined( 'WPINC' ) || die(); @@ -10,7 +11,9 @@ const SLUG = 'contributor'; const SLUG_PL = 'contributors'; const CPT_ID = FiveForTheFuture\PREFIX . '_' . SLUG; -add_action( 'init', __NAMESPACE__ . '\register_custom_post_type', 0 ); +add_action( 'init', __NAMESPACE__ . '\register_custom_post_type', 0 ); +add_filter( 'manage_edit-' . CPT_ID . '_columns', __NAMESPACE__ . '\add_list_table_columns' ); +add_action( 'manage_' . CPT_ID . '_posts_custom_column', __NAMESPACE__ . '\populate_list_table_columns', 10, 2 ); /** * Register the post type(s). @@ -64,3 +67,111 @@ function register_custom_post_type() { register_post_type( CPT_ID, $args ); } + +/** + * Add columns to the Contributors list table. + * + * @param array $columns + * + * @return array + */ +function add_list_table_columns( $columns ) { + $first = array_slice( $columns, 0, 2, true ); + $last = array_slice( $columns, 2, null, true ); + + $new_columns = array( + 'pledge' => __( 'Pledge', 'wporg' ), + ); + + return array_merge( $first, $new_columns, $last ); +} + +/** + * Render content in the custom columns added to the Contributors list table. + * + * @param string $column + * @param int $post_id + * + * @return void + */ +function populate_list_table_columns( $column, $post_id ) { + switch ( $column ) { + case 'pledge': + $contributor = get_post( $post_id ); + $pledge = get_post( $contributor->post_parent ); + + $pledge_name = get_the_title( $pledge ); + + if ( current_user_can( 'edit_post', $pledge->ID ) ) { + $pledge_name = sprintf( + '%2$s', + get_edit_post_link( $pledge ), + $pledge_name + ); + } + + echo $pledge_name; + break; + } +} + +/** + * Create a contributor post as a child of a pledge post. + * + * @param string $wporg_username + * @param int $pledge_id + * + * @return int|WP_Error Post ID on success. Otherwise WP_Error. + */ +function create_new_contributor( $wporg_username, $pledge_id ) { + $args = array( + 'post_type' => CPT_ID, + 'post_title' => sanitize_user( $wporg_username ), + 'post_parent' => $pledge_id, + 'post_status' => 'pending', + ); + + return wp_insert_post( $args, true ); +} + +/** + * Get the contributor posts associated with a particular pledge post. + * + * @param int $pledge_id The post ID of the pledge. + * @param string $status Optional. 'all', 'pending', or 'publish'. + * + * @return array An array of contributor posts. If $status is set to 'all', will be + * a multidimensional array with keys for each status. + */ +function get_pledge_contributors( $pledge_id, $status = 'publish' ) { + $args = array( + 'post_type' => CPT_ID, + 'post_parent' => $pledge_id, + 'numberposts' => -1, + 'orderby' => 'title', + 'order' => 'asc', + ); + + if ( 'all' === $status ) { + $args['post_status'] = array( 'pending', 'publish' ); + } else { + $args['post_status'] = sanitize_key( $status ); + } + + $posts = get_posts( $args ); + + if ( 'all' === $status && ! empty( $posts ) ) { + $initial = array( + 'publish' => array(), + 'pending' => array(), + ); + + $posts = array_reduce( $posts, function( $carry, WP_Post $item ) { + $carry[ $item->post_status ][] = $item; + + return $carry; + }, $initial ); + } + + return $posts; +} diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index b989573..a36840f 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -240,40 +240,6 @@ function has_existing_pledge( $key, $key_type, int $current_pledge_id = 0 ) { return ! empty( $matching_pledge ); } -/** - * TODO Move this to the contributor cpt include file. - * - * @param int $pledge_id - * - * @return array - */ -function get_pledge_contributors( $pledge_id = 0 ) { - $contributors = array(); - - // Get POST'd submission, if it exists. - $submission = filter_input( INPUT_POST, 'org-pledge-contributors', FILTER_SANITIZE_STRING ); - - // Get existing pledge, if it exists. - $pledge = get_post( $pledge_id ); - - if ( ! empty( $submission ) ) { - $contributors = array_map( 'sanitize_user', explode( ',', $submission ) ); - } elseif ( $pledge instanceof WP_Post ) { - // TODO the Contributor post type is being introduced in a separate PR. These details may change. - - $contributor_posts = get_posts( array( - 'post_type' => '', - 'post_status' => array( 'pending', 'publish' ), - 'post_parent' => $pledge_id, - 'numberposts' => -1, - ) ); - - $contributors = wp_list_pluck( $contributor_posts, 'post_title' ); - } - - return $contributors; -} - /** * Ensure each item in a list of usernames is valid and corresponds to a user. * diff --git a/plugins/wporg-5ftf/includes/pledge-meta.php b/plugins/wporg-5ftf/includes/pledge-meta.php index b197d44..9cf24ef 100755 --- a/plugins/wporg-5ftf/includes/pledge-meta.php +++ b/plugins/wporg-5ftf/includes/pledge-meta.php @@ -8,6 +8,7 @@ namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta; use WordPressDotOrg\FiveForTheFuture; use WordPressDotOrg\FiveForTheFuture\Pledge; use WordPressDotOrg\FiveForTheFuture\PledgeForm; +use WordPressDotOrg\FiveForTheFuture\Contributor; use WP_Post, WP_Error; defined( 'WPINC' ) || die(); @@ -153,12 +154,14 @@ function add_meta_boxes() { */ function render_meta_boxes( $pledge, $box ) { $readonly = ! current_user_can( 'edit_page', $pledge->ID ); - $data = array(); + $data = array(); foreach ( get_pledge_meta_config() as $key => $config ) { $data[ $key ] = get_post_meta( $pledge->ID, META_PREFIX . $key, $config['single'] ); } + $contributors = Contributor\get_pledge_contributors( $pledge->ID, 'all' ); + echo '
'; switch ( $box['id'] ) { diff --git a/plugins/wporg-5ftf/views/inputs-pledge-contributors.php b/plugins/wporg-5ftf/views/inputs-pledge-contributors.php index 2aa5519..c98d34a 100644 --- a/plugins/wporg-5ftf/views/inputs-pledge-contributors.php +++ b/plugins/wporg-5ftf/views/inputs-pledge-contributors.php @@ -28,7 +28,49 @@ namespace WordPressDotOrg\FiveForTheFuture\View;
+ $group ) : ?> + +

+ +

+ + + + +