Skip to content

Commit

Permalink
New reimbursement request and vendor payments status and notes functi…
Browse files Browse the repository at this point in the history
…onality (#1051)

* add new wcb-needs-followup status for payments

* add new wcb-needs-followup status to network payments views

* take new wcb-pending-approval status into consideration when doing the overdue vendor payments list

* extend the notes field for vendor payments

* add new private note metabox to vendor payments and reimbursement requests

* limit the new notes for network admins and the request's author

---------

Co-authored-by: ren <[email protected]>
  • Loading branch information
timiwahalahti and renintw authored May 20, 2024
1 parent 97c1d78 commit e21ab60
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public static function get_current_tab() {
'paid',
'cancelled-failed',
'incomplete',
'needs-followup',
);

if ( isset( $_REQUEST['wcp-section'] ) && in_array( $_REQUEST['wcp-section'], $tabs ) ) {
Expand All @@ -363,6 +364,7 @@ public static function render_dashboard_tabs() {
'paid' => __( 'Paid', 'wordcamporg' ),
'cancelled-failed' => __( 'Cancelled/Failed', 'wordcamporg' ),
'incomplete' => __( 'Incomplete', 'wordcamporg' ),
'needs-followup' => __( 'Needs Follow-up', 'wordcamporg' ),
);

foreach ( $sections as $section_key => $section_caption ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function prepare_items() {
$order = 'asc';

if ( 'overdue' == $view ) {
$where .= $wpdb->prepare( " AND `status` = 'wcb-pending-approval' AND `due` > 0 AND `due` <= %d ", time() );
$where .= $wpdb->prepare( " AND `status` IN ( 'wcb-pending-approval', 'wcb-needs-followup' ) AND `due` > 0 AND `due` <= %d ", time() );
} elseif ( 'pending-approval' == $view ) {
$where .= " AND `status` = 'wcb-pending-approval' ";
} elseif ( 'approved' == $view ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function render_submenu_page() {
$section_explanation = 'These requests have been reviewed by a deputy, and sent back to the organizer because they lacked some required information.';
break;

case 'wcb-needs-followup':
$section_explanation = 'These requests have been reviewed by a deputy, and need more information or second opinion.';
break;

case 'wcb-cancelled':
$section_explanation = 'These requests have been reviewed by a deputy and cancelled/rejected.';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public static function get_post_statuses() {
'draft',
'wcb-incomplete',
'wcb-pending-approval',
'wcb-needs-followup',
'wcb-approved',
'wcb-pending-payment',
'wcb-paid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function get_post_statuses() {
'draft',
'wcb-incomplete',
'wcb-pending-approval',
'wcb-needs-followup',
'wcb-approved',
'wcb-pending-payment',
'wcb-paid',
Expand Down Expand Up @@ -162,15 +163,6 @@ function init_meta_boxes() {
'high'
);

add_meta_box(
'wcbrr_notes',
esc_html__( 'Notes', 'wordcamporg' ),
__NAMESPACE__ . '\render_notes_metabox',
POST_TYPE,
'side',
'high'
);

add_meta_box(
'wcbrr_general_information',
esc_html__( 'General Information', 'wordcamporg' ),
Expand Down Expand Up @@ -316,19 +308,6 @@ function render_status_metabox( $post ) {
require_once dirname( __DIR__ ) . '/views/reimbursement-request/metabox-status.php';
}

/**
* Render the Notes metabox
*
* @param WP_Post $post
*/
function render_notes_metabox( $post ) {
wp_nonce_field( 'notes', 'notes_nonce' );

$existing_notes = get_post_meta( $post->ID, '_wcbrr_notes', true );

require_once dirname( __DIR__ ) . '/views/reimbursement-request/metabox-notes.php';
}

/**
* Render General Information Metabox
*
Expand Down Expand Up @@ -520,7 +499,6 @@ function save_request( $post_id, $post ) {
}

verify_metabox_nonces();
validate_and_save_notes( $post, $_POST['wcbrr_new_note'] );

/*
* We need to determine if the user is allowed to modify the request -- in terms of this plugin's post_status
Expand Down Expand Up @@ -673,7 +651,6 @@ function render_log_metabox( $post ) {
function verify_metabox_nonces() {
$nonces = array(
'status_nonce',
'notes_nonce',
'general_information_nonce',
'payment_details_nonce',
'expenses_nonce',
Expand Down Expand Up @@ -743,95 +720,6 @@ function validate_and_save_expenses( $post_id, $expenses ) {
update_post_meta( $post_id, '_wcbrr_expenses', $expenses );
}

/**
* Validate and save expense data
*
* @param WP_Post $post
* @param string $new_note_message
*/
function validate_and_save_notes( $post, $new_note_message ) {

// Save incomplete message.
if ( isset( $_POST['wcp_mark_incomplete_notes'] ) ) {
$safe_value = '';
if ( $post->post_status == 'wcb-incomplete' ) {
$safe_value = wp_kses( $_POST['wcp_mark_incomplete_notes'], wp_kses_allowed_html( 'strip' ) );
}

update_post_meta( $post->ID, '_wcp_incomplete_notes', $safe_value );
}

$new_note_message = sanitize_text_field( wp_unslash( $new_note_message ) );

if ( empty( $new_note_message ) ) {
return;
}

$notes = get_post_meta( $post->ID, '_wcbrr_notes', true );
if ( ! is_array( $notes ) ) {
$notes = array();
}

$new_note = array(
'timestamp' => time(),
'author_id' => get_current_user_id(),
'message' => $new_note_message,
);

$notes[] = $new_note;

update_post_meta( $post->ID, '_wcbrr_notes', $notes );
notify_parties_of_new_note( $post, $new_note );

\WordCamp_Budgets::log( $post->ID, get_current_user_id(), sprintf( 'Note: %s', $new_note_message ), array(
'action' => 'note-added',
) );
}

/**
* Notify WordCamp Central or the request author when new notes are added
*
* @param WP_Post $request
* @param array $note
*/
function notify_parties_of_new_note( $request, $note ) {
$note_author = get_user_by( 'id', $note['author_id'] );

if ( $note_author->has_cap( 'manage_network' ) ) {
$to = \WordCamp_Budgets::get_requester_formatted_email( $request->post_author );
$subject_prefix = sprintf( '[%s] ', get_wordcamp_name() );
} else {
$to = '[email protected]';
$subject_prefix = '';
}

if ( ! $to ) {
return;
}

$subject = sprintf( '%sNew note on `%s`', $subject_prefix, sanitize_text_field( $request->post_title ) );
$note_author_name = \WordCamp_Budgets::get_requester_name( $note['author_id'] );
$request_url = admin_url( sprintf( 'post.php?post=%s&action=edit', $request->ID ) );
$headers = array( 'Reply-To: [email protected]' );

$message = sprintf( '
%s has added the following note on the reimbursement request for %s:
%s
You can view the request and respond to their note at:
%s',
sanitize_text_field( $note_author_name ),
sanitize_text_field( $request->post_title ),
sanitize_text_field( $note['message'] ),
esc_url_raw( $request_url )
);
$message = str_replace( "\t", '', $message );

wp_mail( $to, $subject, $message, $headers );
}

/**
* Notify the organizer when the status of their reimbursement changes or when notes are added
*
Expand Down
Loading

0 comments on commit e21ab60

Please sign in to comment.