| 1 |
<?php |
| 2 |
class BBlocksQRCode extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
} |
| 6 |
|
| 7 |
function register(){ |
| 8 |
$this::registerScripts( 'qr-code', array( |
| 9 |
'attributes' => $this->attributes(), |
| 10 |
'render_callback' => [$this, 'render'] |
| 11 |
) ); |
| 12 |
} |
| 13 |
|
| 14 |
function attributes(){ |
| 15 |
return array( |
| 16 |
'align' => array( 'type' => 'string', 'default' => '' ), |
| 17 |
'cId' => array( 'type' => 'string', 'default' => '' ), |
| 18 |
|
| 19 |
'qrAlign' => array( 'type' => 'string', 'default' => 'center' ), |
| 20 |
|
| 21 |
'url' => array( 'type' => 'string', 'default' => '' ), |
| 22 |
'dimension' => array( 'type' => 'number', 'default' => 180 ), |
| 23 |
|
| 24 |
'color' => array( 'type' => 'string', 'default' => '#000' ), |
| 25 |
'bgColor' => array( 'type' => 'string', 'default' => '#fff' ) |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
function render( $attributes ){ |
| 30 |
extract( $attributes ); |
| 31 |
|
| 32 |
// Generate Styles |
| 33 |
$qrCodeStyle = new BBlocksStyleGenerator(); |
| 34 |
$qrCodeStyle::addStyle( "#bBlocksQRCode-$cId .bBlocksQRCode", array( |
| 35 |
'text-align' => $qrAlign |
| 36 |
) ); |
| 37 |
|
| 38 |
$siteUrl = $url ? $url : 'http://bplugins.com'; |
| 39 |
|
| 40 |
$qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=". $dimension ."x". $dimension ."&color=". str_replace('#', '', $color) ."&bgcolor=". str_replace('#', '', $bgColor) ."&ecc=L&qzone=1&data=". $siteUrl; |
| 41 |
|
| 42 |
ob_start(); ?> |
| 43 |
<div class='wp-block-b-blocks-qr-code <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksQRCode-<?php echo esc_attr( $cId ); ?>'> |
| 44 |
<style><?php echo wp_kses( $qrCodeStyle::renderStyle(), array() ); ?></style> |
| 45 |
|
| 46 |
<div class='bBlocksQRCode'> |
| 47 |
<img src='<?php echo esc_url( $qrUrl ); ?>' alt='<?php echo esc_url( $url ); ?>' /> |
| 48 |
</div> |
| 49 |
</div> |
| 50 |
<?php $qrCodeStyle::$styles = array(); // Empty styles |
| 51 |
return ob_get_clean(); |
| 52 |
} |
| 53 |
} |
| 54 |
new BBlocksQRCode(); |