Skip to content

Commit e5ff08d

Browse files
authored
Merge pull request #168 from OpenSourceOrg/fix/flamingo-unspam-166-develop
Send supporter form unspams to supporter CPT - develop
2 parents 0c67e03 + 94d48b0 commit e5ff08d

File tree

1 file changed

+82
-28
lines changed

1 file changed

+82
-28
lines changed

themes/osi/functions.php

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -426,34 +426,6 @@ function osi_wpdc_comment_body( string $comment_body ) {
426426
}
427427
add_filter( 'wpdc_comment_body', 'osi_wpdc_comment_body', 10, 1 );
428428

429-
/**
430-
*
431-
* Create a new Supporter in ACF, based on a Contact Forms 7 submission.
432-
*
433-
*/
434-
add_action('wpcf7_before_send_mail', 'save_form_data_to_cpt');
435-
function save_form_data_to_cpt($contact_form) {
436-
$submission = WPCF7_Submission::get_instance();
437-
if ($submission) {
438-
$data = $submission->get_posted_data();
439-
440-
$post_id = wp_insert_post(array(
441-
'post_title' => $data['your-name'],
442-
'post_type' => 'supporter',
443-
'post_status' => 'pending'
444-
));
445-
update_field('name', $data['your-name'], $post_id);
446-
update_field('organization', $data['your-org'], $post_id);
447-
update_field('endorsement_type', $data['your-endorsement'], $post_id);
448-
update_field('quote', $data['your-message'], $post_id);
449-
} else {
450-
error_log('WPCF7_Submission instance is null.');
451-
}
452-
}
453-
454-
455-
456-
457429
function osi_register_block_template() {
458430
$post_type = 'page'; // Assign the template to pages
459431
$template_slug = 'ai-template';
@@ -496,3 +468,85 @@ function osi_register_block_template() {
496468
});
497469
}
498470
add_action('init', 'osi_register_block_template');
471+
472+
/**
473+
* Process the supporter form submission.
474+
*
475+
* @param WPCF7_ContactForm $contact_form The Contact Form 7 instance.
476+
*
477+
* @return void
478+
*/
479+
function osi_handle_support_form_submission( WPCF7_ContactForm $contact_form ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
480+
$submission = WPCF7_Submission::get_instance();
481+
482+
if ( $submission ) {
483+
$data = $submission->get_posted_data();
484+
485+
osi_save_supporter_form_data_to_cpt( $data );
486+
}
487+
}
488+
add_action( 'wpcf7_before_send_mail', 'osi_handle_support_form_submission' );
489+
490+
/**
491+
* Save supporter form data to a custom post type.
492+
*
493+
* @param array $form_data The form data.
494+
*
495+
* @return void
496+
*/
497+
function osi_save_supporter_form_data_to_cpt( array $form_data ): void {
498+
$post_id = wp_insert_post(
499+
array(
500+
'post_title' => $form_data['your-name'],
501+
'post_type' => 'supporter',
502+
'post_status' => 'pending',
503+
)
504+
);
505+
506+
// If we have a wp_error, abort.
507+
if ( is_wp_error( $post_id ) ) {
508+
return;
509+
}
510+
511+
if ( $post_id ) {
512+
update_field( 'name', $form_data['your-name'], $post_id );
513+
update_field( 'organization', $form_data['your-org'], $post_id );
514+
update_field( 'endorsement_type', $form_data['your-endorsement'], $post_id );
515+
update_field( 'quote', $form_data['your-message'], $post_id );
516+
}
517+
}
518+
519+
/**
520+
* Handle the supporter form spam status change.
521+
*
522+
* @param string $new_status The new status.
523+
* @param string $old_status The old status.
524+
* @param WP_Post $post The post object.
525+
*
526+
* @return void
527+
*/
528+
function osi_handle_supporter_form_flamingo_spam_status_change( string $new_status, string $old_status, WP_Post $post ): void {
529+
if ( 'flamingo_inbound' !== get_post_type( $post ) ) {
530+
return;
531+
}
532+
533+
if ( 'flamingo-spam' === $old_status && 'publish' === $new_status ) {
534+
$term_obj_list = get_the_terms( $post->ID, 'flamingo_inbound_channel' );
535+
536+
if ( empty( $term_obj_list ) || is_wp_error( $term_obj_list ) || 'OSAID Endorsement' !== $term_obj_list[0]->name ) {
537+
return;
538+
}
539+
540+
$form_data = array(
541+
'your-name' => get_post_meta( $post->ID, '_field_your-name', true ),
542+
'your-org' => get_post_meta( $post->ID, '_field_your-org', true ),
543+
'your-endorsement' => get_post_meta( $post->ID, '_field_your-endorsement', true ),
544+
'your-message' => get_post_meta( $post->ID, '_field_your-message', true ),
545+
);
546+
547+
if ( ! empty( $form_data['your-name'] ) ) {
548+
osi_save_supporter_form_data_to_cpt( $form_data );
549+
}
550+
}
551+
}
552+
add_action( 'transition_post_status', 'osi_handle_supporter_form_flamingo_spam_status_change', 10, 3 );

0 commit comments

Comments
 (0)