blocks-animation
Last commit date
build
3 years ago
blocks-animation.php
3 years ago
class-blocks-animation.php
3 years ago
readme.md
3 years ago
readme.txt
3 years ago
class-blocks-animation.php
264 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for Animation logic. |
| 4 | * |
| 5 | * @package ThemeIsle |
| 6 | */ |
| 7 | |
| 8 | namespace ThemeIsle\GutenbergBlocks; |
| 9 | |
| 10 | /** |
| 11 | * Class Blocks_Animation |
| 12 | */ |
| 13 | class Blocks_Animation { |
| 14 | |
| 15 | /** |
| 16 | * The main instance var. |
| 17 | * |
| 18 | * @var Blocks_Animation |
| 19 | */ |
| 20 | public static $instance = null; |
| 21 | |
| 22 | /** |
| 23 | * Flag to mark that the scripts which have loaded. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | public static $scripts_loaded = array( |
| 28 | 'animation' => false, |
| 29 | 'count' => false, |
| 30 | 'typing' => false, |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * Allow to load in frontend. |
| 35 | * |
| 36 | * @var bool |
| 37 | */ |
| 38 | public static $can_load_frontend = true; |
| 39 | |
| 40 | /** |
| 41 | * Initialize the class |
| 42 | */ |
| 43 | public function init() { |
| 44 | if ( ! defined( 'BLOCKS_ANIMATION_URL' ) ) { |
| 45 | define( 'BLOCKS_ANIMATION_URL', OTTER_BLOCKS_URL ); |
| 46 | define( 'BLOCKS_ANIMATION_PATH', OTTER_BLOCKS_PATH ); |
| 47 | } |
| 48 | |
| 49 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 50 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_frontend_assets' ) ); |
| 51 | add_filter( 'render_block', array( $this, 'frontend_load' ), 800, 2 ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Load Gutenberg editor assets. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | * @access public |
| 59 | */ |
| 60 | public function enqueue_editor_assets() { |
| 61 | $asset_file = include BLOCKS_ANIMATION_PATH . '/build/animation/index.asset.php'; |
| 62 | |
| 63 | wp_enqueue_style( |
| 64 | 'otter-animation', |
| 65 | BLOCKS_ANIMATION_URL . 'build/animation/index.css', |
| 66 | array(), |
| 67 | $asset_file['version'] |
| 68 | ); |
| 69 | |
| 70 | if ( defined( 'OTTER_BLOCKS_VERSION' ) ) { |
| 71 | array_push( $asset_file['dependencies'], 'otter-blocks' ); |
| 72 | } |
| 73 | |
| 74 | wp_enqueue_script( |
| 75 | 'otter-animation', |
| 76 | BLOCKS_ANIMATION_URL . 'build/animation/index.js', |
| 77 | $asset_file['dependencies'], |
| 78 | $asset_file['version'], |
| 79 | true |
| 80 | ); |
| 81 | |
| 82 | wp_localize_script( |
| 83 | 'otter-animation', |
| 84 | 'blocksAnimation', |
| 85 | array( |
| 86 | 'hasOtter' => defined( 'OTTER_BLOCKS_VERSION' ), |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | wp_set_script_translations( 'otter-animation', 'blocks-animation' ); |
| 91 | |
| 92 | $asset_file = include BLOCKS_ANIMATION_PATH . '/build/animation/anim-count.asset.php'; |
| 93 | wp_enqueue_script( |
| 94 | 'otter-count', |
| 95 | BLOCKS_ANIMATION_URL . 'build/animation/anim-count.js', |
| 96 | $asset_file['dependencies'], |
| 97 | $asset_file['version'], |
| 98 | true |
| 99 | ); |
| 100 | |
| 101 | wp_script_add_data( 'otter-count', 'defer', true ); |
| 102 | |
| 103 | wp_enqueue_script( |
| 104 | 'otter-typing', |
| 105 | BLOCKS_ANIMATION_URL . 'build/animation/anim-typing.js', |
| 106 | $asset_file['dependencies'], |
| 107 | $asset_file['version'], |
| 108 | true |
| 109 | ); |
| 110 | |
| 111 | wp_script_add_data( 'otter-typing', 'defer', true ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Load Gutenberg assets. |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | * @access public |
| 119 | */ |
| 120 | public function enqueue_block_frontend_assets() { |
| 121 | if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { |
| 122 | self::$can_load_frontend = false; |
| 123 | } |
| 124 | |
| 125 | global $post; |
| 126 | |
| 127 | if ( is_singular() && strpos( get_the_content( null, false, $post ), '<!-- wp:' ) === false ) { |
| 128 | self::$can_load_frontend = false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Load assets in frontend. |
| 134 | * |
| 135 | * @param string $block_content Content of block. |
| 136 | * @param array $block Block Attributes. |
| 137 | * @return string |
| 138 | * @since 2.0.5 |
| 139 | */ |
| 140 | public function frontend_load( $block_content, $block ) { |
| 141 | $asset_file = include BLOCKS_ANIMATION_PATH . '/build/animation/frontend.asset.php'; |
| 142 | |
| 143 | wp_register_style( |
| 144 | 'otter-animation', |
| 145 | BLOCKS_ANIMATION_URL . 'build/animation/index.css', |
| 146 | array(), |
| 147 | $asset_file['version'] |
| 148 | ); |
| 149 | |
| 150 | if ( ! self::$can_load_frontend ) { |
| 151 | return $block_content; |
| 152 | } |
| 153 | |
| 154 | if ( ! self::$scripts_loaded['animation'] && strpos( $block_content, 'animated' ) ) { |
| 155 | if ( ! defined( 'OTTER_BLOCKS_VERSION' ) || ( defined( 'OTTER_BLOCKS_VERSION' ) && ! get_option( 'themeisle_blocks_settings_optimize_animations_css', true ) ) ) { |
| 156 | wp_enqueue_style( 'otter-animation' ); |
| 157 | } |
| 158 | |
| 159 | wp_enqueue_script( |
| 160 | 'otter-animation-frontend', |
| 161 | BLOCKS_ANIMATION_URL . 'build/animation/frontend.js', |
| 162 | $asset_file['dependencies'], |
| 163 | $asset_file['version'], |
| 164 | true |
| 165 | ); |
| 166 | |
| 167 | wp_script_add_data( 'otter-animation-frontend', 'async', true ); |
| 168 | |
| 169 | add_action( 'wp_head', array( $this, 'add_fontend_anim_inline_style' ), 10, 3 ); |
| 170 | |
| 171 | self::$scripts_loaded['animation'] = true; |
| 172 | } |
| 173 | |
| 174 | if ( ! self::$scripts_loaded['count'] && strpos( $block_content, 'o-anim-count' ) ) { |
| 175 | $asset_file = include BLOCKS_ANIMATION_PATH . '/build/animation/anim-count.asset.php'; |
| 176 | wp_enqueue_script( |
| 177 | 'otter-count', |
| 178 | BLOCKS_ANIMATION_URL . 'build/animation/anim-count.js', |
| 179 | $asset_file['dependencies'], |
| 180 | $asset_file['version'], |
| 181 | true |
| 182 | ); |
| 183 | |
| 184 | wp_script_add_data( 'otter-count', 'defer', true ); |
| 185 | self::$scripts_loaded['count'] = true; |
| 186 | } |
| 187 | |
| 188 | if ( ! self::$scripts_loaded['typing'] && strpos( $block_content, 'o-anim-typing' ) ) { |
| 189 | $asset_file = include BLOCKS_ANIMATION_PATH . '/build/animation/anim-typing.asset.php'; |
| 190 | wp_enqueue_script( |
| 191 | 'otter-typing', |
| 192 | BLOCKS_ANIMATION_URL . 'build/animation/anim-typing.js', |
| 193 | $asset_file['dependencies'], |
| 194 | $asset_file['version'], |
| 195 | true |
| 196 | ); |
| 197 | |
| 198 | wp_script_add_data( 'otter-typing', 'defer', true ); |
| 199 | self::$scripts_loaded['typing'] = true; |
| 200 | } |
| 201 | |
| 202 | return $block_content; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add no script tag. |
| 207 | * |
| 208 | * @access public |
| 209 | * @since 2.0.14 |
| 210 | */ |
| 211 | public static function add_fontend_anim_inline_style() { |
| 212 | echo '<style id="o-anim-hide-inline-css"> .animated:not(.o-anim-ready) { |
| 213 | visibility: hidden; |
| 214 | animation-play-state: paused; |
| 215 | }</style> |
| 216 | <noscript><style>.animated { visibility: visible; animation-play-state: running; }</style></noscript>'; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * The instance method for the static class. |
| 221 | * Defines and returns the instance of the static class. |
| 222 | * |
| 223 | * @static |
| 224 | * @since 1.0.0 |
| 225 | * @access public |
| 226 | * @return Blocks_Animation |
| 227 | */ |
| 228 | public static function instance() { |
| 229 | if ( is_null( self::$instance ) ) { |
| 230 | self::$instance = new self(); |
| 231 | self::$instance->init(); |
| 232 | } |
| 233 | |
| 234 | return self::$instance; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Throw error on object clone |
| 239 | * |
| 240 | * The whole idea of the singleton design pattern is that there is a single |
| 241 | * object therefore, we don't want the object to be cloned. |
| 242 | * |
| 243 | * @access public |
| 244 | * @since 1.0.0 |
| 245 | * @return void |
| 246 | */ |
| 247 | public function __clone() { |
| 248 | // Cloning instances of the class is forbidden. |
| 249 | _doing_it_wrong( __FUNCTION__, 'Cheatin’ huh?', '1.0.0' ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Disable unserializing of the class |
| 254 | * |
| 255 | * @access public |
| 256 | * @since 1.0.0 |
| 257 | * @return void |
| 258 | */ |
| 259 | public function __wakeup() { |
| 260 | // Unserializing instances of the class is forbidden. |
| 261 | _doing_it_wrong( __FUNCTION__, 'Cheatin’ huh?', '1.0.0' ); |
| 262 | } |
| 263 | } |
| 264 |