Project

General

Profile

Feature #8900 » force-alt-tags.diff

Boone Gorges, 2019-11-14 11:22 AM

View differences:

wp-content/mu-plugins/cac-functions.php
1686 1686
	);
1687 1687
}
1688 1688

  
1689
/**
1690
 * Enqueues the 'force alt tags' script.
1691
 */
1692
add_action(
1693
	'admin_enqueue_scripts',
1694
	function( $hook_suffix ) {
1695
		$load = [
1696
			'post.php',
1697
			'post-new.php',
1698
		];
1699

  
1700
		if ( ! in_array( $hook_suffix, $load, true ) ) {
1701
			return;
1702
		}
1703

  
1704
		wp_enqueue_script(
1705
			'cac-force-alt-tags',
1706
			home_url( 'wp-content/mu-plugins/js/cac-force-alt-tags.js' ),
1707
			[ 'jquery' ]
1708
		);
1709

  
1710
		wp_enqueue_style(
1711
			'cac-force-alt-tags',
1712
			home_url( 'wp-content/mu-plugins/css/cac-force-alt-tags.css' )
1713
		);
1714
	}
1715
);
1716

  
1689 1717
/**
1690 1718
 * "Selector" scripts.
1691 1719
 */
wp-content/mu-plugins/css/cac-force-alt-tags.css
1
.alt-tag-warning {
2
	color: #f00;
3
	height: 100%;
4
	margin-top: 10px; /* To match .media-toolbar-primary .media-button */
5
}
6

  
7
.media-sidebar .attachment-details .setting.has-error input {
8
	border-color: #f00;
9
}
wp-content/mu-plugins/js/cac-force-alt-tags.js
1
(function($){
2
	$(document).ready(function(){
3
		var selection,
4
		  $altInput,
5
		  $mediaInsert,
6
		  $toolbarEl;
7

  
8
		var removeWarning = function() {
9
			$toolbarEl.find('.alt-tag-warning').remove();
10
		}
11

  
12
		var disableSubmit = function() {
13
			$mediaInsert.attr('disabled', 'disabled');
14
		}
15

  
16
		var enableSubmit = function() {
17
			$mediaInsert.removeAttr('disabled');
18
		}
19

  
20
		var bindSelectionEvents = function() {
21
			selection = wp.media.frame.state().get('selection');
22

  
23
			$toolbarEl = wp.media.frame.toolbar.view.$el;
24

  
25
			$mediaInsert = $toolbarEl.find('button.media-button-insert');
26

  
27
			selection.on( 'selection:single selection:multiple', function() {
28
				// This element is not available until 'selection'.
29
				$('label[data-setting="alt"] input').on('keyup', checkForAltText);
30

  
31
				checkForAltText();
32
			});
33

  
34
			var uploaderView = wp.media.frame.views.get('.media-frame-uploader')[0]
35
			uploaderView.on('ready', function() {
36
				uploaderView.uploader.success = function( attData ) {
37
					console.log(1);
38
					visibleSidebarCallback();
39
					wp.media.frame.views.get('.media-frame-content')[0].sidebar.$el.addClass( 'visible' );
40
				}
41
			})
42
		}
43

  
44
		var checkForAltText = function() {
45
			var hasAltText = true;
46
			var imageIsSelected = false;
47

  
48
			selection.each(function(item){
49
				if ( ! hasAltText ) {
50
					return;
51
				}
52

  
53
				if ( 'image' !== item.attributes.type ) {
54
					return;
55
				} else {
56
					imageIsSelected = true;
57
				}
58

  
59
				if ( ! item.attributes.hasOwnProperty( 'alt' ) ) {
60
					hasAltText = false;
61
					return;
62
				}
63

  
64
				var $altInput = $('label[data-setting="alt"] input');
65

  
66
				hasAltText = $altInput.length > 0 && 0 !== $altInput.val().length;
67
			});
68

  
69
			// Existing warning should be removed in all cases.
70
			removeWarning();
71

  
72
			// Nothing more to do.
73
			if ( ! imageIsSelected ) {
74
				return;
75
			}
76

  
77
			if ( hasAltText ) {
78
				enableSubmit();
79

  
80
			} else {
81
				var warning = '<div id="alt-tag-warning" class="alt-tag-warning">You must supply alt text before inserting this image.</div>';
82

  
83
				$toolbarEl.find('.media-frame-toolbar .media-toolbar-primary').append(warning);
84
				var sidebar = wp.media.frame.views.get('.media-frame-content')[0].sidebar;
85
				sidebar.$el.find('label.setting[data-setting="alt"]').addClass('has-error');
86

  
87
				disableSubmit();
88
			}
89
		}
90

  
91
		// Bind our events to successful uploads.
92
		$(document).ready(function(){
93
			if ( typeof wp.Uploader === 'function' ) {
94
				$.extend( wp.Uploader.prototype, {
95
					success: function( attachment ) {
96
						// Selection events must be rebound after redraw.
97
						bindSelectionEvents();
98

  
99
						checkForAltText();
100
					}
101
				} );
102
			}
103
		});
104

  
105
		// Bind to the 'selection' events.
106
		wp.media.view.Modal.prototype.on( 'ready', function() {
107

  
108

  
109
		// cannot make this work - have to hide the message when switching to Upload Files tab
110
			var router = wp.media.frame.views.get('.media-frame-router')[0];
111

  
112
			console.log(router.views.view);
113
			router.on('ready',function(){
114
				console.log('no no no');
115
			});
116
			router.on('browse',function(){
117
				console.log('amybe');
118
			});
119
			wp.media.view.Modal.prototype.on( 'open', bindSelectionEvents );
120
		});
121
	});
122
}(jQuery));
    (1-1/1)