| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: Typing Text |
| 5 |
* Description: Make Your Website Interactive With Typing Text Animation |
| 6 |
* Version: 1.3.0 |
| 7 |
* Author: WPDeveloper |
| 8 |
* Author URI: https://wpdeveloper.net |
| 9 |
* License: GPL-3.0-or-later |
| 10 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 11 |
* Text Domain: typing-text |
| 12 |
* Requires PHP: 7.4 |
| 13 |
* Requires at least: 6.0 |
| 14 |
* Tested up to: 7.1 |
| 15 |
* |
| 16 |
* @package typing-text |
| 17 |
*/ |
| 18 |
|
| 19 |
// Exit if accessed directly. |
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Registers all block assets so that they can be enqueued through the block editor |
| 26 |
* in the corresponding context. |
| 27 |
* |
| 28 |
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/ |
| 29 |
*/ |
| 30 |
|
| 31 |
require_once __DIR__ . '/includes/font-loader.php'; |
| 32 |
require_once __DIR__ . '/includes/post-meta.php'; |
| 33 |
require_once __DIR__ . '/includes/helpers.php'; |
| 34 |
|
| 35 |
/* |
| 36 |
* `EbStyleHandler` is a deliberately shared class name: the file guards itself |
| 37 |
* with `class_exists()`, so whichever plugin in the Essential Blocks family |
| 38 |
* loads first supplies the single instance and no fatal redeclaration occurs. |
| 39 |
* The class cannot be renamed here — lib/style-handler is a git submodule shared |
| 40 |
* with the sibling plugins, so a rename would have to land upstream. |
| 41 |
* |
| 42 |
* Reusing another plugin's copy is safe for this plugin's purposes: every |
| 43 |
* version emits the same artefacts Typing Text depends on — a per-post |
| 44 |
* `uploads/eb-style/eb-style-{ID}.min.css` enqueued under the handle |
| 45 |
* `eb-block-style-{ID}` — and generates it from each block's `blockMeta` |
| 46 |
* attribute generically, without a per-block allow-list. Essential Blocks core |
| 47 |
* does not define `EbStyleHandler` at all (it has its own namespaced |
| 48 |
* `EssentialBlocks\Modules\StyleHandler` writing the same filenames), so core |
| 49 |
* and this plugin do not redeclare each other either. |
| 50 |
* |
| 51 |
* What does differ between revisions is optional behaviour: the older revision |
| 52 |
* still shipped by some siblings lacks `enqueue_frontend_assets()`, the |
| 53 |
* `eb_generated_css_frontend_deps` filter and the breakpoint-change regeneration |
| 54 |
* hooks. None of that breaks Typing Text, but it is worth surfacing while |
| 55 |
* debugging rather than leaving as a silent behavioural difference. |
| 56 |
* |
| 57 |
* The file_exists() guard covers an uninitialised submodule checkout, which |
| 58 |
* would otherwise fatal here. |
| 59 |
*/ |
| 60 |
if ( file_exists( __DIR__ . '/lib/style-handler/style-handler.php' ) ) { |
| 61 |
require_once __DIR__ . '/lib/style-handler/style-handler.php'; |
| 62 |
|
| 63 |
if ( |
| 64 |
defined( 'WP_DEBUG' ) && WP_DEBUG |
| 65 |
&& class_exists( 'EbStyleHandler' ) |
| 66 |
&& ! method_exists( 'EbStyleHandler', 'enqueue_frontend_assets' ) |
| 67 |
) { |
| 68 |
error_log( |
| 69 |
'Typing Text: an older EbStyleHandler was already loaded by another plugin. ' |
| 70 |
. 'Frontend CSS still generates, but breakpoint-change regeneration is unavailable.' |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
function create_block_typing_text_block_init() { |
| 76 |
|
| 77 |
if ( ! defined( 'TYPING_TEXT_BLOCKS_VERSION' ) ) { |
| 78 |
define( 'TYPING_TEXT_BLOCKS_VERSION', "1.3.0" ); |
| 79 |
} |
| 80 |
if ( ! defined( 'TYPING_TEXT_BLOCKS_ADMIN_URL' ) ) { |
| 81 |
define( 'TYPING_TEXT_BLOCKS_ADMIN_URL', plugin_dir_url( __FILE__ ) ); |
| 82 |
} |
| 83 |
if ( ! defined( 'TYPING_TEXT_BLOCKS_ADMIN_PATH' ) ) { |
| 84 |
define( 'TYPING_TEXT_BLOCKS_ADMIN_PATH', dirname( __FILE__ ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
* Essential Blocks core already provides `essential-blocks/typing-text`. |
| 89 |
* Stand down completely rather than registering a second implementation. |
| 90 |
* |
| 91 |
* The check has to happen *before* any asset registration, not just before |
| 92 |
* register_block_type(). Registering the scripts and styles anyway is what |
| 93 |
* put this plugin's copies of the animation, editor-CSS and controls assets |
| 94 |
* into the same handle namespace as core's, where WordPress keeps only the |
| 95 |
* first registrant and silently discards the rest. |
| 96 |
* |
| 97 |
* Calling this here also pins the decision for `Typing_Text_Helper::enqueues()`, |
| 98 |
* which runs later on `admin_enqueue_scripts`. |
| 99 |
*/ |
| 100 |
if ( Typing_Text_Helper::is_standing_down() ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$script_asset_path = TYPING_TEXT_BLOCKS_ADMIN_PATH . "/dist/index.asset.php"; |
| 105 |
if ( ! file_exists( $script_asset_path ) ) { |
| 106 |
// Build output missing (`npm run build` not run). Bail out instead of |
| 107 |
// throwing an uncaught Error, which would take the whole site down. |
| 108 |
return; |
| 109 |
} |
| 110 |
$index_js = TYPING_TEXT_BLOCKS_ADMIN_URL . 'dist/index.js'; |
| 111 |
$script_asset = require $script_asset_path; |
| 112 |
if ( ! is_array( $script_asset ) || ! isset( $script_asset['dependencies'] ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
$all_dependencies = array_merge( $script_asset['dependencies'], [ |
| 116 |
'wp-blocks', |
| 117 |
'wp-i18n', |
| 118 |
'wp-element', |
| 119 |
'wp-block-editor', |
| 120 |
Typing_Text_Helper::CONTROLS_HANDLE, |
| 121 |
'typing-text-eb-animation' |
| 122 |
] ); |
| 123 |
|
| 124 |
wp_register_script( |
| 125 |
'typing-text-block-editor-js', |
| 126 |
$index_js, |
| 127 |
$all_dependencies, |
| 128 |
$script_asset['version'] |
| 129 |
); |
| 130 |
|
| 131 |
// Was `essential-blocks-eb-animation`. Essential Blocks core registers that |
| 132 |
// handle for its own copy (includes/Core/Block.php), and so do the other |
| 133 |
// carved-out block plugins, so only one of the files ever loaded. |
| 134 |
$load_animation_js = TYPING_TEXT_BLOCKS_ADMIN_URL . 'assets/js/eb-animation-load.js'; |
| 135 |
wp_register_script( |
| 136 |
'typing-text-eb-animation', |
| 137 |
$load_animation_js, |
| 138 |
[], |
| 139 |
TYPING_TEXT_BLOCKS_VERSION, |
| 140 |
true |
| 141 |
); |
| 142 |
|
| 143 |
// Was `essential-blocks-animation`, likewise owned by core. |
| 144 |
$animate_css = TYPING_TEXT_BLOCKS_ADMIN_URL . 'assets/css/animate.min.css'; |
| 145 |
wp_register_style( |
| 146 |
'typing-text-animation', |
| 147 |
$animate_css, |
| 148 |
[], |
| 149 |
TYPING_TEXT_BLOCKS_VERSION |
| 150 |
); |
| 151 |
|
| 152 |
$style_css = TYPING_TEXT_BLOCKS_ADMIN_URL . 'dist/style.css'; |
| 153 |
$style_css_path = TYPING_TEXT_BLOCKS_ADMIN_PATH . '/dist/style.css'; |
| 154 |
wp_register_style( |
| 155 |
'typing-text-block-frontend-style', |
| 156 |
$style_css, |
| 157 |
["typing-text-animation"], |
| 158 |
file_exists( $style_css_path ) ? filemtime( $style_css_path ) : TYPING_TEXT_BLOCKS_VERSION |
| 159 |
); |
| 160 |
|
| 161 |
$typed_js = TYPING_TEXT_BLOCKS_ADMIN_URL . 'assets/js/typed.min.js'; |
| 162 |
wp_register_script( |
| 163 |
'typig-text-blocks-typedjs', |
| 164 |
$typed_js, |
| 165 |
["jquery"], |
| 166 |
TYPING_TEXT_BLOCKS_VERSION, |
| 167 |
true |
| 168 |
); |
| 169 |
|
| 170 |
$frontend_asset_path = TYPING_TEXT_BLOCKS_ADMIN_PATH . "/dist/frontend/index.asset.php"; |
| 171 |
if ( ! file_exists( $frontend_asset_path ) ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
$frontend_js_path = require $frontend_asset_path; |
| 175 |
if ( ! is_array( $frontend_js_path ) || ! isset( $frontend_js_path['dependencies'] ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
$frontend_js = "dist/frontend/index.js"; |
| 179 |
wp_register_script( |
| 180 |
'eb-typing-text-frontend', |
| 181 |
plugins_url( $frontend_js, __FILE__ ), |
| 182 |
array_merge( ["typig-text-blocks-typedjs", "jquery", "typing-text-eb-animation"], $frontend_js_path['dependencies'] ), |
| 183 |
$frontend_js_path['version'], |
| 184 |
true |
| 185 |
); |
| 186 |
|
| 187 |
// Own block name, not core's: this guards against registering |
| 188 |
// `typing-text/typing-text-block` twice, which WordPress rejects with a |
| 189 |
// _doing_it_wrong notice. The core-provides-it case is handled by the |
| 190 |
// stand-down above. |
| 191 |
if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'typing-text/typing-text-block' ) ) { |
| 192 |
register_block_type( |
| 193 |
Typing_Text_Helper::get_block_register_path( 'typing-text/typing-text-block', TYPING_TEXT_BLOCKS_ADMIN_PATH ), |
| 194 |
[ |
| 195 |
'editor_script' => 'typing-text-block-editor-js', |
| 196 |
'style' => 'typing-text-block-frontend-style', |
| 197 |
'script' => 'eb-typing-text-frontend' |
| 198 |
] |
| 199 |
); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
add_action( 'init', 'create_block_typing_text_block_init', 99 ); |
| 204 |
|