Site icon Hip-Hop Website Design and Development

How one can disable Publish button on Edit submit if submit title exists

I need to ask a query: I’ve a supply code, it solely works for the post-new, however not edit submit. So the right way to disable Publish button on Edit submit if submit title exists?

add_action('wp_ajax_check-posts-by-title', perform(){
    $title = isset( $_REQUEST['title'] ) ? esc_attr( $_REQUEST['title'] ) : null;
    // verify for title
    if ( !$title ) {
        return wp_send_json(array(
            'enable_btn' => false,
            'message' => 'No title specified!' 
        ));
    }
    $submit = get_page_by_title($title, OBJECT, 'submit');
    // verify for submit
    if ( isset($post->ID) && 'publish' === $post->post_status ) {
        return wp_send_json(array(
            'enable_btn' => false
        ));
    }
    // nothing caught
    return wp_send_json(array(
        'enable_btn' => true
    ));
    // kill response
    wp_die();
});

add_action('admin_footer', perform() { 
    world $pagenow;

    if ( !$pagenow || !in_array($pagenow, array( 'post-new.php' )) )
        return; // solely enqueue when writing/modifying posts
    ?>
    <script>
        jQuery(doc).prepared(perform($){
            $(doc).on("change", "#titlediv #title", perform(e){
                var i = $(this)
                  , f = i.closest('kind')
                  , t = i.val()
                  , b = $('enter[name="publish"]', f).first();

                if ( self.xhr ) {
                    self.xhr.abort();
                } else {
                    var xhr;
                }

                if ( !b.size ) return;
                b.attr('disabled','disabled');

                if ( $.trim( t ) ) {
                    self.xhr = $.ajax({kind: 'submit', url: 'admin-ajax.php', knowledge: {
                        motion: 'check-posts-by-title',
                        title: t
                    }, success: perform(res){
                        if ( res && res.enable_btn ) {
                            b.removeAttr('disabled');
                        } else {
                            if ( res.message ) {
                                alert(res.message);
                            }
                        }
                    }, error: perform(){
                        i.set off('change'); // roll over
                    }});
                }
            });
            $('#titlediv #title').set off('change');
        });
    </script>
    <?php
});

Thanks! <3