PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 1.2.8
Forumax – AI Powered Advanced Community Forum Plugin v1.2.8
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 2.0.0 2.1.0 All 29 releases
bbp-core / includes / features / bbpc_attachments / code / front.php

front.php in Forumax – AI Powered Advanced Community Forum Plugin 1.2.8, at includes/features/bbpc_attachments/code/front.php

433 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 class GDATTFront {
7 private $edit_mode = false;
8 private $icons = [
9 'code' => 'c|cc|h|js|class',
10 'xml' => 'xml',
11 'excel' => 'xla|xls|xlsx|xlt|xlw|xlam|xlsb|xlsm|xltm',
12 'word' => 'docx|dotx|docm|dotm',
13 'image' => 'png|gif|jpg|jpeg|jpe|jp|bmp|tif|tiff|svg',
14 'psd' => 'psd',
15 'ai' => 'ai',
16 'archive' => 'zip|rar|gz|gzip|tar',
17 'text' => 'txt|asc|nfo',
18 'powerpoint' => 'pot|pps|ppt|pptx|ppam|pptm|sldm|ppsm|potm',
19 'pdf' => 'pdf',
20 'html' => 'htm|html|css',
21 'video' => 'avi|asf|asx|wax|wmv|wmx|divx|flv|mov|qt|mpeg|mpg|mpe|mp4|m4v|ogv|mkv',
22 'documents' => 'odt|odp|ods|odg|odc|odb|odf|wp|wpd|rtf',
23 'audio' => 'mp3|m4a|m4b|mp4|m4v|wav|ra|ram|ogg|oga|mid|midi|wma|mka',
24 'icon' => 'ico',
25 ];
26
27 function __construct() {
28 add_action( 'bbp_init', [ $this, 'load' ] );
29 }
30
31 public static function instance() {
32 static $instance = false;
33
34 if ( $instance === false ) {
35 $instance = new GDATTFront();
36 }
37
38 return $instance;
39 }
40
41 public function load() {
42 add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] );
43
44 add_action( 'bbp_theme_before_reply_form_submit_wrapper', [ $this, 'embed_form' ] );
45 add_action( 'bbp_theme_before_topic_form_submit_wrapper', [ $this, 'embed_form' ] );
46
47 add_action( 'bbp_edit_reply', [ $this, 'edit_reply' ], 10, 5 );
48 add_action( 'bbp_edit_topic', [ $this, 'edit_topic' ], 10, 4 );
49 add_action( 'bbp_new_reply', [ $this, 'save_reply' ], 10, 5 );
50 add_action( 'bbp_new_topic', [ $this, 'save_topic' ], 10, 4 );
51
52 add_filter( 'bbp_get_reply_content', [ $this, 'embed_attachments' ], 100, 2 );
53 add_filter( 'bbp_get_topic_content', [ $this, 'embed_attachments' ], 100, 2 );
54
55 if ( bbpc_bba_o( 'is_attachment_icon' ) == 1 ) {
56 add_action( 'bbp_theme_before_topic_title', [ $this, 'show_attachments_icon' ] );
57 }
58
59 $this->register_scripts_and_styles();
60 }
61
62 private function icon( $ext ) {
63 foreach ( $this->icons as $icon => $list ) {
64 $list = explode( '|', $list );
65
66 if ( in_array( $ext, $list ) ) {
67 return $icon;
68 }
69 }
70
71 return 'generic';
72 }
73
74 public function register_scripts_and_styles() {
75 //TODO: Fix the constant for version
76 wp_register_style( 'bbpc-attachments', BBPCATTACHMENT_URL . 'css/front.min.css', [], BBPC_VERSION );
77 wp_register_script( 'bbpc-attachments', BBPCATTACHMENT_URL . 'js/front.min.js', [ 'jquery' ], BBPC_VERSION, true );
78 }
79
80 public function include_scripts_and_styles() {
81 wp_enqueue_style( 'bbpc-attachments' );
82 wp_enqueue_script( 'bbpc-attachments' );
83
84 wp_localize_script(
85 'bbpc-attachments',
86 'bbpcAttachmentsInit',
87 [
88 'max_files' => apply_filters( 'bbpc_bbpressattchment_allow_upload', BBPCATTCore::instance()->get_max_files(), bbp_get_forum_id() ),
89 'are_you_sure' => __( 'This operation is not reversible. Are you sure?', 'bbp-core' ),
90 ]
91 );
92 }
93
94 public function wp_enqueue_scripts() {
95 if ( bbpc_is_bbpress() ) {
96 $this->include_scripts_and_styles();
97 }
98 }
99
100 public function edit_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
101 $this->edit_mode = true;
102 $this->process_attachments( 0, $topic_id, $forum_id, $anonymous_data, $topic_author );
103 }
104
105 public function edit_reply( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) {
106 $this->edit_mode = true;
107 $this->process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author );
108 }
109
110 public function save_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
111 $this->edit_mode = false;
112 $this->process_attachments( 0, $topic_id, $forum_id, $anonymous_data, $topic_author );
113 }
114
115 public function save_reply( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) {
116 $this->edit_mode = false;
117 $this->process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author );
118 }
119
120 public function process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) {
121 $uploads = [];
122 $revision = false;
123
124 if ( ! empty( $_FILES ) && ! empty( $_FILES['bbpc_attachment'] ) ) {
125 require_once ABSPATH . 'wp-admin/includes/file.php';
126
127 $errors = new gdbbp_Error();
128 $overrides = [
129 'test_form' => false,
130 'upload_error_handler' => 'bbpc_bbattachment_handle_upload_error',
131 ];
132
133 foreach ( $_FILES['bbpc_attachment']['error'] as $key => $error ) {
134 $file_name = $_FILES['bbpc_attachment']['name'][ $key ];
135
136 if ( $error == UPLOAD_ERR_OK ) {
137 $file = [
138 'name' => $file_name,
139 'type' => $_FILES['bbpc_attachment']['type'][ $key ],
140 'size' => $_FILES['bbpc_attachment']['size'][ $key ],
141 'tmp_name' => $_FILES['bbpc_attachment']['tmp_name'][ $key ],
142 'error' => $_FILES['bbpc_attachment']['error'][ $key ],
143 ];
144
145 $file_name = sanitize_file_name( $file_name );
146
147 if ( BBPCATTCore::instance()->is_right_size( $file, $forum_id ) ) {
148 $upload = wp_handle_upload( $file, $overrides );
149
150 if ( ! is_wp_error( $upload ) ) {
151 $uploads[] = $upload;
152 } else {
153 $errors->add( 'wp_upload', $upload->errors['wp_upload_error'][0], $file_name );
154 }
155 } else {
156 $errors->add( 'bbpc_upload', 'File exceeds allowed file size.', $file_name );
157 }
158 } else {
159 switch ( $error ) {
160 default:
161 case 'UPLOAD_ERR_NO_FILE':
162 $errors->add( 'php_upload', 'File not uploaded.', $file_name );
163 break;
164 case 'UPLOAD_ERR_INI_SIZE':
165 $errors->add( 'php_upload', 'Upload file size exceeds PHP maximum file size allowed.', $file_name );
166 break;
167 case 'UPLOAD_ERR_FORM_SIZE':
168 $errors->add( 'php_upload', 'Upload file size exceeds FORM specified file size.', $file_name );
169 break;
170 case 'UPLOAD_ERR_PARTIAL':
171 $errors->add( 'php_upload', 'Upload file only partially uploaded.', $file_name );
172 break;
173 case 'UPLOAD_ERR_CANT_WRITE':
174 $errors->add( 'php_upload', 'Can\'t write file to the disk.', $file_name );
175 break;
176 case 'UPLOAD_ERR_NO_TMP_DIR':
177 $errors->add( 'php_upload', 'Temporary folder for upload is missing.', $file_name );
178 break;
179 case 'UPLOAD_ERR_EXTENSION':
180 $errors->add( 'php_upload', 'Server extension restriction stopped upload.', $file_name );
181 break;
182 }
183 }
184 }
185 }
186
187 $post_id = $reply_id == 0 ? $topic_id : $reply_id;
188
189 if ( ! empty( $errors->errors ) ) {
190 foreach ( $errors->errors as $code => $errs ) {
191 foreach ( $errs as $error ) {
192 if ( $error[0] != '' && $error[1] != '' ) {
193 add_post_meta(
194 $post_id,
195 '_bbp_attachment_upload_error',
196 [
197 'code' => $code,
198 'file' => $error[1],
199 'message' => $error[0],
200 ]
201 );
202 }
203 }
204 }
205
206 $revision = true;
207 }
208
209 if ( ! empty( $uploads ) ) {
210 require_once ABSPATH . 'wp-admin/includes/media.php';
211 require_once ABSPATH . 'wp-admin/includes/image.php';
212
213 foreach ( $uploads as $upload ) {
214 $wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
215 $attachment = [
216 'post_mime_type' => $wp_filetype['type'],
217 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $upload['file'] ) ),
218 'post_content' => '',
219 'post_status' => 'inherit',
220 ];
221
222 $attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
223 $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
224
225 wp_update_attachment_metadata( $attach_id, $attach_data );
226 update_post_meta( $attach_id, '_bbp_attachment', '1' );
227 }
228
229 $revision = true;
230 }
231
232 if ( $this->edit_mode && $revision ) {
233 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'post_has_changed' ] );
234 }
235 }
236
237 public function post_has_changed() {
238 remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'post_has_changed' ] );
239
240 return true;
241 }
242
243 public function show_attachments_icon() {
244 $topic_id = bbp_get_topic_id();
245 $count = bbpc_topic_attachments_count( $topic_id, true );
246
247 if ( $count > 0 ) {
248 echo '<span class="bbp-attachments-count" title="' . $count . ' ' . _n( 'attachment', 'attachments', $count, 'bbp-core' ) . '"></span>';
249 }
250 }
251
252 public function embed_attachments( $content, $id ) {
253 global $user_ID;
254
255 $attachments = bbpc_get_post_attachments( $id );
256
257 $post = get_post( $id );
258 $author_id = $post->post_author;
259
260 $opt = get_option( 'bbp_core_settings' );
261
262 if ( ! empty( $attachments ) ) {
263 $content .= '<div class="bbp-attachments">';
264 $content .= '<h6>' . __( 'Attachments', 'bbp-core' ) . ':</h6>';
265
266 $_download = ' download';
267
268 if ( ! is_user_logged_in() && BBPCATTCore::instance()->is_hidden_from_visitors() ) {
269 $content .= sprintf( __( "You must be <a href='%s'>logged in</a> to view attached files.", 'bbp-core' ), wp_login_url( get_permalink() ) );
270 } else {
271 $listing = '<ol';
272 $thumbnails = '<ol';
273
274 $listing .= ' class="with-icons d4p-bbp-listing"';
275 $thumbnails .= ' class="with-icons d4p-bba-thumbnails"';
276
277 $listing .= '>';
278 $thumbnails .= '>';
279 $images = $files = 0;
280
281 foreach ( $attachments as $attachment ) {
282 $actions = [];
283
284 $url = add_query_arg( '_wpnonce', wp_create_nonce( 'd4p-bbpress-attachments' ) );
285 $url = add_query_arg( 'att_id', $attachment->ID, $url );
286 $url = add_query_arg( 'bbp_id', $id, $url );
287
288 $allow = 'no';
289 if ( bbpc_is_user_admin() ) {
290 $allow = 'delete';
291 } elseif ( bbpc_is_user_moderator() ) {
292 $allow = 'delete';
293 } elseif ( $author_id == $user_ID ) {
294 $allow = false;
295 }
296
297 if ( $allow == 'delete' || $allow == 'both' ) {
298 $actions[] = '<a class="d4p-bba-action-delete" href="' . esc_url( add_query_arg( 'd4pbbaction', 'delete', $url ) ) . '">' . __( 'delete', 'bbp-core' ) . '</a>';
299 }
300
301 if ( $allow == 'detach' || $allow == 'both' ) {
302 $actions[] = '<a class="d4p-bba-action-detach" href="' . esc_url( add_query_arg( 'd4pbbaction', 'detach', $url ) ) . '">' . __( 'detach', 'bbp-core' ) . '</a>';
303 }
304
305 if ( count( $actions ) > 0 ) {
306 $actions = ' <span class="d4p-bba-actions">[' . join( ' | ', $actions ) . ']</span>';
307 } else {
308 $actions = '';
309 }
310
311 $file = get_attached_file( $attachment->ID );
312 $ext = pathinfo( $file, PATHINFO_EXTENSION );
313 $filename = pathinfo( $file, PATHINFO_BASENAME );
314 $file_url = wp_get_attachment_url( $attachment->ID );
315
316 $html = $class_li = $class_a = $rel_a = '';
317 $a_title = $filename;
318 $caption = false;
319
320 $is_lighbox = '';
321 if ( bbpc_is_premium() ) {
322 $is_lighbox = $opt['image_link_type'] == 'lightbox' ? ' bbpc-lightbox' : '';
323 $_download = $opt['image_link_type'] == 'lightbox' ? '' : $_download;
324 }
325
326 $img = false;
327 if ( ( $opt['attachment_image_x'] ?? false ) && ( $opt['attachment_image_y'] ?? false ) ) {
328 $html = wp_get_attachment_image( $attachment->ID, 'bbpc-attachment-thumb' );
329
330 if ( $html != '' ) {
331 $img = true;
332
333 $class_li = 'bbp-atthumb';
334
335 $caption = bbpc_bba_o( 'image_thumbnail_caption' ) == 1;
336 }
337 }
338
339 $item = '<li id="bbpcore-attach_' . $attachment->ID . '" class="bbpcore-attach bbpcore-attachment-' . $ext . ' ' . $class_li . '">';
340
341 if ( $html == '' ) {
342 $html = $filename;
343 $class_li = 'bbp-atticon bbp-atticon-' . $this->icon( $ext );
344 }
345
346 if ( $img ) {
347
348 if ( $caption ) {
349 $item .= '<div style="width: ' . bbpc_bba_o( 'attachment_image_x' ) . 'px" class="wp-caption">';
350 }
351
352 $item .= '<a class="' . $class_a . $is_lighbox . '" href="' . $file_url . '" title="' . $a_title .'" '. $_download .'>' . $html . '</a>';
353
354 if ( $caption ) {
355 $a_title = '<a class="' . $is_lighbox . '" href="' . $file_url . '"' . $_download . '>' . $a_title . '</a>';
356
357 $item .= '<p class="wp-caption-text">' . $a_title . '<br/>' . $actions . '</p></div>';
358 }
359 } else {
360 $item .= '<span role="presentation" class="' . $class_li . '"></span> ';
361 $item .= '<div class="d4p-bbp-att-wrapper"><a class="' . $class_a . $is_lighbox . '"' . $rel_a . $_download . ' href="' . $file_url . '" title="' . $a_title . '">' . $html . '</a>' . $actions . '</div>';
362 }
363
364 $item .= '</li>';
365
366 if ( $img ) {
367 $thumbnails .= $item;
368 $images++;
369 } else {
370 $listing .= $item;
371 $files++;
372 }
373 }
374
375 $thumbnails .= '</ol>';
376 $listing .= '</ol>';
377
378 if ( $images > 0 ) {
379 $content .= $thumbnails;
380 }
381
382 if ( $files > 0 ) {
383 $content .= $listing;
384 }
385 }
386
387 $content .= '</div>';
388 }
389
390 if ( bbpc_is_user_admin() || bbpc_is_user_moderator() ) {
391 $errors = get_post_meta( $id, '_bbp_attachment_upload_error' );
392
393 if ( ! empty( $errors ) ) {
394 $content .= '<div class="bbp-attachments-errors">';
395 $content .= '<h6>' . __( 'Upload Errors', 'bbp-core' ) . ':</h6>';
396 $content .= '<ol';
397
398 $class_li = 'bbp-file-error';
399
400 $content .= ' class="with-icons"';
401 $class_li .= ' bbp-atticon bbp-atticon-error';
402
403 $content .= '>';
404
405 foreach ( $errors as $error ) {
406 $content .= '<li class="' . $class_li . '"><span role="presentation" class="' . $class_li . '"></span> ';
407 $content .= '<div class="d4p-bbp-att-wrapper"><strong>' . esc_html( $error['file'] ) . '</strong>: ' . __( $error['message'], 'bbp-core' ) . '</div></li>';
408 }
409
410 $content .= '</ol></div>';
411 }
412 }
413
414 return $content;
415 }
416
417 public function embed_form() {
418 $can_upload = apply_filters( 'bbpc_bbpressattchment_allow_upload', BBPCATTCore::instance()->is_user_allowed(), bbpc_get_forum_id() );
419 if ( ! $can_upload ) {
420 return;
421 }
422
423 $is_enabled = apply_filters( 'bbpc_bbpressattchment_forum_enabled', BBPCATTCore::instance()->enabled_for_forum(), bbpc_get_forum_id() );
424 if ( ! $is_enabled ) {
425 return;
426 }
427
428 $file_size = apply_filters( 'bbpc_bbpressattchment_max_file_size', BBPCATTCore::instance()->get_file_size(), bbpc_get_forum_id() );
429
430 include BBPCATTACHMENTS_PATH . 'forms/uploader.php';
431 }
432 }
433