Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15596 closed enhancement (fixed)

Allow JS functions in TinyMCE $initArray

Reported by: janfabry Owned by:
Milestone: 3.2 Priority: normal
Severity: normal Version: 3.1
Component: TinyMCE Keywords:
Focuses: Cc:


r16090 allowed booleans, JSON-encoded arrays and objects to be passed to the TinyMCE configuration, by not wrapping them in quotes. If the check for { and [ could be expanded to include (, we could also pass anonymous functions (of the form (function() { if ( someClientSideProperty ) return 'a'; return 'b'; })()), which allows integration of client-side information in the TinyMCE configuration. This would greatly increase the flexibility of plugins to pass extra information to TinyMCE without jumping through hoops.

Change History (5)

comment:1 Denis-de-Bernardy3 years ago

This would be sooo neat.

Ran into the issue myself, and I had to do weird a workaround to get multi-dimensional objects in there, aka:

		// ...
		$campaign = array(
			'publish' => __('Launch', 'sem-backend'),
			'publishNow' => __('Launch:', 'sem-backend'),
			'publishOn' => __('Launch on:', 'sem-backend'),
			'publishOnPast' => __('Launched on:', 'sem-backend'),
			'unit' => __('Coupon left', 'sem-backend'),
			'units' => __('Coupons left', 'sem-backend'),
		$extra_captions = compact('defaults', 'product', 'campaign');
		$extra_captions = apply_filters('sem_backend_localize_js', $extra_captions);
		foreach ( $extra_captions as $key => $extra ) {
			$data = "\n\nsem_backendL10n['$key'] = {\n";
			$eol = '';
			foreach ( $extra as $var => $val ) {
				$data .= "$eol\t$var: \"" . esc_js($val) . '"';
				$eol = ",\n";
			$data .= "\n};\n";
			$data .= "try{convertEntities(sem_backendL10n.$key);}catch(e){};\n";
			$captions['l10n_print_after'] .= $data;
		wp_localize_script('sb-data-admin', 'sem_backendL10n', $captions);

comment:2 janfabry3 years ago

Indeed, my proposal covered only TinyMCE init, but there are other places where Javascript is generated, like in WP_Script::print_script_l10n(), which is used more and more as a generic data passing method, not just for localization.

We should keep backwards compatibility in mind: I wouldn't want a string like '(Closed)' to be interpreted as a non-string. But the same goes for '[Closed]' and '{Closed}', which will not be embedded in quotes now, even if they should be. Maybe add " to the list too, so once can "self-escape" and pass a variable as '"[This is a string, not an array]"'?

comment:3 koopersmith3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:4 azaozz3 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [17522]) Allow JS functions in the TinyMCE init array, fixes #15596

comment:5 ryan3 years ago

  • Milestone changed from Future Release to 3.2
Note: See TracTickets for help on using tickets.