← All changes
|
includes/features/bbpc_attachments/code/public.php
+85
-82
1.3.3
→
2.2.0
View file →
| @@ -1,82 +1,85 @@ | ||
| 1 | -<?php | |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - exit; | |
| 4 | -} | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * Get the list of attachments for a post. | |
| 8 | - * | |
| 9 | - * @param int $post_id topic or reply ID to get attachments for | |
| 10 | - * | |
| 11 | - * @return array list of attachments objects | |
| 12 | - */ | |
| 13 | -function bbpc_get_post_attachments( $post_id ) { | |
| 14 | - $args = [ | |
| 15 | - 'post_type' => 'attachment', | |
| 16 | - 'numberposts' => -1, | |
| 17 | - 'post_status' => null, | |
| 18 | - 'post_parent' => $post_id, | |
| 19 | - 'orderby' => 'ID', | |
| 20 | - 'order' => 'ASC', | |
| 21 | - ]; | |
| 22 | - | |
| 23 | - return get_posts( $args ); | |
| 24 | -} | |
| 25 | - | |
| 26 | -/** | |
| 27 | - * Count attachments for the forum topic. It can include topic replies in the count. | |
| 28 | - * | |
| 29 | - * @param int $topic_id id of the topic to count attachments for | |
| 30 | - * @param bool $include_replies true, to include reply attachments | |
| 31 | - * | |
| 32 | - * @return int number of attachments | |
| 33 | - */ | |
| 34 | -function bbpc_topic_attachments_count( $topic_id, $include_replies = false ) { | |
| 35 | - global $wpdb; | |
| 36 | - | |
| 37 | - $sql = 'select ID from ' . $wpdb->posts . ' where post_parent = ' . $topic_id . " and post_type = 'attachment'"; | |
| 38 | - | |
| 39 | - if ( $include_replies ) { | |
| 40 | - $sql = '(' . $sql . ') union (select ID from ' . $wpdb->posts . ' where post_parent in (select ID from ' . $wpdb->posts . ' where post_parent = ' . $topic_id . " and post_type = 'reply') and post_type = 'attachment')"; | |
| 41 | - } | |
| 42 | - | |
| 43 | - $attachments = $wpdb->get_results( $sql ); | |
| 44 | - return count( $attachments ); | |
| 45 | -} | |
| 46 | - | |
| 47 | -/** | |
| 48 | - * Handle upload file error. | |
| 49 | - * | |
| 50 | - * @param string $file file with error | |
| 51 | - * @param string $message error message | |
| 52 | - * | |
| 53 | - * @return WP_Error error message | |
| 54 | - */ | |
| 55 | -function bbpc_bbattachment_handle_upload_error( &$file, $message ) { | |
| 56 | - return new WP_Error( 'wp_upload_error', $message ); | |
| 57 | -} | |
| 58 | - | |
| 59 | -/** | |
| 60 | - * Get current page forum ID. Handles the edge cases with the edit forms. | |
| 61 | - * | |
| 62 | - * @return int | |
| 63 | - */ | |
| 64 | -function bbpc_get_forum_id() { | |
| 65 | - $forum_id = bbp_get_forum_id(); | |
| 66 | - | |
| 67 | - if ( $forum_id == 0 ) { | |
| 68 | - if ( bbp_is_topic_edit() ) { | |
| 69 | - $topic_id = bbp_get_topic_id(); | |
| 70 | - $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 71 | - } elseif ( bbp_is_reply_edit() ) { | |
| 72 | - $reply_id = bbp_get_reply_id(); | |
| 73 | - $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 74 | - } | |
| 75 | - } | |
| 76 | - | |
| 77 | - return $forum_id; | |
| 78 | -} | |
| 79 | - | |
| 80 | -function bbpc_bba_o( $name ) { | |
| 81 | - return BBPCATTCore::instance()->o[ $name ]; | |
| 82 | -} | |
| 1 | +<?php | |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | + exit; | |
| 4 | +} | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Get the list of attachments for a post. | |
| 8 | + * | |
| 9 | + * @param int $post_id topic or reply ID to get attachments for | |
| 10 | + * | |
| 11 | + * @return array list of attachments objects | |
| 12 | + */ | |
| 13 | +function forumax_get_post_attachments( $post_id ) { | |
| 14 | + $args = [ | |
| 15 | + 'post_type' => 'attachment', | |
| 16 | + 'numberposts' => -1, | |
| 17 | + 'post_status' => null, | |
| 18 | + 'post_parent' => $post_id, | |
| 19 | + 'orderby' => 'ID', | |
| 20 | + 'order' => 'ASC', | |
| 21 | + ]; | |
| 22 | + | |
| 23 | + return get_posts( $args ); | |
| 24 | +} | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * Count attachments for the forum topic. It can include topic replies in the count. | |
| 28 | + * | |
| 29 | + * @param int $topic_id id of the topic to count attachments for | |
| 30 | + * @param bool $include_replies true, to include reply attachments | |
| 31 | + * | |
| 32 | + * @return int number of attachments | |
| 33 | + */ | |
| 34 | +function forumax_topic_attachments_count( $topic_id, $include_replies = false ) { | |
| 35 | + global $wpdb; | |
| 36 | + | |
| 37 | + $sql = 'select ID from ' . $wpdb->posts . ' where post_parent = ' . $topic_id . " and post_type = 'attachment'"; | |
| 38 | + | |
| 39 | + if ( $include_replies ) { | |
| 40 | + $sql = '(' . $sql . ') union (select ID from ' . $wpdb->posts . ' where post_parent in (select ID from ' . $wpdb->posts . ' where post_parent = ' . $topic_id . " and post_type = 'reply') and post_type = 'attachment')"; | |
| 41 | + } | |
| 42 | + | |
| 43 | + $attachments = $wpdb->get_results( $sql ); | |
| 44 | + return count( $attachments ); | |
| 45 | +} | |
| 46 | + | |
| 47 | +/** | |
| 48 | + * Handle upload file error. | |
| 49 | + * | |
| 50 | + * @param string $file file with error | |
| 51 | + * @param string $message error message | |
| 52 | + * | |
| 53 | + * @return WP_Error error message | |
| 54 | + */ | |
| 55 | +function forumax_bbattachment_handle_upload_error( &$file, $message ) { | |
| 56 | + return new WP_Error( 'wp_upload_error', $message ); | |
| 57 | +} | |
| 58 | + | |
| 59 | +/** | |
| 60 | + * Get current page forum ID. Handles the edge cases with the edit forms. | |
| 61 | + * | |
| 62 | + * @return int | |
| 63 | + */ | |
| 64 | +function forumax_get_forum_id() { | |
| 65 | + $forum_id = bbp_get_forum_id(); | |
| 66 | + | |
| 67 | + if ( $forum_id == 0 ) { | |
| 68 | + if ( bbp_is_topic_edit() ) { | |
| 69 | + $topic_id = bbp_get_topic_id(); | |
| 70 | + $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 71 | + } elseif ( bbp_is_reply_edit() ) { | |
| 72 | + $reply_id = bbp_get_reply_id(); | |
| 73 | + $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + return $forum_id; | |
| 78 | +} | |
| 79 | + | |
| 80 | +function forumax_bba_o( $name ) { | |
| 81 | + $opts = BBPCATTCore::instance()->o; | |
| 82 | + // Return the option value if present, otherwise null. | |
| 83 | + return isset( $opts[ $name ] ) ? $opts[ $name ] : null; | |
| 84 | +} | |
| 85 | + | |