| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Parallax Section - Block |
| 4 |
* Description: Makes background element scrolls slower than foreground content. |
| 5 |
* Version: 1.0.8 |
| 6 |
* Author: bPlugins LLC |
| 7 |
* Author URI: http://bplugins.com |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt |
| 10 |
* Text Domain: parallax-section |
| 11 |
*/ |
| 12 |
|
| 13 |
// ABS PATH |
| 14 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 15 |
|
| 16 |
// Constant |
| 17 |
define( 'PSB_PLUGIN_VERSION', isset( $_SERVER['HTTP_HOST'] ) && 'localhost' === $_SERVER['HTTP_HOST'] ? time() : '1.0.8' ); |
| 18 |
define( 'PSB_DIR_URL', plugin_dir_url( __FILE__ ) ); |
| 19 |
define( 'PSB_DIR_PATH',plugin_dir_path( __FILE__ ) ); |
| 20 |
|
| 21 |
class PSBParallaxSection{ |
| 22 |
function __construct(){ |
| 23 |
add_action( 'init', [$this, 'onInit'] ); |
| 24 |
} |
| 25 |
|
| 26 |
function onInit() { |
| 27 |
wp_register_style( 'psb-parallax-style', PSB_DIR_URL . 'dist/style.css', [], PSB_PLUGIN_VERSION ); // Style |
| 28 |
wp_register_style( 'psb-parallax-editor-style', PSB_DIR_URL . 'dist/editor.css', [ 'psb-parallax-style' ], PSB_PLUGIN_VERSION ); // Backend Style |
| 29 |
|
| 30 |
register_block_type( __DIR__, [ |
| 31 |
'editor_style' => 'psb-parallax-editor-style', |
| 32 |
'render_callback' => [$this, 'render'] |
| 33 |
] ); // Register Block |
| 34 |
|
| 35 |
wp_set_script_translations( 'psb-parallax-editor-script', 'parallax-section', PSB_DIR_PATH . 'languages' ); |
| 36 |
} |
| 37 |
|
| 38 |
function render( $attributes, $content ){ |
| 39 |
extract( $attributes ); |
| 40 |
|
| 41 |
wp_enqueue_style( 'psb-parallax-style' ); |
| 42 |
wp_enqueue_script( 'psb-parallax-script', PSB_DIR_URL . 'dist/script.js', [], PSB_PLUGIN_VERSION, true ); |
| 43 |
wp_set_script_translations( 'psb-parallax-script', 'parallax-section', PSB_DIR_PATH . 'languages' ); |
| 44 |
|
| 45 |
$className = $className ?? ''; |
| 46 |
$blockClassName = "wp-block-psb-parallax $className align$align"; |
| 47 |
|
| 48 |
// Styles |
| 49 |
$bgCSS = $this->getBackgroundCSS($background); |
| 50 |
$paddingCSS = $this->getSpaceCSS($padding); |
| 51 |
|
| 52 |
$mainSl = "#psbParallaxSection-$cId"; |
| 53 |
$styles = " |
| 54 |
$mainSl{ |
| 55 |
min-height: $minHeight; |
| 56 |
} |
| 57 |
$mainSl .psbParallaxSection{ |
| 58 |
justify-content: $verticalAlign; |
| 59 |
text-align: $textAlign; |
| 60 |
min-height: $minHeight; |
| 61 |
padding: $paddingCSS; |
| 62 |
} |
| 63 |
$mainSl .psbParallaxImg{ |
| 64 |
$bgCSS |
| 65 |
} |
| 66 |
"; |
| 67 |
|
| 68 |
// Style disappearing problem |
| 69 |
global $allowedposttags; |
| 70 |
$allowed_html = wp_parse_args( ['style' => []], $allowedposttags ); |
| 71 |
|
| 72 |
ob_start(); ?> |
| 73 |
<div class='<?php echo esc_attr( $blockClassName ); ?>' id='psbParallaxSection-<?php echo esc_attr( $cId ) ?>'> |
| 74 |
<style> |
| 75 |
<?php echo esc_html( $styles ); ?> |
| 76 |
</style> |
| 77 |
|
| 78 |
<div class='psbParallaxImg' data-speed='<?php echo esc_attr( $speed ) ?>'></div> |
| 79 |
|
| 80 |
<div class='psbParallaxSection'> |
| 81 |
<?php echo wp_kses( $content, $allowed_html ); ?> |
| 82 |
</div> |
| 83 |
</div> |
| 84 |
|
| 85 |
<?php return ob_get_clean(); |
| 86 |
} // Render |
| 87 |
|
| 88 |
function getBackgroundCSS( $bg, $isSolid = true, $isGradient = true, $isImage = true ) { |
| 89 |
extract( $bg ); |
| 90 |
$type = $type ?? 'solid'; |
| 91 |
$color = $color ?? '#000000b3'; |
| 92 |
$gradient = $gradient ?? 'linear-gradient(135deg, #4527a4, #8344c5)'; |
| 93 |
$image = $image ?? []; |
| 94 |
$position = $position ?? 'center center'; |
| 95 |
$attachment = $attachment ?? 'initial'; |
| 96 |
$repeat = $repeat ?? 'no-repeat'; |
| 97 |
$size = $size ?? 'cover'; |
| 98 |
$overlayColor = $overlayColor ?? '#000000b3'; |
| 99 |
|
| 100 |
$gradientCSS = $isGradient ? "background: $gradient;" : ''; |
| 101 |
|
| 102 |
$imgUrl = $image['url'] ?? ''; |
| 103 |
$imageCSS = $isImage ? "background: url($imgUrl); background-color: $overlayColor; background-position: $position; background-size: $size; background-repeat: $repeat; background-attachment: $attachment; background-blend-mode: overlay;" : ''; |
| 104 |
|
| 105 |
$solidCSS = $isSolid ? "background: $color;" : ''; |
| 106 |
|
| 107 |
$styles = 'gradient' === $type ? $gradientCSS : ( 'image' === $type ? $imageCSS : $solidCSS ); |
| 108 |
|
| 109 |
return $styles; |
| 110 |
} |
| 111 |
|
| 112 |
function getSpaceCSS( $space ) { |
| 113 |
extract( $space ); |
| 114 |
$side = $side ?? 2; |
| 115 |
$vertical = $vertical ?? '0px'; |
| 116 |
$horizontal = $horizontal ?? '0px'; |
| 117 |
$top = $top ?? '0px'; |
| 118 |
$right = $right ?? '0px'; |
| 119 |
$bottom = $bottom ?? '0px'; |
| 120 |
$left = $left ?? '0px'; |
| 121 |
|
| 122 |
$styles = ( 2 === $side ) ? "$vertical $horizontal" : "$top $right $bottom $left"; |
| 123 |
|
| 124 |
return $styles; |
| 125 |
} |
| 126 |
} |
| 127 |
new PSBParallaxSection; |