• Resolved khelmar

    (@khelmar)


    I’m trying to get a system set up to show public users the final version of the document, but allow folks who are logged in the ability to create updated versions. I’ve tried the following WP-Query:

    array(
    ‘taxonomy’ => ‘workflow_state’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘final’
    )

    However, if the most recent revision isn’t final, it just doesn’t show anything rather than getting the most recent revision that IS final. Is there a way to retrieve the most recent revision that has a workflow-state of final?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor nwjames

    (@nwjames)

    @khelmar.
    The underlying problem with standard WP processing is that the underlying data model does not meet with your requirement.

    This is because neither the attachment records (that hold the document file location) nor the revision posts contain any linkage to the status information.

    Only the document post record contains a linkage to the current term and no history of its value is kept.

    To modify the code to meet your requirement, I would modify the code as follows:

    a) Add an action to “save_post_document”

    See https://developer.www.ads-software.com/reference/hooks/save_post_post-post_type/

    This action is called whenever a document is saved. There is already a plugin function based on this hook.

    Yours should be called with a lower priority than standard to be invoked after the workflow_state term have been added (and featured image has been dealt with).

    The processing that I would then invoke is to find out if a final term is held on the post, and if so, what is its attachment.

    Then store (or update) a postmeta data item called something like _wpdr_final that contains the attachment id.

    b) Add a filter to “document_serve”

    
    		 * @param string  $file       File name to be served.
    		 * @param integer $post->ID   Post id of the document.
    		 * @param integer $attach->ID Post id of the attachment.
    		 */
    		$file = apply_filters( 'document_serve', $file, $post->ID, $attach->ID );

    This is more complex. The file name is the file name coming from the latest attachment.

    The logic needed is to see
    a) if the user is not logged on, then
    b) if the document has not got a Final status then we need to update the file name.

    Get the attachment ID held on the _wpdr_final and get the attached file (function get_attached_file() ).

    Hope this gives you some pointers,
    Neil James

    Thread Starter khelmar

    (@khelmar)

    Thanks! I just figured out yesterday that taxonomies aren’t saved with revisions, so this is really helpful!

    Plugin Contributor nwjames

    (@nwjames)

    @khelmar,
    I have been thinking further about youe issue and can see that the workflow is a reasonable one if you have a publishing process and subsequent revisioning process.

    I have therefore created an issue on the plugin GtHub page and have created a pull request to simplify your potential coding by creating an action point and filter where you can link your processing.

    Since these are not currently available, I’ll give the code that you will need to include in Version 3.3.1 that hopefully will be in Version 3.4 of the plugin when it is released.

    1. Action document_saved

    This is actioned after all save processing for the document is done.

    Code to insert after line 1265 of includes/class-wp-document-revisions-admin.php
    which reads wp_set_post_terms( $doc_id, array( $ws ), 'workflow_state' )

    		// find the attachment id.
    		$attach_id = get_post_field( 'post_content', $doc_id );
    
    		/**
    		 * Fires once a document has been saved.
    		 *
    		 * @since 3.4.0
    		 *
    		 * @param int $doc_id    id of Document post.
    		 * @param int $attach_id id of Attachment post.
    		 */
    		do_action( 'document_saved', $doc_id, $attach_id );

    Using this action you create/update the postmeta data when the status is final.

    2. Filter document_serve_attachment

    The attachment corresponding to the document (or revision) post is retrieved. This is then filtered.

    Code to insert after line 1036 of includes/class-wp-document-revisions.php
    which reads $attach = get_post( get_post_field( 'post_content', $rev_id ) )

    		/*
    		 * Filter the attachment post to serve.
    		 *
    		 * @param WP_Post $attach Attachment Post corresponding to document / revisions selected.
    		 * @param int     $rev_id Id of document / revision selected.
    		 */
    		$attach = apply_filters( 'document_serve_attachment', $attach, $rev_id );

    This is where you determine whether the user is logged on or not If not to return the Attachment Post corresponding to the value stored in the postmeta field.

    You will need to check that the $rev_id is pointing to a document and not a revision. Also the various cases of not Final documents that don’t have the meta data.

    If you do not wish to display the document you will need to manage this yourself. In the future, you will be able to return false and it will die gracefully.

    Regards,
    Neil James

    Thread Starter khelmar

    (@khelmar)

    I’ll take a look – I worked out a way around this using Advanced Custom Fields to create version and a document state item metadata on each document, then, when I loop through all the documents, I use this code:

    $args3 = array(
    'orderby' => 'modified', 
    'order' => 'DESC',
    'fields' => 'ids',
    );
            
    $revisions = wp_get_post_revisions(get_the_ID(), $args3);

    if (get_field(‘document_status’) == “final”) {
    <output code here>
    }

    It means I don’t use WP Document Revision’s workflow state, but it seems to work for now. Thanks for all the advice!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get all final document versions?’ is closed to new replies.