define_constants();
}
return self::$_instance;
}
/**
* The Constructor.
*/
public function __construct() {
add_action( 'enqueue_block_assets', [ $this, 'block_assets' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_assets' ] );
add_action( 'init', [ $this, 'init' ] );
add_action( 'rest_api_init', [ $this, 'register_api' ] );
}
public function init(){
// Return early if this function does not exist.
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
$this->register_block();
}
/**
* Block assets.
*/
public function block_assets() {
$dependencies = require_once( HTCONTACTFORM_BLOCK_PATH . '/build/htcontactform-block.asset.php' );
wp_enqueue_script(
'ht-contactform-blocks',
HTCONTACTFORM_BLOCK_URL . '/build/htcontactform-block.js',
$dependencies['dependencies'],
$dependencies['version'],
true
);
wp_enqueue_style(
'ht-contactform-block-style',
HTCONTACTFORM_BLOCK_URL . '/src/assets/css/style-index.css',
array(),
HTCONTACTFORM_VERSION
);
wp_localize_script(
'ht-contactform-blocks',
'htcontactdata',
[
'pluginDirPath' => plugin_dir_path( __DIR__ ),
'pluginDirUrl' => plugin_dir_url( __DIR__ ),
'security' => wp_create_nonce('htcontactform-nonce'),
]
);
}
/**
* Block editor assets.
*/
public function block_editor_assets() {
wp_enqueue_style( 'ht-contactform-block-editor-style', HTCONTACTFORM_BLOCK_URL . '/src/assets/css/editor-style.css', false, HTCONTACTFORM_VERSION, 'all' );
}
private function register_block(){
ob_start();
include HTCONTACTFORM_BLOCK_PATH . '/src/ht-contactform-block/block.json';
$attributes = json_decode( ob_get_clean(), true );
register_block_type(
'block/ht-contactform', array(
'render_callback' => [ $this, 'render_content' ],
'attributes' => $attributes,
)
);
}
/**
* Define the required plugin constants
*
* @return void
*/
public function define_constants() {
$this->define( 'HTCONTACTFORM_BLOCK_FILE', __FILE__ );
$this->define( 'HTCONTACTFORM_BLOCK_PATH', __DIR__ );
$this->define( 'HTCONTACTFORM_BLOCK_URL', plugins_url( '', HTCONTACTFORM_BLOCK_FILE ) );
}
/**
* Define constant if not already set
*
* @param string $name
* @param string|bool $value
* @return type
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
public function render_content($attr){
if(isset($attr['formId']) && !empty($attr['formId'])){
$block_uniqueid = '#ht-block-'.$attr['blockUniqId'];
ob_start();
echo '
';
echo do_shortcode( '[contact-form-7 id="'.$attr['formId'].'"]' );
echo "
";
?>
'.esc_html__( "Please Select a contact form.", "ht-contactform" ).'';
}
}
private function generate_css($settings, $attribute, $device, $css_attr, $important = ''){
if('' == $device && 'border' != $css_attr ){
$value = !empty( $settings[$attribute] ) ? $settings[$attribute] : '';
}else{
$value = !empty( $settings[$attribute][$device] ) ? $settings[$attribute][$device] : '';
}
if('border' == $css_attr){
$value = $attribute;
}
if( !empty( $value ) && 'NaN' !== $value ){
$css_attr .= ":{$value}";
return $css_attr."{$important};";
}else{
return "";
}
}
private function dimentation($settings, $attribute, $device, $css_attr, $important = ''){
$dimensions = !empty( $settings[$attribute] ) ? $settings[$attribute] : array();
if('' == $device){
if( isset( $dimensions['top'] ) || isset( $dimensions['right'] ) || isset( $dimensions['bottom'] ) || isset( $dimensions['left'] ) ){
$unit = empty( $dimensions['unit'] ) ? 'px' : $dimensions['unit'];
$top = ( $dimensions['top'] !== '' ) ? $dimensions['top'].$unit : null;
$right = ( $dimensions['right'] !== '' ) ? $dimensions['right'].$unit : null;
$bottom = ( $dimensions['bottom'] !== '' ) ? $dimensions['bottom'].$unit : null;
$left = ( $dimensions['left'] !== '' ) ? $dimensions['left'].$unit : null;
$css_dimension = ( ($top != null) || ($right !=null) || ($bottom != null) || ($left != '') ) ? ( $css_attr.":{$top} {$right} {$bottom} {$left}" ) : '';
return $css_dimension."{$important};";
}else{
return "";
}
}else{
if( isset( $dimensions[$device]['top'] ) || isset( $dimensions[$device]['right'] ) || isset( $dimensions[$device]['bottom'] ) || isset( $dimensions[$device]['left'] ) ){
$unit = empty( $dimensions['unit'] ) ? 'px' : $dimensions['unit'];
$top = ( $dimensions[$device]['top'] !== '' ) ? $dimensions[$device]['top'].$unit : null;
$right = ( $dimensions[$device]['right'] !== '' ) ? $dimensions[$device]['right'].$unit : null;
$bottom = ( $dimensions[$device]['bottom'] !== '' ) ? $dimensions[$device]['bottom'].$unit : null;
$left = ( $dimensions[$device]['left'] !== '' ) ? $dimensions[$device]['left'].$unit : null;
$css_dimension = ( ($top != null) || ($right !=null) || ($bottom != null) || ($left != '') ) ? ( $css_attr.":{$top} {$right} {$bottom} {$left}" ) : '';
return $css_dimension."{$important};";
}else{
return "";
}
}
}
public function register_api(){
$api = new Api\Api();
$api->register_routes();
}
}
Contactform_Block::instance();