Javascript 他的(支票); 如果(选中此项){ $('.media frame toolbar>.media toolbar.media toolbar primary').show(); $('.media frame toolbar>.media toolbar.

Javascript 他的(支票); 如果(选中此项){ $('.media frame toolbar>.media toolbar.media toolbar primary').show(); $('.media frame toolbar>.media toolbar.,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,他的(支票); 如果(选中此项){ $('.media frame toolbar>.media toolbar.media toolbar primary').show(); $('.media frame toolbar>.media toolbar.media toolbar secondary.credit notice').remove(); }否则{ $('.media frame toolbar>.media toolbar.media toolbar primary').hide

他的(支票); 如果(选中此项){ $('.media frame toolbar>.media toolbar.media toolbar primary').show(); $('.media frame toolbar>.media toolbar.media toolbar secondary.credit notice').remove(); }否则{ $('.media frame toolbar>.media toolbar.media toolbar primary').hide(); $('.media frame toolbar>.media toolbar>.media toolbar secondary').html(注意); } }); } } ); } }); //当模式关闭时执行此代码。 wp.media.view.Modal.prototype.on(“关闭”,函数(){ $('.credit notice').remove(); }); } })(jQuery);
( function( $ ) {
    var media_credit = '';
    var selection    = '';
    if ( wp.media ) {

        wp.media.view.Modal.prototype.on( "open", function() {
            if ( typeof wp.media.frame.state() !== 'undefined' ) {
                selection = wp.media.frame.state().get('selection')
            } else if ( typeof wp.media.featuredImage.frame().state('featured-image') !== 'undefined' ) {
                selection = wp.media.featuredImage.frame().state('featured-image').get('selection');
            }
            console.log('state', wp.media.frame.state().get('selection').first());

            if ( selection ) {
                console.log('add', selection);

                selection.on( 'selection:unsingle', function ( attachment ) {
                    console.log('unselected');
                    $('.confirm_field').remove();
                    $('.media-frame-toolbar > .media-toolbar .media-toolbar-secondary .credit-notice').remove();
                });

                selection.on( 'add', function ( attachment ) {

                    console.log('insert add', attachment);

                    var attach_id = '';
                    if (typeof attachment.id !== 'undefined' ) { // this is for selected from library
                        console.log(attachment);
                        attach_id = attachment.id;
                        console.log('attachment.id without delay', attach_id, $('#attachments-' + attach_id + '-media_credit') );
                        media_watch(attach_id);
                    } else {
                        // this is for uploaded
                        attach_id = selection.first().changed.id;
                        console.log('attachment.id with delay', attach_id, $('#attachments-' + attach_id + '-media_credit') );
                        media_watch(attach_id);
                    }

                    function media_watch(aid) {
                        // Check if credit data has value
                        let media_credit = $('#attachments-' + aid + '-media_credit').val();

                        let confirm_field = '<table class="confirm_field"><tr class="compat-field-media_confirm"><th scope="row" class="label"><input type="checkbox" class="checkbox" id="confirmNoCredit" value="yes"></th><td class="field"><label for="confirmNoCredit"><p class="description">Confirm without credit</p></label></td></tr></table>';
                        let notice        = '<div class="credit-notice notice notice-warning notice-alt inline"><p>The selected image has no Credit value. You are advised to add proper Credit. </p></div>';

                        // Hide the select button and show warning message
                        if ( media_credit == '' || ! media_credit ) {
                            $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').hide();
                            $('.media-sidebar').append(confirm_field);
                            $('.media-frame-toolbar > .media-toolbar > .media-toolbar-secondary').html(notice);
                        } else {
                            $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').show();
                        }

                        $(document).on('change', '#attachments-' + aid + '-media_credit', function() {
                            console.log('credit value', this.value.length);
                            if(this.value.length == 0) {
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').hide();
                                $('.confirm_field').remove();
                                $('.media-sidebar').append(confirm_field);
                                $('.media-frame-toolbar > .media-toolbar > .media-toolbar-secondary').html(notice);
                            } else {
                                $('.confirm_field').remove();
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').show();
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-secondary .credit-notice').remove();
                            }
                        });

                        // When editor check the checkbox (this is working)
                        $(document).on('change', "#confirmNoCredit", function() {
                            console.log('checked state', this.checked );
                            if(this.checked) {
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').show();
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-secondary .credit-notice').remove();
                            } else {
                                $('.media-frame-toolbar > .media-toolbar .media-toolbar-primary').hide();
                                $('.media-frame-toolbar > .media-toolbar > .media-toolbar-secondary').html(notice);
                            }
                        });
                    }

                } );
            }
        });

        // Execute this code when a Modal is closed.
        wp.media.view.Modal.prototype.on( "close", function() {
            $('.credit-notice').remove();
        });

    }
})( jQuery );