mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-07-02 01:01:18 +03:00
Contributors: Add Contributors CPT and related admin UI
This commit is contained in:
parent
aaf8b1a712
commit
75428a3c95
|
@ -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(
|
||||
'<a href="%1$s">%2$s</a>',
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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 '<div class="pledge-form">';
|
||||
|
||||
switch ( $box['id'] ) {
|
||||
|
|
|
@ -28,7 +28,49 @@ namespace WordPressDotOrg\FiveForTheFuture\View;
|
|||
<?php else : ?>
|
||||
|
||||
<div class="5ftf-contributors">
|
||||
<?php foreach ( $contributors as $status => $group ) : ?>
|
||||
<?php if ( ! empty( $group ) ) : ?>
|
||||
<h3 class="contributor-list-heading">
|
||||
<?php
|
||||
switch ( $status ) {
|
||||
case 'pending':
|
||||
esc_html_e( 'Unconfirmed', 'wporg' );
|
||||
break;
|
||||
case 'publish':
|
||||
esc_html_e( 'Confirmed', 'wporg' );
|
||||
break;
|
||||
}
|
||||
?>
|
||||
</h3>
|
||||
|
||||
<ul class="contributor-list <?php echo esc_attr( $status ); ?>">
|
||||
<?php foreach ( $group as $contributor_post ) :
|
||||
$contributor = get_user_by( 'login', $contributor_post->post_title );
|
||||
?>
|
||||
<li>
|
||||
<?php echo get_avatar( $contributor->user_email, 32 ); ?>
|
||||
<?php echo $contributor_post->post_title; ?>
|
||||
<!-- TODO These buttons don't do anything yet.
|
||||
<button class="button-primary" data-action="remove" data-contributor-post="<?php echo esc_attr( $contributor_post->ID ); ?>">
|
||||
<?php esc_html_e( 'Remove', 'wporg' ); ?>
|
||||
</button>
|
||||
<?php if ( 'pending' === $contributor_post->post_status ) : ?>
|
||||
<button class="button-secondary" data-action="resend-confirmation" data-contributor-post="<?php echo esc_attr( $contributor_post->ID ); ?>">
|
||||
<?php esc_html_e( 'Resend confirmation', 'wporg' ); ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
-->
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<!-- TODO This button doesn't do anything yet.
|
||||
<button class="button-primary" data-action="add-contributor">
|
||||
<?php esc_html_e( 'Add new contributor', 'wporg' ); ?>
|
||||
</button>
|
||||
-->
|
||||
</div>
|
||||
|
||||
<?php endif; ?>
|
||||
|
|
Loading…
Reference in a new issue