This repository has been archived on 2023-08-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
footnotes/src/admin/class-wysiwyg.php

93 lines
2.8 KiB
PHP
Raw Normal View History

2021-04-30 18:03:15 +01:00
<?php
/**
2021-05-01 21:34:46 +01:00
* Admin: WYSIWYG class
*
2021-05-01 21:34:46 +01:00
* The Admin. subpackage is initialised at runtime by the {@see Admin}
* class, which draws in the {@see WYSIWYG} class for WYSIWYG editor
2021-04-30 18:03:15 +01:00
* integration and the {@see footnotes\admin_layout} subpackage for rendering
* dashboard pages.
*
2021-05-01 21:34:46 +01:00
* @package footnotes
2021-04-30 18:03:15 +01:00
* @since 1.5.0
* @since 2.8.0 Rename file from `wysiwyg.php` to `class-footnotes-wysiwyg.php`,
2021-05-01 19:22:41 +01:00
* move from `class/` sub-directory to `admin/`.
*/
2021-05-02 19:33:29 +01:00
2021-05-02 19:19:46 +01:00
declare(strict_types=1);
2021-05-01 21:34:46 +01:00
namespace footnotes\admin;
use footnotes\includes as Includes;
/**
2021-04-30 18:03:15 +01:00
* Class providing WYSIWYG editor intergration for the plugin.
*
2021-05-01 21:34:46 +01:00
* @package footnotes
2021-04-30 18:03:15 +01:00
* @since 1.5.0
*/
2021-05-01 21:34:46 +01:00
class WYSIWYG {
/**
* Append a new Button to the WYSIWYG editor of Posts and Pages.
*
* @param string[] $buttons Already-defined editor buttons.
2021-04-30 18:03:15 +01:00
* @return string[]
2021-04-27 09:54:07 +01:00
*
2021-04-30 18:03:15 +01:00
* @since 1.5.0
* @todo Should this be `static`?
*/
public static function new_visual_editor_button( array $buttons ): array {
$buttons[] = 'footnotes';
return $buttons;
}
/**
* Add a new button to the plain text editor.
*
2021-04-30 18:03:15 +01:00
* @since 1.5.0
*/
2021-05-02 19:19:46 +01:00
public static function new_plain_text_editor_button(): void {
$template = new Includes\Template( \footnotes\includes\Template::DASHBOARD, 'editor-button' );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $template->get_content();
// phpcs:enable
}
/**
* Includes the Plugins WYSIWYG editor script.
*
* @param string[] $plugins Scripts to be included by the editor.
2021-04-30 18:03:15 +01:00
* @return string[]
2021-04-27 09:54:07 +01:00
*
2021-04-30 18:03:15 +01:00
* @since 1.5.0
* @todo Should this be `static`?
*/
public static function include_scripts( array $plugins ): array {
$plugins['footnotes'] = plugins_url( '/../admin/js/wysiwyg-editor' . ( ( PRODUCTION_ENV ) ? '.min' : '' ) . '.js', __FILE__ );
return $plugins;
}
/**
* AJAX Callback function when the Footnotes Button is clicked. Either in the Plain text or Visual editor.
* Returns an JSON encoded array with the Footnotes start and end short code.
*
2021-04-30 18:03:15 +01:00
* @since 1.5.0
*/
2021-05-02 19:19:46 +01:00
public static function ajax_callback(): void {
// Get start and end tag for the footnotes short code.
$starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_START );
$ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_END );
if ( 'userdefined' === $starting_tag || 'userdefined' === $ending_tag ) {
$starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_START_USER_DEFINED );
$ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_END_USER_DEFINED );
}
2021-04-26 17:15:48 +01:00
echo wp_json_encode(
array(
'start' => htmlspecialchars( $starting_tag ),
'end' => htmlspecialchars( $ending_tag ),
)
);
exit;
}
}