Skip to content

Commit

Permalink
Merge pull request #105 from jdevalk/jdv/fix-db-issues
Browse files Browse the repository at this point in the history
Fix DB issues
  • Loading branch information
jdevalk authored Dec 9, 2022
2 parents 54faa20 + 8663f5b commit b7db85b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
9 changes: 0 additions & 9 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,4 @@
</properties>
</rule>

<!-- Direct database queries need careful review.
Ticket: https://github.com/Yoast/comment-hacks/issues/50
-->
<rule ref="WordPress.DB.DirectDatabaseQuery">
<exclude-pattern>/admin/comment-parent\.php$</exclude-pattern>
<exclude-pattern>/inc/clean-emails\.php$</exclude-pattern>
<exclude-pattern>/inc/email-links\.php$</exclude-pattern>
</rule>

</ruleset>
5 changes: 3 additions & 2 deletions admin/comment-parent.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public function update_comment_parent() {
}

if ( $comment_id ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_parent = %d WHERE comment_ID = %d", $comment_parent, $comment_id ) );
$comment = get_comment( $comment_id );
$comment->comment_parent = $comment_parent;
wp_update_comment( $comment );
}
}
}
3 changes: 1 addition & 2 deletions inc/clean-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ private function setup_data( int $comment_id ): void {
* Adds a sentence about the number of comments awaiting moderation.
*/
private function get_moderation_msg(): void {
global $wpdb;
$comments_waiting = $wpdb->get_var( "SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'" );
$comments_waiting = get_comment_count()['awaiting_moderation'];

if ( $comments_waiting > 1 ) {
--$comments_waiting;
Expand Down
24 changes: 17 additions & 7 deletions inc/email-links.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,33 @@ public function admin_bar_comment_link(): void {

$current_user = \wp_get_current_user();

$results = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT comment_author_email FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_type = 'comment' AND comment_approved = '1'", $post->ID ) );

if ( \count( $results ) === 0 ) {
$comments = get_comments(
[
'post_id' => $post->ID,
'type' => 'comment',
'status' => 'approve',
]
);
if ( count( $comments ) === 0 ) {
return;
}
$emails = [];
foreach ( $comments as $comment ) {
$emails[] = $comment->comment_author_email;
}
$emails = array_unique( $emails );

$url = 'mailto:' . $current_user->user_email . '?bcc=';
foreach ( $results as $comment ) {
if ( $comment->comment_author_email !== $current_user->user_email ) {
$url .= \rawurlencode( $comment->comment_author_email . ',' );
foreach ( $emails as $email ) {
if ( $email !== $current_user->user_email ) {
$url .= \rawurlencode( $email . ',' );
}
}
$url .= '&subject=' . $this->replace_variables( $this->options['email_subject'], false, $post->ID );
$url .= '&body=' . $this->replace_variables( $this->options['mass_email_body'], false, $post->ID );

// We can't set the 'href' attribute to the $url as then esc_url would garble the mailto link.
// So we do a nasty bit of JS workaround. The reason we grab the a href from the alternate link is
// So we do a nasty bit of JS workaround. The reason we grab the href from the alternate link is
// so browser extensions like the Google Mail one that change mailto: links still work.
echo '<a href="' . \esc_attr( $url ) . '" id="yst_email_commenters_alternate"></a><script>
function yst_email_commenters(e){
Expand Down

0 comments on commit b7db85b

Please sign in to comment.