Skip to content
Open
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: 9 additions & 0 deletions includes/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Texty\Models\SmsStat;
use WeDevs\WPKit\DataLayer\DataLayerFactory;
use Texty\Gateways\GatewayInterface;
use WP_Comment;

/**
* Dispatcher Class
Expand Down Expand Up @@ -76,6 +77,14 @@ public function user_register( $user_id ) {
* @return void
*/
public function new_comment( $comment_id ) {
// The comment may have been deleted, trashed, or flagged as spam
// (e.g. by an anti-spam plugin hooked on `comment_post`) before this
// runs. Bail so we don't render and bill an SMS for a comment that no
// longer exists.
if ( ! get_comment( $comment_id ) instanceof WP_Comment ) {
return;
}

$class = texty()->notifications()->get( 'comment' );
$notifier = new $class();

Expand Down
19 changes: 17 additions & 2 deletions includes/Notifications/WP/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Texty\Notifications\WP;

use Texty\Notifications\Notification;
use WP_Comment;

class Comment extends Notification {

Expand Down Expand Up @@ -52,9 +53,23 @@ public function get_message() {

$comment = get_comment( $this->comment_id );

// The comment can vanish between the `comment_post` hook and the send
// (deleted, trashed, or spam). Bail to the global-key pass instead of
// dereferencing null and rendering an empty-bodied message.
if ( ! $comment instanceof WP_Comment ) {
return $this->replace_global_keys( $message );
}

foreach ( $this->replacement_keys() as $search => $value ) {
$value = ( $search === 'post_url' ) ? get_permalink( $comment->comment_post_ID ) : $comment->$value;
$message = str_replace( '{' . $search . '}', $value, $message );
// `post_url` has no comment property; everything else reads off the
// comment via WP_Comment's magic getter (e.g. `post_title` proxies
// to the post). Coerce to string so a missing value never reaches
// str_replace as null (deprecated since PHP 8.1).
$replacement = ( 'post_url' === $search )
? (string) get_permalink( $comment->comment_post_ID )
: (string) ( $comment->$value ?? '' );

$message = str_replace( '{' . $search . '}', $replacement, $message );
}

$message = $this->replace_global_keys( $message );
Expand Down
Loading