This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dw-document-revisions.php
1429 lines (1091 loc) · 41.9 KB
/
dw-document-revisions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: DW Document Revisions
Description: A document management and version control plugin for WordPress that allows teams of any size to collaboratively edit files and manage their workflow. Forked from the WP Document Revisions Plugin by Ben Balter (http://ben.balter.com).
Version: 2.1.0
Author: Ministry of Justice
License: GPL3
*/
/* DW Document Revisions
*
* A document management and version control plugin for WordPress that allows
* teams of any size to collaboratively edit files and manage their workflow.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright 2011-2014
* @license GPL v3
* @version 1.0.0
* @package DW_Document_Revisions
*/
class Document_Revisions {
static $instance;
public $version = '2.1.0';
/**
* Initiates an instance of the class and adds hooks
* @since 0.5
*/
function __construct() {
self::$instance = &$this;
//admin
add_action( 'init', array( &$this, 'admin_init' ) );
add_action( 'init', array( &$this, 'i18n' ), 5 );
//CPT/CT
add_action( 'init', array( &$this, 'register_cpt' ) );
register_activation_hook( __FILE__, array( &$this, 'add_caps' ) );
add_filter( 'the_content', array( &$this, 'content_filter' ), 1 );
add_action( 'wp_loaded', array( &$this, 'register_term_count_cb' ), 100, 1 );
//rewrites and permalinks
add_filter( 'rewrite_rules_array' , array( &$this, 'revision_rewrite' ) );
add_filter( 'init', array( &$this, 'inject_rules' ) );
add_action( 'post_type_link', array(&$this, 'permalink'), 10, 4 );
add_action( 'post_link', array(&$this, 'permalink'), 10, 4 );
add_filter( 'template_include', array(&$this, 'serve_file'), 10, 1 );
add_filter( 'serve_document_auth', array( &$this, 'serve_document_auth'), 10, 3 );
add_action( 'parse_request', array( &$this, 'ie_cache_fix' ) );
add_filter( 'query_vars', array(&$this, 'add_query_var'), 10, 4 );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );
add_filter( 'get_sample_permalink_html', array(&$this, 'sample_permalink_html_filter'), 10, 4);
add_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ), 10, 2 );
add_filter( 'image_downsize', array( &$this, 'image_downsize'), 10, 3 );
add_filter( 'document_path', array( &$this, 'wamp_document_path_filter' ), 9, 1 );
add_filter( 'redirect_canonical', array( &$this, 'redirect_canonical_filter' ), 10, 2 );
// Format post titles
add_filter( 'private_title_format', array( &$this, 'no_title_prepend' ), 20, 1 );
add_filter( 'protected_title_format', array( &$this, 'no_title_prepend' ), 20, 1 );
add_filter( 'the_title', array( &$this, 'add_revision_num_to_title' ), 20, 1 );
//uploads
add_filter( 'upload_dir', array( &$this, 'document_upload_dir_filter' ), 10, 2);
add_filter( 'attachment_link', array( &$this, 'attachment_link_filter'), 10, 2);
add_filter( 'wp_handle_upload_prefilter', array(&$this, 'filename_rewrite' ) );
add_filter( 'wp_handle_upload', array( &$this, 'rewrite_file_url' ), 10, 2);
//cache
add_action( 'save_post', array( &$this, 'clear_cache' ), 10, 1 );
//File Restrict
add_filter('gettext', array(&$this, 'alter_file_type_error_text'), 10, 3);
//load front-end features (shortcode, widgets, etc.)
include dirname( __FILE__ ) . '/includes/front-end.php';
new Document_Revisions_Front_End( $this );
}
/**
* Init i18n files
* Must be done early on init because they need to be in place when register_cpt is called
*/
function i18n() {
load_plugin_textdomain( 'wp-document-revisions', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Extends class with admin functions when in admin backend
* @since 0.5
*/
function admin_init() {
//only fire on admin + escape hatch to prevent fatal errors
if ( !is_admin() || class_exists( 'Document_Revisions_Admin' ) )
return;
include dirname( __FILE__ ) . '/includes/admin.php';
$this->admin = new Document_Revisions_Admin( self::$instance );
}
/**
* Registers the document custom post type
* @since 0.5
*/
function register_cpt() {
$labels = array(
'name' => _x( 'Documents', 'post type general name', 'wp-document-revisions' ),
'singular_name' => _x( 'Document', 'post type singular name', 'wp-document-revisions' ),
'add_new' => _x( 'Add Document', 'document', 'wp-document-revisions' ),
'add_new_item' => __( 'Add New Document', 'wp-document-revisions' ),
'edit_item' => __( 'Edit Document', 'wp-document-revisions' ),
'new_item' => __( 'New Document', 'wp-document-revisions' ),
'view_item' => __( 'View Document', 'wp-document-revisions' ),
'search_items' => __( 'Search Documents', 'wp-document-revisions' ),
'not_found' => __( 'No documents found', 'wp-document-revisions' ),
'not_found_in_trash' => __( 'No documents found in Trash', 'wp-document-revisions' ),
'parent_item_colon' => '',
'menu_name' => __( 'Documents MoJ', 'wp-document-revisions' ),
'all_items' => __( 'All Documents', 'wp-document-revisions' ),
);
$args = array(
'labels' => $labels,
'publicly_queryable' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => array( 'document', 'documents'),
'map_meta_cap' => true,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'register_meta_box_cb' => array( &$this->admin, 'meta_cb' ),
'supports' => array( 'title', 'author', 'revisions', 'excerpt', 'custom-fields' ),
'menu_icon' => plugins_url( '/img/menu-icon.png', __FILE__ ),
);
register_post_type( 'document', apply_filters( 'document_revisions_cpt', $args ) );
}
/**
* Defaults document visibility to private
* @since 0.5
*/
function make_private() {
global $post;
//verify that this is a new document
if ( !isset( $post) || !$this->verify_post_type() || strlen( $post->post_content ) > 0 )
return;
$post_pre = clone $post;
if ( $post->post_status == 'draft' || $post->post_status == 'auto-draft' )
$post->post_status = 'private';
$post = apply_filters( 'document_to_private', $post, $post_pre );
}
/**
* Given a post object, returns all attached uploads
* @since 0.5
* @param object $post (optional) post object
* @return object all attached uploads
*/
function get_attachments( $post = '' ) {
if ($post == '')
global $post;
//verify that it's an object
if ( !is_object( $post ) )
$post = get_post( $post );
//check for revisions
if ( $parent = wp_is_post_revision( $post ) )
$post = get_post( $parent );
//check for attachments
if ( $post->post_type == 'attachment' )
$post = get_post( $post->post_parent );
if ( !isset( $post->ID ) )
return array();
$args = array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'order' => 'DESC',
'orderby' => 'post_date',
);
$args = apply_filters( 'document_revision_query', $args );
return get_children( $args );
}
/**
* Checks if document is locked, if so, returns the lock holder's name
* @since 0.5
* @param object|int $post the post object or postID
* @return bool|string false if no lock, user's display name if locked
*/
function get_document_lock( $post ) {
if ( !is_object( $post ) )
$post = get_post( $post );
if ( !$post )
return false;
//get the post lock
if ( !( $user = wp_check_post_lock( $post->ID ) ) )
$user = false;
//allow others to shortcircuit
$user = apply_filters( 'document_lock_check', $user, $post );
if ( !$user )
return false;
//get displayname from userID
$last_user = get_userdata( $user );
return ( $last_user ) ? $last_user->display_name : __( 'Somebody' );
}
/**
* Given a file, returns the file's extension
* @since 0.5
* @param string $file URL, path, or filename to file
* @return string extension
*/
function get_extension( $file ) {
$extension = '.' . pathinfo( $file, PATHINFO_EXTENSION );
//don't return a . extension
if ( $extension == '.' )
return '';
return apply_filters( 'document_extension', $extension, $file );
}
/**
* Gets a file extension from a post
* @since 0.5
* @param object|int $post document or attachment
* @return string the extension to the latest revision
*/
function get_file_type( $post = '' ) {
if ( $post == '' )
global $post;
if ( !is_object( $post ) )
$post = get_post( $post );
//note, changing $post here would break $post in the global scope
// rename $post to attachment, or grab the attachment from $post
// either way, $attachment is now the object we're looking to query
if ( get_post_type( $post ) == 'attachment' ) {
$attachment = $post;
} else if ( get_post_type( $post ) == 'document' ) {
$latest_revision = $this->get_latest_revision( $post->ID );
// verify a previous revision exists
if ( !$latest_revision )
return '';
$attachment = get_post( $latest_revision->post_content );
//sanity check in case post_content somehow doesn't represent an attachment,
// or in case some sort of non-document, non-attachment object/ID was passed
if ( get_post_type( $attachment ) != 'attachment' )
return '';
}
return $this->get_extension( get_attached_file( $attachment->ID ) );
}
/**
* Adds document CPT rewrite rules
* @since 0.5
*/
function inject_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( "%document%", '([^.]+)\.[A-Za-z0-9]{3,4}?', 'document=' );
}
/**
* Adds document rewrite rules to the rewrite array
* @since 0.5
* @param $rules array rewrite rules
* @return array rewrite rules
*/
function revision_rewrite( $rules ) {
$slug = $this->document_slug();
$my_rules = array();
//revisions in the form of yyyy/mm/[slug]-revision-##.[extension], yyyy/mm/[slug]-revision-##.[extension]/, yyyy/mm/[slug]-revision-##/ and yyyy/mm/[slug]-revision-##
$my_rules[ $slug . '/([0-9]{4})/([0-9]{1,2})/([^.]+)-' . __( 'revision', 'wp-document-revisions' ) . '-([0-9]+)\.[A-Za-z0-9]{3,4}/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&document=$matches[3]&revision=$matches[4]';
//documents in the form of yyyy/mm/[slug]-revision-##.[extension], yyyy/mm/[slug]-revision-##.[extension]/
$my_rules[ $slug . '/([0-9]{4})/([0-9]{1,2})/([^.]+)\.[A-Za-z0-9]{3,4}/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&document=$matches[3]';
// site.com/documents/ should list all documents that user has access to (private, public)
$my_rules[ $slug . '/?$'] = 'index.php?post_type=document';
$my_rules[ $slug . '/page/?([0-9]{1,})/?$'] = 'index.php?post_type=document&paged=$matches[1]';
$my_rules = apply_filters( 'document_rewrite_rules', $my_rules, $rules );
return $my_rules + $rules;
}
/**
* Tell's WP to recognize document query vars
* @since 0.5
* @param array $vars the query vars
* @return array the modified query vars
*/
function add_query_var( $vars ) {
$vars[] = "revision";
$vars[] = "document";
return $vars;
}
/**
* Builds document post type permalink
* @since 0.5
* @param string $link original permalink
* @param object $post post object
* @param unknown $leavename
* @param unknown $sample (optional)
* @return string the real permalink
*/
function permalink( $link, $post, $leavename, $sample = '' ) {
global $wp_rewrite;
$revision_num = false;
//if this isn't our post type, kick
if ( !$this->verify_post_type( $post ) )
return $link;
//check if it's a revision
if ( $post->post_type == 'revision' ) {
$parent = clone get_post( $post->post_parent );
$revision_num = $this->get_revision_number( $post->ID );
$parent->post_name = $parent->post_name . __('-revision-', 'wp-document-revisions') . $revision_num;
$post = $parent;
}
//if no permastruct
if ( $wp_rewrite->permalink_structure == '' || in_array( $post->post_status, array( 'pending', 'draft' ) ) ) {
$link = site_url( '?post_type=document&p=' . $post->ID );
if ( $revision_num ) $link = add_query_arg( 'revision', $revision_num, $link );
return apply_filters( 'document_permalink', $link, $post );
}
// build documents/yyyy/mm/slug
$extension = $this->get_file_type( $post );
$timestamp = strtotime($post->post_date);
$link = home_url() . '/' . $this->document_slug() .'/' . date('Y', $timestamp) . '/' . date('m', $timestamp) . '/';
$link .= ( $leavename ) ? '%document%' : $post->post_name;
$link .= $extension;
$link = apply_filters( 'document_permalink', $link, $post );
return $link;
}
/**
* Filters permalink displayed on edit screen in the event that there is no attachment yet uploaded
* @rerurns string modified HTML
* @since 0.5
* @param string $html original HTML
* @param int $id Post ID
* @return unknown
*/
function sample_permalink_html_filter( $html, $id ) {
$post = get_post( $id );
//verify post type
if ( !$this->verify_post_type( $post ) )
return $html;
//grab attachments
$attachments = $this->get_attachments( $post );
//if no attachments, return nothing
if ( empty( $attachments ) )
return '';
//otherwise return html unfiltered
return $html;
}
/**
* Retrieves all revisions for a given post (including the current post)
* Workaround for #16215 to ensure revision author is accurate
* http://core.trac.wordpress.org/ticket/16215
* @since 1.0
* @param int $postID the post ID
* @return array array of post objects
*/
function get_revisions( $postID ) {
// Revision authors are actually shifted by one
// This moves each revision author up one, and then uses the post_author as the initial revision
//get the actual post
$post = get_post( $postID );
if ( !$post )
return false;
if ( $cache = wp_cache_get( $postID, 'document_revisions' ) )
return $cache;
//correct the modified date
$post->post_date = date( 'Y-m-d H:i:s', (int) get_post_modified_time( 'U', null, $postID ) );
//grab the post author
$post_author = $post->post_author;
//fix for Quotes in the most recent post because it comes from get_post
$post->post_excerpt = html_entity_decode( $post->post_excerpt );
//get revisions, and prepend the post
$revs = wp_get_post_revisions( $postID, array( 'order' => 'DESC' ) );
array_unshift( $revs, $post );
//loop through revisions
foreach ( $revs as $ID => &$rev ) {
//if this is anything other than the first revision, shift author 1
if ( $ID < sizeof( $revs ) - 1)
$rev->post_author = $revs[$ID+1]->post_author;
//if last revision, get the post author
else
$rev->post_author = $post_author;
}
wp_cache_set( $postID, $revs, 'document_revisions' );
return $revs;
}
/**
* For a given post, builds a 1-indexed array of revision post ID's
* @since 0.5
* @param int $post_id the parent post id
* @return array array of revisions
*/
function get_revision_indices( $post_id ) {
if ( $cache = wp_cache_get( $post_id, 'document_revision_indices' ) )
return $cache;
$revs = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
$i = 1;
foreach ( $revs as $rev )
$output[ $i++ ] = $rev->ID;
wp_cache_set( $post_id, $output, 'document_revision_indices' );
return $output;
}
/**
* Given a revision id (post->ID) returns the revisions spot in the sequence
* @since 0.5
* @param unknown $revision_id
* @return int revision #
*/
function get_revision_number( $revision_id ) {
$revision = get_post( $revision_id );
if ( !isset( $revision->post_parent ) )
return false;
$index = $this->get_revision_indices( $revision->post_parent );
return array_search( $revision_id, $index );
}
/**
* Given a revision number (e.g., 4 from foo-revision-4) returns the revision ID
* @since 0.5
* @param int $revision_num the 1-indexed revision #
* @param int $post_id the ID of the parent post
* @return int the ID of the revision
*/
function get_revision_id( $revision_num, $post_id ) {
$index = $this->get_revision_indices( $post_id );
return ( isset( $index[ $revision_num ] ) ) ? $index[ $revision_num ] : false;
}
/**
* Serves document files
* @since 0.5
* @param unknown $template
* @return unknown
*/
function serve_file( $template ) {
global $post;
global $wp_query;
global $wp;
if ( !is_single() )
return $template;
if ( !$this->verify_post_type( $post ) )
return $template;
//if this is a passworded document and no password is sent
//use the normal template which should prompt for password
if ( post_password_required( $post ) )
return $template;
//grab the post revision if any
$version = get_query_var( 'revision' );
//if there's not a post revision given, default to the latest
if ( !$version )
$rev_id = $this->get_latest_revision( $post->ID );
else
$rev_id = $this->get_revision_id ( $version, $post->ID );
$rev_post = get_post ( $rev_id );
$revision = get_post( $rev_post->post_content ); //@todo can this be simplified?
$file = get_attached_file( $revision->ID );
//flip slashes for WAMP settups to prevent 404ing on the next line
$file = apply_filters( 'document_path', $file );
//return 404 if the file is a dud or malformed
if ( !is_file( $file ) ) {
//note: this message will log to apache's php error log and/or to the screen/debug bar
trigger_error( "Unable to read file '$file' while attempting to serve the document '$post->post_title'" );
//this will send 404 and no cache headers
//and tell wp_query that this is a 404 so that is_404() works as expected
//and theme formats appropriatly
$wp_query->posts = array();
$wp_query->queried_object = null;
$wp->handle_404();
//tell WP to serve the theme's standard 404 template, this is a filter after all...
return get_404_template();
}
//note: authentication is happeneing via a hook here to allow shortcircuiting
if ( ! apply_filters( 'serve_document_auth', true, $post, $version ) ) {
wp_die( __( 'You are not authorized to access that file.', 'wp-document-revisions' ) , null, array( 'response' => 403 ) );
return false; //for unit testing
}
do_action( 'serve_document', $post->ID, $file );
// We may override this later.
status_header( 200 );
//rest inspired by wp-includes/ms-files.php.
$mime = wp_check_filetype( $file );
if ( false === $mime[ 'type' ] && function_exists( 'mime_content_type' ) )
$mime[ 'type' ] = mime_content_type( $file );
if ( $mime[ 'type' ] )
$mimetype = $mime[ 'type' ];
else
$mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
//fake the filename
$filename = $post->post_name;
$filename .= ( $version == '' ) ? '' : __( '-revision-', 'wp-document-revisions' ) . $version;
//we want the true attachment URL, not the permalink, so temporarily remove our filter
remove_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ) );
$filename .= $this->get_extension( wp_get_attachment_url( $revision->ID ) );
add_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ), 10, 2 );
//Set content-disposition header. Two options here:
// "attachment" -- force save-as dialog to pop up when file is downloaded (pre 1.3.1 default)
// "inline" -- attempt to open in browser (e.g., PDFs), if not possible, prompt with save as (1.3.1+ default)
$disposition = ( apply_filters( 'document_content_disposition_inline', true ) ) ? 'inline' : 'attachment';
@header( 'Content-Disposition: ' . $disposition . '; filename="' . $filename . '"' );
//filetype and length
@header( 'Content-Type: ' . $mimetype ); // always send this
@header( 'Content-Length: ' . filesize( $file ) );
//modified
@header( 'Cache-Control: no-cache' );
@header( 'Pragma: no-cache' );
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"';
@header( "Last-Modified: $last_modified GMT" );
@header( 'ETag: ' . $etag );
// Support for Conditional GET
$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
// Make a timestamp for our most recent modification...
$modified_timestamp = strtotime($last_modified);
if ( ( $client_last_modified && $client_etag )
? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) )
: ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) )
) {
status_header( 304 );
return;
}
//in case this is a large file, remove PHP time limits
@set_time_limit( 0 );
// clear output buffer to prevent other plugins from corrupting the file
if (ob_get_level()) {
ob_clean();
flush();
}
// If we made it this far, just serve the file
readfile( $file );
return;
}
/**
* Filter to authenticate document delivery
* @param bool $default true unless overridden by prior filter
* @param obj $post the post object
* @param bool|int $version version of the document being served, if any
* @return unknown
*/
function serve_document_auth( $default, $post, $version ) {
//public file, not a revision, no need to go any further
//note: non-authenticated users only have the "read" cap, so can't auth via read_document
if ( ! $version && $post->post_status == 'publish' )
return $default;
//attempting to access a revision
if ( $version && ! current_user_can( 'read_document_revisions' ) )
return false;
//specific document cap check
if ( ! current_user_can( 'read_document', $post->ID ) )
return false;
return $default;
}
/**
* Depricated for consistency of terms
* @param unknown $id
* @return unknown
*/
function get_latest_version( $id ) {
_deprecated_function( __FUNCTION__, '1.0.3 of WP Document Revisions', 'get_latest_version' );
return $this->get_latest_revision( $id );
}
/**
* Given a post ID, returns the latest revision attachment
* @param unknown $post
* @return object latest revision object
*/
function get_latest_revision( $post ) {
if ( is_object( $post ) )
$post = $post->ID;
$revisions = $this->get_revisions( $post );
if ( !$revisions )
return false;
//verify that there's an upload ID in the content field
//if there's no upload ID for some reason, default to latest attached upload
$revision_found = false;
$revision = '';
foreach ($revisions as $rev) {
if (is_numeric( $rev->post_content ) && !$revision_found) {
$revision_found = true;
$revision = $rev;
}
}
if (!$revision_found) {
$attachments = $this->get_attachments( $post );
if ( empty( $attachments ) )
return false;
$latest_attachment = reset( $attachments );
$revisions[0]->post_content = $latest_attachment->ID;
$revision = $revisions[0];
}
return $revision;
}
/**
* Deprecated for consistency sake
* @param unknown $id
* @return unknown
*/
function get_latest_version_url( $id ) {
_deprecated_function( __FUNCTION__, '1.0.3 of WP Document Revisions', 'get_latest_revision_url' );
return $this->get_latest_revision_url( $id );
}
/**
* Returns the URL to a post's latest revision
* @since 0.5
* @param int $id post ID
* @return string|bool URL to revision or false if no attachment
*/
function get_latest_revision_url( $id ) {
$latest = $this->get_latest_revision( $id );
if ( !$latest )
return false;
//temporarily remove our filter to get the true URL, not the permalink
remove_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ) );
$url = wp_get_attachment_url( $latest->post_content );
add_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ), 10, 2 );
return $url;
}
/**
* Calculated path to upload documents
* @since 0.5
* @return string path to document
*/
function document_upload_dir() {
global $wpdb;
//grab unfiltered defaults
remove_filter( 'upload_dir', array( &$this, 'document_upload_dir_filter' ), 10, 2);
$defaults = wp_upload_dir();
add_filter( 'upload_dir', array( &$this, 'document_upload_dir_filter' ), 10, 2);
//If no options set, default to normal upload dir
if ( !( $dir = get_site_option( 'document_upload_directory' ) ) )
return $defaults['basedir'];
if ( !is_multisite() )
return $dir;
//make site specific on multisite
if ( is_multisite() && !is_network_admin() )
$dir = str_replace( '%site_id%', $wpdb->blogid, $dir );
return $dir;
}
/**
*
* @return unknown
*/
function document_slug() {
$slug = get_site_option( 'document_slug' );
if ( ! $slug )
$slug = 'documents';
return apply_filters( 'document_slug', $slug );
}
/**
* Modifies location of uploaded document revisions
* @since 0.5
* @param array $dir defaults passed from WP
* @return array $dir modified directory
*/
function document_upload_dir_filter( $dir ) {
if ( !$this->verify_post_type ( ) )
return $dir;
$dir['path'] = $this->document_upload_dir() . $dir['subdir'];
$dir['url'] = home_url( '/' . $this->document_slug() ) . $dir['subdir'];
$dir['basedir'] = $this->document_upload_dir();
$dir['baseurl'] = home_url( '/' . $this->document_slug() );
return $dir;
}
/**
* Hides file's true location from users in the Gallery
* @since 0.5
* @param string $link URL to file's tru location
* @param int $id attachment ID
* @return string empty string
*/
function attachment_link_filter( $link, $id ) {
if ( !$this->verify_post_type( $id ) )
return $link;
return '';
}
/**
* Rewrites uploaded revisions filename with secure hash to mask true location
* @since 0.5
* @param array $file file data from WP
* @return array $file file with new filename
*/
function filename_rewrite( $file ) {
//verify this is a document
if ( !$this->verify_post_type( $_POST['post_id'] ) )
return $file;
//hash and replace filename, appending extension
$file['name'] = md5( $file['name'] .time() ) . $this->get_extension( $file['name'] );
$file = apply_filters( 'document_internal_filename', $file );
return $file;
}
/**
* Rewrites a file URL to it's public URL
* @since 0.5
* @param array $file file object from WP
* @return array modified file array
*/
function rewrite_file_url( $file ) {
//verify that this is a document
if ( !$this->verify_post_type( $_POST['post_id'] ) )
return $file;
$file['url'] = get_permalink( $_POST['post_id'] );
return $file;
}
/**
* Checks if a given post is a document
* note: We can't use the screen API because A) used on front end, and B) admin_init is too early (enqueue scripts)
* @param object|int either a post object or a postID
* @since 0.5
* @param unknown $post (optional)
* @return bool true if document, false if not
*/
function verify_post_type( $post = false ) {
//check for post_type query arg (post new)
if ( $post == false && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'document' )
return true;
//if post isn't set, try get vars (edit post)
if ( $post == false )
$post = ( isset( $_GET['post'] ) ) ? $_GET['post'] : false;
//look for post_id via post or get (media upload)
if ( $post == false )
$post = ( isset( $_REQUEST['post_id'] ) ) ? $_REQUEST['post_id'] : false;
$post_type = get_post_type( $post );
//if post is really an attachment or revision, look to the post's parent
if ( $post_type == 'attachment' || $post_type == 'revision' )
$post_type = get_post_type( get_post( $post )->post_parent );
return $post_type == 'document';
}
/**
* Clears cache on post_save
* @param int $postID the post ID
*/
function clear_cache( $postID ) {
wp_cache_delete( $postID, 'document_post_type' );
wp_cache_delete( $postID, 'document_revision_indices' );
wp_cache_delete( $postID, 'document_revisions' );
}
/**
* Adds doc-specific caps to all roles so that 3rd party plugins can manage them
* Gives admins all caps
* @since 1.0
*/
function add_caps() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles;
//default role => capability mapping; based off of _post options