addreplyintopic.php
10 months ago
addtopicinforum.php
10 months ago
subscribeuserinforum.php
10 months ago
addtopicinforum.php
286 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AddTopicInForum. |
| 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 | /** |
| 19 | * AddTopicInForum |
| 20 | * |
| 21 | * @category AddTopicInForum |
| 22 | * @package SureTriggers |
| 23 | * @author BSF <username@example.com> |
| 24 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 25 | * @link https://www.brainstormforce.com/ |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | class AddTopicInForum extends AutomateAction { |
| 29 | |
| 30 | /** |
| 31 | * Integration type. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $integration = 'bbPress'; |
| 36 | |
| 37 | /** |
| 38 | * Action name. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $action = 'add_topic_to_forum'; |
| 43 | |
| 44 | use SingletonLoader; |
| 45 | |
| 46 | /** |
| 47 | * Register a action. |
| 48 | * |
| 49 | * @param array $actions actions. |
| 50 | * @return array |
| 51 | */ |
| 52 | public function register( $actions ) { |
| 53 | $actions[ $this->integration ][ $this->action ] = [ |
| 54 | 'label' => __( 'Add Topic to Forum', 'suretriggers' ), |
| 55 | 'action' => $this->action, |
| 56 | 'function' => [ $this, 'action_listener' ], |
| 57 | ]; |
| 58 | return $actions; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Action listener. |
| 63 | * |
| 64 | * @param int $user_id user_id. |
| 65 | * @param int $automation_id automation_id. |
| 66 | * @param array $fields fields. |
| 67 | * @param array $selected_options selectedOptions. |
| 68 | * @psalm-suppress UndefinedMethod |
| 69 | * @throws Exception Exception. |
| 70 | * |
| 71 | * @return array|bool|void |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | if ( ! class_exists( 'bbPress' ) ) { |
| 75 | return [ |
| 76 | 'status' => 'error', |
| 77 | 'message' => __( 'bbPress class not found.', 'suretriggers' ), |
| 78 | ]; |
| 79 | } |
| 80 | if ( ! function_exists( 'bbp_get_topic_post_type' ) || ! 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' ) ) { |
| 81 | return [ |
| 82 | 'status' => 'error', |
| 83 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | $user_id = $selected_options['wp_user_email']; |
| 88 | if ( is_email( $user_id ) ) { |
| 89 | $user = get_user_by( 'email', $user_id ); |
| 90 | if ( $user ) { |
| 91 | $user_id = $user->ID; |
| 92 | } |
| 93 | } else { |
| 94 | $error = [ |
| 95 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 96 | 'response' => esc_attr__( 'Please enter valid email address.', 'suretriggers' ), |
| 97 | ]; |
| 98 | |
| 99 | return $error; |
| 100 | } |
| 101 | $forum_id = $selected_options['forum']; |
| 102 | $topic_title = $selected_options['topic_title']; |
| 103 | $topic_description = $selected_options['topic_content']; |
| 104 | $anonymous_data = 0; |
| 105 | |
| 106 | if ( ! empty( $forum_id ) ) { |
| 107 | |
| 108 | // Forum is a category. |
| 109 | if ( bbp_is_forum_category( $forum_id ) ) { |
| 110 | return [ |
| 111 | 'status' => 'error', |
| 112 | 'message' => 'Sorry, This forum is a category. No discussions can be created in this forum.', |
| 113 | ]; |
| 114 | |
| 115 | // Forum is not a category. |
| 116 | } else { |
| 117 | |
| 118 | // Forum is closed and user cannot access. |
| 119 | if ( function_exists( 'bbp_is_forum_closed' ) && bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) { |
| 120 | return [ |
| 121 | 'status' => 'error', |
| 122 | 'message' => 'Sorry, This forum has been closed to new discussions.', |
| 123 | ]; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Added logic for group forum |
| 128 | * Current user is part of that group or not. |
| 129 | * We need to check manually because bbpress updating that caps only on group forum page and |
| 130 | * in API those conditional tag will not work. |
| 131 | */ |
| 132 | $is_member = false; |
| 133 | $group_ids = []; |
| 134 | if ( function_exists( 'bbp_get_forum_group_ids' ) ) { |
| 135 | $group_ids = bbp_get_forum_group_ids( $forum_id ); |
| 136 | if ( ! empty( $group_ids ) ) { |
| 137 | foreach ( $group_ids as $group_id ) { |
| 138 | if ( function_exists( 'groups_is_user_member' ) && groups_is_user_member( $user_id, $group_id ) ) { |
| 139 | $is_member = true; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Forum is private and user cannot access. |
| 147 | if ( function_exists( 'bbp_is_forum_private' ) && bbp_is_forum_private( $forum_id ) && function_exists( 'bbp_is_user_keymaster' ) && ! bbp_is_user_keymaster() ) { |
| 148 | if ( |
| 149 | ( empty( $group_ids ) && ! current_user_can( 'read_private_forums' ) ) |
| 150 | || ( ! empty( $group_ids ) && ! $is_member ) |
| 151 | ) { |
| 152 | return [ |
| 153 | 'status' => 'error', |
| 154 | 'message' => 'Sorry, This forum is private and you do not have the capability to read or create new discussions in it.', |
| 155 | ]; |
| 156 | } |
| 157 | |
| 158 | // Forum is hidden and user cannot access. |
| 159 | } elseif ( function_exists( 'bbp_is_forum_hidden' ) && bbp_is_forum_hidden( $forum_id ) && function_exists( 'bbp_is_user_keymaster' ) && ! bbp_is_user_keymaster() ) { |
| 160 | if ( |
| 161 | ( empty( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) ) |
| 162 | || ( ! empty( $group_ids ) && ! $is_member ) |
| 163 | ) { |
| 164 | $action_data['complete_with_errors'] = true; |
| 165 | return [ |
| 166 | 'status' => 'error', |
| 167 | 'message' => 'Sorry, This forum is hidden and you do not have the capability to read or create new discussions in it.', |
| 168 | ]; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if ( function_exists( 'bbp_check_for_duplicate' ) && ! bbp_check_for_duplicate( |
| 175 | [ |
| 176 | 'post_type' => bbp_get_topic_post_type(), |
| 177 | 'post_author' => $user_id, |
| 178 | 'post_content' => $topic_description, |
| 179 | 'anonymous_data' => $anonymous_data, |
| 180 | ] |
| 181 | ) ) { |
| 182 | |
| 183 | return [ |
| 184 | 'status' => 'error', |
| 185 | 'message' => "Duplicate discussion detected; it looks as though you've already said that!.", |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | /** Topic Blacklist */ |
| 190 | if ( function_exists( 'bbp_check_for_blacklist' ) && ! bbp_check_for_blacklist( $anonymous_data, $user_id, $topic_title, $topic_description ) ) { |
| 191 | |
| 192 | return [ |
| 193 | 'status' => 'error', |
| 194 | 'message' => 'Sorry, Your discussion cannot be created at this time.', |
| 195 | ]; |
| 196 | } |
| 197 | |
| 198 | /** Topic Status */ |
| 199 | $topic_status = ''; |
| 200 | // Maybe put into moderation. |
| 201 | if ( function_exists( 'bbp_get_pending_status_id' ) && function_exists( 'bbp_check_for_moderation' ) && ! bbp_check_for_moderation( $anonymous_data, $user_id, $topic_title, $topic_description ) ) { |
| 202 | $topic_status = bbp_get_pending_status_id(); |
| 203 | |
| 204 | } elseif ( function_exists( 'bbp_get_public_status_id' ) ) { |
| 205 | $topic_status = bbp_get_public_status_id(); |
| 206 | } |
| 207 | /** No Errors */ |
| 208 | // Add the content of the form to $topic_data as an array. |
| 209 | // Just in time manipulation of topic data before being created. |
| 210 | $topic_data = apply_filters( |
| 211 | 'bbp_new_topic_pre_insert', |
| 212 | [ |
| 213 | 'post_author' => $user_id, |
| 214 | 'post_title' => $topic_title, |
| 215 | 'post_content' => $topic_description, |
| 216 | 'post_status' => $topic_status, |
| 217 | 'post_parent' => $forum_id, |
| 218 | 'post_type' => bbp_get_topic_post_type(), |
| 219 | 'tax_input' => [], |
| 220 | 'comment_status' => 'closed', |
| 221 | ] |
| 222 | ); |
| 223 | |
| 224 | // Insert topic. |
| 225 | $topic_id = wp_insert_post( $topic_data ); |
| 226 | |
| 227 | if ( empty( $topic_id ) ) { |
| 228 | $append_error = 'We are facing a problem to creating a topic.'; |
| 229 | return [ |
| 230 | 'status' => 'error', |
| 231 | 'message' => $append_error, |
| 232 | ]; |
| 233 | |
| 234 | } |
| 235 | |
| 236 | /** Trash Check */ |
| 237 | // If the forum is trash, or the topic_status is switched to. |
| 238 | // trash, trash it properly. |
| 239 | if ( function_exists( 'bbp_get_trash_status_id' ) && ( bbp_get_trash_status_id() === get_post_field( 'post_status', $forum_id ) |
| 240 | || bbp_get_trash_status_id() === $topic_data['post_status'] ) ) { |
| 241 | |
| 242 | // Trash the reply. |
| 243 | wp_trash_post( $topic_id ); |
| 244 | } |
| 245 | |
| 246 | /** Spam Check */ |
| 247 | // If reply or topic are spam, officially spam this reply. |
| 248 | if ( function_exists( 'bbp_get_public_status_id' ) && ( function_exists( 'bbp_get_spam_status_id' ) && bbp_get_spam_status_id() === $topic_data['post_status'] ) ) { |
| 249 | add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Removed notification sent and called additionally. |
| 254 | * Due to we have moved all filters on title and content. |
| 255 | */ |
| 256 | remove_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11 ); |
| 257 | |
| 258 | /** Update counts, etc... */ |
| 259 | do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $user_id ); |
| 260 | |
| 261 | /** Additional Actions (After Save) */ |
| 262 | do_action( 'bbp_new_topic_post_extras', $topic_id ); |
| 263 | |
| 264 | if ( function_exists( 'bbp_notify_forum_subscribers' ) ) { |
| 265 | /** |
| 266 | * Sends notification emails for new topics to subscribed forums. |
| 267 | */ |
| 268 | bbp_notify_forum_subscribers( $topic_id, $forum_id, $anonymous_data, $user_id ); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | $context = [ |
| 273 | 'user_email' => $selected_options['wp_user_email'], |
| 274 | 'forum_title' => get_the_title( $forum_id ), |
| 275 | 'forum_link' => get_the_permalink( $forum_id ), |
| 276 | 'topic_title' => $topic_title, |
| 277 | 'topic_description' => $topic_description, |
| 278 | 'topic_lonk' => get_the_permalink( $topic_id ), |
| 279 | ]; |
| 280 | |
| 281 | return $context; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | AddTopicInForum::get_instance(); |
| 286 |