$base_url, ) ); register_block_type($reg_args['name'], array( 'render_callback' => empty($reg_args['dynamic']) ? null : function ($attributes, $content, $block) use ($reg_args) { self::$recursive_level++; if (self::$recursive_level > 10) { self::$recursive_level--; return 'Too many nested blocks... Are you including a post within itself?'; } ob_start(); $args = array('attributes' => $attributes, 'content' => $content, 'block' => $block); $template = trailingslashit($reg_args[ 'base_path' ]) . $reg_args['render_template']; if(file_exists( $template )) { require $template; } else { echo "
Dynamic block template $template not found.
"; } self::$recursive_level--; return ob_get_clean(); }, 'editor_script' => $editor_script_handle, 'editor_style' => $editor_style_handle, 'style' => $style_handle, 'script' => $script_handle, 'view_script' => $view_script_handle, 'attributes' => $reg_args['attributes'], 'supports' => isset($reg_args['supports']) ? $reg_args['supports'] : array() ) ); } static function getDefault($args, $prop, $subprop = null) { if (isset($args['block']->block_type->attributes[$prop]['default'])) { if ($subprop) { if (isset($args['block']->block_type->attributes[$prop]['default'][$subprop])) { return $args['block']->block_type->attributes[$prop]['default'][$subprop]; } else { return null; } } else { return $args['block']->block_type->attributes[$prop]['default']; } } return null; } static function getAttribute($args, $prop, $use_default = true, $null = false) { $val = isset($args['attributes']) && isset($args['attributes'][$prop]) ? $args['attributes'][$prop] : null; if (($val === null || $val === '') && $use_default) { $val = self::getDefault($args, $prop); } if($val === '' && $null) { $val = null; } return $val; } static function getAttributeForAction($args, $prop, $field = null) { $val = self::getAttribute($args, $prop); if($field) { if(is_array($val) && isset($val[$field])) { return $val[$field]; } else { return null; } } if(is_array($val)) { if(isset($val['id'])) return $val['id']; } return $val; } static function getAttributeAsPostIds($args, $prop) { $r = array(); $list = self::getAttribute($args, $prop); if (is_array($list)) { foreach ($list as $item) { $r[] = $item['id']; } } if (count($r) === 0) $r[] = 0; return $r; } static function getInnerContent($args) { return isset($args['content']) ? $args['content'] : ''; } static function getImageUrl($args, $prop, $size, $use_default = true) { $a = $args['attributes']; if (!isset($a[$prop])) return $use_default ? self::getDefault($args, $prop, 'url') : null; if (!empty($a[$prop]['url'])) { return $a[$prop]['url']; } if (empty($a[$prop]['id'])) return $use_default ? self::getDefault($args, $prop, 'url') : null; if (!empty($a[$prop]['size'])) { $size = $a[$prop]['size']; } return PG_Image::getUrl($a[$prop]['id'], $size); } static function getImageSVG($args, $prop, $use_default = true) { return self::getImageField($args, $prop, 'svg', $use_default); } static function getImageField($args, $prop, $field, $use_default = true) { $a = $args['attributes']; if (!isset($a[$prop])) return $use_default ? self::getDefault($args, $prop, $field) : null; if (!empty($a[$prop][$field])) { return $a[$prop][$field]; } return $use_default ? self::getDefault($args, $prop, $field) : null; } static function getLinkUrl($args, $prop, $use_default = true) { $a = self::getAttribute($args, $prop); if (is_array($a) && isset($a['url'])) { $val = $a['url']; } else { $val = $a; } if ($val === null || $val === '') $val = $use_default ? self::getDefault($args, $prop, 'url') : $val; return $val; } static function mergeInlineSVGAttributes($svg, $props) { foreach($props as $prop => $val) { if($prop === 'className') $prop = 'class'; if(is_array($val)) { $r = ''; foreach($val as $key => $v) { $key = preg_replace_callback("/([A-Z])/g", function($m) { return '-'.strtolower($m[1]); }, $key); $r .= "$key:$v;"; } $val = $r; } $q = '"'; if(strpos($val, '"') >= 0) { $val = str_replace('"', """, $val); } $re = "/(]*\\s*)($prop=\"[^\"]*\")/i"; if(preg_match($re, $svg)) { $svg = preg_replace($re, '$1' . $prop . '='.$q . $val . $q, $svg); } else { $svg = str_replace('setup_postdata( $post ); } } } } } } }