| @@ -95,8 +95,10 @@ | ||
| 95 | 95 | private $html_editor_style = ''; |
| 96 | 96 | |
| 97 | 97 | /** |
| 98 | 98 | * Constructor |
| 99 | + * | |
| 100 | + * @param ContentBlocksBuilder $the_plugin_instance | |
| 99 | 101 | */ |
| 100 | 102 | public function __construct( $the_plugin_instance ) { |
| 101 | 103 | parent::__construct( $the_plugin_instance ); |
| 102 | 104 | |
| @@ -150,9 +152,9 @@ | ||
| 150 | 152 | // Handle save post. |
| 151 | 153 | add_action( 'save_post_' . $this->post_type, [ $this, 'save_post' ], 10, 2 ); |
| 152 | 154 | |
| 153 | 155 | // Clear the transient cache on upgraded. |
| 154 | - add_action( 'cbb_version_upgraded', [ $this, 'clear_transient_cache' ] ); | |
| 156 | + add_action( 'cbb/version_upgraded', [ $this, 'clear_transient_cache' ] ); | |
| 155 | 157 | |
| 156 | 158 | // Enqueue frontend scripts for non-cbb blocks. |
| 157 | 159 | add_filter( 'render_block', [ $this, 'enqueue_frontend_block_scripts' ], 10, 3 ); |
| 158 | 160 | |
| @@ -546,9 +548,9 @@ | ||
| 546 | 548 | 'type' => 'string', |
| 547 | 549 | ), |
| 548 | 550 | ), |
| 549 | 551 | 'additionalProperties' => array( |
| 550 | - 'type' => [ 'string', 'boolean', 'object', 'array' ], | |
| 552 | + 'type' => [ 'integer', 'number', 'boolean', 'string', 'object', 'array' ], // The order of data type matters. | |
| 551 | 553 | ), |
| 552 | 554 | ), |
| 553 | 555 | ), |
| 554 | 556 | ), |
| @@ -572,9 +574,9 @@ | ||
| 572 | 574 | 'type' => 'string', |
| 573 | 575 | ), |
| 574 | 576 | ), |
| 575 | 577 | 'additionalProperties' => array( |
| 576 | - 'type' => [ 'string', 'boolean', 'object', 'array' ], | |
| 578 | + 'type' => [ 'integer', 'number', 'boolean', 'string', 'object', 'array' ], | |
| 577 | 579 | ), |
| 578 | 580 | ), |
| 579 | 581 | ), |
| 580 | 582 | ), |
| @@ -749,8 +751,9 @@ | ||
| 749 | 751 | 'supports' => [ |
| 750 | 752 | 'cbb' => true, |
| 751 | 753 | 'layout' => true, |
| 752 | 754 | 'migrateClass' => ! empty( $args['migrateClass'] ) ? ( isset( $args['id'] ) ? 'wp-block-boldblocks-custom' : 'wp-block-boldblocks-custom-parent' ) : '', |
| 755 | + 'CSSClasses' => $args['className'] ?? '', | |
| 753 | 756 | ], |
| 754 | 757 | 'uses_context' => [ 'postId', 'postType', 'cbb/block-overrides' ], |
| 755 | 758 | ]; |
| 756 | 759 | |
| @@ -917,47 +920,103 @@ | ||
| 917 | 920 | } |
| 918 | 921 | } |
| 919 | 922 | } |
| 920 | 923 | |
| 921 | - if ( ! $is_backend && ! empty( $args['custom_scripts'] ) ) { | |
| 924 | + if ( ! empty( $args['custom_scripts'] ) && ! $is_backend ) { | |
| 922 | 925 | $custom_scripts = $args['custom_scripts']; |
| 923 | 926 | |
| 924 | - foreach ( $custom_scripts as $script ) { | |
| 925 | - if ( ! $script || ! is_array( $script ) || empty( $script['value'] ) ) { | |
| 926 | - continue; | |
| 927 | + // Refine custom scripts. | |
| 928 | + $custom_scripts = array_reduce( | |
| 929 | + $custom_scripts, | |
| 930 | + function ( $carry, $script ) { | |
| 931 | + // Skip if script is empty. | |
| 932 | + if ( ! $script || ! is_array( $script ) || empty( $script['value'] ) ) { | |
| 933 | + return $carry; | |
| 934 | + } | |
| 935 | + | |
| 936 | + // Refine script type. | |
| 937 | + $script_type = $script['script_type'] ?? ''; | |
| 938 | + | |
| 939 | + // Legacy support for script_type. | |
| 940 | + if ( ! $script_type ) { | |
| 941 | + if ( $script['is_module'] ?? false ) { | |
| 942 | + $script_type = 'module'; | |
| 943 | + } elseif ( ! empty( $script['handle'] ) ) { | |
| 944 | + $script_type = 'custom'; | |
| 945 | + } else { | |
| 946 | + $script_type = 'default'; | |
| 947 | + } | |
| 948 | + | |
| 949 | + $script['script_type'] = $script_type; | |
| 950 | + } | |
| 951 | + | |
| 952 | + // If custom type but no handle, change to default. | |
| 953 | + if ( 'custom' === $script_type && empty( $script['handle'] ) ) { | |
| 954 | + $script['script_type'] = 'default'; | |
| 955 | + } | |
| 956 | + | |
| 957 | + $carry[] = $script; | |
| 958 | + | |
| 959 | + return $carry; | |
| 960 | + }, | |
| 961 | + [] | |
| 962 | + ); | |
| 963 | + | |
| 964 | + $default_handle_scripts = array_filter( | |
| 965 | + $custom_scripts, | |
| 966 | + function ( $script ) { | |
| 967 | + return 'custom' !== $script['script_type']; | |
| 927 | 968 | } |
| 969 | + ); | |
| 928 | 970 | |
| 929 | - $script_type = $script['script_type'] ?? ''; | |
| 930 | - // Legacy support for script_type. | |
| 931 | - if ( ! $script_type ) { | |
| 932 | - if ( $script['is_module'] ?? false ) { | |
| 933 | - $script_type = 'module'; | |
| 934 | - } elseif ( ! empty( $script['handle'] ) ) { | |
| 935 | - $script_type = 'custom'; | |
| 936 | - } else { | |
| 937 | - $script_type = 'default'; | |
| 938 | - } | |
| 971 | + if ( $default_handle_scripts ) { | |
| 972 | + $dep_handles = array_reduce( | |
| 973 | + $default_handle_scripts, | |
| 974 | + function ( $carry, $script ) { | |
| 975 | + if ( 'module' === $script['script_type'] ) { | |
| 976 | + $carry['has_module'] = true; | |
| 977 | + } elseif ( ! empty( $script['deps'] ) ) { | |
| 978 | + $deps = explode( ',', $script['deps'] ); | |
| 979 | + foreach ( $deps as $dep ) { | |
| 980 | + $dep = trim( $dep ); | |
| 981 | + if ( ! $dep || in_array( $dep, $carry['deps'], true ) ) { | |
| 982 | + continue; | |
| 983 | + } | |
| 984 | + | |
| 985 | + $carry['deps'][] = $dep; | |
| 986 | + } | |
| 987 | + } | |
| 988 | + | |
| 989 | + return $carry; | |
| 990 | + }, | |
| 991 | + [ | |
| 992 | + 'has_module' => false, | |
| 993 | + 'deps' => [], | |
| 994 | + ] | |
| 995 | + ); | |
| 996 | + | |
| 997 | + if ( $dep_handles['has_module'] ) { | |
| 998 | + // Register the default handle for module scripts. | |
| 999 | + wp_register_script( $block_module_inline_handle, '', [], BOLDBLOCKS_CBB_VERSION, [ 'in_footer' => true ] ); | |
| 939 | 1000 | } |
| 940 | - $is_module = 'module' === $script_type; | |
| 941 | - $handle = $is_module ? $block_module_inline_handle : $block_inline_handle; | |
| 942 | 1001 | |
| 1002 | + if ( ! in_array( 'CBB_BLOCK_API', $dep_handles['deps'], true ) ) { | |
| 1003 | + array_unshift( $dep_handles['deps'], 'CBB_BLOCK_API' ); | |
| 1004 | + } | |
| 1005 | + | |
| 1006 | + // Register the default handle for inline scripts. | |
| 1007 | + wp_register_script( $block_inline_handle, '', explode( ',', $this->refine_script_handle( implode( ',', $dep_handles['deps'] ), $is_backend ) ), BOLDBLOCKS_CBB_VERSION, [ 'in_footer' => true ] ); | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + foreach ( $custom_scripts as $script ) { | |
| 1011 | + $script_type = $script['script_type']; | |
| 1012 | + | |
| 943 | 1013 | // Determine if a custom handle should be used. |
| 944 | - if ( 'custom' === $script_type && ! empty( $script['handle'] ) ) { | |
| 1014 | + if ( 'custom' === $script_type ) { | |
| 945 | 1015 | $handle = $this->refine_script_handle( $script['handle'], $is_backend ); |
| 946 | 1016 | } else { |
| 947 | - // Get dep handles. | |
| 948 | - $dep_handles = $script['deps'] ?? 'CBB_BLOCK_API'; | |
| 949 | - if ( false === strpos( $dep_handles, 'CBB_BLOCK_API' ) ) { | |
| 950 | - if ( ! $dep_handles ) { | |
| 951 | - $dep_handles = 'CBB_BLOCK_API'; | |
| 952 | - } else { | |
| 953 | - $dep_handles = 'CBB_BLOCK_API,' . $dep_handles; | |
| 954 | - } | |
| 955 | - } | |
| 1017 | + $handle = 'module' === $script_type ? $block_module_inline_handle : $block_inline_handle; | |
| 956 | 1018 | |
| 957 | - // Register the default handle for module and inline scripts. | |
| 958 | - wp_register_script( $handle, '', explode( ',', $this->refine_script_handle( $dep_handles, $is_backend ) ), BOLDBLOCKS_CBB_VERSION, [ 'in_footer' => true ] ); | |
| 959 | - | |
| 960 | 1019 | // Add handle to view scripts and editor scripts if backend. |
| 961 | 1020 | if ( ! in_array( $handle, $handles['view_script_handles'], true ) ) { |
| 962 | 1021 | $handles['view_script_handles'][] = $handle; |
| 963 | 1022 | } |
| @@ -1113,8 +1172,9 @@ | ||
| 1113 | 1172 | $screen = get_current_screen(); |
| 1114 | 1173 | if ( $screen && $screen->is_block_editor ) { |
| 1115 | 1174 | wp_enqueue_code_editor( array( 'type' => 'text/css' ) ); |
| 1116 | 1175 | wp_enqueue_code_editor( array( 'type' => 'javascript' ) ); |
| 1176 | + wp_enqueue_code_editor( array( 'type' => 'application/json' ) ); | |
| 1117 | 1177 | } |
| 1118 | 1178 | } |
| 1119 | 1179 | |
| 1120 | 1180 | /** |
| @@ -1174,8 +1234,9 @@ | ||
| 1174 | 1234 | 'blocks' => $all_custom_blocks, |
| 1175 | 1235 | 'variations' => $this->the_plugin_instance->get_component( Variations::class )->get_all_variations(), |
| 1176 | 1236 | 'variationNonce' => wp_create_nonce( 'cbb_variation_nonce' ), |
| 1177 | 1237 | 'animations' => $this->get_animated_blocks(), |
| 1238 | + 'canEditCSS' => $this->the_plugin_instance->get_component( CustomStyle::class )->user_can_edit_css(), | |
| 1178 | 1239 | ]; |
| 1179 | 1240 | |
| 1180 | 1241 | // Get block name for current variation. |
| 1181 | 1242 | $block_name = $this->the_plugin_instance->get_component( Variations::class )->get_block_name(); |
| @@ -1343,9 +1404,9 @@ | ||
| 1343 | 1404 | } |
| 1344 | 1405 | |
| 1345 | 1406 | $block_name = sprintf( 'boldblocks/%s', $this->sanitize_block_name( $item->post_name ) ); |
| 1346 | 1407 | $block_version = get_post_meta( $item->ID, 'boldblocks_block_version', true ); |
| 1347 | - $migrateClass = version_compare( $block_version, '2.7.14', '<=' ); | |
| 1408 | + $migrate_class = version_compare( $block_version, '2.7.14', '<=' ); | |
| 1348 | 1409 | |
| 1349 | 1410 | $block_args = [ |
| 1350 | 1411 | 'id' => $item->ID, |
| 1351 | 1412 | 'name' => $block_name, |
| @@ -1362,9 +1423,9 @@ | ||
| 1362 | 1423 | 'enableCustomAttributes' => (bool) get_post_meta( $item->ID, 'boldblocks_enable_custom_attributes', true ), |
| 1363 | 1424 | ], |
| 1364 | 1425 | 'enableVariationPicker' => (bool) get_post_meta( $item->ID, 'boldblocks_enable_variation_picker', true ), |
| 1365 | 1426 | 'blockVersion' => $block_version, |
| 1366 | - 'migrateClass' => $migrateClass, | |
| 1427 | + 'migrateClass' => $migrate_class, | |
| 1367 | 1428 | 'dependentBlocks' => get_post_meta( $item->ID, 'boldblocks_block_dependent_blocks', true ), |
| 1368 | 1429 | 'hideOnFrontend' => $hide_on_frontend, |
| 1369 | 1430 | 'isTransformable' => $is_transformable, |
| 1370 | 1431 | 'isHidden' => $is_hidden, |
| @@ -1408,9 +1469,9 @@ | ||
| 1408 | 1469 | 'custom_styles' => $this->refine_custom_scripts( $block_custom_styles, 'CSS', $parent_block_name, $block_name ), |
| 1409 | 1470 | 'hideOnFrontend' => $hide_on_frontend, |
| 1410 | 1471 | 'blockParent' => $block_parent, |
| 1411 | 1472 | 'blockAncestor' => $block_ancestor, |
| 1412 | - 'migrateClass' => $migrateClass, | |
| 1473 | + 'migrateClass' => $migrate_class, | |
| 1413 | 1474 | |
| 1414 | 1475 | ]; |
| 1415 | 1476 | |
| 1416 | 1477 | if ( $keywords ) { |
| @@ -1785,9 +1846,9 @@ | ||
| 1785 | 1846 | } |
| 1786 | 1847 | |
| 1787 | 1848 | if ( $block_instance->block_type->supports['cbb'] ?? false ) { |
| 1788 | 1849 | $GLOBALS['boldblocks_frontend_loaded'] = true; |
| 1789 | - } elseif ( $this->is_enqueue_frontend_script_for_non_cbb_blocks( $block, $block_instance ) ) { | |
| 1850 | + } elseif ( $this->is_enqueue_frontend_script_for_animations( $block ) || $this->is_enqueue_frontend_script_for_sticky_blocks( $block ) ) { | |
| 1790 | 1851 | wp_enqueue_script( $this->custom_blocks_frontend_handle ); |
| 1791 | 1852 | wp_enqueue_style( $this->custom_blocks_handle ); |
| 1792 | 1853 | |
| 1793 | 1854 | $GLOBALS['boldblocks_frontend_loaded'] = true; |
| @@ -1796,15 +1857,14 @@ | ||
| 1796 | 1857 | return $block_content; |
| 1797 | 1858 | } |
| 1798 | 1859 | |
| 1799 | 1860 | /** |
| 1800 | - * Whether to load the frontend scripts for non-cbb blocks. | |
| 1861 | + * Whether to load the frontend scripts for non-cbb sticky blocks. | |
| 1801 | 1862 | * |
| 1802 | - * @param array $block | |
| 1803 | - * @param WP_Block $block_instance | |
| 1863 | + * @param array $block | |
| 1804 | 1864 | * @return boolean |
| 1805 | 1865 | */ |
| 1806 | - private function is_enqueue_frontend_script_for_non_cbb_blocks( $block, $block_instance ) { | |
| 1866 | + private function is_enqueue_frontend_script_for_sticky_blocks( $block ) { | |
| 1807 | 1867 | $block_names = $this->the_plugin_instance->get_component( CustomStyle::class )->non_cbb_blocks; |
| 1808 | 1868 | if ( ! in_array( $block['blockName'] ?? '', $block_names, true ) ) { |
| 1809 | 1869 | return false; |
| 1810 | 1870 | } |
| @@ -1817,8 +1877,18 @@ | ||
| 1817 | 1877 | return false; |
| 1818 | 1878 | } |
| 1819 | 1879 | |
| 1820 | 1880 | /** |
| 1881 | + * Whether to load the frontend scripts for non-cbb animation blocks. | |
| 1882 | + * | |
| 1883 | + * @param array $block | |
| 1884 | + * @return boolean | |
| 1885 | + */ | |
| 1886 | + private function is_enqueue_frontend_script_for_animations( $block ) { | |
| 1887 | + return ! empty( $block['attrs']['boldblocks']['animations'] ) && in_array( $block['blockName'] ?? '', $this->get_animated_blocks(), true ); | |
| 1888 | + } | |
| 1889 | + | |
| 1890 | + /** | |
| 1821 | 1891 | * Enqueue frontend scripts for carousel layout in the query loop block |
| 1822 | 1892 | * |
| 1823 | 1893 | * @param string $block_content |
| 1824 | 1894 | * @param array $block |
| @@ -1937,9 +2007,9 @@ | ||
| 1937 | 2007 | return $block_content; |
| 1938 | 2008 | } |
| 1939 | 2009 | |
| 1940 | 2010 | if ( $block_instance->block_type->supports['hideOnFrontend'] ?? false ) { |
| 1941 | - return null; | |
| 2011 | + return '<!-- ' . esc_html( $block_instance->name ) . ' -->'; | |
| 1942 | 2012 | } |
| 1943 | 2013 | |
| 1944 | 2014 | return $block_content; |
| 1945 | 2015 | } |