addreplyintopic.php
10 months ago
addtopicinforum.php
10 months ago
subscribeuserinforum.php
10 months ago
addreplyintopic.php
337 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AddReplyInTopic. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category AddTopicInForum |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | use SureTriggers\Integrations\AutomateAction; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | use SureTriggers\Integrations\WordPress\WordPress; |
| 17 | /** |
| 18 | * AddTopicInForum |
| 19 | * |
| 20 | * @category AddTopicInForum |
| 21 | * @package SureTriggers |
| 22 | * @author BSF <username@example.com> |
| 23 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 24 | * @link https://www.brainstormforce.com/ |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class AddReplyInTopic extends AutomateAction { |
| 28 | |
| 29 | /** |
| 30 | * Integration type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $integration = 'bbPress'; |
| 35 | |
| 36 | /** |
| 37 | * Action name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $action = 'bbp_add_reply_in_topic'; |
| 42 | |
| 43 | use SingletonLoader; |
| 44 | |
| 45 | /** |
| 46 | * Register a action. |
| 47 | * |
| 48 | * @param array $actions actions. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function register( $actions ) { |
| 52 | $actions[ $this->integration ][ $this->action ] = [ |
| 53 | 'label' => __( 'Add Reply In Topic', 'suretriggers' ), |
| 54 | 'action' => $this->action, |
| 55 | 'function' => [ $this, 'action_listener' ], |
| 56 | ]; |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id user_id. |
| 64 | * @param int $automation_id automation_id. |
| 65 | * @param array $fields fields. |
| 66 | * @param array $selected_options selectedOptions. |
| 67 | * @psalm-suppress UndefinedMethod |
| 68 | * @throws Exception Exception. |
| 69 | * |
| 70 | * @return array|bool|void |
| 71 | */ |
| 72 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 73 | if ( ! class_exists( 'bbPress' ) ) { |
| 74 | return [ |
| 75 | 'status' => 'error', |
| 76 | 'message' => __( 'bbPress class not found.', 'suretriggers' ), |
| 77 | ]; |
| 78 | } |
| 79 | if ( ! function_exists( 'bbp_get_topic' ) || ! function_exists( 'bbp_get_forum' ) || ! function_exists( 'bbp_is_forum_category' ) || ! function_exists( 'bbp_get_reply_post_type' ) || ! function_exists( 'bbp_get_topic_reply_count' ) ) { |
| 80 | return [ |
| 81 | 'status' => 'error', |
| 82 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 83 | ]; |
| 84 | } |
| 85 | $user_id = $selected_options['wp_user_email']; |
| 86 | if ( is_email( $user_id ) ) { |
| 87 | $user = get_user_by( 'email', $user_id ); |
| 88 | if ( $user ) { |
| 89 | $user_id = $user->ID; |
| 90 | } |
| 91 | } else { |
| 92 | $error = [ |
| 93 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 94 | 'response' => esc_attr__( 'Please enter valid email address.', 'suretriggers' ), |
| 95 | ]; |
| 96 | |
| 97 | return $error; |
| 98 | } |
| 99 | $forum_id = $selected_options['forum']; |
| 100 | $topic_id = $selected_options['topic']; |
| 101 | $reply_description = $selected_options['reply_content']; |
| 102 | $anonymous_data = 0; |
| 103 | |
| 104 | if ( ! bbp_get_topic( $topic_id ) ) { |
| 105 | return [ |
| 106 | 'status' => 'error', |
| 107 | 'message' => 'Sorry, Discussion does not exist', |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | if ( ! bbp_get_forum( $forum_id ) ) { |
| 112 | return [ |
| 113 | 'status' => 'error', |
| 114 | 'message' => 'Sorry, Forum does not exist', |
| 115 | ]; |
| 116 | } |
| 117 | // Forum exists. |
| 118 | if ( ! empty( $forum_id ) ) { |
| 119 | |
| 120 | // Forum is a category. |
| 121 | if ( bbp_is_forum_category( $forum_id ) ) { |
| 122 | return [ |
| 123 | 'status' => 'error', |
| 124 | 'message' => 'Sorry, This forum is a category. No discussions can be created in this forum', |
| 125 | ]; |
| 126 | // Forum is not a category. |
| 127 | } else { |
| 128 | |
| 129 | // Forum is closed and user cannot access. |
| 130 | if ( function_exists( 'bbp_is_forum_closed' ) && bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) { |
| 131 | return [ |
| 132 | 'status' => 'error', |
| 133 | 'message' => 'Sorry, This forum has been closed to new discussions', |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Added logic for group forum |
| 139 | * Current user is part of that group or not. |
| 140 | * We need to check manually because bbpress updating that caps only on group forum page and |
| 141 | * in API those conditional tag will not work. |
| 142 | */ |
| 143 | $is_member = false; |
| 144 | $group_ids = []; |
| 145 | if ( function_exists( 'bbp_get_forum_group_ids' ) ) { |
| 146 | $group_ids = bbp_get_forum_group_ids( $forum_id ); |
| 147 | |
| 148 | foreach ( $group_ids as $group_id ) { |
| 149 | if ( function_exists( 'groups_is_user_member' ) && groups_is_user_member( $user_id, $group_id ) ) { |
| 150 | $is_member = true; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Forum is private and user cannot access. |
| 157 | if ( function_exists( 'bbp_is_forum_private' ) && bbp_is_forum_private( $forum_id ) && function_exists( 'bbp_is_user_keymaster' ) && ! bbp_is_user_keymaster() ) { |
| 158 | if ( |
| 159 | ( empty( $group_ids ) && ! current_user_can( 'read_private_forums' ) ) |
| 160 | || ( ! empty( $group_ids ) && ! $is_member ) |
| 161 | ) { |
| 162 | return [ |
| 163 | 'status' => 'error', |
| 164 | 'message' => 'Sorry, This forum is private and you do not have the capability to read or create new discussions in it', |
| 165 | ]; |
| 166 | } |
| 167 | |
| 168 | // Forum is hidden and user cannot access. |
| 169 | } elseif ( function_exists( 'bbp_is_forum_hidden' ) && bbp_is_forum_hidden( $forum_id ) && function_exists( 'bbp_is_user_keymaster' ) && ! bbp_is_user_keymaster() ) { |
| 170 | if ( |
| 171 | ( empty( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) ) |
| 172 | || ( ! empty( $group_ids ) && ! $is_member ) |
| 173 | ) { |
| 174 | return [ |
| 175 | 'status' => 'error', |
| 176 | 'message' => 'Sorry, This forum is hidden and you do not have the capability to read or create new discussions in it', |
| 177 | ]; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** Unfiltered HTML */ |
| 184 | // Remove kses filters from title and content for capable users and if the nonce is verified. |
| 185 | if ( current_user_can( 'unfiltered_html' ) ) { |
| 186 | remove_filter( 'bbp_new_reply_pre_title', 'wp_filter_kses' ); |
| 187 | remove_filter( 'bbp_new_reply_pre_content', 'bbp_encode_bad', 10 ); |
| 188 | remove_filter( 'bbp_new_reply_pre_content', 'bbp_filter_kses', 30 ); |
| 189 | } |
| 190 | |
| 191 | // Filter and sanitize. |
| 192 | $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_description ); |
| 193 | |
| 194 | /** Reply Duplicate */ |
| 195 | if ( function_exists( 'bbp_check_for_duplicate' ) && ! bbp_check_for_duplicate( |
| 196 | [ |
| 197 | 'post_type' => bbp_get_reply_post_type(), |
| 198 | 'post_author' => $user_id, |
| 199 | 'post_content' => $reply_content, |
| 200 | 'post_parent' => $topic_id, |
| 201 | 'anonymous_data' => $anonymous_data, |
| 202 | ] |
| 203 | ) ) { |
| 204 | return [ |
| 205 | 'status' => 'error', |
| 206 | 'message' => "Duplicate reply detected; it looks as though you've already said that!", |
| 207 | ]; |
| 208 | } |
| 209 | |
| 210 | /** Topic Closed */ |
| 211 | // If topic is closed, moderators can still reply. |
| 212 | if ( function_exists( 'bbp_is_topic_closed' ) && bbp_is_topic_closed( $topic_id ) && ! current_user_can( 'moderate' ) ) { |
| 213 | return [ |
| 214 | 'status' => 'error', |
| 215 | 'message' => 'Sorry, Discussion is closed.', |
| 216 | ]; |
| 217 | } |
| 218 | |
| 219 | /** Reply Blacklist */ |
| 220 | if ( function_exists( 'bbp_check_for_blacklist' ) && ! bbp_check_for_blacklist( $anonymous_data, $user_id, '', $reply_content ) ) { |
| 221 | return [ |
| 222 | 'status' => 'error', |
| 223 | 'message' => 'Sorry, Your reply cannot be created at this time.', |
| 224 | ]; |
| 225 | } |
| 226 | |
| 227 | /** Reply Status */ |
| 228 | // Maybe put into moderation. |
| 229 | $reply_status = ''; |
| 230 | if ( function_exists( 'bbp_check_for_moderation' ) && function_exists( 'bbp_get_pending_status_id' ) && ! bbp_check_for_moderation( $anonymous_data, $user_id, '', $reply_content ) ) { |
| 231 | $reply_status = bbp_get_pending_status_id(); |
| 232 | } elseif ( function_exists( 'bbp_get_public_status_id' ) ) { |
| 233 | $reply_status = bbp_get_public_status_id(); |
| 234 | } |
| 235 | |
| 236 | /** Topic Closed */ |
| 237 | // If topic is closed, moderators can still reply. |
| 238 | if ( function_exists( 'bbp_is_topic_closed' ) && bbp_is_topic_closed( $topic_id ) && ! current_user_can( 'moderate' ) ) { |
| 239 | return [ |
| 240 | 'status' => 'error', |
| 241 | 'message' => 'Sorry, Discussion is closed.', |
| 242 | ]; |
| 243 | } |
| 244 | |
| 245 | /** No Errors */ |
| 246 | |
| 247 | // Add the content of the form to $reply_data as an array. |
| 248 | // Just in time manipulation of reply data before being created. |
| 249 | $reply_data = apply_filters( |
| 250 | 'bbp_new_reply_pre_insert', |
| 251 | [ |
| 252 | 'post_author' => $user_id, |
| 253 | 'post_title' => '', |
| 254 | 'post_content' => $reply_content, |
| 255 | 'post_status' => $reply_status, |
| 256 | 'post_parent' => $topic_id, |
| 257 | 'post_type' => bbp_get_reply_post_type(), |
| 258 | 'comment_status' => 'closed', |
| 259 | 'menu_order' => bbp_get_topic_reply_count( $topic_id, false ) + 1, |
| 260 | ] |
| 261 | ); |
| 262 | |
| 263 | // Insert reply. |
| 264 | $reply_id = wp_insert_post( $reply_data ); |
| 265 | |
| 266 | if ( empty( $reply_id ) ) { |
| 267 | return [ |
| 268 | 'status' => 'error', |
| 269 | 'message' => 'We are facing a problem to creating a reply', |
| 270 | ]; |
| 271 | } |
| 272 | |
| 273 | /** Trash Check */ |
| 274 | // If this reply starts as trash, add it to pre_trashed_replies. |
| 275 | // for the topic, so it is properly restored. |
| 276 | if ( ( function_exists( 'bbp_is_topic_trash' ) && bbp_is_topic_trash( $topic_id ) ) || ( function_exists( 'bbp_get_trash_status_id' ) && bbp_get_trash_status_id() === $reply_data['post_status'] ) ) { |
| 277 | |
| 278 | // Trash the reply. |
| 279 | wp_trash_post( $reply_id ); |
| 280 | |
| 281 | // Only add to pre-trashed array if topic is trashed. |
| 282 | if ( function_exists( 'bbp_is_topic_trash' ) && bbp_is_topic_trash( $topic_id ) ) { |
| 283 | |
| 284 | // Get pre_trashed_replies for topic. |
| 285 | $pre_trashed_replies = (array) get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true ); |
| 286 | |
| 287 | // Add this reply to the end of the existing replies. |
| 288 | $pre_trashed_replies[] = $reply_id; |
| 289 | |
| 290 | // Update the pre_trashed_reply post meta. |
| 291 | update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies ); |
| 292 | } |
| 293 | |
| 294 | /** Spam Check */ |
| 295 | // If reply or topic are spam, officially spam this reply. |
| 296 | } elseif ( function_exists( 'bbp_get_public_status_id' ) && ( ( function_exists( 'bbp_is_topic_spam' ) && bbp_is_topic_spam( $topic_id ) ) || ( function_exists( 'bbp_get_spam_status_id' ) && bbp_get_spam_status_id() === $reply_data['post_status'] ) ) ) { |
| 297 | add_post_meta( $reply_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); |
| 298 | |
| 299 | // Only add to pre-spammed array if topic is spam. |
| 300 | if ( function_exists( 'bbp_is_topic_spam' ) && bbp_is_topic_spam( $topic_id ) ) { |
| 301 | |
| 302 | // Get pre_spammed_replies for topic. |
| 303 | $pre_spammed_replies = (array) get_post_meta( $topic_id, '_bbp_pre_spammed_replies', true ); |
| 304 | |
| 305 | // Add this reply to the end of the existing replies. |
| 306 | $pre_spammed_replies[] = $reply_id; |
| 307 | |
| 308 | // Update the pre_spammed_replies post meta. |
| 309 | update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies ); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Removed notification sent and called additionally. |
| 315 | * Due to we have moved all filters on title and content. |
| 316 | */ |
| 317 | remove_action( 'bbp_new_reply', 'bbp_notify_topic_subscribers', 11 ); |
| 318 | |
| 319 | /** Update counts, etc... */ |
| 320 | do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $user_id, false, 0 ); |
| 321 | |
| 322 | /** Additional Actions (After Save) */ |
| 323 | do_action( 'bbp_new_reply_post_extras', $reply_id ); |
| 324 | |
| 325 | $context = [ |
| 326 | 'user_email' => $selected_options['wp_user_email'], |
| 327 | 'forum_title' => get_the_title( $forum_id ), |
| 328 | 'forum_link' => get_the_permalink( $forum_id, true ), |
| 329 | 'reply_description' => $reply_content, |
| 330 | 'reply_link' => get_the_permalink( $reply_id, true ), |
| 331 | ]; |
| 332 | return $context; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | AddReplyInTopic::get_instance(); |
| 337 |