Changeset 6142
- Timestamp:
- 12/08/2016 05:00:47 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/topics/functions.php
r6141 r6142 2063 2063 } 2064 2064 2065 /** 2066 * Return array of available topic toggle actions 2067 * 2068 * @since 2.6.0 bbPress (r6133) 2069 * 2070 * @param int $topic_id Optional. Topic id. 2071 * 2072 * @return array 2073 */ 2074 function bbp_get_topic_toggles( $topic_id = 0 ) { 2075 return apply_filters( 'bbp_get_toggle_topic_actions', array( 2076 'bbp_toggle_topic_close', 2077 'bbp_toggle_topic_stick', 2078 'bbp_toggle_topic_spam', 2079 'bbp_toggle_topic_trash', 2080 'bbp_toggle_topic_approve' 2081 ), $topic_id ); 2082 } 2083 2065 2084 /** Stickies ******************************************************************/ 2066 2085 … … 2139 2158 } 2140 2159 2141 // Setup possible get actions 2142 $possible_actions = array( 2143 'bbp_toggle_topic_close', 2144 'bbp_toggle_topic_stick', 2145 'bbp_toggle_topic_spam', 2146 'bbp_toggle_topic_trash', 2147 'bbp_toggle_topic_approve' 2148 ); 2160 // What's the topic id? 2161 $topic_id = bbp_get_topic_id( (int) $_GET['topic_id'] ); 2162 2163 // Get possible topic-handler toggles 2164 $toggles = bbp_get_topic_toggles( $topic_id ); 2149 2165 2150 2166 // Bail if actions aren't meant for this function 2151 if ( ! in_array( $action, $ possible_actions) ) {2167 if ( ! in_array( $action, $toggles, true ) ) { 2152 2168 return; 2153 2169 } 2154 2155 $failure = ''; // Empty failure string2156 $view_all = false; // Assume not viewing all2157 $topic_id = (int) $_GET['topic_id']; // What's the topic id?2158 $success = false; // Flag2159 $post_data = array( 'ID' => $topic_id ); // Prelim array2160 $redirect = ''; // Empty redirect URL2161 2170 2162 2171 // Make sure topic exists 2163 2172 $topic = bbp_get_topic( $topic_id ); 2164 2173 if ( empty( $topic ) ) { 2174 bbp_add_error( 'bbp_toggle_topic_missing', __( '<strong>ERROR:</strong> This topic could not be found or no longer exists.', 'bbpress' ) ); 2165 2175 return; 2166 2176 } 2167 2177 2168 2178 // What is the user doing here? 2169 if ( ! current_user_can( 'edit_topic', $topic ->ID ) || ( 'bbp_toggle_topic_trash' === $action && ! current_user_can( 'delete_topic', $topic->ID) ) ) {2179 if ( ! current_user_can( 'edit_topic', $topic_id ) || ( 'bbp_toggle_topic_trash' === $action && ! current_user_can( 'delete_topic', $topic_id ) ) ) { 2170 2180 bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have permission to do that.', 'bbpress' ) ); 2171 2181 return; 2172 2182 } 2173 2183 2184 // Sub-action? 2185 $sub_action = ! empty( $_GET['sub_action'] ) 2186 ? sanitize_key( $_GET['sub_action'] ) 2187 : false; 2188 2189 // Preliminary array 2190 $post_data = array( 'ID' => $topic_id ); 2191 2192 // Do the topic toggling 2193 $retval = bbp_toggle_topic( array( 2194 'id' => $topic_id, 2195 'action' => $action, 2196 'sub_action' => $sub_action, 2197 'data' => $post_data 2198 ) ); 2199 2200 // Do additional topic toggle actions 2201 do_action( 'bbp_toggle_topic_handler', $retval['status'], $post_data, $action ); 2202 2203 // No errors 2204 if ( ( false !== $retval['status'] ) && ! is_wp_error( $retval['status'] ) ) { 2205 bbp_redirect( $retval['redirect_to'] ); 2206 2207 // Handle errors 2208 } else { 2209 bbp_add_error( 'bbp_toggle_topic', $retval['message'] ); 2210 } 2211 } 2212 2213 /** 2214 * Do the actual topic toggling 2215 * 2216 * This function is used by `bbp_toggle_topic_handler()` to do the actual heavy 2217 * lifting when it comes to toggling topic. It only really makes sense to call 2218 * within that context, so if you need to call this function directly, make sure 2219 * you're also doing what the handler does too. 2220 * 2221 * @since 2.6.0 2222 * @access private 2223 * 2224 * @param array $args 2225 */ 2226 function bbp_toggle_topic( $args = array() ) { 2227 2228 // Parse the arguments 2229 $r = bbp_parse_args( $args, array( 2230 'id' => 0, 2231 'action' => '', 2232 'sub_action' => '', 2233 'data' => array() 2234 ) ); 2235 2236 // Build the nonce suffix 2237 $nonce_suffix = bbp_get_topic_post_type() . '_' . (int) $r['id']; 2238 2239 // Default return values 2240 $retval = array( 2241 'status' => 0, 2242 'message' => '', 2243 'redirect_to' => bbp_get_topic_permalink( $r['id'], bbp_get_redirect_to() ), 2244 'view_all' => false 2245 ); 2246 2174 2247 // What action are we trying to perform? 2175 switch ( $ action) {2248 switch ( $r['action'] ) { 2176 2249 2177 2250 // Toggle approve/unapprove 2178 2251 case 'bbp_toggle_topic_approve' : 2179 check_ajax_referer( 'approve-topic_' . $topic_id ); 2180 2181 $is_pending = bbp_is_topic_pending( $topic_id ); 2182 $success = true === $is_pending ? bbp_approve_topic( $topic_id ) : bbp_unapprove_topic( $topic_id ); 2183 $failure = true === $is_pending ? __( '<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress' ); 2252 check_ajax_referer( "approve-{$nonce_suffix}" ); 2253 2254 $is_pending = bbp_is_topic_pending( $r['id'] ); 2255 $retval['status'] = true === $is_pending ? bbp_approve_topic( $r['id'] ) : bbp_unapprove_topic( $r['id'] ); 2256 $retval['message'] = true === $is_pending ? __( '<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress' ); 2257 $retval['view_all'] = ! $is_pending; 2184 2258 2185 2259 break; … … 2187 2261 // Toggle open/close 2188 2262 case 'bbp_toggle_topic_close' : 2189 check_ajax_referer( 'close-topic_' . $topic_id);2190 2191 $is_open = bbp_is_topic_open( $topic_id);2192 $ success = true === $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id);2193 $ failure= true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );2263 check_ajax_referer( "close-{$nonce_suffix}" ); 2264 2265 $is_open = bbp_is_topic_open( $r['id'] ); 2266 $retval['status'] = true === $is_open ? bbp_close_topic( $r['id'] ) : bbp_open_topic( $r['id'] ); 2267 $retval['message'] = true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' ); 2194 2268 2195 2269 break; … … 2197 2271 // Toggle sticky/super-sticky/unstick 2198 2272 case 'bbp_toggle_topic_stick' : 2199 check_ajax_referer( 'stick-topic_' . $topic_id);2200 2201 $is_sticky = bbp_is_topic_sticky( $topic_id);2202 $is_super = false === $is_sticky && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false;2203 $ success = true === $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );2204 $ failure= true === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );2273 check_ajax_referer( "stick-{$nonce_suffix}" ); 2274 2275 $is_sticky = bbp_is_topic_sticky( $r['id'] ); 2276 $is_super = false === $is_sticky && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false; 2277 $retval['status'] = true === $is_sticky ? bbp_unstick_topic( $r['id'] ) : bbp_stick_topic( $r['id'], $is_super ); 2278 $retval['message'] = true === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' ); 2205 2279 2206 2280 break; … … 2208 2282 // Toggle spam 2209 2283 case 'bbp_toggle_topic_spam' : 2210 check_ajax_referer( 'spam-topic_' . $topic_id);2211 2212 $is_spam = bbp_is_topic_spam( $topic_id);2213 $ success = true === $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id);2214 $ failure= true === $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' );2215 $ view_all= ! $is_spam;2284 check_ajax_referer( "spam-{$nonce_suffix}" ); 2285 2286 $is_spam = bbp_is_topic_spam( $r['id'] ); 2287 $retval['status'] = true === $is_spam ? bbp_unspam_topic( $r['id'] ) : bbp_spam_topic( $r['id'] ); 2288 $retval['message'] = true === $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' ); 2289 $retval['view_all'] = ! $is_spam; 2216 2290 2217 2291 break; … … 2220 2294 case 'bbp_toggle_topic_trash' : 2221 2295 2222 $sub_action = ! empty( $_GET['sub_action'] ) && in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false; 2223 2224 if ( empty( $sub_action ) ) { 2225 break; 2226 } 2227 2228 switch ( $sub_action ) { 2296 switch ( $r['sub_action'] ) { 2229 2297 case 'trash': 2230 check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id);2298 check_ajax_referer( "trash-{$nonce_suffix}" ); 2231 2299 2232 2300 $view_all = true; 2233 $ success = wp_trash_post( $topic_id);2234 $ failure= __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );2301 $retval['status'] = wp_trash_post( $r['id'] ); 2302 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' ); 2235 2303 2236 2304 break; 2237 2305 2238 2306 case 'untrash': 2239 check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id);2240 2241 $ success = wp_untrash_post( $topic_id);2242 $ failure= __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );2307 check_ajax_referer( "untrash-{$nonce_suffix}" ); 2308 2309 $retval['status'] = wp_untrash_post( $r['id'] ); 2310 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' ); 2243 2311 2244 2312 break; 2245 2313 2246 2314 case 'delete': 2247 check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id);2248 2249 $ success = wp_delete_post( $topic_id);2250 $ failure= __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );2315 check_ajax_referer( "delete-{$nonce_suffix}" ); 2316 2317 $retval['status'] = wp_delete_post( $r['id'] ); 2318 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' ); 2251 2319 2252 2320 break; … … 2256 2324 } 2257 2325 2258 // Do additional topic toggle actions 2259 do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action ); 2260 2261 // No errors 2262 if ( false !== $success && ! is_wp_error( $success ) ) { 2263 2264 // Redirect back to the topic's forum 2265 if ( isset( $sub_action ) && ( 'delete' === $sub_action ) ) { 2266 $redirect = bbp_get_forum_permalink( $success->post_parent ); 2267 2268 // Redirect back to the topic 2269 } else { 2270 2271 // Get the redirect detination 2272 $permalink = bbp_get_topic_permalink( $topic_id ); 2273 $redirect = bbp_add_view_all( $permalink, $view_all ); 2274 } 2275 2276 bbp_redirect( $redirect ); 2277 2278 // Handle errors 2279 } else { 2280 bbp_add_error( 'bbp_toggle_topic', $failure ); 2281 } 2326 // Maybe redirect back to the topic's forum 2327 if ( isset( $r['sub_action'] ) && ( 'delete' === $r['sub_action'] ) ) { 2328 $retval['redirect_to'] = bbp_get_forum_permalink( $retval['status']->post_parent ); 2329 } 2330 2331 // Add view all if needed 2332 if ( ! empty( $retval['view_all'] ) ) { 2333 $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true ); 2334 } 2335 2336 // Filter & return 2337 return apply_filters( 'bbp_toggle_topic', $retval, $r, $args ); 2282 2338 } 2283 2339
Note: See TracChangeset
for help on using the changeset viewer.