| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class BBPCATTCore { |
| 7 |
private $wp_version; |
| 8 |
private $plugin_path; |
| 9 |
private $plugin_url; |
| 10 |
|
| 11 |
public $l; |
| 12 |
public $o; |
| 13 |
|
| 14 |
function __construct() { |
| 15 |
global $wp_version; |
| 16 |
|
| 17 |
$this->wp_version = substr( str_replace( '.', '', $wp_version ), 0, 2 ); |
| 18 |
|
| 19 |
$this->o = get_option( 'bbp_core_settings' ); |
| 20 |
|
| 21 |
$this->plugin_path = dirname( dirname( __FILE__ ) ) . '/'; |
| 22 |
$this->plugin_url = plugins_url( '/', __DIR__ ); |
| 23 |
|
| 24 |
define( 'BBPCATTACHMENT_URL', $this->plugin_url ); |
| 25 |
define( 'BBPCATTACHMENTS_PATH', $this->plugin_path ); |
| 26 |
|
| 27 |
add_action( 'after_setup_theme', [ $this, 'load' ], 999 ); |
| 28 |
add_action( 'before_delete_post', [ $this, 'topic_attachment_deletion' ]); |
| 29 |
add_action( 'before_delete_post', [ $this, 'reply_attachment_deletion' ]); |
| 30 |
} |
| 31 |
|
| 32 |
public static function instance() { |
| 33 |
static $instance = false; |
| 34 |
|
| 35 |
if ( $instance === false ) { |
| 36 |
$instance = new BBPCATTCore(); |
| 37 |
} |
| 38 |
|
| 39 |
return $instance; |
| 40 |
} |
| 41 |
|
| 42 |
private function _upgrade( $old, $new ) { |
| 43 |
foreach ( $new as $key => $value ) { |
| 44 |
if ( ! isset( $old[ $key ] ) ) { |
| 45 |
$old[ $key ] = $value; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$unset = []; |
| 50 |
foreach ( $old as $key => $value ) { |
| 51 |
if ( ! isset( $new[ $key ] ) ) { |
| 52 |
$unset[] = $key; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
foreach ( $unset as $key ) { |
| 57 |
unset( $old[ $key ] ); |
| 58 |
} |
| 59 |
|
| 60 |
return $old; |
| 61 |
} |
| 62 |
|
| 63 |
public function load() { |
| 64 |
add_action( 'init', [ $this, 'init_thumbnail_size' ], 1 ); |
| 65 |
add_action( 'init', [ $this, 'delete_attachments' ] ); |
| 66 |
|
| 67 |
add_action( 'before_delete_post', [ $this, 'delete_post' ] ); |
| 68 |
|
| 69 |
if ( is_admin() ) { |
| 70 |
require_once BBPCATTACHMENTS_PATH . 'code/admin.php'; |
| 71 |
require_once BBPCATTACHMENTS_PATH . 'code/meta.php'; |
| 72 |
|
| 73 |
GDATTAdmin::instance(); |
| 74 |
GDATTAdminMeta::instance(); |
| 75 |
} else { |
| 76 |
require_once BBPCATTACHMENTS_PATH . 'code/front.php'; |
| 77 |
|
| 78 |
GDATTFront::instance(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function init_thumbnail_size() { |
| 83 |
add_image_size( 'bbpc-attachment-thumb', $this->o['attachment_image_x'] ?? 300, $this->o['attachment_image_y'] ?? 300, true ); |
| 84 |
} |
| 85 |
|
| 86 |
public function delete_attachments() { |
| 87 |
if ( isset( $_GET['d4pbbaction'] ) ) { |
| 88 |
$nonce = wp_verify_nonce( $_GET['_wpnonce'], 'd4p-bbpress-attachments' ); |
| 89 |
|
| 90 |
if ( $nonce ) { |
| 91 |
global $user_ID; |
| 92 |
|
| 93 |
$action = $_GET['d4pbbaction']; |
| 94 |
$att_id = intval( $_GET['att_id'] ); |
| 95 |
$bbp_id = intval( $_GET['bbp_id'] ); |
| 96 |
|
| 97 |
$post = get_post( $bbp_id ); |
| 98 |
$author_ID = $post->post_author; |
| 99 |
|
| 100 |
$file = get_attached_file( $att_id ); |
| 101 |
$file = pathinfo( $file, PATHINFO_BASENAME ); |
| 102 |
|
| 103 |
$allow = 'no'; |
| 104 |
if ( forumax_is_user_admin() ) { |
| 105 |
$allow = 'delete'; |
| 106 |
} elseif ( forumax_is_user_moderator() ) { |
| 107 |
$allow = 'delete'; |
| 108 |
} elseif ( $author_ID == $user_ID ) { |
| 109 |
$allow = false; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $action == 'delete' && ( $allow == 'delete' || $allow == 'both' ) ) { |
| 113 |
wp_delete_attachment( $att_id ); |
| 114 |
|
| 115 |
add_post_meta( |
| 116 |
$bbp_id, |
| 117 |
'_bbp_attachment_log', |
| 118 |
[ |
| 119 |
'code' => 'delete_attachment', |
| 120 |
'user' => $user_ID, |
| 121 |
'file' => $file, |
| 122 |
] |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
if ( $action == 'detach' && ( $allow == 'detach' || $allow == 'both' ) ) { |
| 127 |
global $wpdb; |
| 128 |
$wpdb->update( $wpdb->posts, [ 'post_parent' => 0 ], [ 'ID' => $att_id ] ); |
| 129 |
|
| 130 |
add_post_meta( |
| 131 |
$bbp_id, |
| 132 |
'_bbp_attachment_log', |
| 133 |
[ |
| 134 |
'code' => 'detach_attachment', |
| 135 |
'user' => $user_ID, |
| 136 |
'file' => $file, |
| 137 |
] |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$url = remove_query_arg( [ '_wpnonce', 'd4pbbaction', 'att_id', 'bbp_id' ] ); |
| 143 |
wp_redirect( $url ); |
| 144 |
exit; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function delete_post( $id ) { |
| 149 |
if ( forumax_has_bbpress() ) { |
| 150 |
if ( bbp_is_reply( $id ) || bbp_is_topic( $id ) ) { |
| 151 |
//TODO: Add conditions in pro |
| 152 |
// if ( $this->o['delete_attachments'] == 'delete' ) { |
| 153 |
// $files = forumax_get_post_attachments( $id ); |
| 154 |
|
| 155 |
// if ( is_array( $files ) && ! empty( $files ) ) { |
| 156 |
// foreach ( $files as $file ) { |
| 157 |
// wp_delete_attachment( $file->ID ); |
| 158 |
// } |
| 159 |
// } |
| 160 |
// } elseif ( $this->o['delete_attachments'] == 'detach' ) { |
| 161 |
global $wpdb; |
| 162 |
|
| 163 |
$wpdb->update( |
| 164 |
$wpdb->posts, |
| 165 |
[ 'post_parent' => 0 ], |
| 166 |
[ |
| 167 |
'post_parent' => $id, |
| 168 |
'post_type' => 'attachment', |
| 169 |
] |
| 170 |
); |
| 171 |
//} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
public function enabled_for_forum( $id = 0 ) { |
| 177 |
$meta = get_post_meta( bbp_get_forum_id( $id ), '_gdbbatt_settings', true ); |
| 178 |
return ! isset( $meta['disable'] ) || ( isset( $meta['disable'] ) && $meta['disable'] == 0 ); |
| 179 |
} |
| 180 |
|
| 181 |
public function get_file_size( $global_only = false, $forum_id = 0 ) { |
| 182 |
$forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id; |
| 183 |
|
| 184 |
if ( ! class_exists( 'Forumax_Pro' ) ){ |
| 185 |
$value = 512; |
| 186 |
// $value = (int) $this->o['max_file_size'] ?? 512; |
| 187 |
}else{ |
| 188 |
$value = $this->o['max_file_size'] ?? 512; |
| 189 |
} |
| 190 |
|
| 191 |
if ( ! $global_only ) { |
| 192 |
$meta = get_post_meta( $forum_id, '_gdbbatt_settings', true ); |
| 193 |
|
| 194 |
if ( is_array( $meta ) && $meta['to_override'] == 1 ) { |
| 195 |
$value = $meta['max_file_size']; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return $value; |
| 200 |
} |
| 201 |
|
| 202 |
public function get_max_files( $global_only = false, $forum_id = 0 ) { |
| 203 |
$forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id; |
| 204 |
if( ! class_exists( 'Forumax_Pro' ) ){ |
| 205 |
$value = 4; |
| 206 |
}else{ |
| 207 |
$value = $this->o['max_file_uploads'] ?? 4; |
| 208 |
} |
| 209 |
|
| 210 |
if ( ! $global_only ) { |
| 211 |
$meta = get_post_meta( $forum_id, '_gdbbatt_settings', true ); |
| 212 |
|
| 213 |
if ( is_array( $meta ) && $meta['to_override'] == 1 ) { |
| 214 |
$value = $meta['max_to_upload'] ?? 4; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return $value; |
| 219 |
} |
| 220 |
|
| 221 |
public function is_right_size( $file, $forum_id = 0 ) { |
| 222 |
$forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id; |
| 223 |
|
| 224 |
$file_size = apply_filters( 'bbpc_bbpressattchment_max_file_size', $this->get_file_size( false, $forum_id ), $forum_id ); |
| 225 |
|
| 226 |
return $file['size'] < $file_size * 1024; |
| 227 |
} |
| 228 |
|
| 229 |
public function is_user_allowed() { |
| 230 |
$allowed = false; |
| 231 |
|
| 232 |
if ( is_user_logged_in() ) { |
| 233 |
if ( ! isset( $this->o['roles_to_upload'] ) ) { |
| 234 |
$allowed = true; |
| 235 |
} else { |
| 236 |
$value = $this->o['roles_to_upload']; |
| 237 |
if ( ! is_array( $value ) ) { |
| 238 |
$allowed = true; |
| 239 |
} |
| 240 |
|
| 241 |
global $current_user; |
| 242 |
if ( is_array( $current_user->roles ) ) { |
| 243 |
$matched = array_intersect( $current_user->roles, $value ); |
| 244 |
$allowed = ! empty( $matched ); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return apply_filters( 'bbpc_bbpressattchment_is_user_allowed', $allowed ); |
| 250 |
} |
| 251 |
|
| 252 |
public function is_hidden_from_visitors( $forum_id = 0 ) { |
| 253 |
$forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id; |
| 254 |
|
| 255 |
$value = $this->o['is_hide_attachment'] ?? 1; |
| 256 |
$meta = get_post_meta( $forum_id, '_gdbbatt_settings', true ); |
| 257 |
|
| 258 |
if ( is_array( $meta ) && $meta['to_override'] == 1 ) { |
| 259 |
$value = $meta['hide_from_visitors']; |
| 260 |
} |
| 261 |
|
| 262 |
return apply_filters( 'bbpc_bbpressattchment_is_hidden_from_visitors', $value == 1 ); |
| 263 |
} |
| 264 |
|
| 265 |
public function topic_attachment_deletion($topic_id){ |
| 266 |
if( class_exists( 'Forumax_Pro' ) ){ |
| 267 |
$topic_deletion = $this->o['is_attachment_deletion'] ?? ''; |
| 268 |
|
| 269 |
if( $topic_deletion == 1 && get_post_type($topic_id) == bbp_get_topic_post_type() ) { |
| 270 |
$topic_attachments = get_attached_media( '', $topic_id ); |
| 271 |
foreach ($topic_attachments as $attachment) { |
| 272 |
wp_delete_attachment( $attachment->ID, 'true' ); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
public function reply_attachment_deletion($reply_id){ |
| 279 |
|
| 280 |
if( class_exists( 'Forumax_Pro' ) ){ |
| 281 |
$reply_deletion = $this->o['is_attachment_deletion'] ?? ''; |
| 282 |
|
| 283 |
if( $reply_deletion == 1 && get_post_type($reply_id) == bbp_get_reply_post_type() ) { |
| 284 |
$replies_attachments = get_attached_media( '', $reply_id ); |
| 285 |
|
| 286 |
foreach ($replies_attachments as $attachment) { |
| 287 |
wp_delete_attachment( $attachment->ID, 'true' ); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|