add-user-to-group.php
10 months ago
remove-user-from-group.php
10 months ago
set-user-reputation.php
10 months ago
wf-create-topic.php
10 months ago
wf-delete-topic.php
10 months ago
wf-create-topic.php
194 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WfCreateTopic. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category WfCreateTopic |
| 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 | namespace SureTriggers\Integrations\wpForo\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use SureTriggers\Integrations\WordPress\WordPress; |
| 19 | use Exception; |
| 20 | use wpforo\classes\Members; |
| 21 | use wpforo\classes\Permissions; |
| 22 | |
| 23 | /** |
| 24 | * WfCreateTopic |
| 25 | * |
| 26 | * @category WfCreateTopic |
| 27 | * @package SureTriggers |
| 28 | * @author BSF <username@example.com> |
| 29 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 30 | * @link https://www.brainstormforce.com/ |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | class WfCreateTopic extends AutomateAction { |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Integration type. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $integration = 'wpForo'; |
| 42 | |
| 43 | /** |
| 44 | * Action name. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $action = 'wp_foro_create_topic'; |
| 49 | |
| 50 | use SingletonLoader; |
| 51 | |
| 52 | /** |
| 53 | * Register an action. |
| 54 | * |
| 55 | * @param array $actions actions. |
| 56 | * @return array |
| 57 | */ |
| 58 | public function register( $actions ) { |
| 59 | $actions[ $this->integration ][ $this->action ] = [ |
| 60 | 'label' => __( 'Create Topic', 'suretriggers' ), |
| 61 | 'action' => $this->action, |
| 62 | 'function' => [ $this, 'action_listener' ], |
| 63 | ]; |
| 64 | return $actions; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Action listener. |
| 69 | * |
| 70 | * @param int $user_id user id. |
| 71 | * @param int $automation_id automation_id. |
| 72 | * @param array $fields template fields. |
| 73 | * @param array $selected_options saved template data. |
| 74 | * @throws Exception Exception. |
| 75 | * |
| 76 | * @return bool|array |
| 77 | */ |
| 78 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 79 | |
| 80 | $user_email = $selected_options['wp_user_email']; |
| 81 | |
| 82 | if ( ! function_exists( 'WPF' ) || ! class_exists( 'wpforo\classes\Members' ) || ! function_exists( 'wpfkey' ) || |
| 83 | ! function_exists( 'wpfval' ) || ! function_exists( 'wpforo_length' ) || ! function_exists( 'wpforo_setting' ) || ! class_exists( 'wpforo\classes\Permissions' ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if ( is_email( $user_email ) ) { |
| 88 | $user = get_user_by( 'email', $user_email ); |
| 89 | if ( $user ) { |
| 90 | $user_id = $user->ID; |
| 91 | WPF()->current_user = $user; |
| 92 | WPF()->current_userid = $user->ID; |
| 93 | WPF()->current_user_login = $user->user_login; |
| 94 | WPF()->current_user_email = $user->user_email; |
| 95 | WPF()->current_user_display_name = $user->display_name; |
| 96 | $member = new Members(); |
| 97 | $user = $member->get_member( $user->ID ); |
| 98 | if ( ! wpfkey( $user, 'groupid' ) || ! method_exists( $member, 'get_member' ) || ! method_exists( $member, 'synchronize_user' ) ) { |
| 99 | $member->synchronize_user( $user->ID ); |
| 100 | $user = $member->get_member( $user->ID ); |
| 101 | } |
| 102 | $user_meta = get_user_meta( $user->ID ); |
| 103 | WPF()->current_user = $user; |
| 104 | WPF()->current_usermeta = $user_meta; |
| 105 | WPF()->current_user_groupid = WPF()->current_user['groupid']; |
| 106 | WPF()->current_user_secondary_groupids = WPF()->current_user['secondary_groupids']; |
| 107 | WPF()->current_user_groupids = array_unique( array_filter( array_merge( (array) WPF()->current_user_groupid, (array) WPF()->current_user_secondary_groupids ) ) ); |
| 108 | WPF()->current_user_status = (string) wpfval( $user, 'status' ); |
| 109 | $user_permissions = new Permissions(); |
| 110 | WPF()->current_user_accesses = $user_permissions->get_forum_accesses_by_usergroup(); |
| 111 | if ( function_exists( 'WPF' ) ) { |
| 112 | $forum_id = $selected_options['forum_id']; |
| 113 | $args['forumid'] = $forum_id; |
| 114 | $args['title'] = $selected_options['title']; |
| 115 | $args['body'] = preg_replace( '#</pre>[\r\n\t\s\0]*<pre>#isu', "\r\n", (string) $selected_options['content'] ); |
| 116 | $args['userid'] = $user_id; |
| 117 | $args['tags'] = $selected_options['topic_tags']; |
| 118 | $args['private'] = ( isset( $selected_options['topic_private'] ) && $selected_options['topic_private'] ? 1 : 0 ); |
| 119 | WPF()->member->set_guest_cookies( $args ); |
| 120 | $min = wpforo_setting( 'posting', 'topic_body_min_length' ); |
| 121 | if ( $min ) { |
| 122 | if ( wpfkey( $args, 'body' ) && (int) $min > wpforo_length( $args['body'] ) ) { |
| 123 | return [ |
| 124 | 'status' => 'error', |
| 125 | 'message' => 'The content is too short', |
| 126 | ]; |
| 127 | } |
| 128 | } |
| 129 | if ( ! isset( $args['forumid'] ) ) { |
| 130 | return [ |
| 131 | 'status' => 'error', |
| 132 | 'message' => 'Add Topic error: No forum selected', |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | if ( ! WPF()->forum->get_forum( $args['forumid'] ) ) { |
| 137 | return [ |
| 138 | 'status' => 'error', |
| 139 | 'message' => 'Add Topic error: No forum selected', |
| 140 | ]; |
| 141 | } |
| 142 | |
| 143 | if ( ! WPF()->perm->forum_can( 'ct', $args['forumid'] ) ) { |
| 144 | return [ |
| 145 | 'status' => 'error', |
| 146 | 'message' => 'You don\'t have permission to create topic into this forum', |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | if ( ! WPF()->perm->can_post_now() ) { |
| 151 | return [ |
| 152 | 'status' => 'error', |
| 153 | 'message' => 'You are posting too quickly. Slow down.', |
| 154 | ]; |
| 155 | } |
| 156 | $topicid = WPF()->topic->add( $args ); |
| 157 | if ( $topicid ) { |
| 158 | return [ |
| 159 | 'topic' => WPF()->topic->get_topic( $topicid ), |
| 160 | 'user' => WordPress::get_user_context( $args['userid'] ), |
| 161 | ]; |
| 162 | } else { |
| 163 | return [ |
| 164 | 'status' => 'error', |
| 165 | 'message' => 'Topic not created.', |
| 166 | ]; |
| 167 | } |
| 168 | } else { |
| 169 | return [ |
| 170 | 'status' => 'error', |
| 171 | 'message' => 'Can not create topic.', |
| 172 | ]; |
| 173 | } |
| 174 | } else { |
| 175 | return [ |
| 176 | 'status' => 'error', |
| 177 | 'message' => 'User not found.', |
| 178 | ]; |
| 179 | } |
| 180 | } else { |
| 181 | $error = [ |
| 182 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 183 | 'response' => esc_attr__( 'Please enter valid email address.', 'suretriggers' ), |
| 184 | |
| 185 | ]; |
| 186 | |
| 187 | return $error; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | WfCreateTopic::get_instance(); |
| 194 |