-
Notifications
You must be signed in to change notification settings - Fork 0
/
doblock.sh
147 lines (144 loc) · 4.93 KB
/
doblock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
echo "# Wanna create a new block huh, give me the name?"
read -r -p "-> " blockname
read -r -p "It's $blockname, or what? [y/N] " response
echo "# And now in german:"
read -r -p "-> " blocknameGerman
read -r -p "# Do you need a js file too? [y/N] " jsyesorno
case "$response" in
[yY][eE][sS] | [yY])
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ACF_DIR=${SCRIPT_DIR}'/acf-json/'
RANDOM_ACF_KEY=$(cat /dev/urandom | LC_ALL=C tr -dc 'a-z0-9' | fold -w 13 | head -n 1)
RANDOM_ACF_FIELD_KEY=$(cat /dev/urandom | LC_ALL=C tr -dc 'a-z0-9' | fold -w 13 | head -n 1)
RANDOM_ACF_FIELD_MODIFIED=$(cat /dev/urandom | LC_ALL=C tr -dc '1-9' | fold -w 10 | head -n 1)
SCSS_DIR=${SCRIPT_DIR}'/src/scss/'
JS_DIR=${SCRIPT_DIR}'/src/js/'
BLOCK_REGISTER_DIR=${SCRIPT_DIR}'/inc/blocks-registrations/'
BLOCK_TEMPLATES_DIR=${SCRIPT_DIR}'/template-parts/blocks/'
cd ${BLOCK_REGISTER_DIR}
touch "${blockname}.php"
echo '<?php
// Register a'${blockname}'block.
acf_register_block_type( [
"name" => "'${blockname}'",
"title" => __( "'${blocknameGerman}'" ),
"description" => __( "A custom block: '${blocknameGerman}'." ),
"render_template" => "template-parts/blocks/'${blockname}'.php",
"mode" => "edit",
"align" => "full",
"category" => "custom-blocks",
"icon" => "admin-generic",
"keywords" => [ "'${blocknameGerman}'" ],
"example" => [
"attributes" => [
"mode" => "preview",
"data" => ["_is_preview" => "true",],
],
],' >${blockname}.php
case "$jsyesorno" in [yY][eE][sS] | [yY])
echo '"enqueue_assets" => function () {
if ( ! is_admin() ) {
wp_enqueue_script( "customs-'${blockname}'-js", filePath( "'${blockname}'.min.js" ), [], fileTimeVersion( "'${blockname}'.min.js" ), true );
}
},' >>${blockname}.php
cd ${SCRIPT_DIR}
search='entry: {'
replace=${search}'\n\t\t['"'"${blockname}"'"'] : '"'"'.\/src\/js\/'${blockname}'.js'"'"','
sed -i '' "s/${search}/${replace}/g" webpack.common.js
cd ${JS_DIR}
touch "${blockname}.js"
echo '#JS file' ${blockname}'.js was created. You have to add a new entry to your webpack.common.js!'
;;
*) ;;
esac
cd ${BLOCK_REGISTER_DIR}
echo '] );' >>${blockname}.php
cd ${BLOCK_TEMPLATES_DIR}
touch "${blockname}.php"
echo '<?php
/**
* '${blocknameGerman}' Block Template.
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param (int|string) $post_id The post ID this block is saved to.
*/
// create class attribute allowing for custom "className" values && adding classes from admin panel if exist
$className = basename( __FILE__, ".php" ) . ( ! empty( $block["className"] ) ? " " . $block["className"] : "" );
//frontend
if ( ! is_admin() ) : ?>
<section class="section <?= $className ?>">
<div class="full-width">
<div class="section__inner">
<div class="container">
</div>
</div>
</div>
</section>
<?php
//display preview html & on block hover
elseif ( is_admin() && ( $is_preview ?? "" ) ): $screen = get_current_screen(); ?>
<img src="<?= get_template_directory_uri() . "/assets/imgs/previews/'${blockname}.png'" ?>" style="width: 100%; height: auto" alt="Preview of what the '${blockname}' custom block is">
<?php endif;' >${blockname}.php
cd ${SCSS_DIR}'/components/'
touch "_${blockname}.scss"
echo '.'${blockname}' {
&__ {
}
}' >_${blockname}.scss
cd ${SCSS_DIR}
echo '@import "components/'${blockname}'";' >>main.scss
cd ${ACF_DIR}
touch "group_${RANDOM_ACF_FIELD_KEY}.json"
echo '{
"key": "group_'${RANDOM_ACF_FIELD_KEY}'",
"title": "Block: '${blocknameGerman}'",
"fields": [
{
"key": "field_'${RANDOM_ACF_FIELD_KEY}'",
"label": "Block: '${blocknameGerman}'",
"name": "",
"aria-label": "",
"type": "message",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"new_lines": "wpautop",
"esc_html": 0
}
],
"location": [
[
{
"param": "block",
"operator": "==",
"value": "acf\/'${blockname}'"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0,
"modified": '${RANDOM_ACF_FIELD_MODIFIED}'
}' >group_${RANDOM_ACF_FIELD_KEY}.json
echo "# It's done now. Don't forget to add a preview img for the new block as png!"
exit 1
;;
*)
exit 0
;;
esac