Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DB issues #105

Merged
merged 2 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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