From 30921008fc89b2c4555d4b79a78f760b223e2be2 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 27 Nov 2019 13:57:14 -0500 Subject: [PATCH] bump to 3.2.4 --- docs/changelog.md | 9 + docs/header.md | 2 +- includes/class-wp-document-revisions.php | 1073 +++++++++++----------- readme.txt | 11 +- wp-document-revisions.php | 11 +- 5 files changed, 573 insertions(+), 533 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 8512cfc0..e80acd8f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,14 @@ ## Changelog +### 3.2.4 + +* Address technical debt for WP Document Standards (#192) @NeilWJames +* On plugin activation, check that the user has edit_documents capability. If not, a warning message will be output that the menu may be incorrect. (#180) @NeilWJames +* PHPCS review (#179) @NeilWJames +* Bump phpunit/phpunit from 8.2.5 to 8.3.4 (#177) @dependabot-preview +* Addresses phpunit and toolset versions and prepare for future release (#174) @NeilWJames +* Version 3.2.2 gives an "property of non-object" at line 1403 on load (#161) @NeilWJames + ### 3.2.3 * On plugin activation, admin warning if user doesn't have edit_documents capability (#180) @NeilWJames diff --git a/docs/header.md b/docs/header.md index b139c0ae..2418897a 100644 --- a/docs/header.md +++ b/docs/header.md @@ -4,4 +4,4 @@ Contributors: benbalter Tags: documents, uploads, attachments, document management, enterprise, version control, revisions, collaboration, journalism, government, files, revision log, document management, intranet, digital asset management Requires at least: 4.6 Tested up to: 5.3 -Stable tag: 3.2.3 +Stable tag: 3.2.4 diff --git a/includes/class-wp-document-revisions.php b/includes/class-wp-document-revisions.php index 4bbb0e39..3a752198 100644 --- a/includes/class-wp-document-revisions.php +++ b/includes/class-wp-document-revisions.php @@ -1,4 +1,5 @@ 11 to allow for edit flow support. - add_action( 'admin_init', array( &$this, 'initialize_workflow_states' ) ); - add_filter( 'the_content', array( &$this, 'content_filter' ), 1 ); - add_action( 'wp_loaded', array( &$this, 'register_term_count_cb' ), 100, 1 ); + add_action('init', array(&$this, 'register_cpt')); + add_action('init', array(&$this, 'register_ct'), 15); // note: priority must be > 11 to allow for edit flow support. + add_action('admin_init', array(&$this, 'initialize_workflow_states')); + 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( 'transient_rewrite_rules', array( &$this, 'revision_rewrite' ) ); - add_action( '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 ); - add_filter( 'default_feed', array( &$this, 'hijack_feed' ) ); - add_action( 'do_feed_revision_log', array( &$this, 'do_feed_revision_log' ) ); - add_action( 'template_redirect', array( $this, 'revision_feed_auth' ) ); - 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 ); + add_filter('rewrite_rules_array', array(&$this, 'revision_rewrite')); + add_filter('transient_rewrite_rules', array(&$this, 'revision_rewrite')); + add_action('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); + add_filter('default_feed', array(&$this, 'hijack_feed')); + add_action('do_feed_revision_log', array(&$this, 'do_feed_revision_log')); + add_action('template_redirect', array($this, 'revision_feed_auth')); + 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); // RSS. - add_filter( 'private_title_format', array( &$this, 'no_title_prepend' ), 20 ); - add_filter( 'protected_title_format', array( &$this, 'no_title_prepend' ), 20 ); - add_filter( 'the_title', array( &$this, 'add_revision_num_to_title' ), 20, 2 ); + add_filter('private_title_format', array(&$this, 'no_title_prepend'), 20); + add_filter('protected_title_format', array(&$this, 'no_title_prepend'), 20); + add_filter('the_title', array(&$this, 'add_revision_num_to_title'), 20, 2); // 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 ); + 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); // locking. - add_action( 'wp_ajax_override_lock', array( &$this, 'override_lock' ) ); + add_action('wp_ajax_override_lock', array(&$this, 'override_lock')); // cache. - add_action( 'save_post', array( &$this, 'clear_cache' ), 10, 1 ); + add_action('save_post', array(&$this, 'clear_cache'), 10, 1); // edit flow. - add_action( 'init', array( &$this, 'edit_flow_support' ), 11 ); - add_action( 'init', array( &$this, 'use_workflow_states' ), 50 ); + add_action('init', array(&$this, 'edit_flow_support'), 11); + add_action('init', array(&$this, 'use_workflow_states'), 50); // load front-end features (shortcode, widgets, etc.). - include dirname( __FILE__ ) . '/class-wp-document-revisions-front-end.php'; - include dirname( __FILE__ ) . '/class-wp-document-revisions-recently-revised-widget.php'; - new WP_Document_Revisions_Front_End( $this ); - + include dirname(__FILE__) . '/class-wp-document-revisions-front-end.php'; + include dirname(__FILE__) . '/class-wp-document-revisions-recently-revised-widget.php'; + new WP_Document_Revisions_Front_End($this); } /** @@ -152,12 +155,13 @@ public function __construct() { * * @return void */ - public function activation_hook() { + public function activation_hook() + { $this->add_caps(); flush_rewrite_rules(); - if ( ! current_user_can( 'edit_documents' ) ) { + if (!current_user_can('edit_documents')) { // Unfortunately we cannot create a message directly out of the activation process, so create transient data. - set_transient( 'wpdr_activation_issue', true, 15 ); + set_transient('wpdr_activation_issue', true, 15); } } @@ -169,16 +173,20 @@ public function activation_hook() { * @since 3.2.3 * @return void */ - public function activation_error_notice() { - if ( get_transient( 'wpdr_activation_issue' ) ) { - delete_transient( 'wpdr_activation_issue' ); + public function activation_error_notice() + { + if (get_transient('wpdr_activation_issue')) { + delete_transient('wpdr_activation_issue'); ?> -

- -

- -

- +

+ +

+

+ +

