aristath/kirki

View on GitHub
packages/kirki-framework/control-editor/src/Control/Editor.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
/**
 * Customizer Control: editor.
 *
 * Creates a TinyMCE textarea.
 *
 * @package   kirki-framework/control-editor
 * @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
 * @license   https://opensource.org/licenses/MIT
 * @since     1.0
 */

namespace Kirki\Control;

use Kirki\Control\Base;
use Kirki\URL;

/**
 * A TinyMCE control.
 *
 * @since 1.0
 */
class Editor extends Base {

    /**
     * The control type.
     *
     * @access public
     * @since 1.0
     * @var string
     */
    public $type = 'kirki-editor';

    /**
     * The version. Used in scripts & styles for cache-busting.
     *
     * @static
     * @access public
     * @since 1.0
     * @var string
     */
    public static $control_ver = '1.0';

    /**
     * Args to pass to TinyMCE.
     *
     * @access public
     * @since 1.0
     * @var bool
     */
    public $choices = [];

    /**
     * Enqueue control related scripts/styles.
     *
     * @access public
     * @since 1.0
     * @return void
     */
    public function enqueue() {
        parent::enqueue();

        // Enqueue the script.
        wp_enqueue_script( 'kirki-control-editor', URL::get_from_path( dirname( dirname( __DIR__ ) ) . '/dist/control.js' ), [ 'jquery', 'customize-base', 'kirki-control-base' ], self::$control_ver, false );

        // Enqueue the style.
        wp_enqueue_style( 'kirki-control-editor-style', URL::get_from_path( dirname( dirname( __DIR__ ) ) . '/dist/control.css' ), [], self::$control_ver );
    }

    /**
     * Refresh the parameters passed to the JavaScript via JSON.
     *
     * @access public
     * @since 1.0
     * @return void
     */
    public function to_json() {
        parent::to_json();
        $this->json['choices'] = $this->choices;
    }

    /**
     * An Underscore (JS) template for this control's content (but not its container).
     *
     * Class variables for this control class are available in the `data` JS object;
     * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
     *
     * The actual editor is added from the \Kirki\Field\Editor class.
     * All this template contains is a button that triggers the global editor on/off
     * and a hidden textarea element that is used to mirror save the options.
     *
     * @see WP_Customize_Control::print_template()
     *
     * @access protected
     * @since 1.0
     * @return void
     */
    protected function content_template() {
        ?>
        <label>
            <# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
            <# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
        </label>
        <textarea id="kirki-editor-{{{ data.id.replace( '[', '' ).replace( ']', '' ) }}}" {{{ data.inputAttrs }}} {{{ data.link }}}>{{ data.value }}</textarea>
        <?php
    }
}