Skip to content

Commit 74325d9

Browse files
authored
Merge pull request #167 from OpenSourceOrg/fix/flamingo-unspam-166-main
Send supporter form unspams to supporter CPT - main
2 parents a9f045a + 69c957a commit 74325d9

1 file changed

Lines changed: 63 additions & 17 deletions

File tree

themes/osi/functions.php

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -427,37 +427,83 @@ function osi_wpdc_comment_body( string $comment_body ) {
427427
add_filter( 'wpdc_comment_body', 'osi_wpdc_comment_body', 10, 1 );
428428

429429
/**
430-
* Save form data to a custom post type.
430+
* Process the supporter form submission.
431431
*
432432
* @param WPCF7_ContactForm $contact_form The Contact Form 7 instance.
433433
*
434434
* @return void
435435
*/
436-
function osi_save_form_data_to_cpt( WPCF7_ContactForm $contact_form ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
436+
function osi_handle_support_form_submission( WPCF7_ContactForm $contact_form ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
437437
$submission = WPCF7_Submission::get_instance();
438438

439439
if ( $submission ) {
440440
$data = $submission->get_posted_data();
441441

442-
$post_id = wp_insert_post(
443-
array(
444-
'post_title' => $data['your-name'],
445-
'post_type' => 'supporter',
446-
'post_status' => 'pending',
447-
)
448-
);
442+
osi_save_supporter_form_data_to_cpt( $data );
443+
}
444+
}
445+
add_action( 'wpcf7_before_send_mail', 'osi_handle_support_form_submission' );
446+
447+
/**
448+
* Save supporter form data to a custom post type.
449+
*
450+
* @param array $form_data The form data.
451+
*
452+
* @return void
453+
*/
454+
function osi_save_supporter_form_data_to_cpt( array $form_data ): void {
455+
$post_id = wp_insert_post(
456+
array(
457+
'post_title' => $form_data['your-name'],
458+
'post_type' => 'supporter',
459+
'post_status' => 'pending',
460+
)
461+
);
449462

450-
// If we have a wp_error, abort.
451-
if ( is_wp_error( $post_id ) ) {
463+
// If we have a wp_error, abort.
464+
if ( is_wp_error( $post_id ) ) {
465+
return;
466+
}
467+
468+
if ( $post_id ) {
469+
update_field( 'name', $form_data['your-name'], $post_id );
470+
update_field( 'organization', $form_data['your-org'], $post_id );
471+
update_field( 'endorsement_type', $form_data['your-endorsement'], $post_id );
472+
update_field( 'quote', $form_data['your-message'], $post_id );
473+
}
474+
}
475+
476+
/**
477+
* Handle the supporter form spam status change.
478+
*
479+
* @param string $new_status The new status.
480+
* @param string $old_status The old status.
481+
* @param WP_Post $post The post object.
482+
*
483+
* @return void
484+
*/
485+
function osi_handle_supporter_form_flamingo_spam_status_change( string $new_status, string $old_status, WP_Post $post ): void {
486+
if ( 'flamingo_inbound' !== get_post_type( $post ) ) {
487+
return;
488+
}
489+
490+
if ( 'flamingo-spam' === $old_status && 'publish' === $new_status ) {
491+
$term_obj_list = get_the_terms( $post->ID, 'flamingo_inbound_channel' );
492+
493+
if ( empty( $term_obj_list ) || is_wp_error( $term_obj_list ) || 'OSAID Endorsement' !== $term_obj_list[0]->name ) {
452494
return;
453495
}
454496

455-
if ( $post_id ) {
456-
update_field( 'name', $data['your-name'], $post_id );
457-
update_field( 'organization', $data['your-org'], $post_id );
458-
update_field( 'endorsement_type', $data['your-endorsement'], $post_id );
459-
update_field( 'quote', $data['your-message'], $post_id );
497+
$form_data = array(
498+
'your-name' => get_post_meta( $post->ID, '_field_your-name', true ),
499+
'your-org' => get_post_meta( $post->ID, '_field_your-org', true ),
500+
'your-endorsement' => get_post_meta( $post->ID, '_field_your-endorsement', true ),
501+
'your-message' => get_post_meta( $post->ID, '_field_your-message', true ),
502+
);
503+
504+
if ( ! empty( $form_data['your-name'] ) ) {
505+
osi_save_supporter_form_data_to_cpt( $form_data );
460506
}
461507
}
462508
}
463-
add_action( 'wpcf7_before_send_mail', 'osi_save_form_data_to_cpt' );
509+
add_action( 'transition_post_status', 'osi_handle_supporter_form_flamingo_spam_status_change', 10, 3 );

0 commit comments

Comments
 (0)