| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace WPDeveloper\BetterDocs\Editors\BlockEditor; |
| 4 | 4 | |
| 5 | 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | - exit; // Exit if accessed directly | |
| 6 | + exit; // Exit if accessed directly | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | use WPDeveloper\BetterDocs\Utils\Base; |
| 10 | 10 | use WPDeveloper\BetterDocs\Traits\EditorHelper; |
| @@ -18,188 +18,237 @@ | ||
| 18 | 18 | * |
| 19 | 19 | * @since 2.5.0 |
| 20 | 20 | */ |
| 21 | 21 | abstract class Block extends Base { |
| 22 | - use EditorHelper; | |
| 22 | + use EditorHelper; | |
| 23 | 23 | |
| 24 | - public $is_pro = false; | |
| 24 | + public $is_pro = false; | |
| 25 | 25 | |
| 26 | - /** | |
| 27 | - * Enqueue | |
| 28 | - * @var Enqueue | |
| 29 | - */ | |
| 30 | - protected $assets_manager = null; | |
| 26 | + /** | |
| 27 | + * Enqueue | |
| 28 | + * @var Enqueue | |
| 29 | + */ | |
| 30 | + protected $assets_manager = null; | |
| 31 | 31 | |
| 32 | - protected $dir = ''; | |
| 32 | + protected $dir = ''; | |
| 33 | 33 | |
| 34 | - protected $editor_scripts = ['betterdocs-blocks-editor']; | |
| 34 | + protected $editor_scripts = [ 'betterdocs-blocks-editor' ]; | |
| 35 | 35 | |
| 36 | - protected $editor_styles = ['betterdocs-blocks-editor']; | |
| 36 | + protected $editor_styles = [ 'betterdocs-blocks-editor' ]; | |
| 37 | 37 | |
| 38 | - protected $frontend_styles = []; | |
| 39 | - protected $frontend_scripts = []; | |
| 38 | + protected $frontend_styles = []; | |
| 39 | + protected $frontend_scripts = []; | |
| 40 | 40 | |
| 41 | - /** | |
| 42 | - * unique name of block | |
| 43 | - * @return string | |
| 44 | - */ | |
| 45 | - abstract public function get_name(); | |
| 41 | + /** | |
| 42 | + * unique name of block | |
| 43 | + * @return string | |
| 44 | + */ | |
| 45 | + abstract public function get_name(); | |
| 46 | 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 | - } | |
| 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 | 57 | |
| 58 | - public function path( $name = '' ) { | |
| 59 | - if ( empty( $name ) ) { | |
| 60 | - $name = $this->get_name(); | |
| 61 | - } | |
| 58 | + public function path( $name = '' ) { | |
| 59 | + if ( empty( $name ) ) { | |
| 60 | + $name = $this->get_name(); | |
| 61 | + } | |
| 62 | 62 | |
| 63 | - return BETTERDOCS_BLOCKS_DIRECTORY . $name; | |
| 64 | - } | |
| 63 | + return BETTERDOCS_BLOCKS_DIRECTORY . $name; | |
| 64 | + } | |
| 65 | 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 | - } | |
| 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 | 72 | |
| 73 | - public function load_frontend_styles() { | |
| 74 | - if ( empty( $this->frontend_styles ) ) { | |
| 75 | - return; | |
| 76 | - } | |
| 73 | + public function load_frontend_styles() { | |
| 74 | + if ( empty( $this->frontend_styles ) ) { | |
| 75 | + return; | |
| 76 | + } | |
| 77 | 77 | |
| 78 | - foreach ( $this->frontend_styles as $handle ) { | |
| 79 | - wp_enqueue_style( $handle ); | |
| 80 | - } | |
| 81 | - } | |
| 78 | + foreach ( $this->frontend_styles as $handle ) { | |
| 79 | + wp_enqueue_style( $handle ); | |
| 80 | + } | |
| 81 | + } | |
| 82 | 82 | |
| 83 | - public function load_frontend_scripts() { | |
| 84 | - if ( empty( $this->frontend_scripts ) ) { | |
| 85 | - return; | |
| 86 | - } | |
| 83 | + public function load_frontend_scripts() { | |
| 84 | + if ( empty( $this->frontend_scripts ) ) { | |
| 85 | + return; | |
| 86 | + } | |
| 87 | 87 | |
| 88 | - foreach ( $this->frontend_scripts as $handle ) { | |
| 89 | - wp_enqueue_script( $handle ); | |
| 90 | - } | |
| 91 | - } | |
| 88 | + foreach ( $this->frontend_scripts as $handle ) { | |
| 89 | + wp_enqueue_script( $handle ); | |
| 90 | + } | |
| 91 | + } | |
| 92 | 92 | |
| 93 | - public function load_scripts() { | |
| 94 | - $this->load_frontend_styles(); | |
| 95 | - $this->load_frontend_scripts(); | |
| 96 | - } | |
| 93 | + public function load_scripts() { | |
| 94 | + $this->load_frontend_styles(); | |
| 95 | + $this->load_frontend_scripts(); | |
| 96 | + } | |
| 97 | 97 | |
| 98 | - public function string_to_array( $data = [], $key = 'value' ) { | |
| 99 | - $_return_val = []; | |
| 98 | + /** | |
| 99 | + * Flatten a decoded selection attribute into a plain list of values. | |
| 100 | + * | |
| 101 | + * Selection attributes are saved by the editor as `[{ value, label }]` | |
| 102 | + * objects, but a bare list of values — `[5, 7]` — is what REST clients and | |
| 103 | + * other programmatic writers naturally produce. Both shapes are accepted: | |
| 104 | + * an array element contributes its `$key`, a scalar element contributes | |
| 105 | + * itself, and elements that carry no value are dropped. | |
| 106 | + * | |
| 107 | + * @since 4.9.0 | |
| 108 | + * | |
| 109 | + * @param mixed $decoded Decoded attribute value. Anything that is not an array yields an empty list. | |
| 110 | + * @param string $key Key holding the value on object elements. Default 'value'. | |
| 111 | + * | |
| 112 | + * @return array Re-indexed list of values. | |
| 113 | + */ | |
| 114 | + public static function normalize_id_list( $decoded, $key = 'value' ) { | |
| 115 | + if ( ! is_array( $decoded ) ) { | |
| 116 | + return []; | |
| 117 | + } | |
| 100 | 118 | |
| 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 | - } | |
| 119 | + $_values = array_map( | |
| 120 | + function ( $item ) use ( $key ) { | |
| 121 | + return is_array( $item ) ? ( isset( $item[ $key ] ) ? $item[ $key ] : null ) : $item; | |
| 122 | + }, | |
| 123 | + $decoded | |
| 124 | + ); | |
| 107 | 125 | |
| 108 | - return $_return_val; | |
| 109 | - } | |
| 126 | + return array_values( | |
| 127 | + array_filter( | |
| 128 | + $_values, | |
| 129 | + function ( $value ) { | |
| 130 | + return null !== $value; | |
| 131 | + } | |
| 132 | + ) | |
| 133 | + ); | |
| 134 | + } | |
| 110 | 135 | |
| 111 | - public function register( $assets_manager ) { | |
| 112 | - $this->assets_manager = $assets_manager; | |
| 136 | + /** | |
| 137 | + * Decode a JSON attribute string into a list of values. | |
| 138 | + * | |
| 139 | + * @since 4.9.0 Bare-value lists (`"[5]"`) are accepted alongside the | |
| 140 | + * editor's `[{ value, label }]` form, and no longer warn. | |
| 141 | + * | |
| 142 | + * @param mixed $data JSON encoded attribute value; non-strings yield an empty list. | |
| 143 | + * @param string $key Key holding the value on object elements. Default 'value'. | |
| 144 | + * | |
| 145 | + * @return array | |
| 146 | + */ | |
| 147 | + public function string_to_array( $data = [], $key = 'value' ) { | |
| 148 | + $_return_val = []; | |
| 113 | 149 | |
| 114 | - $_args = []; | |
| 150 | + if ( is_string( $data ) && ! empty( $data ) ) { | |
| 151 | + $_return_val = self::normalize_id_list( json_decode( $data, true ), $key ); | |
| 152 | + } | |
| 115 | 153 | |
| 116 | - if ( method_exists( $this, 'register_scripts' ) ) { | |
| 117 | - $this->register_scripts(); | |
| 118 | - } | |
| 154 | + return $_return_val; | |
| 155 | + } | |
| 119 | 156 | |
| 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 | - } | |
| 157 | + public function register( $assets_manager ) { | |
| 158 | + $this->assets_manager = $assets_manager; | |
| 128 | 159 | |
| 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 | - } | |
| 160 | + $_args = []; | |
| 137 | 161 | |
| 138 | - $_args['editor_script'] = $this->editor_scripts; | |
| 139 | - $_args['editor_script_handles'] = $this->editor_scripts; | |
| 162 | + if ( method_exists( $this, 'register_scripts' ) ) { | |
| 163 | + $this->register_scripts(); | |
| 164 | + } | |
| 140 | 165 | |
| 141 | - $_args['editor_style'] = $this->editor_styles; | |
| 142 | - $_args['editor_style_handles'] = $this->editor_styles; | |
| 166 | + if ( method_exists( $this, 'render_callback' ) ) { | |
| 167 | + $_args['render_callback'] = function ( $attributes, $content ) { | |
| 168 | + if ( ! is_admin() ) { | |
| 169 | + $this->load_scripts(); | |
| 170 | + } | |
| 171 | + return $this->render_callback( $attributes, $content ); | |
| 172 | + }; | |
| 173 | + } | |
| 143 | 174 | |
| 144 | - if ( property_exists( $this, 'attributes' ) ) { | |
| 145 | - $_args['attributes'] = $this->attributes; | |
| 146 | - } | |
| 175 | + if ( ( ! empty( $this->frontend_scripts ) || ! empty( $this->frontend_styles ) ) && ! method_exists( $this, 'render_callback' ) ) { | |
| 176 | + $_args['render_callback'] = function ( $attributes, $content ) { | |
| 177 | + if ( ! is_admin() ) { | |
| 178 | + $this->load_scripts(); | |
| 179 | + } | |
| 180 | + return $content; | |
| 181 | + }; | |
| 182 | + } | |
| 147 | 183 | |
| 148 | - return $this->register_block_type( $this->get_name(), $_args ); | |
| 149 | - } | |
| 184 | + $_args['editor_script'] = $this->editor_scripts; | |
| 185 | + $_args['editor_script_handles'] = $this->editor_scripts; | |
| 150 | 186 | |
| 151 | - public function get_default_attributes() { | |
| 152 | - return []; | |
| 153 | - } | |
| 187 | + $_args['editor_style'] = $this->editor_styles; | |
| 188 | + $_args['editor_style_handles'] = $this->editor_styles; | |
| 154 | 189 | |
| 155 | - abstract public function render( $attributes, $content ); | |
| 190 | + if ( property_exists( $this, 'attributes' ) ) { | |
| 191 | + $_args['attributes'] = $this->attributes; | |
| 192 | + } | |
| 156 | 193 | |
| 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 ); | |
| 194 | + return $this->register_block_type( $this->get_name(), $_args ); | |
| 195 | + } | |
| 160 | 196 | |
| 161 | - do_action_ref_array( 'betterdocs_before_render', [ & $this, 'blocks'] ); | |
| 197 | + public function get_default_attributes() { | |
| 198 | + return []; | |
| 199 | + } | |
| 162 | 200 | |
| 163 | - ob_start(); | |
| 164 | - $this->render( $this->attributes, $content ); | |
| 165 | - $content = ob_get_clean(); | |
| 201 | + abstract public function render( $attributes, $content ); | |
| 166 | 202 | |
| 167 | - do_action_ref_array( 'betterdocs_after_render', [ & $this, 'blocks'] ); | |
| 203 | + public function render_callback( $attributes, $content ) { | |
| 204 | + $this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() ); | |
| 205 | + $this->attributes = $this->remove_deprecated_attributes( $this->attributes, false, false ); | |
| 168 | 206 | |
| 169 | - return $content; | |
| 170 | - } | |
| 207 | + do_action_ref_array( 'betterdocs_before_render', [ & $this, 'blocks' ] ); | |
| 171 | 208 | |
| 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 ); | |
| 209 | + ob_start(); | |
| 210 | + $this->render( $this->attributes, $content ); | |
| 211 | + $content = ob_get_clean(); | |
| 193 | 212 | |
| 194 | - $_return_value = []; | |
| 213 | + do_action_ref_array( 'betterdocs_after_render', [ & $this, 'blocks' ] ); | |
| 195 | 214 | |
| 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 | - } | |
| 215 | + return $content; | |
| 216 | + } | |
| 202 | 217 | |
| 203 | - return array_merge( $attributes, $_return_value ); | |
| 204 | - } | |
| 218 | + public function normalize_attributes( $attributes, $mixed_settings = [] ) { | |
| 219 | + $mixed_settings = wp_parse_args( | |
| 220 | + [ | |
| 221 | + 'prefix' => 'count_prefix', | |
| 222 | + 'suffix' => 'count_suffix', | |
| 223 | + 'suffixSingular' => 'count_suffix_singular', | |
| 224 | + 'showIcon' => 'show_icon', | |
| 225 | + 'showTitle' => 'show_title', | |
| 226 | + 'titleTag' => 'title_tag', | |
| 227 | + 'showCount' => 'show_count', | |
| 228 | + 'showButton' => 'show_button', | |
| 229 | + 'showButtonIcon' => 'show_button_icon', | |
| 230 | + 'buttonIconPosition' => 'button_icon_position', | |
| 231 | + 'buttonText' => 'button_text', | |
| 232 | + 'buttonIcon' => 'button_icon', | |
| 233 | + 'showList' => 'show_list', | |
| 234 | + 'showHeader' => 'show_header', | |
| 235 | + 'postsPerPage' => 'post_per_page', | |
| 236 | + 'postsOrderBy' => 'post_orderby', | |
| 237 | + 'postsOrder' => 'post_order', | |
| 238 | + 'enableNestedSubcategory' => 'nested_subcategory' | |
| 239 | + ], | |
| 240 | + $mixed_settings | |
| 241 | + ); | |
| 242 | + | |
| 243 | + $_return_value = []; | |
| 244 | + | |
| 245 | + foreach ( $mixed_settings as $key => $old_key ) { | |
| 246 | + if ( isset( $attributes[ $key ] ) ) { | |
| 247 | + $_return_value[ $old_key ] = $attributes[ $key ]; | |
| 248 | + unset( $attributes[ $key ] ); | |
| 249 | + } | |
| 250 | + } | |
| 251 | + | |
| 252 | + return array_merge( $attributes, $_return_value ); | |
| 253 | + } | |
| 205 | 254 | } |