PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.1
Forumax – AI Powered Advanced Community Forum Plugin v2.5.1
2.5.1 2.5.0 2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 All 31 releases
← All changes | includes/features/bbpc_attachments/code/class.php +299 -292 1.3.3 → 2.5.1 View file →
@@ -1,292 +1,299 @@
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 ( bbpc_is_user_admin() ) {
105 - $allow = 'delete';
106 - } elseif ( bbpc_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 ( bbpc_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 = bbpc_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( 'BBPCorePro' ) ){
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( 'BBPCorePro' ) ){
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( 'BBPCorePro' ) ){
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( 'BBPCorePro' ) ){
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 -}
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 = isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'd4p-bbpress-attachments' );
89 + $post = isset( $_GET['bbp_id'] ) ? get_post( intval( $_GET['bbp_id'] ) ) : null;
90 +
91 + if ( $nonce && $post && isset( $_GET['att_id'] ) ) {
92 + global $user_ID;
93 +
94 + $action = sanitize_key( wp_unslash( $_GET['d4pbbaction'] ) );
95 + $att_id = intval( $_GET['att_id'] );
96 + $bbp_id = (int) $post->ID;
97 +
98 + // Only act on an attachment of this topic/reply, never any media ID.
99 + if ( 'attachment' !== get_post_type( $att_id ) || (int) wp_get_post_parent_id( $att_id ) !== $bbp_id ) {
100 + return;
101 + }
102 +
103 + $author_ID = $post->post_author;
104 +
105 + $file = get_attached_file( $att_id );
106 + $file = pathinfo( $file, PATHINFO_BASENAME );
107 +
108 + $allow = 'no';
109 + if ( forumax_is_user_admin() ) {
110 + $allow = 'delete';
111 + } elseif ( forumax_is_user_moderator() ) {
112 + $allow = 'delete';
113 + } elseif ( $author_ID == $user_ID ) {
114 + $allow = false;
115 + }
116 +
117 + if ( $action == 'delete' && ( $allow == 'delete' || $allow == 'both' ) ) {
118 + wp_delete_attachment( $att_id );
119 +
120 + add_post_meta(
121 + $bbp_id,
122 + '_bbp_attachment_log',
123 + [
124 + 'code' => 'delete_attachment',
125 + 'user' => $user_ID,
126 + 'file' => $file,
127 + ]
128 + );
129 + }
130 +
131 + if ( $action == 'detach' && ( $allow == 'detach' || $allow == 'both' ) ) {
132 + global $wpdb;
133 + $wpdb->update( $wpdb->posts, [ 'post_parent' => 0 ], [ 'ID' => $att_id ] );
134 +
135 + add_post_meta(
136 + $bbp_id,
137 + '_bbp_attachment_log',
138 + [
139 + 'code' => 'detach_attachment',
140 + 'user' => $user_ID,
141 + 'file' => $file,
142 + ]
143 + );
144 + }
145 + }
146 +
147 + $url = remove_query_arg( [ '_wpnonce', 'd4pbbaction', 'att_id', 'bbp_id' ] );
148 + wp_redirect( $url );
149 + exit;
150 + }
151 + }
152 +
153 + public function delete_post( $id ) {
154 + if ( forumax_has_bbpress() ) {
155 + if ( bbp_is_reply( $id ) || bbp_is_topic( $id ) ) {
156 + //TODO: Add conditions in pro
157 + // if ( $this->o['delete_attachments'] == 'delete' ) {
158 + // $files = forumax_get_post_attachments( $id );
159 +
160 + // if ( is_array( $files ) && ! empty( $files ) ) {
161 + // foreach ( $files as $file ) {
162 + // wp_delete_attachment( $file->ID );
163 + // }
164 + // }
165 + // } elseif ( $this->o['delete_attachments'] == 'detach' ) {
166 + global $wpdb;
167 +
168 + $wpdb->update(
169 + $wpdb->posts,
170 + [ 'post_parent' => 0 ],
171 + [
172 + 'post_parent' => $id,
173 + 'post_type' => 'attachment',
174 + ]
175 + );
176 + //}
177 + }
178 + }
179 + }
180 +
181 + public function enabled_for_forum( $id = 0 ) {
182 + $meta = get_post_meta( bbp_get_forum_id( $id ), '_gdbbatt_settings', true );
183 + return ! isset( $meta['disable'] ) || ( isset( $meta['disable'] ) && $meta['disable'] == 0 );
184 + }
185 +
186 + public function get_file_size( $global_only = false, $forum_id = 0 ) {
187 + $forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id;
188 +
189 + if ( ! class_exists( 'Forumax_Pro' ) ){
190 + $value = 512;
191 + // $value = (int) $this->o['max_file_size'] ?? 512;
192 + }else{
193 + $value = $this->o['max_file_size'] ?? 512;
194 + }
195 +
196 + if ( ! $global_only ) {
197 + $meta = get_post_meta( $forum_id, '_gdbbatt_settings', true );
198 +
199 + if ( is_array( $meta ) && $meta['to_override'] == 1 ) {
200 + $value = $meta['max_file_size'];
201 + }
202 + }
203 +
204 + return $value;
205 + }
206 +
207 + public function get_max_files( $global_only = false, $forum_id = 0 ) {
208 + $forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id;
209 + if( ! class_exists( 'Forumax_Pro' ) ){
210 + $value = 4;
211 + }else{
212 + $value = $this->o['max_file_uploads'] ?? 4;
213 + }
214 +
215 + if ( ! $global_only ) {
216 + $meta = get_post_meta( $forum_id, '_gdbbatt_settings', true );
217 +
218 + if ( is_array( $meta ) && $meta['to_override'] == 1 ) {
219 + $value = $meta['max_to_upload'] ?? 4;
220 + }
221 + }
222 +
223 + return $value;
224 + }
225 +
226 + public function is_right_size( $file, $forum_id = 0 ) {
227 + $forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id;
228 +
229 + $file_size = apply_filters( 'bbpc_bbpressattchment_max_file_size', $this->get_file_size( false, $forum_id ), $forum_id );
230 +
231 + return $file['size'] < $file_size * 1024;
232 + }
233 +
234 + public function is_user_allowed() {
235 + $allowed = false;
236 +
237 + if ( is_user_logged_in() ) {
238 + if ( ! isset( $this->o['roles_to_upload'] ) ) {
239 + $allowed = true;
240 + } else {
241 + $value = $this->o['roles_to_upload'];
242 + if ( ! is_array( $value ) ) {
243 + $allowed = true;
244 + }
245 +
246 + global $current_user;
247 + if ( is_array( $current_user->roles ) ) {
248 + $matched = array_intersect( $current_user->roles, $value );
249 + $allowed = ! empty( $matched );
250 + }
251 + }
252 + }
253 +
254 + return apply_filters( 'bbpc_bbpressattchment_is_user_allowed', $allowed );
255 + }
256 +
257 + public function is_hidden_from_visitors( $forum_id = 0 ) {
258 + $forum_id = $forum_id == 0 ? bbp_get_forum_id() : $forum_id;
259 +
260 + $value = $this->o['is_hide_attachment'] ?? 1;
261 + $meta = get_post_meta( $forum_id, '_gdbbatt_settings', true );
262 +
263 + if ( is_array( $meta ) && $meta['to_override'] == 1 ) {
264 + $value = $meta['hide_from_visitors'];
265 + }
266 +
267 + return apply_filters( 'bbpc_bbpressattchment_is_hidden_from_visitors', $value == 1 );
268 + }
269 +
270 + public function topic_attachment_deletion($topic_id){
271 + if( class_exists( 'Forumax_Pro' ) ){
272 + $topic_deletion = $this->o['is_attachment_deletion'] ?? '';
273 +
274 + if( $topic_deletion == 1 && get_post_type($topic_id) == bbp_get_topic_post_type() ) {
275 + $topic_attachments = get_attached_media( '', $topic_id );
276 + foreach ($topic_attachments as $attachment) {
277 + wp_delete_attachment( $attachment->ID, 'true' );
278 + }
279 + }
280 + }
281 + }
282 +
283 + public function reply_attachment_deletion($reply_id){
284 +
285 + if( class_exists( 'Forumax_Pro' ) ){
286 + $reply_deletion = $this->o['is_attachment_deletion'] ?? '';
287 +
288 + if( $reply_deletion == 1 && get_post_type($reply_id) == bbp_get_reply_post_type() ) {
289 + $replies_attachments = get_attached_media( '', $reply_id );
290 +
291 + foreach ($replies_attachments as $attachment) {
292 + wp_delete_attachment( $attachment->ID, 'true' );
293 + }
294 + }
295 + }
296 + }
297 +}
298 +
299 +