prefix . 'anycomment_subscriptions'; } /** * Notify subscribers by specified comment. * Expected that specified comment is the new comment. * * @param mixed $comment Comment ID or instance of WP_Comment * * @see WP_Comment for further information. * * @return bool */ public static function notify_by( $comment ) { if ( ! AnyCommentGenericSettings::is_notify_subscribers() ) { return false; } $working_comment = get_comment( $comment ); if ( (int) $working_comment->comment_post_ID === 0 ) { return false; } global $wpdb; $table = static::tableName(); $sql = "SELECT `user_ID`, `email` FROM $table WHERE post_ID=%d AND is_active=1 AND confirmed_at IS NOT NULL"; /** * @var AnyCommentSubscriptions[]|null $subscribers */ $subscribers = $wpdb->get_results( $wpdb->prepare( $sql, [ $working_comment->comment_post_ID ] ) ); if ( empty( $subscribers ) ) { return true; } foreach ( $subscribers as $key => $subscriber ) { AnyCommentEmailQueue::add_as_subscriber_notification( $subscriber->email, $comment ); } return true; } /** * Check whether user subscribed by specified field and search value. * * @param string $email Database field to search for. Should be taken from table schema. * @param int|null $post_id Post ID to check whether user is subsribed to it. Leave empty to check just for email. * * @return bool */ public static function is_subscribed_by( $email, $post_id = null ) { $condition_array = [ $email ]; $condition = "email = %s"; if ( $post_id !== null ) { $condition .= ' AND post_ID = %d'; $condition_array[] = $post_id; } global $wpdb; $tableName = static::tableName(); $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $tableName WHERE $condition", $condition_array ); $count = $wpdb->get_var( $sql ); return $count >= 1; } /** * Inserts new model. * * * @return false|$this */ public function save() { if ( ! isset( $this->user_agent ) ) { $this->user_agent = AnyCommentHelper::get_user_agent(); } if ( ! isset( $this->ip ) ) { $this->ip = AnyCommentHelper::get_user_ip(); } if ( ! isset( $this->created_at ) ) { $this->created_at = time(); } global $wpdb; unset( $this->ID ); $count = $wpdb->insert( static::tableName(), (array) $this ); if ( $count !== false && $count > 0 ) { $lastId = $wpdb->insert_id; if ( empty( $lastId ) ) { return false; } $this->ID = $lastId; return true; } return false; } }