Contributors: Create functionality for My Pledges page.

This don't add the styling yet, just the functionality.

See #56
Fixes #51
Fixes #30
This commit is contained in:
Ian Dunn 2019-10-28 12:08:21 -07:00 committed by GitHub
parent dbdcf26bd9
commit 67958d9a1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 159 additions and 0 deletions

View file

@ -15,6 +15,8 @@ add_action( 'init', __NAMESPACE__ . '\regis
add_filter( 'manage_edit-' . CPT_ID . '_columns', __NAMESPACE__ . '\add_list_table_columns' ); 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 ); add_action( 'manage_' . CPT_ID . '_posts_custom_column', __NAMESPACE__ . '\populate_list_table_columns', 10, 2 );
add_shortcode( '5ftf_my_pledges', __NAMESPACE__ . '\render_my_pledges' );
/** /**
* Register the post type(s). * Register the post type(s).
* *
@ -209,3 +211,68 @@ function get_contributor_user_objects( array $contributor_posts ) {
return get_user_by( 'login', $post->post_title ); return get_user_by( 'login', $post->post_title );
}, $contributor_posts ); }, $contributor_posts );
} }
/**
* Render the My Pledges shortcode.
*
* @return string
*/
function render_my_pledges() {
$user = wp_get_current_user();
$success_message = process_my_pledges_form();
$pledge_url = get_permalink( get_page_by_path( 'for-organizations' ) );
$contributor_posts = get_posts( array(
'post_type' => CPT_ID,
'post_title' => $user->user_login,
'post_status' => array( 'pending', 'publish' ),
'numberposts' => 100,
) );
ob_start();
require FiveForTheFuture\get_views_path() . 'list-my-pledges.php';
return ob_get_clean();
}
/**
* Process the My Pledges form.
*
* @return string
*/
function process_my_pledges_form() {
$message = '';
$status = false;
$contributor_post_id = filter_input( INPUT_POST, 'contributor_post_id', FILTER_VALIDATE_INT );
$pledge = get_post( get_post( $contributor_post_id )->post_parent );
$nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
if ( filter_input( INPUT_POST, 'join_organization' ) ) {
wp_verify_nonce( $nonce, 'join_decline_organization' ) || wp_nonce_ays( 'join_decline_organization' );
$status = 'publish';
$message = "You have joined the pledge from {$pledge->post_title}.";
} elseif ( filter_input( INPUT_POST, 'decline_invitation' ) ) {
wp_verify_nonce( $nonce, 'join_decline_organization' ) || wp_nonce_ays( 'join_decline_organization' );
$status = 'trash';
$message = "Your have declined the invitation from {$pledge->post_title}.";
} elseif ( filter_input( INPUT_POST, 'leave_organization' ) ) {
wp_verify_nonce( $nonce, 'leave_organization' ) || wp_nonce_ays( 'leave_organization' );
$status = 'trash';
$message = "Your have left the {$pledge->post_title} pledge.";
}
if ( 'publish' === $status ) {
wp_update_post( array(
'ID' => $contributor_post_id,
'post_status' => $status,
) );
} elseif ( 'trash' === $status ) {
wp_delete_post( $contributor_post_id );
}
return $message;
}

View file

@ -0,0 +1,92 @@
<?php
namespace WordPressDotOrg\FiveForTheFuture\View;
use WP_User, WP_Post;
/**
* @var WP_User $user
* @var array $contributor_posts
* @var WP_Post $contributor_post
* @var string $success_message
* @var string $pledge_url
*/
?>
<?php if ( is_user_logged_in() ) : ?>
<?php echo get_avatar( $user->ID, 96, 'blank', "{$user->login}'s avatar" ); ?>
<p>Pledged 10 hours a week across two organizations</p> <?php // todo pull from profiles. ?>
<?php if ( $success_message ) : ?>
<div class="notice notice-success notice-alt">
<p>
<?php echo esc_html( $success_message ); ?>
</p>
</div>
<?php endif; ?>
<?php if ( $contributor_posts ) : ?>
<?php foreach ( $contributor_posts as $contributor_post ) : ?>
<?php $pledge = get_post( $contributor_post->post_parent ); ?>
<?php echo get_the_post_thumbnail( $pledge->ID, 'pledge-logo' ); ?>
<?php echo esc_html( $pledge->post_title ); ?>
<form action="" method="post">
<input type="hidden" name="contributor_post_id" value="<?php echo esc_attr( $contributor_post->ID ); ?>" />
<p>
<?php if ( 'pending' === $contributor_post->post_status ) : ?>
<?php wp_nonce_field( 'join_decline_organization' ); ?>
<input
type="submit"
name="join_organization"
value="Join Organization"
/>
<input
type="submit"
class="button-link"
name="decline_invitation"
value="Decline Invitation"
/>
<?php elseif ( 'publish' === $contributor_post->post_status ) : ?>
<p>
You confirmed this pledge on {date}
</p>
<?php wp_nonce_field( 'leave_organization' ); ?>
<input
type="submit"
name="leave_organization"
value="Leave Organization"
/>
<?php endif; ?>
</p>
</form>
<?php endforeach; ?>
<?php else : ?>
<p>You don't currently have any sponsorships. If your employer is sponsoring part of your time to contribute to WordPress, please ask them to <a href="<?php echo esc_url( $pledge_url ); ?>">submit a pledge</a> and list you as a contributor.</p>
<?php // todo add some resources here about how they can convince their boss to sponsor some of their time? ?>
<?php endif; ?>
<?php else : ?>
<div class="notice notice-error notice-alt">
<p>
Please <a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>">log in to your WordPress.org account</a> in order to view your pledges.
</p>
</div>
<?php endif; ?>