| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 10 |
use WPDeveloper\BetterDocs\Traits\EditorHelper; |
| 11 |
use WPDeveloper\BetterDocs\Utils\Enqueue; |
| 12 |
|
| 13 |
/** |
| 14 |
* Description |
| 15 |
* |
| 16 |
* @method void register_scripts() |
| 17 |
* @property-read mixed $attributes |
| 18 |
* |
| 19 |
* @since 2.5.0 |
| 20 |
*/ |
| 21 |
abstract class Block extends Base { |
| 22 |
use EditorHelper; |
| 23 |
|
| 24 |
public $is_pro = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Enqueue |
| 28 |
* @var Enqueue |
| 29 |
*/ |
| 30 |
protected $assets_manager = null; |
| 31 |
|
| 32 |
protected $dir = ''; |
| 33 |
|
| 34 |
protected $editor_scripts = ['betterdocs-blocks-editor']; |
| 35 |
|
| 36 |
protected $editor_styles = ['betterdocs-blocks-editor']; |
| 37 |
|
| 38 |
protected $frontend_styles = []; |
| 39 |
protected $frontend_scripts = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* unique name of block |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
abstract public function get_name(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Block can be enabled or not. |
| 49 |
* |
| 50 |
* Override if needed. |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public function can_enable() { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
public function path( $name = '' ) { |
| 59 |
if ( empty( $name ) ) { |
| 60 |
$name = $this->get_name(); |
| 61 |
} |
| 62 |
|
| 63 |
return BETTERDOCS_BLOCKS_DIRECTORY . $name; |
| 64 |
} |
| 65 |
|
| 66 |
public function register_block_type( $name, ...$args ) { |
| 67 |
return register_block_type( |
| 68 |
apply_filters_ref_array( 'betterdocs.blocks.path', [ $this->path( $name ), $name, &$this ] ), |
| 69 |
...$args |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public function load_frontend_styles() { |
| 74 |
if ( empty( $this->frontend_styles ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $this->frontend_styles as $handle ) { |
| 79 |
wp_enqueue_style( $handle ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function load_frontend_scripts() { |
| 84 |
if ( empty( $this->frontend_scripts ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ( $this->frontend_scripts as $handle ) { |
| 89 |
wp_enqueue_script( $handle ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function load_scripts() { |
| 94 |
$this->load_frontend_styles(); |
| 95 |
$this->load_frontend_scripts(); |
| 96 |
} |
| 97 |
|
| 98 |
public function string_to_array( $data = [], $key = 'value' ) { |
| 99 |
$_return_val = []; |
| 100 |
|
| 101 |
if ( is_string( $data ) && ! empty( $data ) ) { |
| 102 |
$_return_val = json_decode( $data, true ); |
| 103 |
$_return_val = array_map( function ( $item ) use ( &$key ) { |
| 104 |
return $item[$key]; |
| 105 |
}, $_return_val ); |
| 106 |
} |
| 107 |
|
| 108 |
return $_return_val; |
| 109 |
} |
| 110 |
|
| 111 |
public function register( $assets_manager ) { |
| 112 |
$this->assets_manager = $assets_manager; |
| 113 |
|
| 114 |
$_args = []; |
| 115 |
|
| 116 |
if ( method_exists( $this, 'register_scripts' ) ) { |
| 117 |
$this->register_scripts(); |
| 118 |
} |
| 119 |
|
| 120 |
if ( method_exists( $this, 'render_callback' ) ) { |
| 121 |
$_args['render_callback'] = function ( $attributes, $content ) { |
| 122 |
if ( ! is_admin() ) { |
| 123 |
$this->load_scripts(); |
| 124 |
} |
| 125 |
return $this->render_callback( $attributes, $content ); |
| 126 |
}; |
| 127 |
} |
| 128 |
|
| 129 |
if ( ( ! empty( $this->frontend_scripts ) || ! empty( $this->frontend_styles ) ) && ! method_exists( $this, 'render_callback' ) ) { |
| 130 |
$_args['render_callback'] = function ( $attributes, $content ) { |
| 131 |
if ( ! is_admin() ) { |
| 132 |
$this->load_scripts(); |
| 133 |
} |
| 134 |
return $content; |
| 135 |
}; |
| 136 |
} |
| 137 |
|
| 138 |
$_args['editor_script'] = $this->editor_scripts; |
| 139 |
$_args['editor_script_handles'] = $this->editor_scripts; |
| 140 |
|
| 141 |
$_args['editor_style'] = $this->editor_styles; |
| 142 |
$_args['editor_style_handles'] = $this->editor_styles; |
| 143 |
|
| 144 |
if ( property_exists( $this, 'attributes' ) ) { |
| 145 |
$_args['attributes'] = $this->attributes; |
| 146 |
} |
| 147 |
|
| 148 |
return $this->register_block_type( $this->get_name(), $_args ); |
| 149 |
} |
| 150 |
|
| 151 |
public function get_default_attributes() { |
| 152 |
return []; |
| 153 |
} |
| 154 |
|
| 155 |
abstract public function render( $attributes, $content ); |
| 156 |
|
| 157 |
public function render_callback( $attributes, $content ) { |
| 158 |
$this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() ); |
| 159 |
$this->attributes = $this->remove_deprecated_attributes( $this->attributes, false, false ); |
| 160 |
|
| 161 |
do_action_ref_array( 'betterdocs_before_render', [ & $this, 'blocks'] ); |
| 162 |
|
| 163 |
ob_start(); |
| 164 |
$this->render( $this->attributes, $content ); |
| 165 |
$content = ob_get_clean(); |
| 166 |
|
| 167 |
do_action_ref_array( 'betterdocs_after_render', [ & $this, 'blocks'] ); |
| 168 |
|
| 169 |
return $content; |
| 170 |
} |
| 171 |
|
| 172 |
public function normalize_attributes( $attributes, $mixed_settings = [] ) { |
| 173 |
$mixed_settings = wp_parse_args( [ |
| 174 |
'prefix' => 'count_prefix', |
| 175 |
'suffix' => 'count_suffix', |
| 176 |
'suffixSingular' => 'count_suffix_singular', |
| 177 |
'showIcon' => 'show_icon', |
| 178 |
'showTitle' => 'show_title', |
| 179 |
'titleTag' => 'title_tag', |
| 180 |
'showCount' => 'show_count', |
| 181 |
'showButton' => 'show_button', |
| 182 |
'showButtonIcon' => 'show_button_icon', |
| 183 |
'buttonIconPosition' => 'button_icon_position', |
| 184 |
'buttonText' => 'button_text', |
| 185 |
'buttonIcon' => 'button_icon', |
| 186 |
'showList' => 'show_list', |
| 187 |
'showHeader' => 'show_header', |
| 188 |
'postsPerPage' => 'post_per_page', |
| 189 |
'postsOrderBy' => 'post_orderby', |
| 190 |
'postsOrder' => 'post_order', |
| 191 |
'enableNestedSubcategory' => 'nested_subcategory' |
| 192 |
], $mixed_settings ); |
| 193 |
|
| 194 |
$_return_value = []; |
| 195 |
|
| 196 |
foreach ( $mixed_settings as $key => $old_key ) { |
| 197 |
if ( isset( $attributes[$key] ) ) { |
| 198 |
$_return_value[$old_key] = $attributes[$key]; |
| 199 |
unset( $attributes[$key] ); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return array_merge( $attributes, $_return_value ); |
| 204 |
} |
| 205 |
} |
| 206 |
|