← All changes
|
includes/features/bbpc_attachments/code/front.php
+432
-430
1.3.3
→
2.2.0
View file →
| @@ -1,430 +1,432 @@ | ||
| 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.css', [], BBPC_VERSION ); | |
| 77 | - wp_register_script( 'bbpc-attachments', BBPCATTACHMENT_URL . 'js/front.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 | - if ( ! empty( $attachments ) ) { | |
| 261 | - $content .= '<div class="bbp-attachments">'; | |
| 262 | - $content .= '<h6>' . __( 'Attachments', 'bbp-core' ) . ':</h6>'; | |
| 263 | - | |
| 264 | - $_download = ' download'; | |
| 265 | - | |
| 266 | - if ( ! is_user_logged_in() && BBPCATTCore::instance()->is_hidden_from_visitors() ) { | |
| 267 | - $content .= sprintf( __( "You must be <a href='%s'>logged in</a> to view attached files.", 'bbp-core' ), wp_login_url( get_permalink() ) ); | |
| 268 | - } else { | |
| 269 | - $listing = '<ol'; | |
| 270 | - $thumbnails = '<ol'; | |
| 271 | - | |
| 272 | - $listing .= ' class="with-icons d4p-bbp-listing"'; | |
| 273 | - $thumbnails .= ' class="with-icons d4p-bba-thumbnails"'; | |
| 274 | - | |
| 275 | - $listing .= '>'; | |
| 276 | - $thumbnails .= '>'; | |
| 277 | - $images = $files = 0; | |
| 278 | - | |
| 279 | - foreach ( $attachments as $attachment ) { | |
| 280 | - $actions = []; | |
| 281 | - | |
| 282 | - $url = add_query_arg( '_wpnonce', wp_create_nonce( 'd4p-bbpress-attachments' ) ); | |
| 283 | - $url = add_query_arg( 'att_id', $attachment->ID, $url ); | |
| 284 | - $url = add_query_arg( 'bbp_id', $id, $url ); | |
| 285 | - | |
| 286 | - $allow = 'no'; | |
| 287 | - if ( bbpc_is_user_admin() ) { | |
| 288 | - $allow = 'delete'; | |
| 289 | - } elseif ( bbpc_is_user_moderator() ) { | |
| 290 | - $allow = 'delete'; | |
| 291 | - } elseif ( $author_id == $user_ID ) { | |
| 292 | - $allow = false; | |
| 293 | - } | |
| 294 | - | |
| 295 | - if ( $allow == 'delete' || $allow == 'both' ) { | |
| 296 | - $actions[] = '<a class="d4p-bba-action-delete" href="' . esc_url( add_query_arg( 'd4pbbaction', 'delete', $url ) ) . '">' . __( 'delete', 'bbp-core' ) . '</a>'; | |
| 297 | - } | |
| 298 | - | |
| 299 | - if ( $allow == 'detach' || $allow == 'both' ) { | |
| 300 | - $actions[] = '<a class="d4p-bba-action-detach" href="' . esc_url( add_query_arg( 'd4pbbaction', 'detach', $url ) ) . '">' . __( 'detach', 'bbp-core' ) . '</a>'; | |
| 301 | - } | |
| 302 | - | |
| 303 | - if ( count( $actions ) > 0 ) { | |
| 304 | - $actions = ' <span class="d4p-bba-actions">[' . join( ' | ', $actions ) . ']</span>'; | |
| 305 | - } else { | |
| 306 | - $actions = ''; | |
| 307 | - } | |
| 308 | - | |
| 309 | - $file = get_attached_file( $attachment->ID ); | |
| 310 | - $ext = pathinfo( $file, PATHINFO_EXTENSION ); | |
| 311 | - $filename = pathinfo( $file, PATHINFO_BASENAME ); | |
| 312 | - $file_url = wp_get_attachment_url( $attachment->ID ); | |
| 313 | - | |
| 314 | - $html = $class_li = $class_a = $rel_a = ''; | |
| 315 | - $a_title = $filename; | |
| 316 | - $caption = false; | |
| 317 | - | |
| 318 | - $is_lighbox = ''; | |
| 319 | - if ( bbpc_is_premium() ) { | |
| 320 | - $is_lighbox = bbpc_get_opt( 'image_link_type' ) == 'lightbox' ? ' bbpc-lightbox' : ''; | |
| 321 | - $_download = bbpc_get_opt( 'image_link_type' ) == 'lightbox' ? '' : $_download; | |
| 322 | - } | |
| 323 | - | |
| 324 | - $img = false; | |
| 325 | - if ( bbpc_get_opt( 'attachment_image_x', false ) && bbpc_get_opt( 'attachment_image_y', false ) ) { | |
| 326 | - $html = wp_get_attachment_image( $attachment->ID, 'bbpc-attachment-thumb' ); | |
| 327 | - | |
| 328 | - if ( $html != '' ) { | |
| 329 | - $img = true; | |
| 330 | - | |
| 331 | - $class_li = 'bbp-atthumb'; | |
| 332 | - | |
| 333 | - $caption = bbpc_bba_o( 'image_thumbnail_caption' ) == 1; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - $item = '<li id="bbpcore-attach_' . $attachment->ID . '" class="bbpcore-attach bbpcore-attachment-' . $ext . ' ' . $class_li . '">'; | |
| 338 | - | |
| 339 | - if ( $html == '' ) { | |
| 340 | - $html = $filename; | |
| 341 | - $class_li = 'bbp-atticon bbp-atticon-' . $this->icon( $ext ); | |
| 342 | - } | |
| 343 | - | |
| 344 | - if ( $img ) { | |
| 345 | - | |
| 346 | - if ( $caption ) { | |
| 347 | - $item .= '<div style="width: ' . bbpc_bba_o( 'attachment_image_x' ) . 'px" class="wp-caption">'; | |
| 348 | - } | |
| 349 | - | |
| 350 | - $item .= '<a class="' . $class_a . $is_lighbox . '" href="' . $file_url . '" title="' . $a_title .'" '. $_download .'>' . $html . '</a>'; | |
| 351 | - | |
| 352 | - if ( $caption ) { | |
| 353 | - $a_title = '<a class="' . $is_lighbox . '" href="' . $file_url . '"' . $_download . '>' . $a_title . '</a>'; | |
| 354 | - | |
| 355 | - $item .= '<p class="wp-caption-text">' . $a_title . '<br/>' . $actions . '</p></div>'; | |
| 356 | - } | |
| 357 | - } else { | |
| 358 | - $item .= '<span role="presentation" class="' . $class_li . '"></span> '; | |
| 359 | - $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>'; | |
| 360 | - } | |
| 361 | - | |
| 362 | - $item .= '</li>'; | |
| 363 | - | |
| 364 | - if ( $img ) { | |
| 365 | - $thumbnails .= $item; | |
| 366 | - $images++; | |
| 367 | - } else { | |
| 368 | - $listing .= $item; | |
| 369 | - $files++; | |
| 370 | - } | |
| 371 | - } | |
| 372 | - | |
| 373 | - $thumbnails .= '</ol>'; | |
| 374 | - $listing .= '</ol>'; | |
| 375 | - | |
| 376 | - if ( $images > 0 ) { | |
| 377 | - $content .= $thumbnails; | |
| 378 | - } | |
| 379 | - | |
| 380 | - if ( $files > 0 ) { | |
| 381 | - $content .= $listing; | |
| 382 | - } | |
| 383 | - } | |
| 384 | - | |
| 385 | - $content .= '</div>'; | |
| 386 | - } | |
| 387 | - | |
| 388 | - if ( bbpc_is_user_admin() || bbpc_is_user_moderator() ) { | |
| 389 | - $errors = get_post_meta( $id, '_bbp_attachment_upload_error' ); | |
| 390 | - | |
| 391 | - if ( ! empty( $errors ) ) { | |
| 392 | - $content .= '<div class="bbp-attachments-errors">'; | |
| 393 | - $content .= '<h6>' . __( 'Upload Errors', 'bbp-core' ) . ':</h6>'; | |
| 394 | - $content .= '<ol'; | |
| 395 | - | |
| 396 | - $class_li = 'bbp-file-error'; | |
| 397 | - | |
| 398 | - $content .= ' class="with-icons"'; | |
| 399 | - $class_li .= ' bbp-atticon bbp-atticon-error'; | |
| 400 | - | |
| 401 | - $content .= '>'; | |
| 402 | - | |
| 403 | - foreach ( $errors as $error ) { | |
| 404 | - $content .= '<li class="' . $class_li . '"><span role="presentation" class="' . $class_li . '"></span> '; | |
| 405 | - $content .= '<div class="d4p-bbp-att-wrapper"><strong>' . esc_html( $error['file'] ) . '</strong>: ' . __( $error['message'], 'bbp-core' ) . '</div></li>'; | |
| 406 | - } | |
| 407 | - | |
| 408 | - $content .= '</ol></div>'; | |
| 409 | - } | |
| 410 | - } | |
| 411 | - | |
| 412 | - return $content; | |
| 413 | - } | |
| 414 | - | |
| 415 | - public function embed_form() { | |
| 416 | - $can_upload = apply_filters( 'bbpc_bbpressattchment_allow_upload', BBPCATTCore::instance()->is_user_allowed(), bbpc_get_forum_id() ); | |
| 417 | - if ( ! $can_upload ) { | |
| 418 | - return; | |
| 419 | - } | |
| 420 | - | |
| 421 | - $is_enabled = apply_filters( 'bbpc_bbpressattchment_forum_enabled', BBPCATTCore::instance()->enabled_for_forum(), bbpc_get_forum_id() ); | |
| 422 | - if ( ! $is_enabled ) { | |
| 423 | - return; | |
| 424 | - } | |
| 425 | - | |
| 426 | - $file_size = apply_filters( 'bbpc_bbpressattchment_max_file_size', BBPCATTCore::instance()->get_file_size(), bbpc_get_forum_id() ); | |
| 427 | - | |
| 428 | - include BBPCATTACHMENTS_PATH . 'forms/uploader.php'; | |
| 429 | - } | |
| 430 | -} | |
| 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 ( forumax_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 | + wp_register_style( 'bbpc-attachments', BBPCATTACHMENT_URL . 'css/front.css', [], FORUMAX_VERSION ); | |
| 76 | + wp_register_script( 'bbpc-attachments', BBPCATTACHMENT_URL . 'js/front.js', [ 'jquery' ], FORUMAX_VERSION, true ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + public function include_scripts_and_styles() { | |
| 80 | + wp_enqueue_style( 'bbpc-attachments' ); | |
| 81 | + wp_enqueue_script( 'bbpc-attachments' ); | |
| 82 | + | |
| 83 | + wp_localize_script( | |
| 84 | + 'bbpc-attachments', | |
| 85 | + 'bbpcAttachmentsInit', | |
| 86 | + [ | |
| 87 | + 'max_files' => apply_filters( 'bbpc_bbpressattchment_allow_upload', BBPCATTCore::instance()->get_max_files(), bbp_get_forum_id() ), | |
| 88 | + 'are_you_sure' => __( 'This operation is not reversible. Are you sure?', 'forumax' ), | |
| 89 | + ] | |
| 90 | + ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + public function wp_enqueue_scripts() { | |
| 94 | + if ( forumax_is_bbpress() ) { | |
| 95 | + $this->include_scripts_and_styles(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + public function edit_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) { | |
| 100 | + $this->edit_mode = true; | |
| 101 | + $this->process_attachments( 0, $topic_id, $forum_id, $anonymous_data, $topic_author ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + public function edit_reply( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) { | |
| 105 | + $this->edit_mode = true; | |
| 106 | + $this->process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + public function save_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) { | |
| 110 | + $this->edit_mode = false; | |
| 111 | + $this->process_attachments( 0, $topic_id, $forum_id, $anonymous_data, $topic_author ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + public function save_reply( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) { | |
| 115 | + $this->edit_mode = false; | |
| 116 | + $this->process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function process_attachments( $reply_id, $topic_id, $forum_id, $anonymous_data = null, $reply_author = null ) { | |
| 120 | + $uploads = []; | |
| 121 | + $revision = false; | |
| 122 | + | |
| 123 | + if ( ! empty( $_FILES ) && ! empty( $_FILES['bbpc_attachment'] ) ) { | |
| 124 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 125 | + | |
| 126 | + $errors = new gdbbp_Error(); | |
| 127 | + $overrides = [ | |
| 128 | + 'test_form' => false, | |
| 129 | + 'upload_error_handler' => 'bbpc_bbattachment_handle_upload_error', | |
| 130 | + ]; | |
| 131 | + | |
| 132 | + foreach ( $_FILES['bbpc_attachment']['error'] as $key => $error ) { | |
| 133 | + $file_name = $_FILES['bbpc_attachment']['name'][ $key ]; | |
| 134 | + | |
| 135 | + if ( $error == UPLOAD_ERR_OK ) { | |
| 136 | + $file = [ | |
| 137 | + 'name' => $file_name, | |
| 138 | + 'type' => $_FILES['bbpc_attachment']['type'][ $key ], | |
| 139 | + 'size' => $_FILES['bbpc_attachment']['size'][ $key ], | |
| 140 | + 'tmp_name' => $_FILES['bbpc_attachment']['tmp_name'][ $key ], | |
| 141 | + 'error' => $_FILES['bbpc_attachment']['error'][ $key ], | |
| 142 | + ]; | |
| 143 | + | |
| 144 | + $file_name = sanitize_file_name( $file_name ); | |
| 145 | + | |
| 146 | + if ( BBPCATTCore::instance()->is_right_size( $file, $forum_id ) ) { | |
| 147 | + $upload = wp_handle_upload( $file, $overrides ); | |
| 148 | + | |
| 149 | + if ( ! is_wp_error( $upload ) ) { | |
| 150 | + $uploads[] = $upload; | |
| 151 | + } else { | |
| 152 | + $errors->add( 'wp_upload', $upload->errors['wp_upload_error'][0], $file_name ); | |
| 153 | + } | |
| 154 | + } else { | |
| 155 | + $errors->add( 'bbpc_upload', 'File exceeds allowed file size.', $file_name ); | |
| 156 | + } | |
| 157 | + } else { | |
| 158 | + switch ( $error ) { | |
| 159 | + default: | |
| 160 | + case 'UPLOAD_ERR_NO_FILE': | |
| 161 | + $errors->add( 'php_upload', 'File not uploaded.', $file_name ); | |
| 162 | + break; | |
| 163 | + case 'UPLOAD_ERR_INI_SIZE': | |
| 164 | + $errors->add( 'php_upload', 'Upload file size exceeds PHP maximum file size allowed.', $file_name ); | |
| 165 | + break; | |
| 166 | + case 'UPLOAD_ERR_FORM_SIZE': | |
| 167 | + $errors->add( 'php_upload', 'Upload file size exceeds FORM specified file size.', $file_name ); | |
| 168 | + break; | |
| 169 | + case 'UPLOAD_ERR_PARTIAL': | |
| 170 | + $errors->add( 'php_upload', 'Upload file only partially uploaded.', $file_name ); | |
| 171 | + break; | |
| 172 | + case 'UPLOAD_ERR_CANT_WRITE': | |
| 173 | + $errors->add( 'php_upload', 'Can\'t write file to the disk.', $file_name ); | |
| 174 | + break; | |
| 175 | + case 'UPLOAD_ERR_NO_TMP_DIR': | |
| 176 | + $errors->add( 'php_upload', 'Temporary folder for upload is missing.', $file_name ); | |
| 177 | + break; | |
| 178 | + case 'UPLOAD_ERR_EXTENSION': | |
| 179 | + $errors->add( 'php_upload', 'Server extension restriction stopped upload.', $file_name ); | |
| 180 | + break; | |
| 181 | + } | |
| 182 | + } | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 186 | + $post_id = $reply_id == 0 ? $topic_id : $reply_id; | |
| 187 | + | |
| 188 | + if ( ! empty( $errors->errors ) ) { | |
| 189 | + foreach ( $errors->errors as $code => $errs ) { | |
| 190 | + foreach ( $errs as $error ) { | |
| 191 | + if ( $error[0] != '' && $error[1] != '' ) { | |
| 192 | + add_post_meta( | |
| 193 | + $post_id, | |
| 194 | + '_bbp_attachment_upload_error', | |
| 195 | + [ | |
| 196 | + 'code' => $code, | |
| 197 | + 'file' => $error[1], | |
| 198 | + 'message' => $error[0], | |
| 199 | + ] | |
| 200 | + ); | |
| 201 | + } | |
| 202 | + } | |
| 203 | + } | |
| 204 | + | |
| 205 | + $revision = true; | |
| 206 | + } | |
| 207 | + | |
| 208 | + if ( ! empty( $uploads ) ) { | |
| 209 | + require_once ABSPATH . 'wp-admin/includes/media.php'; | |
| 210 | + require_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 211 | + | |
| 212 | + foreach ( $uploads as $upload ) { | |
| 213 | + $wp_filetype = wp_check_filetype( basename( $upload['file'] ), null ); | |
| 214 | + $attachment = [ | |
| 215 | + 'post_mime_type' => $wp_filetype['type'], | |
| 216 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $upload['file'] ) ), | |
| 217 | + 'post_content' => '', | |
| 218 | + 'post_status' => 'inherit', | |
| 219 | + ]; | |
| 220 | + | |
| 221 | + $attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id ); | |
| 222 | + $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] ); | |
| 223 | + | |
| 224 | + wp_update_attachment_metadata( $attach_id, $attach_data ); | |
| 225 | + update_post_meta( $attach_id, '_bbp_attachment', '1' ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + $revision = true; | |
| 229 | + } | |
| 230 | + | |
| 231 | + if ( $this->edit_mode && $revision ) { | |
| 232 | + add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'post_has_changed' ] ); | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + public function post_has_changed() { | |
| 237 | + remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'post_has_changed' ] ); | |
| 238 | + | |
| 239 | + return true; | |
| 240 | + } | |
| 241 | + | |
| 242 | + public function show_attachments_icon() { | |
| 243 | + $topic_id = bbp_get_topic_id(); | |
| 244 | + $count = bbpc_topic_attachments_count( $topic_id, true ); | |
| 245 | + | |
| 246 | + if ( $count > 0 ) { | |
| 247 | + echo '<span class="bbp-attachments-count" title="' . esc_attr( $count . ' ' . _n( 'attachment', 'attachments', $count, 'forumax' ) ) . '"></span>'; | |
| 248 | + } | |
| 249 | + | |
| 250 | + } | |
| 251 | + | |
| 252 | + public function embed_attachments( $content, $id ) { | |
| 253 | + global $user_ID; | |
| 254 | + | |
| 255 | + $attachments = forumax_get_post_attachments( $id ); | |
| 256 | + | |
| 257 | + $post = get_post( $id ); | |
| 258 | + $author_id = $post->post_author; | |
| 259 | + | |
| 260 | + if ( ! empty( $attachments ) ) { | |
| 261 | + $content .= '<div class="bbp-attachments">'; | |
| 262 | + $content .= '<h6>' . __( 'Attachments', 'forumax' ) . ':</h6>'; | |
| 263 | + | |
| 264 | + $_download = ' download'; | |
| 265 | + | |
| 266 | + if ( ! is_user_logged_in() && BBPCATTCore::instance()->is_hidden_from_visitors() ) { | |
| 267 | + /* translators: %s: Login URL */ | |
| 268 | + $content .= sprintf( __( "You must be <a href='%s'>logged in</a> to view attached files.", 'forumax' ), wp_login_url( get_permalink() ) ); | |
| 269 | + } else { | |
| 270 | + $listing = '<ol'; | |
| 271 | + $thumbnails = '<ol'; | |
| 272 | + | |
| 273 | + $listing .= ' class="with-icons d4p-bbp-listing"'; | |
| 274 | + $thumbnails .= ' class="with-icons d4p-bba-thumbnails"'; | |
| 275 | + | |
| 276 | + $listing .= '>'; | |
| 277 | + $thumbnails .= '>'; | |
| 278 | + $images = $files = 0; | |
| 279 | + | |
| 280 | + foreach ( $attachments as $attachment ) { | |
| 281 | + $actions = []; | |
| 282 | + | |
| 283 | + $url = add_query_arg( '_wpnonce', wp_create_nonce( 'd4p-bbpress-attachments' ) ); | |
| 284 | + $url = add_query_arg( 'att_id', $attachment->ID, $url ); | |
| 285 | + $url = add_query_arg( 'bbp_id', $id, $url ); | |
| 286 | + | |
| 287 | + $allow = 'no'; | |
| 288 | + if ( forumax_is_user_admin() ) { | |
| 289 | + $allow = 'delete'; | |
| 290 | + } elseif ( forumax_is_user_moderator() ) { | |
| 291 | + $allow = 'delete'; | |
| 292 | + } elseif ( $author_id == $user_ID ) { | |
| 293 | + $allow = false; | |
| 294 | + } | |
| 295 | + | |
| 296 | + if ( $allow == 'delete' || $allow == 'both' ) { | |
| 297 | + $actions[] = '<a class="d4p-bba-action-delete" href="' . esc_url( add_query_arg( 'd4pbbaction', 'delete', $url ) ) . '">' . __( 'delete', 'forumax' ) . '</a>'; | |
| 298 | + } | |
| 299 | + | |
| 300 | + if ( $allow == 'detach' || $allow == 'both' ) { | |
| 301 | + $actions[] = '<a class="d4p-bba-action-detach" href="' . esc_url( add_query_arg( 'd4pbbaction', 'detach', $url ) ) . '">' . __( 'detach', 'forumax' ) . '</a>'; | |
| 302 | + } | |
| 303 | + | |
| 304 | + if ( count( $actions ) > 0 ) { | |
| 305 | + $actions = ' <span class="d4p-bba-actions">[' . join( ' | ', $actions ) . ']</span>'; | |
| 306 | + } else { | |
| 307 | + $actions = ''; | |
| 308 | + } | |
| 309 | + | |
| 310 | + $file = get_attached_file( $attachment->ID ); | |
| 311 | + $ext = pathinfo( $file, PATHINFO_EXTENSION ); | |
| 312 | + $filename = pathinfo( $file, PATHINFO_BASENAME ); | |
| 313 | + $file_url = wp_get_attachment_url( $attachment->ID ); | |
| 314 | + | |
| 315 | + $html = $class_li = $class_a = $rel_a = ''; | |
| 316 | + $a_title = $filename; | |
| 317 | + $caption = false; | |
| 318 | + | |
| 319 | + $is_lighbox = ''; | |
| 320 | + if ( forumax_is_premium() ) { | |
| 321 | + $is_lighbox = forumax_get_opt( 'image_link_type' ) == 'lightbox' ? ' bbpc-lightbox' : ''; | |
| 322 | + $_download = forumax_get_opt( 'image_link_type' ) == 'lightbox' ? '' : $_download; | |
| 323 | + } | |
| 324 | + | |
| 325 | + $img = false; | |
| 326 | + if ( forumax_get_opt( 'attachment_image_x', false ) && forumax_get_opt( 'attachment_image_y', false ) ) { | |
| 327 | + $html = wp_get_attachment_image( $attachment->ID, 'bbpc-attachment-thumb' ); | |
| 328 | + | |
| 329 | + if ( $html != '' ) { | |
| 330 | + $img = true; | |
| 331 | + | |
| 332 | + $class_li = 'bbp-atthumb'; | |
| 333 | + | |
| 334 | + $caption = forumax_bba_o( 'image_thumbnail_caption' ) == 1; | |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + $item = '<li id="bbpcore-attach_' . $attachment->ID . '" class="bbpcore-attach bbpcore-attachment-' . $ext . ' ' . $class_li . '">'; | |
| 339 | + | |
| 340 | + if ( $html == '' ) { | |
| 341 | + $html = $filename; | |
| 342 | + $class_li = 'bbp-atticon bbp-atticon-' . $this->icon( $ext ); | |
| 343 | + } | |
| 344 | + | |
| 345 | + if ( $img ) { | |
| 346 | + | |
| 347 | + if ( $caption ) { | |
| 348 | + $item .= '<div style="width: ' . forumax_bba_o( 'attachment_image_x' ) . 'px" class="wp-caption">'; | |
| 349 | + } | |
| 350 | + | |
| 351 | + $item .= '<a class="' . $class_a . $is_lighbox . '" href="' . $file_url . '" title="' . $a_title .'" '. $_download .'>' . $html . '</a>'; | |
| 352 | + | |
| 353 | + if ( $caption ) { | |
| 354 | + $a_title = '<a class="' . $is_lighbox . '" href="' . $file_url . '"' . $_download . '>' . $a_title . '</a>'; | |
| 355 | + | |
| 356 | + $item .= '<p class="wp-caption-text">' . $a_title . '<br/>' . $actions . '</p></div>'; | |
| 357 | + } | |
| 358 | + } else { | |
| 359 | + $item .= '<span role="presentation" class="' . $class_li . '"></span> '; | |
| 360 | + $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>'; | |
| 361 | + } | |
| 362 | + | |
| 363 | + $item .= '</li>'; | |
| 364 | + | |
| 365 | + if ( $img ) { | |
| 366 | + $thumbnails .= $item; | |
| 367 | + $images++; | |
| 368 | + } else { | |
| 369 | + $listing .= $item; | |
| 370 | + $files++; | |
| 371 | + } | |
| 372 | + } | |
| 373 | + | |
| 374 | + $thumbnails .= '</ol>'; | |
| 375 | + $listing .= '</ol>'; | |
| 376 | + | |
| 377 | + if ( $images > 0 ) { | |
| 378 | + $content .= $thumbnails; | |
| 379 | + } | |
| 380 | + | |
| 381 | + if ( $files > 0 ) { | |
| 382 | + $content .= $listing; | |
| 383 | + } | |
| 384 | + } | |
| 385 | + | |
| 386 | + $content .= '</div>'; | |
| 387 | + } | |
| 388 | + | |
| 389 | + if ( forumax_is_user_admin() || forumax_is_user_moderator() ) { | |
| 390 | + $errors = get_post_meta( $id, '_bbp_attachment_upload_error' ); | |
| 391 | + | |
| 392 | + if ( ! empty( $errors ) ) { | |
| 393 | + $content .= '<div class="bbp-attachments-errors">'; | |
| 394 | + $content .= '<h6>' . __( 'Upload Errors', 'forumax' ) . ':</h6>'; | |
| 395 | + $content .= '<ol'; | |
| 396 | + | |
| 397 | + $class_li = 'bbp-file-error'; | |
| 398 | + | |
| 399 | + $content .= ' class="with-icons"'; | |
| 400 | + $class_li .= ' bbp-atticon bbp-atticon-error'; | |
| 401 | + | |
| 402 | + $content .= '>'; | |
| 403 | + | |
| 404 | + foreach ( $errors as $error ) { | |
| 405 | + $content .= '<li class="' . $class_li . '"><span role="presentation" class="' . $class_li . '"></span> '; | |
| 406 | + $content .= '<div class="d4p-bbp-att-wrapper"><strong>' . esc_html( $error['file'] ) . '</strong>: ' . esc_html( $error['message'] ) . '</div></li>'; | |
| 407 | + } | |
| 408 | + | |
| 409 | + $content .= '</ol></div>'; | |
| 410 | + } | |
| 411 | + } | |
| 412 | + | |
| 413 | + return $content; | |
| 414 | + } | |
| 415 | + | |
| 416 | + public function embed_form() { | |
| 417 | + $can_upload = apply_filters( 'bbpc_bbpressattchment_allow_upload', BBPCATTCore::instance()->is_user_allowed(), forumax_get_forum_id() ); | |
| 418 | + if ( ! $can_upload ) { | |
| 419 | + return; | |
| 420 | + } | |
| 421 | + | |
| 422 | + $is_enabled = apply_filters( 'bbpc_bbpressattchment_forum_enabled', BBPCATTCore::instance()->enabled_for_forum(), forumax_get_forum_id() ); | |
| 423 | + if ( ! $is_enabled ) { | |
| 424 | + return; | |
| 425 | + } | |
| 426 | + | |
| 427 | + $file_size = apply_filters( 'bbpc_bbpressattchment_max_file_size', BBPCATTCore::instance()->get_file_size(), forumax_get_forum_id() ); | |
| 428 | + | |
| 429 | + include BBPCATTACHMENTS_PATH . 'forms/uploader.php'; | |
| 430 | + } | |
| 431 | +} | |
| 432 | + | |