WP मीडिया मोडल विंडो में अनुलग्नक हटाएं


15

मैं डुप्लिकेट फ़ाइलों का पता लगाने और एक पुरानी डुप्लिकेट पाए जाने पर एक नई फ़ाइल को हटाने के लिए WP मोडल मीडिया विंडो में एक विकल्प बनाने की कोशिश कर रहा हूं। मेरे पास डुप्लिकेट फ़ाइल को अचयनित करने और मीडिया मोडल में मूल फ़ाइल का चयन करने के लिए निम्नलिखित कोड काम कर रहे हैं ('अनुलग्नक_फिल्ड_टोइट' फ़िल्टर के साथ)। मैं क्या करना चाहता हूं, जब कोई उपयोगकर्ता बटन पर क्लिक करता है, तो मूल फ़ाइल को हटा दें (या कम से कम इसे मीडिया लाइब्रेरी विंडो में छिपाएं ताकि मैं इसे बाद में हटा सकूं)।

( function( $ ) {

    var _AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay;
    wp.media.view.Settings.AttachmentDisplay = _AttachmentDisplay.extend({
        render: function() {
            _AttachmentDisplay.prototype.render.apply(this, arguments);
            currentselection = this.controller.state().get('selection').first().toJSON();
            selection = this.controller.state().get('selection');

            $('button.dmc').on('click', function(e){

                e.preventDefault();

                var id = $(e.currentTarget).data("id");
                if(currentselection.id == id) {

                    currentattachment = wp.media.attachment(id);
                    selection.remove(currentattachment);

                    console.dir(wp.media.view.Attachment);

                    newattachment = wp.media.attachment($(e.currentTarget).data("original"));
                    selection.add(newattachment);

                }
            });
        }
    });

} )( jQuery );

इंटरफ़ेस संलग्न छवि की तरह दिखता है।

डुप्लीकेट मीडिया चैक का स्क्रीनशॉट

मैं 5873 लाइन में मीडिया-views.js में देख सकता हूं कि 'डिलीट। डेली-अटैचमेंट' से जुड़ी डिलीट एक्ट फ़ंक्शन है। इमेज आईडी या अटैचमेंट ऑब्जेक्ट में पास करके, मैं अपने वर्तमान सेटअप को कैसे एक्सेस कर सकता हूं?


4
क्या यह प्रश्न अभी भी खुला है या आपको अभी तक कोई उत्तर मिला है?
संलग्न

@ इस प्रश्न पर अब एक खुला इनाम है। तो इसके लिए जाएं :-)
पीटर गोएन्ग

जवाबों:


5

कुछ हद तक विहित (या कम से कम भरपूर) जवाब देने का प्रयास करते हुए, यह wpse142997.jsबाल टेम्पलेट के लिए जावास्क्रिप्ट है :

