| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenify Count Down Block file. |
| 4 |
* |
| 5 |
* @package Gutenify |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Gutenify_Block_Count_Down { |
| 14 |
public static function init(){ |
| 15 |
add_action( 'init', array( __CLASS__, 'register_block_type' ) ); |
| 16 |
add_action( 'enqueue_block_assets', array( __CLASS__, 'frontend_assets' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
public static function register_block_type(){ |
| 20 |
$deps = include_once( sprintf( '%s/assets/count-down_admin.asset.php', GUTENIFY_ABSPATH ) ); |
| 21 |
$deps['dependencies'] = array_merge( $deps['dependencies'], array( 'gutenify-google-fonts' ) ); |
| 22 |
wp_register_script( |
| 23 |
'gutenify-google-fonts', |
| 24 |
'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js', array(), '1.6.26', true |
| 25 |
); |
| 26 |
|
| 27 |
wp_register_script( |
| 28 |
'gutenify-script', |
| 29 |
plugins_url( 'assets/count-down_admin.js', GUTENIFY_PLUGIN_FILE ), $deps['dependencies'], $deps['version'], true |
| 30 |
); |
| 31 |
|
| 32 |
wp_register_style( |
| 33 |
'gutenify-style', |
| 34 |
plugins_url( 'assets/count-down_admin.css', GUTENIFY_PLUGIN_FILE ), |
| 35 |
array('gutenify-main-style'), |
| 36 |
time() |
| 37 |
); |
| 38 |
|
| 39 |
$translation_array['google_fonts'] = Gutenify_Helpers::get_google_fonts(); |
| 40 |
wp_localize_script( 'gutenify-script', 'gutenify_data', $translation_array ); |
| 41 |
|
| 42 |
register_block_type( 'gutenify/count-down', array( |
| 43 |
'editor_script' => 'gutenify-script', |
| 44 |
'editor_style' => 'gutenify-style', |
| 45 |
'render_callback' => array( __CLASS__, 'frontend_dynamic_render_callback' ) |
| 46 |
) ); |
| 47 |
} |
| 48 |
|
| 49 |
public static function frontend_dynamic_render_callback($attributes) { |
| 50 |
$defaults = include( sprintf( '%sinc/blocks/count-down/defaults.php', GUTENIFY_ABSPATH ) ); |
| 51 |
$attributes = wp_parse_args( $attributes, $defaults ); |
| 52 |
$wrapperClass = ! empty( $attributes['className'] ) ? $attributes['className'] . ' gutenify-count-down-wrapper' : ' gutenify-count-down-wrapper'; |
| 53 |
$blockId = !empty($attributes['blockClientId'])?$attributes['blockClientId']:''; |
| 54 |
$blockId = str_replace('-','_', $blockId ) . '_' . md5( uniqid (rand (),true) ); |
| 55 |
// return print_r($attributes,true).'<div class="' . $wrapperClass . '" id="' . $blockId . '"></div> |
| 56 |
return '<div class="' . $wrapperClass . '" id="' . $blockId . '"></div> |
| 57 |
<script> |
| 58 |
var '.$blockId.' = '.json_encode($attributes).'; |
| 59 |
</script> |
| 60 |
<style>'.gutenify_count_down_style($blockId, $attributes).'</style> |
| 61 |
'; |
| 62 |
} |
| 63 |
|
| 64 |
public static function frontend_assets() { |
| 65 |
if( !is_admin() ) { |
| 66 |
$deps = include_once( sprintf( '%s/assets/count-down_frontend.asset.php', GUTENIFY_ABSPATH ) ); |
| 67 |
$deps['dependencies'][] = 'gutenify-google-fonts'; |
| 68 |
wp_register_script( |
| 69 |
'gutenify-google-fonts', |
| 70 |
'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js', array(), '1.6.26', true |
| 71 |
); |
| 72 |
wp_enqueue_script( |
| 73 |
'gutenify-frontend-script', |
| 74 |
plugins_url( 'assets/count-down_frontend.js', GUTENIFY_PLUGIN_FILE ), $deps['dependencies'], $deps['version'], true |
| 75 |
); |
| 76 |
wp_enqueue_style( |
| 77 |
'gutenify-frontend-style', |
| 78 |
plugins_url( 'assets/count-down_frontend.css', GUTENIFY_PLUGIN_FILE ), |
| 79 |
array(), |
| 80 |
time() |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
Gutenify_Block_Count_Down::init(); |