| 1 |
<?php |
| 2 |
/** Shared opt-in motion settings. Existing blocks remain unchanged. */ |
| 3 |
defined('ABSPATH') || exit; |
| 4 |
function bkbg_motion_attributes() { |
| 5 |
return array( |
| 6 |
'bkbgEntrance' => array('type'=>'string','default'=>''), |
| 7 |
'bkbgEntranceDuration'=>array('type'=>'number','default'=>0.8), |
| 8 |
'bkbgEntranceDelay'=>array('type'=>'number','default'=>0), |
| 9 |
'bkbgEntranceOnce'=>array('type'=>'boolean','default'=>true), |
| 10 |
'bkbgRotate'=>array('type'=>'boolean','default'=>false), |
| 11 |
'bkbgRotateDuration'=>array('type'=>'number','default'=>8), |
| 12 |
'bkbgRotateDelay'=>array('type'=>'number','default'=>0), |
| 13 |
'bkbgFloat'=>array('type'=>'boolean','default'=>false), |
| 14 |
'bkbgFloatX'=>array('type'=>'number','default'=>0), |
| 15 |
'bkbgFloatY'=>array('type'=>'number','default'=>-20), |
| 16 |
'bkbgFloatDuration'=>array('type'=>'number','default'=>6), |
| 17 |
'bkbgFloatDelay'=>array('type'=>'number','default'=>0), |
| 18 |
); |
| 19 |
} |
| 20 |
add_filter('register_block_type_args', function($args,$name) { |
| 21 |
if (strpos($name,'blockenberg/')===0) $args['attributes']=array_merge($args['attributes']??array(),bkbg_motion_attributes()); |
| 22 |
return $args; |
| 23 |
},5,2); |
| 24 |
add_action('enqueue_block_editor_assets',function(){ |
| 25 |
wp_enqueue_script('bkbg-motion-editor',plugins_url('../js/motion-editor.js',__FILE__),array('wp-blocks','wp-hooks','wp-element','wp-components','wp-i18n'),filemtime(__DIR__.'/../js/motion-editor.js'),false); |
| 26 |
}); |
| 27 |
add_action('init',function(){ |
| 28 |
wp_register_script('bkbg-motion-editor',plugins_url('../js/motion-editor.js',__FILE__),array('wp-blocks','wp-hooks','wp-element','wp-components','wp-i18n'),filemtime(__DIR__.'/../js/motion-editor.js'),false); |
| 29 |
wp_register_style('bkbg-motion',plugins_url('../css/motion.css',__FILE__),array(),filemtime(__DIR__.'/../css/motion.css')); |
| 30 |
wp_register_script('bkbg-motion',plugins_url('../js/motion.js',__FILE__),array(),filemtime(__DIR__.'/../js/motion.js'),true); |
| 31 |
}); |
| 32 |
function bkbg_motion_clamp($v,$min,$max,$default) { return is_numeric($v)?max($min,min($max,(float)$v)):$default; } |
| 33 |
add_filter('render_block',function($html,$block){ |
| 34 |
if (strpos($block['blockName']??'','blockenberg/')!==0) return $html; |
| 35 |
$a=$block['attrs']??array(); $entrance=$a['bkbgEntrance']??''; $float=!empty($a['bkbgFloat']);$rotate=!empty($a['bkbgRotate']); |
| 36 |
$allowed=array('fadeIn','fadeInUp','fadeInDown','fadeInLeft','fadeInRight','zoomIn'); |
| 37 |
if (!in_array($entrance,$allowed,true)) $entrance=''; |
| 38 |
if (!$entrance&&!$float&&!$rotate) return $html; |
| 39 |
$p=new WP_HTML_Tag_Processor($html); |
| 40 |
while($p->next_tag()) { if(!in_array($p->get_tag(),array('STYLE','SCRIPT','LINK'),true)) break; } |
| 41 |
if (!$p->get_tag()) return $html; |
| 42 |
$vars=''; |
| 43 |
if($entrance){ |
| 44 |
$p->set_attribute('data-bkbg-entrance',$entrance); |
| 45 |
$p->set_attribute('data-bkbg-once',($a['bkbgEntranceOnce']??true)?'1':'0'); |
| 46 |
$vars.='--bkbg-enter-duration:'.bkbg_motion_clamp($a['bkbgEntranceDuration']??0.8,0.05,10,0.8).'s;--bkbg-enter-delay:'.bkbg_motion_clamp($a['bkbgEntranceDelay']??0,0,10,0).'s;'; |
| 47 |
} |
| 48 |
if($float){ |
| 49 |
$p->set_attribute('data-bkbg-float','1'); |
| 50 |
$vars.='--bkbg-float-x:'.bkbg_motion_clamp($a['bkbgFloatX']??0,-200,200,0).'px;--bkbg-float-y:'.bkbg_motion_clamp($a['bkbgFloatY']??-20,-200,200,-20).'px;--bkbg-float-duration:'.bkbg_motion_clamp($a['bkbgFloatDuration']??6,0.5,60,6).'s;--bkbg-float-delay:'.bkbg_motion_clamp($a['bkbgFloatDelay']??0,0,60,0).'s;'; |
| 51 |
} |
| 52 |
if($rotate){$p->set_attribute('data-bkbg-rotate','1');$vars.='--bkbg-rotate-duration:'.bkbg_motion_clamp($a['bkbgRotateDuration']??8,.5,120,8).'s;--bkbg-rotate-delay:'.bkbg_motion_clamp($a['bkbgRotateDelay']??0,0,60,0).'s;';} |
| 53 |
$p->set_attribute('style',($p->get_attribute('style')??'').';'.$vars); |
| 54 |
wp_enqueue_style('bkbg-motion'); wp_enqueue_script('bkbg-motion'); |
| 55 |
return $p->get_updated_html(); |
| 56 |
},30,2); |
| 57 |
|
| 58 |
function bkbg_post_has_motion($blocks){foreach($blocks as $b){if(strpos($b['blockName']??'','blockenberg/')===0&&(!empty($b['attrs']['bkbgEntrance'])||!empty($b['attrs']['bkbgFloat'])||!empty($b['attrs']['bkbgRotate'])))return true;if(!empty($b['innerBlocks'])&&bkbg_post_has_motion($b['innerBlocks']))return true;}return false;} |
| 59 |
add_action('wp_enqueue_scripts',function(){if(is_singular()&&bkbg_post_has_motion(parse_blocks(get_post_field('post_content',get_queried_object_id())))){wp_enqueue_style('bkbg-motion');wp_enqueue_script('bkbg-motion');}}); |
| 60 |
|