jQuery( document ).ready(function() {
    ( function( $ ) {
        var media = wp.media,
            l10n = media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n,
            attachments = media.model.Attachments.all,
            attachments_uploaded = [];

        if ( typeof wp.Uploaded === 'undefined') return;

        // Keep track of files uploaded.
        wp.Uploader.queue.on( 'add', function ( attachment ) {
            attachments_uploaded.push( attachment );
        });

        // The Uploader (in wp-includes/js/plupload/wp-plupload.js) resets the queue when all uploads are complete.
        wp.Uploader.queue.on( 'reset', function () {
            var idx, uploaded = attachments_uploaded.slice(0); // Clone
            attachments_uploaded = [];
            for ( idx = 0; idx < uploaded.length; idx++ ) {
                if ( uploaded[idx].get('name').match(/-[0-9]+$/) ) {
                    $.post( ajaxurl, {
                            action: 'wpse142997_is_dup',
                            dup_id: uploaded[idx].id,
                            nonce: wpse142997_params.is_dup_nonce
                        }, function( response ) {
                            var original, dup, dup_view, sidebar, selection;
                            if ( response && !response.error && response.original_id && response.dup_id ) {
                                original = attachments.get( response.original_id );
                                dup = attachments.get( response.dup_id );
                                if ( original && dup ) {
                                    dup.set( 'dup_original', original ); // May be ungood - mostly doing it so can use wp.templates.
                                    dup_view = media.view.Attachment.extend({
                                        tagName:   'div',
                                        className: 'attachment-dmc',
                                        template: media.template('attachment-dmc'),
                                        events: {
                                            'click button.dmc': 'removeDupSelectOriginal'
                                        },
                                        initialize: function() {
                                            this.focusManager = new media.view.FocusManager({
                                                el: this.el
                                            });
                                            media.view.Attachment.prototype.initialize.apply( this, arguments );
                                        },
                                        render: function() {
                                            if ( this.get_dup_original() ) {
                                                media.view.Attachment.prototype.render.apply( this, arguments );
                                                this.focusManager.focus();
                                            }
                                            return this;
                                        },
                                        removeDupSelectOriginal: function( event ) {
                                            var dup_original = this.get_dup_original();
                                            event.preventDefault();

                                            if ( dup_original && confirm( l10n.warnDelete ) ) {
                                                this.model.destroy();
                                                this.controller.state().get('selection').add( dup_original );
                                                this.remove();
                                            }
                                        },
                                        get_dup_original: function () {
                                            var dup_original = this.model.get('dup_original');
                                            return dup_original && attachments.get( dup_original.id ) ? dup_original : null;
                                        }
                                    });
                                    // A hacky way to get the sidebar.
                                    sidebar = media.frame.content.view.views.get('.media-frame-content')[0].sidebar;
                                    selection = sidebar.controller.state().get('selection');
                                    // The sidebar boxes get deleted and recreated on each select - hack into this to do the same.
                                    selection.on( 'selection:single', function ( event ) {
                                        if ( selection.single().get('dup_original') ) {
                                            sidebar.set( 'dmc', new dup_view({
                                                controller: sidebar.controller,
                                                model: selection.single(),
                                                priority: 100
                                            }) );
                                        }
                                    } );
                                    selection.on( 'selection:unsingle', function ( event ) {
                                        sidebar.unset('dmc');
                                    } );
                                    // Refire the select as we missed it (could/should just do the view create code here again).
                                    selection.trigger('selection:single');
                                }
                            }
                        }, 'json'
                    );
                }
            }
        });
    } )( jQuery );
});

यह है functions.php:

function wpse142997_wp_enqueue_scripts() {
    wp_enqueue_script( 'wpse142997', get_stylesheet_directory_uri() . '/wpse142997.js', array( 'jquery', 'media-views' ), '1.0' );
    $params = array(
        'is_dup_nonce' => wp_create_nonce( 'wpse142997_is_dup_submit_' ),
    );
    wp_localize_script( 'wpse142997', 'wpse142997_params', $params );
    ob_start();
    ?>
<style>
.attachment-dmc { float:left; overflow:hidden; position:relative; }
.attachment-dmc div { background-color:#FFEBE7; border:1px solid #CB9495; border-radius:5px; margin-top:16px; padding:6px; }
.attachment-dmc div h3 { margin-top:0; }
.attachment-dmc div h3 span { background-color:#E70000; border-radius:5px; color:white; margin-top:0; padding:0 6px; }
</style>
    <?php
    wp_add_inline_style( 'media-views', str_replace( array( '<style>', '</style>' ), '', ob_get_clean() ) );
}

function wpse142997_print_media_templates() {
?>
<script type="text/html" id="tmpl-attachment-dmc">
    <# if ( data.dup_original ) { #>
        <div>
            <h3><span><?php _e( 'Duplicate file detected' ); ?></span></h3>
            <p>
                <?php _e( 'This file appears to be a duplicate of <a href="{{ data.dup_original.attributes.editLink }}&amp;image-editor" target="_blank">{{ data.dup_original.attributes.filename }}</a> uploaded on {{ data.dup_original.attributes.dateFormatted }}' ); ?>
            </p>
            <button id="run_dmc" class="dmc" name="dmc"><?php _e( 'Remove duplicate and select original' ); ?></button>
        </div>
    <# } #>
</script>
<?php
}

function wpse142997_is_dup() {
    $ret = array( 'error' => false );

    if ( ! check_ajax_referer( 'wpse142997_is_dup_submit_', 'nonce', false /*die*/ ) ) {
        $ret['error'] = __( 'Permission error' );
    } else {
        $dup_id = isset( $_POST['dup_id'] ) ? $_POST['dup_id'] : '';
        if ( ! $dup_id || ! ( $post = get_post( $dup_id ) ) ) {
            $ret['error'] = __( 'Bad dup_id' );
        } else {
            $post_name = preg_replace( '/-[0-9]+$/', '', $post->post_name ); 
            global $wpdb;
            $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE'
                . ' post_title = %s AND post_type = %s AND post_mime_type = %s AND post_status = %s AND post_name = %s ORDER BY post_date ASC LIMIT 1',
                $post->post_title, $post->post_type, $post->post_mime_type, $post->post_status, $post_name
            );
            if ( $original_id = $wpdb->get_var( $sql ) ) {
                $ret['original_id'] = $original_id;
                $ret['dup_id'] = $dup_id;
            }
        }
    }

    wp_send_json( $ret );
}

add_action( 'admin_enqueue_scripts', 'wpse142997_wp_enqueue_scripts' );
add_action( 'print_media_templates', 'wpse142997_print_media_templates' );
add_action( 'wp_ajax_wpse142997_is_dup', 'wpse142997_is_dup' );

जावास्क्रिप्ट WP मीडिया मोडल तरीके का पालन करने की कोशिश करता है जितना कि मैं इसे समझता हूं, जो केवल आंशिक है। यह media.view.Attachmentएक wp.templateटेम्पलेट बनाता है और एक टेम्पलेट का उपयोग करता है । कुछ हैक किए गए बिट्स हैं - विशेष रूप से फ्रेम ऑब्जेक्ट में एक लंबी पहुंच के माध्यम से साइडबार प्राप्त करना संदेहास्पद लगता है (और केवल आसपास बहुत प्रहार के बाद पाया गया था)।


धन्यवाद, यह निश्चित रूप से उस तरह का है जो मैं लक्ष्य कर रहा था। विहित भाग के बारे में, वहाँ से चुनने के लिए बहुत सारे कारण नहीं हैं और मैं अपने दम पर एक बेहतर पाठ का पता नहीं लगा सका, इसलिए यह बहुत निकट है। मैं अगले हफ्ते करीब से देखूंगा।
निकोलई

मैंने 2014 के फंक्शन्स में कोड को कॉपी किया। एफपी और जेएस को वर्णित के रूप में सेट किया और अभी भी डुप्लिकेट नोटिफिकेशन सेक्शन को नहीं देख सकता। इसे देखने के लिए एक बार कोड होने के बाद मुझे क्या कार्रवाई करनी चाहिए?
कालेंजी

मुझे लगता है कि आपने एक डुप्लिकेट छवि अपलोड की है! ...
बोंगर

4

आपको बस destroyमेथड ऑन कॉल करना हैattachment मॉडल । यह दोनों मीडिया लाइब्रेरी दृश्य से लगाव को हटा देगा, और डेटाबेस में संलग्नक और अपलोड की गई निर्देशिका में सभी लिंक की गई फ़ाइलों को हटाने के लिए बैकएंड पर एक अजाक्स कॉल भेजेगा।

आपको आईडी प्राप्त करने के लिए JSON के अटैचमेंट को बदलने की आवश्यकता नहीं है: आप सीधे बैकबोन मॉडल में हेरफेर कर सकते हैं। selectionकई संलग्नक के एक संग्रह है।

( function( $ ) {

    var _AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay;
    wp.media.view.Settings.AttachmentDisplay = _AttachmentDisplay.extend({
        render: function() {
            _AttachmentDisplay.prototype.render.apply(this, arguments);

            $('button.dmc').on('click', $.proxy(function(e){

                e.preventDefault();
                selection = this.controller.state().get('selection');
                firstAttachment = selection.first();

                var id = $(e.currentTarget).data("id");
                if(currentselection.id == id) {

                    selection.remove(firstAttachment);
                    firstAttachment.destroy();

                    console.dir(wp.media.view.Attachment);

                    newattachment = wp.media.attachment($(e.currentTarget).data("original"));
                    selection.add(newattachment);

                }
            }, this));
        }
    });

} )( jQuery );

मैंने क्लिक इवेंट कॉलबैक के अंदर उपयोग करने में सक्षम होने के लिए $ .proxy कॉल भी जोड़ा है this


1
धन्यवाद, मुझे अच्छा लग रहा है। बेशक मेरे जेएस कौशल और वर्डप्रेस के उन हिस्सों का ज्ञान उतना महान नहीं है। निश्चित रूप से अगले सप्ताह यह कोशिश करना चाहते हैं। एक और बात, यह दिखावा करता है कि मुझे WP के बारे में बहुत कुछ पता नहीं है, मैं आपके द्वारा पोस्ट किए गए कोड का उपयोग कैसे कर सकता हूं?
निकोलई

जैसा कि मुझे आपके किसी भी समाधान की कोशिश करने का समय नहीं मिला है, मैंने @bonger के उत्तर के लिए इनाम दिया है - सिर्फ इसलिए कि यह जटिल लगता है, हालांकि फिर से जवाब देने के लिए धन्यवाद।
निकोलई
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.