+ +admin = new WP_Document_Revisions_Admin( self::$instance ); + include dirname(__FILE__) . '/class-wp-document-revisions-admin.php'; + $this->admin = new WP_Document_Revisions_Admin(self::$instance); } @@ -241,27 +253,28 @@ public function admin_init() { * * @since 0.5 */ - public function register_cpt() { + public 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' ), - 'view_items' => __( 'View Documents', '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' ), + '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'), + 'view_items' => __('View Documents', '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', 'wp-document-revisions' ), - 'all_items' => __( 'All Documents', 'wp-document-revisions' ), - 'featured_image' => __( 'Document Image', 'wp-document-revisions' ), - 'set_featured_image' => __( 'Set Document Image', 'wp-document-revisions' ), - 'remove_featured_image' => __( 'Remove Document Image', 'wp-document-revisions' ), - 'use_featured_image' => __( 'Use as Document Image', 'wp-document-revisions' ), + 'menu_name' => __('Documents', 'wp-document-revisions'), + 'all_items' => __('All Documents', 'wp-document-revisions'), + 'featured_image' => __('Document Image', 'wp-document-revisions'), + 'set_featured_image' => __('Set Document Image', 'wp-document-revisions'), + 'remove_featured_image' => __('Remove Document Image', 'wp-document-revisions'), + 'use_featured_image' => __('Use as Document Image', 'wp-document-revisions'), ); $args = array( @@ -272,14 +285,14 @@ public function register_cpt() { 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, - 'capability_type' => array( 'document', 'documents' ), + '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', 'thumbnail' ), - 'menu_icon' => plugins_url( '../img/menu-icon.png', __FILE__ ), + 'register_meta_box_cb' => array(&$this->admin, 'meta_cb'), + 'supports' => array('title', 'author', 'revisions', 'excerpt', 'custom-fields', 'thumbnail'), + 'menu_icon' => plugins_url('../img/menu-icon.png', __FILE__), ); /** @@ -289,21 +302,20 @@ public function register_cpt() { * * @param array $args delivered document type definition. */ - register_post_type( 'document', apply_filters( 'document_revisions_cpt', $args ) ); + register_post_type('document', apply_filters('document_revisions_cpt', $args)); // Ensure that there is a post-thumbnail size set - could/should be set by theme - copy from thumbnail. - if ( ! array_key_exists( 'post-thumbnail', wp_get_additional_image_sizes() ) ) { - add_image_size( 'post-thumbnail', get_option( 'thumbnail_size_w' ), get_option( 'thumbnail_size_h' ), false ); + if (!array_key_exists('post-thumbnail', wp_get_additional_image_sizes())) { + add_image_size('post-thumbnail', get_option('thumbnail_size_w'), get_option('thumbnail_size_h'), false); } - if ( empty( self::$wp_default_dir ) ) { + if (empty(self::$wp_default_dir)) { // Set the default upload directory cache. $this->set_default_dir_cache(); } // Set Global for Document Image from Cookie doc_image (may be updated later). - self::$doc_image = ( isset( $_COOKIE['doc_image'] ) ? 'true' === $_COOKIE['doc_image'] : true ); - + self::$doc_image = (isset($_COOKIE['doc_image']) ? 'true' === $_COOKIE['doc_image'] : true); } @@ -312,20 +324,21 @@ public function register_cpt() { * * @since 0.5 */ - public function register_ct() { + public function register_ct() + { $labels = array( - 'name' => _x( 'Workflow States', 'taxonomy general name', 'wp-document-revisions' ), - 'singular_name' => _x( 'Workflow State', 'taxonomy singular name', 'wp-document-revisions' ), - 'search_items' => __( 'Search Workflow States', 'wp-document-revisions' ), - 'all_items' => __( 'All Workflow States', 'wp-document-revisions' ), - 'parent_item' => __( 'Parent Workflow State', 'wp-document-revisions' ), - 'parent_item_colon' => __( 'Parent Workflow State:', 'wp-document-revisions' ), - 'edit_item' => __( 'Edit Workflow State', 'wp-document-revisions' ), - 'update_item' => __( 'Update Workflow State', 'wp-document-revisions' ), - 'add_new_item' => __( 'Add New Workflow State', 'wp-document-revisions' ), - 'new_item_name' => __( 'New Workflow State Name', 'wp-document-revisions' ), - 'menu_name' => __( 'Workflow States', 'wp-document-revisions' ), + 'name' => _x('Workflow States', 'taxonomy general name', 'wp-document-revisions'), + 'singular_name' => _x('Workflow State', 'taxonomy singular name', 'wp-document-revisions'), + 'search_items' => __('Search Workflow States', 'wp-document-revisions'), + 'all_items' => __('All Workflow States', 'wp-document-revisions'), + 'parent_item' => __('Parent Workflow State', 'wp-document-revisions'), + 'parent_item_colon' => __('Parent Workflow State:', 'wp-document-revisions'), + 'edit_item' => __('Edit Workflow State', 'wp-document-revisions'), + 'update_item' => __('Update Workflow State', 'wp-document-revisions'), + 'add_new_item' => __('Add New Workflow State', 'wp-document-revisions'), + 'new_item_name' => __('New Workflow State Name', 'wp-document-revisions'), + 'menu_name' => __('Workflow States', 'wp-document-revisions'), ); /** @@ -337,7 +350,7 @@ public function register_ct() { */ register_taxonomy( 'workflow_state', - array( 'document' ), + array('document'), apply_filters( 'document_revisions_ct', array( @@ -345,11 +358,10 @@ public function register_ct() { 'labels' => $labels, 'show_ui' => true, 'rewrite' => false, - 'update_count_callback' => array( &$this, 'term_count_cb' ), + 'update_count_callback' => array(&$this, 'term_count_cb'), ) ) ); - } @@ -359,7 +371,8 @@ public function register_ct() { * @since 0.5 * @return unknown */ - public function initialize_workflow_states() { + public function initialize_workflow_states() + { $terms = get_terms( 'workflow_state', @@ -368,15 +381,15 @@ public function initialize_workflow_states() { ) ); - if ( ! empty( $terms ) ) { + if (!empty($terms)) { return false; } $states = array( - __( 'In Progress', 'wp-document-revisions' ) => __( 'Document is in the process of being written', 'wp-document-revisions' ), - __( 'Initial Draft', 'wp-document-revisions' ) => __( 'Document is being edited and refined', 'wp-document-revisions' ), - __( 'Under Review', 'wp-document-revisions' ) => __( 'Document is pending final review', 'wp-document-revisions' ), - __( 'Final', 'wp-document-revisions' ) => __( 'Document is in its final form', 'wp-document-revisions' ), + __('In Progress', 'wp-document-revisions') => __('Document is in the process of being written', 'wp-document-revisions'), + __('Initial Draft', 'wp-document-revisions') => __('Document is being edited and refined', 'wp-document-revisions'), + __('Under Review', 'wp-document-revisions') => __('Document is pending final review', 'wp-document-revisions'), + __('Final', 'wp-document-revisions') => __('Document is in its final form', 'wp-document-revisions'), ); /** @@ -386,9 +399,9 @@ public function initialize_workflow_states() { * * @param array $states default workflow_state values. */ - $states = apply_filters( 'default_workflow_states', $states ); + $states = apply_filters('default_workflow_states', $states); - foreach ( $states as $state => $desc ) { + foreach ($states as $state => $desc) { wp_insert_term( $state, 'workflow_state', @@ -397,7 +410,6 @@ public function initialize_workflow_states() { ) ); } - } /** @@ -407,30 +419,31 @@ public function initialize_workflow_states() { * @param object $document (optional) post object. * @return object all attached uploads */ - public function get_attachments( $document = '' ) { + public function get_attachments($document = '') + { - if ( '' === $document ) { + if ('' === $document) { global $post; $document = $post; } // verify that it's an object. - if ( ! is_object( $document ) ) { - $document = get_post( $document ); + if (!is_object($document)) { + $document = get_post($document); } // check for revisions. - $parent = wp_is_post_revision( $document ); - if ( $parent ) { - $document = get_post( $parent ); + $parent = wp_is_post_revision($document); + if ($parent) { + $document = get_post($parent); } // check for attachments. - if ( 'attachment' === $document->post_type ) { - $document = get_post( $document->post_parent ); + if ('attachment' === $document->post_type) { + $document = get_post($document->post_parent); } - if ( ! isset( $document->ID ) ) { + if (!isset($document->ID)) { return array(); } @@ -447,10 +460,9 @@ public function get_attachments( $document = '' ) { * * @param array $args Delivered WP Query to fetch all attachments for a document. */ - $args = apply_filters( 'document_revision_query', $args ); - - return get_children( $args ); + $args = apply_filters('document_revision_query', $args); + return get_children($args); } @@ -461,19 +473,20 @@ public function get_attachments( $document = '' ) { * @param object|int $document the post object or postID. * @return bool|string false if no lock, user's display name if locked */ - public function get_document_lock( $document ) { + public function get_document_lock($document) + { - if ( ! is_object( $document ) ) { - $document = get_post( $document ); + if (!is_object($document)) { + $document = get_post($document); } - if ( ! $document ) { + if (!$document) { return false; } // get the post lock. - $user = wp_check_post_lock( $document->ID ); - if ( ! ( $user ) ) { + $user = wp_check_post_lock($document->ID); + if (!($user)) { $user = false; } @@ -484,16 +497,15 @@ public function get_document_lock( $document ) { * @param string $user user locking the document. * @param object $document Post object. */ - $user = apply_filters( 'document_lock_check', $user, $document ); + $user = apply_filters('document_lock_check', $user, $document); - if ( ! $user ) { + if (!$user) { return false; } // get displayname from userID. - $last_user = get_userdata( $user ); - return ( $last_user ) ? $last_user->display_name : __( 'Somebody', 'wp-document-revisions' ); - + $last_user = get_userdata($user); + return ($last_user) ? $last_user->display_name : __('Somebody', 'wp-document-revisions'); } @@ -504,12 +516,13 @@ public function get_document_lock( $document ) { * @param string $file URL, path, or filename to file. * @return string extension */ - public function get_extension( $file ) { + public function get_extension($file) + { - $extension = '.' . pathinfo( $file, PATHINFO_EXTENSION ); + $extension = '.' . pathinfo($file, PATHINFO_EXTENSION); // don't return a . extension. - if ( '.' === $extension ) { + if ('.' === $extension) { return ''; } @@ -521,8 +534,7 @@ public function get_extension( $file ) { * @param string $extension attachment file name extension. * @param string $file attachment file name. */ - return apply_filters( 'document_extension', $extension, $file ); - + return apply_filters('document_extension', $extension, $file); } @@ -533,43 +545,43 @@ public function get_extension( $file ) { * @param object|int $document_or_attachment document or attachment. * @return string the extension to the latest revision */ - public function get_file_type( $document_or_attachment = '' ) { + public function get_file_type($document_or_attachment = '') + { - if ( '' === $document_or_attachment ) { + if ('' === $document_or_attachment) { global $post; $document_or_attachment = $post; } - if ( ! is_object( $document_or_attachment ) ) { - $document_or_attachment = get_post( $document_or_attachment ); + if (!is_object($document_or_attachment)) { + $document_or_attachment = get_post($document_or_attachment); } // 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 ( 'attachment' === get_post_type( $document_or_attachment ) ) { + if ('attachment' === get_post_type($document_or_attachment)) { $attachment = $document_or_attachment; - } elseif ( 'document' === get_post_type( $document_or_attachment ) ) { - $latest_revision = $this->get_latest_revision( $document_or_attachment->ID ); + } elseif ('document' === get_post_type($document_or_attachment)) { + $latest_revision = $this->get_latest_revision($document_or_attachment->ID); - // verify a previous revision exists. - if ( ! $latest_revision ) { + // verify a previous revision exists. + if (!$latest_revision) { return ''; } - $attachment = get_post( $latest_revision->post_content ); + $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' ) { + if (get_post_type($attachment) !== 'attachment') { return ''; } } // although get_attached_file uses the standard directory, // not used, so doesn't matter so no correction needed. - return $this->get_extension( get_attached_file( $attachment->ID ) ); - + return $this->get_extension(get_attached_file($attachment->ID)); } @@ -578,11 +590,11 @@ public function get_file_type( $document_or_attachment = '' ) { * * @since 0.5 */ - public function inject_rules() { + public function inject_rules() + { global $wp_rewrite; - $wp_rewrite->add_rewrite_tag( '%document%', '([^.]+)\.[A-Za-z0-9]{3,4}?', 'document=' ); - + $wp_rewrite->add_rewrite_tag('%document%', '([^.]+)\.[A-Za-z0-9]{3,4}?', 'document='); } @@ -593,24 +605,25 @@ public function inject_rules() { * @param Array $rules rewrite rules. * @return Array rewrite rules */ - public function revision_rewrite( $rules ) { + public 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]'; + $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]'; // revision feeds in the form of yyyy/mm/[slug]-revision-##.[extension]/feed/, yyyy/mm/[slug]-revision-##/feed/, etc. - $my_rules[ $slug . '/([0-9]{4})/([0-9]{1,2})/([^.]+)(\.[A-Za-z0-9]{3,4})?/feed/?$' ] = 'index.php?year=$matches[1]&monthnum=$matches[2]&document=$matches[3]&feed=feed'; + $my_rules[$slug . '/([0-9]{4})/([0-9]{1,2})/([^.]+)(\.[A-Za-z0-9]{3,4})?/feed/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&document=$matches[3]&feed=feed'; // 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]'; + $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[$slug . '/?$'] = 'index.php?post_type=document'; + $my_rules[$slug . '/page/?([0-9]{1,})/?$'] = 'index.php?post_type=document&paged=$matches[1]'; /** * Filters the Document rewrite rules. @@ -618,7 +631,7 @@ public function revision_rewrite( $rules ) { * @param array $my_rules Array of rewrite rules to add for for Documents. * @param array $rules Array of rewrite rules being built. */ - $my_rules = apply_filters( 'document_rewrite_rules', $my_rules, $rules ); + $my_rules = apply_filters('document_rewrite_rules', $my_rules, $rules); return $my_rules + $rules; } @@ -631,7 +644,8 @@ public function revision_rewrite( $rules ) { * @param array $vars the query vars. * @return array the modified query vars */ - public function add_query_var( $vars ) { + public function add_query_var($vars) + { $vars[] = 'revision'; $vars[] = 'document'; return $vars; @@ -648,39 +662,39 @@ public function add_query_var( $vars ) { * @param String $sample (optional) not used. * @return string the real permalink */ - public function permalink( $link, $document, $leavename, $sample = '' ) { + public function permalink($link, $document, $leavename, $sample = '') + { global $wp_rewrite; $revision_num = false; // if this isn't our post type, kick. - if ( ! $this->verify_post_type( $document ) ) { + if (!$this->verify_post_type($document)) { return $link; } // check if it's a revision. - if ( 'revision' === $document->post_type ) { - $parent = clone get_post( $document->post_parent ); - $revision_num = $this->get_revision_number( $document->ID ); - $parent->post_name = $parent->post_name . __( '-revision-', 'wp-document-revisions' ) . $revision_num; + if ('revision' === $document->post_type) { + $parent = clone get_post($document->post_parent); + $revision_num = $this->get_revision_number($document->ID); + $parent->post_name = $parent->post_name . __('-revision-', 'wp-document-revisions') . $revision_num; $document = $parent; } // if no permastruct. - if ( '' === $wp_rewrite->permalink_structure || in_array( $document->post_status, array( 'pending', 'draft' ), true ) ) { - $link = site_url( '?post_type=document&p=' . $document->ID ); - if ( $revision_num ) { - $link = add_query_arg( 'revision', $revision_num, $link ); + if ('' === $wp_rewrite->permalink_structure || in_array($document->post_status, array('pending', 'draft'), true)) { + $link = site_url('?post_type=document&p=' . $document->ID); + if ($revision_num) { + $link = add_query_arg('revision', $revision_num, $link); } } else { // build documents/yyyy/mm/slug. - $extension = $this->get_file_type( $document ); - $timestamp = strtotime( $document->post_date ); + $extension = $this->get_file_type($document); + $timestamp = strtotime($document->post_date); - $link = home_url() . '/' . $this->document_slug() . '/' . gmdate( 'Y', $timestamp ) . '/' . gmdate( 'm', $timestamp ) . '/'; - $link .= ( $leavename ) ? '%document%' : $document->post_name; + $link = home_url() . '/' . $this->document_slug() . '/' . gmdate('Y', $timestamp) . '/' . gmdate('m', $timestamp) . '/'; + $link .= ($leavename) ? '%document%' : $document->post_name; $link .= $extension; - } /** @@ -689,7 +703,7 @@ public function permalink( $link, $document, $leavename, $sample = '' ) { * @param string $link generated permalink. * @param object $document Post object. */ - $link = apply_filters( 'document_permalink', $link, $document ); + $link = apply_filters('document_permalink', $link, $document); return $link; } @@ -704,20 +718,21 @@ public function permalink( $link, $document, $leavename, $sample = '' ) { * @param int $id Post ID. * @return unknown */ - public function sample_permalink_html_filter( $html, $id ) { + public function sample_permalink_html_filter($html, $id) + { - $document = get_post( $id ); + $document = get_post($id); // verify post type. - if ( ! $this->verify_post_type( $document ) ) { + if (!$this->verify_post_type($document)) { return $html; } // grab attachments. - $attachments = $this->get_attachments( $document ); + $attachments = $this->get_attachments($document); // if no attachments, return nothing. - if ( empty( $attachments ) ) { + if (empty($attachments)) { return ''; } @@ -735,30 +750,31 @@ public function sample_permalink_html_filter( $html, $id ) { * @param int $post_id the post ID. * @return array array of post objects */ - public function get_revisions( $post_id ) { + public function get_revisions($post_id) + { // 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. - $document = get_post( $post_id ); + $document = get_post($post_id); - if ( ! $document ) { + if (!$document) { return false; } - $cache = wp_cache_get( $post_id, 'document_revisions' ); - if ( $cache ) { + $cache = wp_cache_get($post_id, 'document_revisions'); + if ($cache) { return $cache; } // correct the modified date. - $document->post_date = gmdate( 'Y-m-d H:i:s', (int) get_post_modified_time( 'U', null, $post_id ) ); + $document->post_date = gmdate('Y-m-d H:i:s', (int) get_post_modified_time('U', null, $post_id)); // grab the post author. $post_author = $document->post_author; // fix for Quotes in the most recent post because it comes from get_post. - $document->post_excerpt = html_entity_decode( $document->post_excerpt ); + $document->post_excerpt = html_entity_decode($document->post_excerpt); // get revisions, and prepend the post. $revs = wp_get_post_revisions( @@ -767,24 +783,23 @@ public function get_revisions( $post_id ) { 'order' => 'DESC', ) ); - array_unshift( $revs, $document ); + array_unshift($revs, $document); // loop through revisions. - foreach ( $revs as $id => &$rev ) { + foreach ($revs as $id => &$rev) { - if ( $id < count( $revs ) - 1 ) { + if ($id < count($revs) - 1) { // if this is anything other than the first revision, shift author 1. - $rev->post_author = $revs[ $id + 1 ]->post_author; + $rev->post_author = $revs[$id + 1]->post_author; } else { // if last revision, get the post author. $rev->post_author = $post_author; } } - wp_cache_set( $post_id, $revs, 'document_revisions' ); + wp_cache_set($post_id, $revs, 'document_revisions'); return $revs; - } @@ -797,21 +812,21 @@ public function get_revisions( $post_id ) { * @param bool $feed (optional) whether this is a feed. * @return obj|bool the WP_Query object, false on failure */ - public function get_revision_query( $post_id, $feed = false ) { + public function get_revision_query($post_id, $feed = false) + { - $posts = $this->get_revisions( $post_id ); + $posts = $this->get_revisions($post_id); - if ( ! $posts ) { + if (!$posts) { return false; } $rev_query = new WP_Query(); $rev_query->posts = $posts; - $rev_query->post_count = count( $posts ); + $rev_query->post_count = count($posts); $rev_query->is_feed = $feed; return $rev_query; - } @@ -822,10 +837,11 @@ public function get_revision_query( $post_id, $feed = false ) { * @param int $post_id the parent post id. * @return array array of revisions */ - public function get_revision_indices( $post_id ) { + public function get_revision_indices($post_id) + { - $cache = wp_cache_get( $post_id, 'document_revision_indices' ); - if ( $cache ) { + $cache = wp_cache_get($post_id, 'document_revision_indices'); + if ($cache) { return $cache; } @@ -839,16 +855,15 @@ public function get_revision_indices( $post_id ) { $i = 1; $output = array(); - foreach ( $revs as $rev ) { - $output[ $i++ ] = $rev->ID; + foreach ($revs as $rev) { + $output[$i++] = $rev->ID; } - if ( ! empty( $output ) ) { - wp_cache_set( $post_id, $output, 'document_revision_indices' ); + if (!empty($output)) { + wp_cache_set($post_id, $output, 'document_revision_indices'); } return $output; - } @@ -859,18 +874,18 @@ public function get_revision_indices( $post_id ) { * @param int $revision_id the revision ID. * @return int revision number */ - public function get_revision_number( $revision_id ) { + public function get_revision_number($revision_id) + { - $revision = get_post( $revision_id ); + $revision = get_post($revision_id); - if ( ! isset( $revision->post_parent ) ) { + if (!isset($revision->post_parent)) { return false; } - $index = $this->get_revision_indices( $revision->post_parent ); - - return array_search( $revision_id, $index, true ); + $index = $this->get_revision_indices($revision->post_parent); + return array_search($revision_id, $index, true); } @@ -882,12 +897,12 @@ public function get_revision_number( $revision_id ) { * @param int $post_id the ID of the parent post. * @return int the ID of the revision */ - public function get_revision_id( $revision_num, $post_id ) { + public function get_revision_id($revision_num, $post_id) + { - $index = $this->get_revision_indices( $post_id ); - - return ( isset( $index[ $revision_num ] ) ) ? $index[ $revision_num ] : false; + $index = $this->get_revision_indices($post_id); + return (isset($index[$revision_num])) ? $index[$revision_num] : false; } @@ -898,49 +913,50 @@ public function get_revision_id( $revision_num, $post_id ) { * @param String $template the requested template. * @return String the resolved template */ - public function serve_file( $template ) { + public function serve_file($template) + { global $post; global $wp_query; global $wp; - if ( ! is_single() ) { + if (!is_single()) { return $template; } - if ( ! $this->verify_post_type( $post ) ) { + 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 ) ) { + if (post_password_required($post)) { return $template; } // grab the post revision if any. - $version = get_query_var( 'revision' ); + $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 ); + if (!$version) { + $rev_id = $this->get_latest_revision($post->ID); } else { - $rev_id = $this->get_revision_id( $version, $post->ID ); + $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? + $rev_post = get_post($rev_id); + $revision = get_post($rev_post->post_content); // @todo can this be simplified? - $file = get_attached_file( $revision->ID ); + $file = get_attached_file($revision->ID); // Above used a cached version of std directory, so cannot change within call and may be wrong, // so possibly replace it in the output. - if ( empty( self::$wp_default_dir ) ) { + if (empty(self::$wp_default_dir)) { // Set the default upload directory cache. $this->set_default_dir_cache(); } $std_dir = self::$wp_default_dir['basedir']; $doc_dir = $this->document_upload_dir(); - if ( $std_dir !== $doc_dir ) { - $file = str_replace( $std_dir, $doc_dir, $file ); + if ($std_dir !== $doc_dir) { + $file = str_replace($std_dir, $doc_dir, $file); } // flip slashes for WAMP settups to prevent 404ing on the next line. @@ -949,10 +965,10 @@ public function serve_file( $template ) { * * @param string $file attached file name. */ - $file = apply_filters( 'document_path', $file ); + $file = apply_filters('document_path', $file); // return 404 if the file is a dud or malformed. - if ( ! is_file( $file ) ) { + if (!is_file($file)) { // this will send 404 and no cache headers // and tell wp_query that this is a 404 so that is_404() works as expected @@ -963,7 +979,6 @@ public function serve_file( $template ) { // tell WP to serve the theme's standard 404 template, this is a filter after all... return get_404_template(); - } // note: authentication is happening via a hook here to allow shortcircuiting. @@ -974,35 +989,35 @@ public function serve_file( $template ) { * @param object $post WP Post to be served. * @param string $version Document revision. */ - if ( ! apply_filters( 'serve_document_auth', true, $post, $version ) ) { + if (!apply_filters('serve_document_auth', true, $post, $version)) { wp_die( - esc_html__( 'You are not authorized to access that file.', 'wp-document-revisions' ), + esc_html__('You are not authorized to access that file.', 'wp-document-revisions'), null, - array( 'response' => 403 ) + array('response' => 403) ); return false; // for unit testing. } - do_action( 'serve_document', $post->ID, $file ); + do_action('serve_document', $post->ID, $file); // We may override this later. - status_header( 200 ); + status_header(200); // fake the filename. $filename = $post->post_name; - $filename .= ( '' === $version ) ? '' : __( '-revision-', 'wp-document-revisions' ) . $version; + $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 ); + 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); $headers = array(); // 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'; + $disposition = (apply_filters('document_content_disposition_inline', true)) ? 'inline' : 'attachment'; $headers['Content-Disposition'] = $disposition . '; filename="' . $filename . '"'; @@ -1016,35 +1031,35 @@ public function serve_file( $template ) { * @param null|bool|string $mimetype The MIME type for a given file. * @param string $file The file being served. */ - $mimetype = apply_filters( 'document_revisions_mimetype', null, $file ); + $mimetype = apply_filters('document_revisions_mimetype', null, $file); - if ( is_null( $mimetype ) ) { + if (is_null($mimetype)) { // 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 ); + $mime = wp_check_filetype($file); + if (false === $mime['type'] && function_exists('mime_content_type')) { + $mime['type'] = mime_content_type($file); } - if ( $mime['type'] ) { + if ($mime['type']) { $mimetype = $mime['type']; } else { - $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 ); + $mimetype = 'image/' . substr($file, strrpos($file, '.') + 1); } } // Set the Content-Type header if a mimetype has been detected or provided. - if ( is_string( $mimetype ) ) { + if (is_string($mimetype)) { $headers['Content-Type'] = $mimetype; } - $headers['Content-Length'] = filesize( $file ); + $headers['Content-Length'] = filesize($file); // modified. - $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); - $etag = '"' . md5( $last_modified ) . '"'; + $last_modified = gmdate('D, d M Y H:i:s', filemtime($file)); + $etag = '"' . md5($last_modified) . '"'; $headers['Last-Modified'] = $last_modified . ' GMT'; $headers['ETag'] = $etag; - $headers['Expires'] = gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT'; + $headers['Expires'] = gmdate('D, d M Y H:i:s', time() + 100000000) . ' GMT'; /** * Filters the HTTP headers sent when a file is served through WP Document Revisions. @@ -1052,42 +1067,42 @@ public function serve_file( $template ) { * @param array $headers The HTTP headers to be sent. * @param string $file The file being served. */ - $headers = apply_filters( 'document_revisions_serve_file_headers', $headers, $file ); + $headers = apply_filters('document_revisions_serve_file_headers', $headers, $file); - foreach ( $headers as $header => $value ) { + foreach ($headers as $header => $value) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - @header( $header . ': ' . $value ); + @header($header . ': ' . $value); } // Support for Conditional GET. - $client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) ) : false; + $client_etag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes(sanitize_text_field(wp_unslash($_SERVER['HTTP_IF_NONE_MATCH']))) : false; - if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { + if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false; } - $client_last_modified = trim( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) ); + $client_last_modified = trim(sanitize_text_field(wp_unslash($_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; + $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0; // Make a timestamp for our most recent modification... - $modified_timestamp = strtotime( $last_modified ); + $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 ) ) + 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 ); + status_header(304); return; } // in case this is a large file, remove PHP time limits. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - @set_time_limit( 0 ); + @set_time_limit(0); // clear output buffer to prevent other plugins from corrupting the file. - if ( ob_get_level() ) { + if (ob_get_level()) { ob_clean(); flush(); } @@ -1095,8 +1110,7 @@ public function serve_file( $template ) { // If we made it this far, just serve the file // Note: We use readfile, and not WP_Filesystem for memory/performance reasons. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_readfile - readfile( $file ); - + readfile($file); } @@ -1108,26 +1122,26 @@ public function serve_file( $template ) { * @param bool|int $version version of the document being served, if any. * @return unknown */ - public function serve_document_auth( $default, $post, $version ) { + public 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 && 'publish' === $post->post_status ) { + if (!$version && 'publish' === $post->post_status) { return $default; } // attempting to access a revision. - if ( $version && ! current_user_can( 'read_document_revisions' ) ) { + if ($version && !current_user_can('read_document_revisions')) { return false; } // specific document cap check. - if ( ! current_user_can( 'read_document', $post->ID ) ) { + if (!current_user_can('read_document', $post->ID)) { return false; } return $default; - } @@ -1137,9 +1151,10 @@ public function serve_document_auth( $default, $post, $version ) { * @param Int $id the post ID. * @return unknown */ - public function get_latest_version( $id ) { - _deprecated_function( __FUNCTION__, '1.0.3 of WP Document Revisions', 'get_latest_version' ); - return $this->get_latest_revision( $id ); + public function get_latest_version($id) + { + _deprecated_function(__FUNCTION__, '1.0.3 of WP Document Revisions', 'get_latest_version'); + return $this->get_latest_revision($id); } @@ -1149,35 +1164,34 @@ public function get_latest_version( $id ) { * @param Int $post_id the post id. * @return object latest revision object */ - public function get_latest_revision( $post_id ) { + public function get_latest_revision($post_id) + { - if ( is_object( $post_id ) ) { + if (is_object($post_id)) { $post_id = $post_id->ID; } - $revisions = $this->get_revisions( $post_id ); + $revisions = $this->get_revisions($post_id); - if ( ! $revisions ) { + 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. - if ( ! is_numeric( $revisions[0]->post_content ) ) { + if (!is_numeric($revisions[0]->post_content)) { - $attachments = $this->get_attachments( $post_id ); + $attachments = $this->get_attachments($post_id); - if ( empty( $attachments ) ) { + if (empty($attachments)) { return false; } - $latest_attachment = reset( $attachments ); + $latest_attachment = reset($attachments); $revisions[0]->post_content = $latest_attachment->ID; - } return $revisions[0]; - } @@ -1187,9 +1201,10 @@ public function get_latest_revision( $post_id ) { * @param Int $id the post ID. * @return String the revision URL */ - public 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 ); + public 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); } @@ -1200,18 +1215,19 @@ public function get_latest_version_url( $id ) { * @param int $id post ID. * @return string|bool URL to revision or false if no attachment */ - public function get_latest_revision_url( $id ) { + public function get_latest_revision_url($id) + { - $latest = $this->get_latest_revision( $id ); + $latest = $this->get_latest_revision($id); - if ( ! $latest ) { + 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 ); + 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; } @@ -1223,18 +1239,19 @@ public function get_latest_revision_url( $id ) { * @since 0.5 * @return string path to document */ - public function document_upload_dir() { + public function document_upload_dir() + { global $wpdb; - if ( ! is_null( self::$wpdr_document_dir ) ) { + if (!is_null(self::$wpdr_document_dir)) { return self::$wpdr_document_dir; } // If no options set, default to normal upload dir. - $dir = get_site_option( 'document_upload_directory' ); - if ( ! ( $dir ) ) { - if ( empty( self::$wp_default_dir ) ) { + $dir = get_site_option('document_upload_directory'); + if (!($dir)) { + if (empty(self::$wp_default_dir)) { // Set the default upload directory cache. $this->set_default_dir_cache(); } @@ -1243,22 +1260,21 @@ public function document_upload_dir() { } self::$wpdr_document_dir = $dir; - if ( ! is_multisite() ) { + if (!is_multisite()) { return $dir; } // make site specific on multisite. - if ( is_multisite() && ! is_network_admin() ) { - if ( is_main_site() && get_current_network_id() === get_main_network_id() ) { - $dir = str_replace( '/sites/%site_id%', '', $dir ); + if (is_multisite() && !is_network_admin()) { + if (is_main_site() && get_current_network_id() === get_main_network_id()) { + $dir = str_replace('/sites/%site_id%', '', $dir); } - $dir = str_replace( '%site_id%', $wpdb->blogid, $dir ); + $dir = str_replace('%site_id%', $wpdb->blogid, $dir); self::$wpdr_document_dir = $dir; } return $dir; - } @@ -1268,11 +1284,12 @@ public function document_upload_dir() { * * @return string */ - public function document_slug() { + public function document_slug() + { - $slug = get_site_option( 'document_slug' ); + $slug = get_site_option('document_slug'); - if ( ! $slug ) { + if (!$slug) { $slug = 'documents'; } @@ -1281,8 +1298,7 @@ public function document_slug() { * * @param string $slug The slug (default or parameter). */ - return apply_filters( 'document_slug', $slug ); - + return apply_filters('document_slug', $slug); } @@ -1293,16 +1309,17 @@ public function document_slug() { * @param array $dir defaults passed from WP. * @return array $dir modified directory */ - public function document_upload_dir_filter( $dir ) { + public function document_upload_dir_filter($dir) + { - if ( ! $this->verify_post_type() ) { + if (!$this->verify_post_type()) { // Ensure cookie variable is set correctly - if needed elsewhere. self::$doc_image = true; return $dir; } // Ignore if dealing with thumbnail on document page. - if ( self::$doc_image ) { + if (self::$doc_image) { return $dir; } @@ -1318,16 +1335,16 @@ public function document_upload_dir_filter( $dir ) { 'post.php', 'post-new.php', ); - if ( in_array( $pagenow, $pages, true ) ) { + if (in_array($pagenow, $pages, true)) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace - $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $functions = array( 'wp_ajax_get_post_thumbnail_html', '_wp_post_thumbnail_html', 'post_thumbnail_meta_box', ); - foreach ( $trace as $traceline ) : - if ( in_array( $traceline['function'], $functions, true ) ) { + foreach ($trace as $traceline) : + if (in_array($traceline['function'], $functions, true)) { self::$doc_image = true; return $dir; } @@ -1336,12 +1353,11 @@ public function document_upload_dir_filter( $dir ) { self::$doc_image = false; $dir['path'] = $this->document_upload_dir() . $dir['subdir']; - $dir['url'] = home_url( '/' . $this->document_slug() ) . $dir['subdir']; + $dir['url'] = home_url('/' . $this->document_slug()) . $dir['subdir']; $dir['basedir'] = $this->document_upload_dir(); - $dir['baseurl'] = home_url( '/' . $this->document_slug() ); + $dir['baseurl'] = home_url('/' . $this->document_slug()); return $dir; - } @@ -1353,9 +1369,10 @@ public function document_upload_dir_filter( $dir ) { * @param int $id attachment ID. * @return string empty string */ - public function attachment_link_filter( $link, $id ) { + public function attachment_link_filter($link, $id) + { - if ( ! $this->verify_post_type( $id ) ) { + if (!$this->verify_post_type($id)) { return $link; } @@ -1370,33 +1387,34 @@ public function attachment_link_filter( $link, $id ) { * @param array $file file data from WP. * @return array $file file with new filename */ - public function filename_rewrite( $file ) { + public function filename_rewrite($file) + { // verify this is a document. // phpcs:ignore WordPress.Security.NonceVerification.Missing - if ( ! isset( $_POST['post_id'] ) || ! $this->verify_post_type( sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) ) ) { + if (!isset($_POST['post_id']) || !$this->verify_post_type(sanitize_text_field(wp_unslash($_POST['post_id'])))) { self::$doc_image = true; return $file; } // Ignore if dealing with thumbnail on document page. - if ( self::$doc_image ) { + if (self::$doc_image) { return $file; } global $pagenow; - if ( 'async-upload.php' === $pagenow ) { + if ('async-upload.php' === $pagenow) { // got past cookie, but may be in thumbnail code. // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace - $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $functions = array( 'wp_ajax_get_post_thumbnail_html', '_wp_post_thumbnail_html', 'post_thumbnail_meta_box', ); - foreach ( $trace as $traceline ) : - if ( in_array( $traceline['function'], $functions, true ) ) { + foreach ($trace as $traceline) : + if (in_array($traceline['function'], $functions, true)) { self::$doc_image = true; return $file; } @@ -1405,17 +1423,16 @@ public function filename_rewrite( $file ) { self::$doc_image = false; // hash and replace filename, appending extension. - $file['name'] = md5( $file['name'] . time() ) . $this->get_extension( $file['name'] ); + $file['name'] = md5($file['name'] . time()) . $this->get_extension($file['name']); /** * Filters the encoded file name for the attached document. * * @param array $file encoded file name. */ - $file = apply_filters( 'document_internal_filename', $file ); + $file = apply_filters('document_internal_filename', $file); return $file; - } @@ -1426,46 +1443,45 @@ public function filename_rewrite( $file ) { * @param array $file file object from WP. * @return array modified file array */ - public function rewrite_file_url( $file ) { + public function rewrite_file_url($file) + { // verify that this is a document. // phpcs:ignore WordPress.Security.NonceVerification.Missing - if ( ! isset( $_POST['post_id'] ) || ! $this->verify_post_type( sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) ) ) { + if (!isset($_POST['post_id']) || !$this->verify_post_type(sanitize_text_field(wp_unslash($_POST['post_id'])))) { self::$doc_image = true; return $file; } // Ignore if dealing with thumbnail on document page. - if ( self::$doc_image ) { + if (self::$doc_image) { return $file; } global $pagenow; - if ( 'async-upload.php' === $pagenow ) { + if ('async-upload.php' === $pagenow) { // got past cookie, but may be in thumbnail code. // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace - $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $functions = array( 'wp_ajax_get_post_thumbnail_html', '_wp_post_thumbnail_html', 'post_thumbnail_meta_box', ); - foreach ( $trace as $traceline ) : - if ( in_array( $traceline['function'], $functions, true ) ) { + foreach ($trace as $traceline) : + if (in_array($traceline['function'], $functions, true)) { self::$doc_image = true; return $file; } endforeach; - } self::$doc_image = false; // phpcs:ignore WordPress.Security.NonceVerification.Missing - $file['url'] = get_permalink( sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) ); + $file['url'] = get_permalink(sanitize_text_field(wp_unslash($_POST['post_id']))); return $file; - } @@ -1481,57 +1497,58 @@ public function rewrite_file_url( $file ) { * @since 0.5 * @return bool true if document, false if not */ - public function verify_post_type( $documentish = false ) { + public function verify_post_type($documentish = false) + { global $wp_query; - if ( false === $documentish ) { + if (false === $documentish) { // check for post_type query arg (post new). // phpcs:ignore WordPress.Security.NonceVerification.Recommended - if ( isset( $_GET['post_type'] ) && 'document' === $_GET['post_type'] ) { + if (isset($_GET['post_type']) && 'document' === $_GET['post_type']) { return true; } // Assume that a document feed is a document feed, even without a post object. - if ( isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) && - ( array_key_exists( 'post_type', $wp_query->query_vars ) ) && 'document' === $wp_query->query_vars['post_type'] && is_feed() ) { + if ( + isset($wp_query->query_vars) && is_array($wp_query->query_vars) && (array_key_exists('post_type', $wp_query->query_vars)) && 'document' === $wp_query->query_vars['post_type'] && is_feed() + ) { return true; } // if post isn't set, try get vars (edit post). // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( isset( $_GET['post'] ) ) { - $documentish = intval( $_GET['post'] ); - // phpcs:enable WordPress.Security.NonceVerification.Recommended + if (isset($_GET['post'])) { + $documentish = intval($_GET['post']); + // phpcs:enable WordPress.Security.NonceVerification.Recommended } // look for post_id via post or get (media upload). // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( isset( $_REQUEST['post_id'] ) ) { - $documentish = intval( $_REQUEST['post_id'] ); + if (isset($_REQUEST['post_id'])) { + $documentish = intval($_REQUEST['post_id']); } // phpcs:enable WordPress.Security.NonceVerification.Recommended } - if ( false === $documentish ) { + if (false === $documentish) { return false; } - $post = get_post( $documentish ); + $post = get_post($documentish); - if ( ! $post ) { + if (!$post) { return false; } $post_type = $post->post_type; // if post is really an attachment or revision, look to the post's parent. - if ( ( 'attachment' === $post_type || 'revision' === $post_type ) && 0 !== $post->post_parent ) { - $post_type = get_post_type( $post->post_parent ); + if (('attachment' === $post_type || 'revision' === $post_type) && 0 !== $post->post_parent) { + $post_type = get_post_type($post->post_parent); } return 'document' === $post_type; - } @@ -1540,10 +1557,11 @@ public function verify_post_type( $documentish = false ) { * * @param int $post_id the post ID. */ - public function clear_cache( $post_id ) { - wp_cache_delete( $post_id, 'document_post_type' ); - wp_cache_delete( $post_id, 'document_revision_indices' ); - wp_cache_delete( $post_id, 'document_revisions' ); + public function clear_cache($post_id) + { + wp_cache_delete($post_id, 'document_post_type'); + wp_cache_delete($post_id, 'document_revision_indices'); + wp_cache_delete($post_id, 'document_revisions'); } @@ -1552,18 +1570,18 @@ public function clear_cache( $post_id ) { * * @since 0.5 */ - public function do_feed_revision_log() { + public function do_feed_revision_log() + { // because we're in function scope, pass $post as a global. global $post; // remove this filter to A) prevent trimming and B) to prevent WP from using the attachID if there's no revision log. - remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); - remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' ); + remove_filter('get_the_excerpt', 'wp_trim_excerpt'); + remove_filter('get_the_excerpt', 'twentyeleven_custom_excerpt_more'); // include the feed and then die. - load_template( dirname( __FILE__ ) . '/revision-feed.php' ); - + load_template(dirname(__FILE__) . '/revision-feed.php'); } @@ -1576,16 +1594,16 @@ public function do_feed_revision_log() { * @param string $default the original feed. * @return string the slug for our feed */ - public function hijack_feed( $default ) { + public function hijack_feed($default) + { global $post; - if ( ! $this->verify_post_type( ( isset( $post->id ) ? $post->id : false ) ) || ! apply_filters( 'document_custom_feed', true ) ) { + if (!$this->verify_post_type((isset($post->id) ? $post->id : false)) || !apply_filters('document_custom_feed', true)) { return $default; } return 'revision_log'; - } @@ -1596,21 +1614,21 @@ public function hijack_feed( $default ) { * * @since 0.5 */ - public function revision_feed_auth() { + public function revision_feed_auth() + { /** * Allows the RSS feed to be switched off. * * @param boolean true Allows an RSS feed for documents. */ - if ( ! $this->verify_post_type() || ! apply_filters( 'document_verify_feed_key', true ) ) { + if (!$this->verify_post_type() || !apply_filters('document_verify_feed_key', true)) { return; } - if ( is_feed() && ! $this->validate_feed_key() ) { - wp_die( esc_html__( 'Sorry, this is a private feed.', 'wp-document-revisions' ) ); + if (is_feed() && !$this->validate_feed_key()) { + wp_die(esc_html__('Sorry, this is a private feed.', 'wp-document-revisions')); } - } @@ -1620,28 +1638,29 @@ public function revision_feed_auth() { * @since 0.5 * @return bool */ - public function validate_feed_key() { + public function validate_feed_key() + { global $wpdb; // verify key exists. // phpcs:ignore WordPress.Security.NonceVerification.Recommended - if ( empty( $_GET['key'] ) ) { + if (empty($_GET['key'])) { return false; } // make alphanumeric. // phpcs:ignore WordPress.Security.NonceVerification.Recommended - $key = preg_replace( '/[^a-z0-9]/i', '', sanitize_text_field( wp_unslash( $_GET['key'] ) ) ); + $key = preg_replace('/[^a-z0-9]/i', '', sanitize_text_field(wp_unslash($_GET['key']))); // verify length. - if ( strlen( $key ) !== self::$key_length ) { + if (strlen($key) !== self::$key_length) { return false; } // lookup key. // phpcs:ignore WordPress.DB.DirectDatabaseQuery - if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s", $wpdb->prefix . self::$meta_key, $key ) ) ) { + if ($wpdb->get_var($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s", $wpdb->prefix . self::$meta_key, $key))) { return true; } @@ -1655,26 +1674,27 @@ public function validate_feed_key() { * @since 0.5 * @param bool $send_notice (optional) whether or not to send an e-mail to the former lock owner. */ - public function override_lock( $send_notice = true ) { + public function override_lock($send_notice = true) + { - check_ajax_referer( 'wp-document-revisions', 'nonce' ); + check_ajax_referer('wp-document-revisions', 'nonce'); - $post_id = ( isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : false ); + $post_id = (isset($_POST['post_id']) ? sanitize_text_field(wp_unslash($_POST['post_id'])) : false); // verify current user can edit // consider a specific permission check here. - if ( ! $post_id || ! current_user_can( 'edit_document', $post_id ) || ! current_user_can( 'override_document_lock' ) ) { - wp_die( esc_html__( 'Not authorized', 'wp-document-revisions' ) ); + if (!$post_id || !current_user_can('edit_document', $post_id) || !current_user_can('override_document_lock')) { + wp_die(esc_html__('Not authorized', 'wp-document-revisions')); } // verify that there is a lock. - $current_owner = wp_check_post_lock( $post_id ); - if ( ! ( $current_user ) ) { - die( '-1' ); + $current_owner = wp_check_post_lock($post_id); + if (!($current_user)) { + die('-1'); } // update the lock. - wp_set_post_lock( $post_id ); + wp_set_post_lock($post_id); // get the current user ID. $current_user = wp_get_current_user(); @@ -1684,14 +1704,13 @@ public function override_lock( $send_notice = true ) { * * @param boolean $send_notice selector whether to send the locked document. */ - if ( apply_filters( 'send_document_override_notice', $send_notice ) ) { - $this->send_override_notice( $post_id, $current_owner, $current_user->ID ); + if (apply_filters('send_document_override_notice', $send_notice)) { + $this->send_override_notice($post_id, $current_owner, $current_user->ID); } - do_action( 'document_lock_override', $post_id, $current_user->ID, $current_owner ); - - die( '1' ); + do_action('document_lock_override', $post_id, $current_user->ID, $current_owner); + die('1'); } @@ -1704,41 +1723,42 @@ public function override_lock( $send_notice = true ) { * @param int $current_user_id id of user overriding lock. * @return bool true on sucess, false on fail */ - public function send_override_notice( $post_id, $owner_id, $current_user_id ) { + public function send_override_notice($post_id, $owner_id, $current_user_id) + { // get lock owner's details. - $lock_owner = get_userdata( $owner_id ); + $lock_owner = get_userdata($owner_id); // get the current user's details. - $current_user = wp_get_current_user( $current_user_id ); + $current_user = wp_get_current_user($current_user_id); // get the post. - $document = get_post( $post_id ); + $document = get_post($post_id); // build the subject. // translators: %1$s is the blog name, %2$s is the overriding user, %3$s is the document title. - $subject = sprintf( __( '%1$s: %2$s has overridden your lock on %3$s', 'wp-document-revisions' ), get_bloginfo( 'name' ), $current_user->display_name, $document->post_title ); + $subject = sprintf(__('%1$s: %2$s has overridden your lock on %3$s', 'wp-document-revisions'), get_bloginfo('name'), $current_user->display_name, $document->post_title); /** * Filters the locked document email subject text. * * @param string $subject delivered email subject text. */ - $subject = apply_filters( 'lock_override_notice_subject', $subject ); + $subject = apply_filters('lock_override_notice_subject', $subject); // build the message. // translators: %s is the user's name. - $message = sprintf( __( 'Dear %s:', 'wp-document-revisions' ), $lock_owner->display_name ) . "\n\n"; + $message = sprintf(__('Dear %s:', 'wp-document-revisions'), $lock_owner->display_name) . "\n\n"; // translators: %1$s is the overriding user, %2$s is the user's email, %3$s is the document title, %4$s is the document URL. - $message .= sprintf( __( '%1$s (%2$s), has overridden your lock on the document %3$s (%4$s).', 'wp-document-revisions' ), $current_user->display_name, $current_user->user_email, $document->post_title, get_permalink( $document->ID ) ) . "\n\n"; - $message .= __( 'Any changes you have made will be lost.', 'wp-document-revisions' ) . "\n\n"; + $message .= sprintf(__('%1$s (%2$s), has overridden your lock on the document %3$s (%4$s).', 'wp-document-revisions'), $current_user->display_name, $current_user->user_email, $document->post_title, get_permalink($document->ID)) . "\n\n"; + $message .= __('Any changes you have made will be lost.', 'wp-document-revisions') . "\n\n"; // translators: %s is the blog name. - $message .= sprintf( __( '- The %s Team', 'wp-document-revisions' ), get_bloginfo( 'name' ) ); + $message .= sprintf(__('- The %s Team', 'wp-document-revisions'), get_bloginfo('name')); /** * Filters the locked document email message text. * * @param string $message delivered email message text. */ - $message = apply_filters( 'lock_override_notice_message', $message ); + $message = apply_filters('lock_override_notice_message', $message); /** * Filters the lost lock document email text. @@ -1748,11 +1768,10 @@ public function send_override_notice( $post_id, $owner_id, $current_user_id ) { * @param int $current_user_id current user id (who has lost lock). * @param object $lock_owner locking user details. */ - $message = apply_filters( 'document_lock_override_email', $message, $post_id, $current_user_id, $lock_owner ); + $message = apply_filters('document_lock_override_email', $message, $post_id, $current_user_id, $lock_owner); // send mail. - return wp_mail( $lock_owner->user_email, $subject, $message ); - + return wp_mail($lock_owner->user_email, $subject, $message); } @@ -1762,9 +1781,10 @@ public function send_override_notice( $post_id, $owner_id, $current_user_id ) { * @since 1.0 */ - public function add_caps() { + public function add_caps() + { global $wp_roles; - if ( ! is_object( $wp_roles ) ) { + if (!is_object($wp_roles)) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $wp_roles = new WP_Roles(); } @@ -1854,10 +1874,10 @@ public function add_caps() { ), ); - foreach ( $wp_roles->role_names as $role => $label ) { + foreach ($wp_roles->role_names as $role => $label) { // if the role is a standard role, map the default caps, otherwise, map as a subscriber. - $caps = ( array_key_exists( $role, $defaults ) ) ? $defaults[ $role ] : $defaults['subscriber']; + $caps = (array_key_exists($role, $defaults)) ? $defaults[$role] : $defaults['subscriber']; /** * Filter the default capabilities. @@ -1865,18 +1885,17 @@ public function add_caps() { * @param array $caps the default set of capabilities for the role. * @param string $role the role being reviewed (all will be reviewed in turn). */ - $caps = apply_filters( 'document_caps', $caps, $role ); + $caps = apply_filters('document_caps', $caps, $role); - $role_caps = $wp_roles->roles[ $role ]['capabilities']; + $role_caps = $wp_roles->roles[$role]['capabilities']; // loop through capacities for role. - foreach ( $caps as $cap => $grant ) { + foreach ($caps as $cap => $grant) { // add only missing capabilities. - if ( ! array_key_exists( $cap, $role_caps ) ) { - $wp_roles->add_cap( $role, $cap, $grant ); + if (!array_key_exists($cap, $role_caps)) { + $wp_roles->add_cap($role, $cap, $grant); } } } - } @@ -1887,16 +1906,16 @@ public function add_caps() { * @param string $prepend the sprintf formatted string to prepend to the title. * @return string just the string */ - public function no_title_prepend( $prepend ) { + public function no_title_prepend($prepend) + { global $post; - if ( ! $this->verify_post_type( $post->ID ) ) { + if (!$this->verify_post_type($post->ID)) { return $prepend; } return '%s'; - } @@ -1908,26 +1927,27 @@ public function no_title_prepend( $prepend ) { * @param int $post_id The ID of the post for which the title is being generated. * @return string the title possibly with the revision number */ - public function add_revision_num_to_title( $title, $post_id = null ) { + public function add_revision_num_to_title($title, $post_id = null) + { // If a post ID is not provided, do not attempt to filter the title. - if ( is_null( $post_id ) ) { + if (is_null($post_id)) { return $title; } - $post = get_post( $post_id ); + $post = get_post($post_id); // verify post type. - if ( ! $this->verify_post_type( $post ) ) { + if (!$this->verify_post_type($post)) { return $title; } // if this is a document, and not a revision, just filter and return the title. - if ( 'revision' !== $post->post_type ) { + if ('revision' !== $post->post_type) { - if ( is_feed() ) { + if (is_feed()) { // translators: %s is the document title. - $title = sprintf( __( '%s - Latest Revision', 'wp-document-revisions' ), $title ); + $title = sprintf(__('%s - Latest Revision', 'wp-document-revisions'), $title); } /** @@ -1935,21 +1955,20 @@ public function add_revision_num_to_title( $title, $post_id = null ) { * * @param string $title the title retrieved from the post. */ - return apply_filters( 'document_title', $title ); - + return apply_filters('document_title', $title); } // get revision num. - $revision_num = $this->get_revision_number( $post->ID ); + $revision_num = $this->get_revision_number($post->ID); // if for some reason there's no revision num. - if ( ! $revision_num ) { - return apply_filters( 'document_title', $title ); + if (!$revision_num) { + return apply_filters('document_title', $title); } // add title, apply filters, and return. // translators: %1$s is the document title, %2$d is the revision ID. - return apply_filters( 'document_title', sprintf( __( '%1$s - Revision %2$d', 'wp-document-revisions' ), $title, $revision_num ) ); + return apply_filters('document_title', sprintf(__('%1$s - Revision %2$d', 'wp-document-revisions'), $title, $revision_num)); } @@ -1960,19 +1979,19 @@ public function add_revision_num_to_title( $title, $post_id = null ) { * @param string $content the post content. * @return string either the original content or none */ - public function content_filter( $content ) { + public function content_filter($content) + { - if ( ! $this->verify_post_type( get_post() ) ) { + if (!$this->verify_post_type(get_post())) { return $content; } // allow password prompt to display. - if ( post_password_required() ) { + if (post_password_required()) { return $content; } return ''; - } @@ -1981,7 +2000,8 @@ public function content_filter( $content ) { * * @return unknown */ - public function edit_flow_support() { + public function edit_flow_support() + { global $edit_flow; @@ -1991,37 +2011,36 @@ public function edit_flow_support() { * * @param boolean true default value to use Edit_Flow processes if installed and active. */ - if ( ! class_exists( 'EF_Custom_Status' ) || ! apply_filters( 'document_revisions_use_edit_flow', true ) ) { + if (!class_exists('EF_Custom_Status') || !apply_filters('document_revisions_use_edit_flow', true)) { return false; } // verify proper firing order. - if ( ! did_action( 'ef_init' ) ) { - _doing_it_wrong( 'edit_flow_support', 'Cannot call before ef_init has fired', null ); + if (!did_action('ef_init')) { + _doing_it_wrong('edit_flow_support', 'Cannot call before ef_init has fired', null); return false; } // verify custom_status is enabled. - if ( ! $edit_flow->custom_status->module_enabled( 'custom_status' ) ) { + if (!$edit_flow->custom_status->module_enabled('custom_status')) { return false; } // prevent errors if options aren't init'd yet. - if ( ! isset( $edit_flow->custom_status->module->options->post_types['document'] ) ) { + if (!isset($edit_flow->custom_status->module->options->post_types['document'])) { return false; } // check if enabled. - if ( 'off' === $edit_flow->custom_status->module->options->post_types['document'] ) { + if ('off' === $edit_flow->custom_status->module->options->post_types['document']) { return false; } // are we going to use Edit_Flow processes if installed and active. // make sure use_workflow_states returns false. - add_filter( 'document_use_workflow_states', '__return_false' ); + add_filter('document_use_workflow_states', '__return_false'); return true; - } @@ -2030,30 +2049,30 @@ public function edit_flow_support() { * * @return bool true if workflow states are on, otherwise false */ - public function use_workflow_states() { + public function use_workflow_states() + { /** * Filter to switch off use of standard Workflow States taxonomy. For internal use. * * @param boolean true default value to use standard WorkFlow States taxonomy. For internal use. */ - return apply_filters( 'document_use_workflow_states', true ); - + return apply_filters('document_use_workflow_states', true); } /** * Removes front-end hooks to add workflow state support. */ - public function disable_workflow_states() { + public function disable_workflow_states() + { - if ( $this->use_workflow_states() ) { + if ($this->use_workflow_states()) { return; } - remove_action( 'admin_init', array( &$this, 'initialize_workflow_states' ) ); - remove_action( 'init', array( &$this, 'register_ct' ), 15 ); - + remove_action('admin_init', array(&$this, 'initialize_workflow_states')); + remove_action('init', array(&$this, 'register_ct'), 15); } @@ -2066,35 +2085,35 @@ public function disable_workflow_states() { * @param unknown $return_attachments (optional). * @return array an array of post objects */ - public function get_documents( $args = array(), $return_attachments = false ) { + public function get_documents($args = array(), $return_attachments = false) + { $args = (array) $args; $args['post_type'] = 'document'; - $documents = get_posts( $args ); + $documents = get_posts($args); $output = array(); - if ( $return_attachments ) { + if ($return_attachments) { // loop through each document and build an array of attachment objects // this would be the same output as a query for post_type = attachment // but allows querying of document metadata and returns only latest revision. - foreach ( $documents as $document ) { - $document_object = $this->get_latest_revision( $document->ID ); - $output[] = get_post( $document_object->post_content ); + foreach ($documents as $document) { + $document_object = $this->get_latest_revision($document->ID); + $output[] = get_post($document_object->post_content); } } else { // used internal get_revision function so that filter work and revision bug is offset. - foreach ( $documents as $document ) { - $output[] = $this->get_latest_revision( $document->ID ); + foreach ($documents as $document) { + $output[] = $this->get_latest_revision($document->ID); } } // remove empty rows, e.g., created by autodraft, etc. - $output = array_filter( $output ); + $output = array_filter($output); return $output; - } @@ -2108,37 +2127,37 @@ public function get_documents( $args = array(), $return_attachments = false ) { * @param int $post_id the attachment ID. * @return string the modified URL */ - public function attachment_url_filter( $url, $post_id ) { + public function attachment_url_filter($url, $post_id) + { // not an attached attachment. - if ( ! $this->verify_post_type( $post_id ) ) { + if (!$this->verify_post_type($post_id)) { return $url; } - $document = get_post( $post_id ); + $document = get_post($post_id); - if ( ! $document ) { + if (!$document) { return $url; } // user can't read revisions anyways, so just give them the URL of the latest revision. - if ( ! current_user_can( 'read_document_revisions' ) ) { - return get_permalink( $document->post_parent ); + if (!current_user_can('read_document_revisions')) { + return get_permalink($document->post_parent); } // we know there's a revision out there that has the document as its parent and the attachment ID as its body, find it. global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery - $revision_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_content = %d LIMIT 1", $document->post_parent, $post_id ) ); + $revision_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_content = %d LIMIT 1", $document->post_parent, $post_id)); // couldn't find it, just return the true URL. - if ( ! $revision_id ) { + if (!$revision_id) { return $url; } // run through standard permalink filters and return. - return get_permalink( $revision_id ); - + return get_permalink($revision_id); } @@ -2151,9 +2170,10 @@ public function attachment_url_filter( $url, $post_id ) { * @param string $url the permalink. * @return string the modified permalink */ - public function wamp_document_path_filter( $url ) { - $url = preg_replace( '|^([a-z]{1}):|i', '', $url ); // Strip out windows drive letter if it's there. - return str_replace( '\\', '/', $url ); // Windows path sanitization. + public function wamp_document_path_filter($url) + { + $url = preg_replace('|^([a-z]{1}):|i', '', $url); // Strip out windows drive letter if it's there. + return str_replace('\\', '/', $url); // Windows path sanitization. } @@ -2165,10 +2185,11 @@ public function wamp_document_path_filter( $url ) { * @param Array $terms the terms to filter. * @param Object $taxonomy the taxonomy object. */ - public function term_count_cb( $terms, $taxonomy ) { - add_filter( 'query', array( &$this, 'term_count_query_filter' ) ); - _update_post_term_count( $terms, $taxonomy ); - remove_filter( 'query', array( &$this, 'term_count_query_filter' ) ); + public function term_count_cb($terms, $taxonomy) + { + add_filter('query', array(&$this, 'term_count_query_filter')); + _update_post_term_count($terms, $taxonomy); + remove_filter('query', array(&$this, 'term_count_query_filter')); } @@ -2180,8 +2201,9 @@ public function term_count_cb( $terms, $taxonomy ) { * @param Object $query the query object. * @return String the modified query */ - public function term_count_query_filter( $query ) { - return str_replace( "post_status = 'publish'", "post_status != 'trash'", $query ); + public function term_count_query_filter($query) + { + return str_replace("post_status = 'publish'", "post_status != 'trash'", $query); } @@ -2191,7 +2213,8 @@ public function term_count_query_filter( $query ) { * * @since 1.2.1 */ - public function register_term_count_cb() { + public function register_term_count_cb() + { $taxs = get_taxonomies( array( @@ -2201,10 +2224,9 @@ public function register_term_count_cb() { 'objects' ); - foreach ( $taxs as $tax ) { - $tax->update_count_callback = array( &$this, 'term_count_cb' ); + foreach ($taxs as $tax) { + $tax->update_count_callback = array(&$this, 'term_count_cb'); } - } @@ -2219,14 +2241,14 @@ public function register_term_count_cb() { * @param Object $request the request object. * @return String the redirect URL without the trailing slash */ - public function redirect_canonical_filter( $redirect, $request ) { + public function redirect_canonical_filter($redirect, $request) + { - if ( ! $this->verify_post_type() ) { + if (!$this->verify_post_type()) { return $redirect; } - return untrailingslashit( $redirect ); - + return untrailingslashit($redirect); } @@ -2241,29 +2263,29 @@ public function redirect_canonical_filter( $redirect, $request ) { * @param string $size the size requested. * @return array the image array returned from image_downsize() */ - public function image_downsize( $false, $id, $size ) { + public function image_downsize($false, $id, $size) + { - if ( ! $this->verify_post_type( $id ) ) { + if (!$this->verify_post_type($id)) { return false; } - remove_filter( 'image_downsize', array( &$this, 'image_downsize' ) ); - remove_filter( 'wp_get_attachment_url', array( &$this, 'attachment_url_filter' ) ); + remove_filter('image_downsize', array(&$this, 'image_downsize')); + remove_filter('wp_get_attachment_url', array(&$this, 'attachment_url_filter')); - $direct = wp_get_attachment_url( $id ); - $image = image_downsize( $id, $size ); + $direct = wp_get_attachment_url($id); + $image = image_downsize($id, $size); - add_filter( 'image_downsize', array( &$this, 'image_downsize' ), 10, 3 ); - 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('wp_get_attachment_url', array(&$this, 'attachment_url_filter'), 10, 2); // if WordPress is going to return the direct url to the real file, // serve the document permalink (or revision permalink) instead. - if ( $image[0] === $direct ) { - $image[0] = wp_get_attachment_url( $id ); + if ($image[0] === $direct) { + $image[0] = wp_get_attachment_url($id); } return $image; - } @@ -2275,27 +2297,26 @@ public function image_downsize( $false, $id, $size ) { * @param Object $wp The global WP object. * @return the WP global object */ - public function ie_cache_fix( $wp ) { + public function ie_cache_fix($wp) + { // SSL check. - if ( ! is_ssl() ) { + if (!is_ssl()) { return $wp; } // IE check. - if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) || stripos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'MSIE' ) === false ) { + if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos(sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])), 'MSIE') === false) { return $wp; } // verify that they are requesting a document. - if ( ! isset( $wp->query_vars['post_type'] ) || 'document' !== $wp->query_vars['post_type'] ) { + if (!isset($wp->query_vars['post_type']) || 'document' !== $wp->query_vars['post_type']) { return $wp; } - add_filter( 'nocache_headers', '__return_empty_array' ); + add_filter('nocache_headers', '__return_empty_array'); return $wp; - } - } diff --git a/readme.txt b/readme.txt index d6ea24c8..8d724632 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Contributors: benbalter Tags: documents, uploads, attachments, document management, enterprise, version control, revisions, collaboration, journalism, government, files, revision log, document management, intranet, digital asset management Requires at least: 4.6 Tested up to: 5.3 -Stable tag: 3.2.3 +Stable tag: 3.2.4 == Description == @@ -102,6 +102,15 @@ See [the full documentation](http://ben.balter.com/wp-document-revisions) == Changelog == += 3.2.4 = + +* Address technical debt for WP Document Standards (#192) @NeilWJames +* On plugin activation, check that the user has edit_documents capability. If not, a warning message will be output that the menu may be incorrect. (#180) @NeilWJames +* PHPCS review (#179) @NeilWJames +* Bump phpunit/phpunit from 8.2.5 to 8.3.4 (#177) @dependabot-preview +* Addresses phpunit and toolset versions and prepare for future release (#174) @NeilWJames +* Version 3.2.2 gives an "property of non-object" at line 1403 on load (#161) @NeilWJames + = 3.2.3 = * On plugin activation, admin warning if user doesn't have edit_documents capability (#180) @NeilWJames diff --git a/wp-document-revisions.php b/wp-document-revisions.php index 5b906e4a..d4929018 100644 --- a/wp-document-revisions.php +++ b/wp-document-revisions.php @@ -1,9 +1,10 @@ */ -require_once dirname( __FILE__ ) . '/includes/class-wp-document-revisions.php'; +require_once dirname(__FILE__) . '/includes/class-wp-document-revisions.php'; // $wpdr is a global reference to the class. global $wpdr; $wpdr = new WP_Document_Revisions(); -require_once dirname( __FILE__ ) . '/includes/template-functions.php'; +require_once dirname(__FILE__) . '/includes/template-functions.php'; // Activation hooks must be relative to the main plugin file. -register_activation_hook( __FILE__, array( &$wpdr, 'activation_hook' ) ); +register_activation_hook(__FILE__, array(&$wpdr, 'activation_hook'));