(function ($) { $(function () { var options = $.extend({ lang: undefined, plugins_url: undefined, youtube_url_patterns: undefined, youtube_image_url_format: undefined, errors: {} }, anv_edit_form); var frame, $title = $('#title'), $video_url = $('#video-url'), $image_url = $('#image-url'), $image_preview_container = $('.image-preview'), $btn_upload = $('.button-upload'), $btn_generate = $('.button-generate'), $visibility = $('#visibility'); $visibility.hide(); // set required fields var required = [$title, $video_url, $image_url]; for (var i in required) { if (!required.hasOwnProperty(i)) continue; required[i].prop('required', true); } $image_url.on('change', on_image_url_change); $btn_upload.on('click', on_btn_upload_click); $btn_generate.on('click', on_btn_generate_click); function on_btn_upload_click(event) { event.preventDefault(); if ( frame ) { frame.open(); return; } frame = wp.media({ title: 'Select or Upload Media Of Your Chosen Persuasion', button: { text: 'Use this media' }, library: { type: 'image' }, multiple: false }); frame.on( 'select', function() { var image = frame.state().get('selection').first().toJSON(); $image_url.val(image.url); $image_url.trigger('change'); }); frame.open(); } function on_btn_generate_click(event) { event.preventDefault(); var video_url = $video_url.val(); if (video_url) { for (var i in options.youtube_url_patterns) { if (!options.youtube_url_patterns.hasOwnProperty(i)) continue; var re = new RegExp(options.youtube_url_patterns[i]); var result = re.exec(video_url); if (result) { $image_url.val(options.youtube_image_url_format.replace('%s', result[1])); $image_url.trigger('change'); return; } } alert(options.errors.unknown_video_url_format); } else { alert(options.errors.video_url_empty); } } function on_image_url_change() { var $img = $(''); $img.prop('width', '320'); $img.prop('src', $image_url.val()); $image_preview_container.html(''); $image_preview_container.append($img); } }); })(jQuery);