| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module for WordPress comments handling |
| 4 |
*/ |
| 5 |
|
| 6 |
add_action( 'wp_insert_comment', 'flamingo_insert_comment', 10, 1 ); |
| 7 |
|
| 8 |
/** |
| 9 |
* Creates a Flamingo_Contact record for the given comment. |
| 10 |
*/ |
| 11 |
function flamingo_insert_comment( $comment_id ) { |
| 12 |
$comment = get_comment( $comment_id ); |
| 13 |
|
| 14 |
if ( 1 !== (int) $comment->comment_approved ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
Flamingo_Contact::add( array( |
| 19 |
'email' => $comment->comment_author_email, |
| 20 |
'name' => $comment->comment_author, |
| 21 |
'channel' => 'comment', |
| 22 |
) ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
add_action( 'transition_comment_status', |
| 27 |
'flamingo_transition_comment_status', |
| 28 |
10, 3 |
| 29 |
); |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a Flamingo_Contact record when the comment status changes. |
| 33 |
*/ |
| 34 |
function flamingo_transition_comment_status( $new_status, $old_status, $comment ) { |
| 35 |
if ( 'approved' !== $new_status ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$email = $comment->comment_author_email; |
| 40 |
$name = $comment->comment_author; |
| 41 |
|
| 42 |
Flamingo_Contact::add( array( |
| 43 |
'email' => $email, |
| 44 |
'name' => $name, |
| 45 |
'channel' => 'comment', |
| 46 |
) ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
add_action( 'activate_' . FLAMINGO_PLUGIN_BASENAME, |
| 51 |
'flamingo_collect_contacts_from_comments', |
| 52 |
10, 0 |
| 53 |
); |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates Flamingo_Contact records for existing comments. |
| 57 |
*/ |
| 58 |
function flamingo_collect_contacts_from_comments() { |
| 59 |
$comments = get_comments( array( |
| 60 |
'status' => 'approve', |
| 61 |
'type' => 'comment', |
| 62 |
'number' => 20, |
| 63 |
) ); |
| 64 |
|
| 65 |
foreach ( $comments as $comment ) { |
| 66 |
$email = $comment->comment_author_email; |
| 67 |
$name = $comment->comment_author; |
| 68 |
|
| 69 |
if ( empty( $email ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
Flamingo_Contact::add( array( |
| 74 |
'email' => $email, |
| 75 |
'name' => $name, |
| 76 |
'channel' => 'comment', |
| 77 |
) ); |
| 78 |
} |
| 79 |
} |
| 80 |
|