admin quick edit existing column in posts

Here is my meccano made of pieces found somewhere around. This code is from one of my unfinished and abandoned projects, but it worked (to my personal memory).

Note that I’ve prefixed the meta name with the underscore (_tie_post_views) to avoid its appearance in Custom Fields metabox on Post Edit page.

You’ll be forced to play for a while with the field positioning (may be use JavaScript?) because of flawed Quick Edit and Bulk Edit implementation.

Sorry, I have no possibility to describe and comment the code right now, but I’ll try to answer the relevant questions one day. Also, it looks almost self-explanatory.

<?php

///////////////////////// Quick Edit

add_action( 'quick_edit_custom_box', 'tie_display_bulk_quick_edit_postviews', 10, 2 );
add_action( 'bulk_edit_custom_box', 'tie_display_bulk_quick_edit_postviews', 10, 2 );


// Empty form field, data will be populated by JavaScript
function tie_display_bulk_quick_edit_postviews( $column ) {

    if ( 'tie_post_views' === $column ) {

        ?>
        <fieldset class="inline-edit-col-right">
            <div class="inline-edit-col">
                <div class="inline-edit-group wp-clearfix">
                    <label class="alignleft" for="tie_post_views"><span class="title"><?php _e( 'Tie Post Views', 'textdomain' ); ?></span></label>
                    <input type="text" name="_tie_post_views" id="tie_post_views" value="" />
                </div>
            </div>
        </fieldset>
        <?php
    }
}


add_action( 'admin_enqueue_scripts', 'tie_bulk_quick_edit_script' );

function tie_bulk_quick_edit_script() {
    wp_enqueue_script(
        'tie-bulk-quick-edit',
        plugins_url( 'js/tie-bulk-quick-edit.js', __FILE__ ),
        array(
            'jquery',
            'inline-edit-post',
        )
    );
}


add_action( 'save_post', 'tie_save_quick_edit_meta_post_views', 10, 2 );

function tie_save_quick_edit_meta_post_views( $post_id, $post ) {

    // pointless if $_POST is empty (this happens on Bulk Edit)
    if ( empty( $_POST ) ) {
        return $post_id;
    }

    // verify Quick Edit nonce
    if ( isset( $_POST['_inline_edit'] ) && ! wp_verify_nonce( $_POST['_inline_edit'], 'inlineeditnonce' ) ) {
        return $post_id;
    }

    // Regular post meta update goes here

}

///////////////////////// Bulk Edit
// https://developer.wordpress.org/reference/hooks/wp_ajax_action/
add_action( 'wp_ajax_tie_post_views_bulk_save', 'tie_post_views_bulk_save' );

function tie_post_views_bulk_save() {

    $post_ids = ( isset( $_POST['post_ids'] ) && ! empty( $_POST['post_ids'] ) ) ? $_POST['post_ids'] : null;

    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {

        if ( isset( $_POST['_tie_post_views'] ) && ! empty( $_POST['_tie_post_views'] ) ) {

            foreach ( $post_ids as $post_id ) {
                update_post_meta( $post_id, '_tie_post_views', $_POST['_tie_post_views'] );
            }
        }
    }

}

And tie-bulk-quick-edit.js (should be completely rewritten, but it subserves):

jQuery(document).ready(function($){

    //Prepopulating quick-edit
    var $inline_editor = inlineEditPost.edit;
    inlineEditPost.edit = function(id){

        $inline_editor.apply(this, arguments);

        var post_id = 0;
        if( typeof(id) == 'object'){
            post_id = parseInt(this.getId(id));
        }

        if(post_id != 0){
            $row = $('#edit-' + post_id);

            tie_post_views = $('#post-' + post_id + ' .tie_post_views').text();
            $row.find('#tie_post_views').val(tie_post_views);
        }
    }

    // Detect 'Enter' key press inside 'Bulk Edit' and click 'Update' button
    // https://stackoverflow.com/a/18160418/
    $('#bulk-edit').keypress(function (e) {
        var key = e.which;
        if(key == 13) {
            $('#bulk_edit').click();
            return false;
        }
    });

    // Live() is obsolete
    // This is workaround to avoid refactoring
    // Think about a better solution
    // https://stackoverflow.com/questions/14354040/jquery-1-9-live-is-not-a-function
    if (typeof jQuery.fn.live == 'undefined' || !(jQuery.isFunction(jQuery.fn.live))) {
        jQuery.fn.extend({
            live: function (event, callback) {
                if (this.selector) {
                    jQuery(document).on(event, this.selector, callback);
                }
            }
       });
    }

    $( '#bulk_edit' ).live( 'click', function(e) {

        var $bulk_row = $( '#bulk-edit' );

        var $post_ids = new Array();
        $bulk_row.find( '#bulk-titles' ).children().each( function() {
            $post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
        });

        var $tie_post_views = $bulk_row.find( 'input[name="_tie_post_views"]' ).val();

        $.ajax({
            url: ajaxurl,
            type: 'POST',
            async: false,
            cache: false,
            data: {
                action: 'tie_post_views_bulk_save',
                _tie_post_views: $tie_post_views
            }
        });
    });
});