<?php
/**
Plugin Name: Test TinyMCE Toolbars
Plugin URI: https://wordpress.org
Description: Small plugin for testing different configurations for the TinyMCE toolbars.
Version: 1.0
Author: Andrew Ozz
Author URI: http://www.laptoptips.ca/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

	Test TinyMCE Toolbars is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	any later version.

	Test TinyMCE Toolbars is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with WordPress. If not, see https://www.gnu.org/licenses/gpl-2.0.html.

	Copyright (c) Andrew Ozz. All rights reserved.
*/

if ( ! class_exists( 'WP_Test_Tinymce_Toolbars' ) ) :
class WP_Test_Tinymce_Toolbars {
	/**
	 * Available TinyMCE buttons:
	 * bold				Applies the bold format to the current selection.
	 * italic			Applies the italic format to the current selection.
	 * underline		Applies the underline format to the current selection.
	 * strikethrough	Applies strike though format to the current selection.
	 * alignleft		Left aligns the current block or image.
	 * aligncenter		Left aligns the current block or image.
	 * alignright		Right aligns the current block or image.
	 * alignjustify		Full aligns the current block or image.
	 * alignnone		Removes the alignment of the current block or image.
	 * styleselect		Dropdown list with styles to apply to selection.
	 * formatselect		Dropdown list with block formats to apply to selection.
	 * fontselect		Dropdown list with font families to apply to selection.
	 * fontsizeselect	Dropdown list with font sizes to apply to selection.
	 * bullist			Formats the current selection as a bullet list.
	 * numlist			Formats the current selection as a numbered list.
	 * outdent			Outdents the current list item or block element.
	 * indent			Indents the current list item or block element.
	 * blockquote		Applies block quote format to the current block level element.
	 * undo				Undoes the last operation.
	 * redo				Redoes the last undoed operation.
	 * removeformat		Removes the formatting from the current selection.
	 * subscript		Applies subscript format to the current selection.
	 * superscript		Applies superscript format to the current selection.
	 * visualaid		Toggles the visual aids for invisible elements.
	 * hr				Inserts a horizontal rule into the editor.
	 * link				Creates/Edits links within the editor.
	 * unlink			Removes links from the current selection.
	 * charmap			Inserts custom characters into the editor.
	 * pastetext		Toggles plain text pasting mode on/off.
	 * ltr				Sets the directionality of contents to ltr.
	 * rtl				Sets the directionality of contents to rtl.
	 * forecolor		Applies foreground/text color to selection.
	 * backcolor		Applies background color to selection.
	 *
	 * WordPress specific:
	 * wp_more			Inserts the <!-- more --> tag.
	 * wp_help			Opens the help.
	 * wp_adv			Toggles the second toolbar on/off.
	 * dfw				Distraction-free mode on/off.
	 *
	 */

	// Add buttons (once) from the above list.
	private $toolbar_1 = array(
		'formatselect',
		'bold',
		'italic',
		'underline',
		'strikethrough',
		'blockquote',
		'bullist',
		'numlist',
		'aligncenter',
		'link',
		'unlink',
		'hr',
		'wp_more',
		'wp_adv',
		'dfw', // Used only on the "Add/Edit Post" screens. Do not remove.
	);

	private $toolbar_2 = array(
		'alignleft',
		'alignjustify',
		'alignright',
		'indent',
		'outdent',
		'forecolor',
		'backcolor',
		'pastetext',
		'charmap',
		'removeformat',
		'undo',
		'redo',
		'wp_help',
	);

	public function __construct() {
		if ( ! defined( 'ABSPATH' ) ) {
			return;
		}

		add_filter( 'the_editor_content', array( $this, 'add_filters' ) );
	}

	public function add_filters( $content ) {
		if ( get_current_screen()->base === 'post' ) {
			add_filter( 'mce_buttons', array( $this, 'mce_buttons_1' ) );
			add_filter( 'mce_buttons_2', array( $this, 'mce_buttons_2' ) );
			add_filter( 'tiny_mce_before_init', array( $this, 'mce_options' ) );
		}

		return $content;
	}

	public function mce_buttons_1( $buttons_array ) {
		return $this->toolbar_1;
	}

	public function mce_buttons_2( $buttons_array ) {
		return array_diff( $this->toolbar_2, $this->toolbar_1 );
	}

	public function mce_options( $init_array ) {

		$init_array['block_formats'] =
			'Paragraph=p;' .
	//		'Heading 1=h1;' . // Disable H1
			'Heading 2=h2;' .
			'Heading 3=h3;' .
			'Heading 4=h4;' .
			'Heading 5=h5;' .
			'Heading 6=h6;' .
			'Preformatted=pre';

		return $init_array;
	}
}

new WP_Test_Tinymce_Toolbars;
endif;
