@@ -21,29 +21,6 @@ public static function pattern_from_template( $template, $new_slug = null ) {
21
21
);
22
22
}
23
23
24
- public static function pattern_from_wp_block ( $ pattern_post ) {
25
- $ pattern = new stdClass ();
26
- $ pattern ->id = $ pattern_post ->ID ;
27
- $ pattern ->title = $ pattern_post ->post_title ;
28
- $ pattern ->name = sanitize_title_with_dashes ( $ pattern_post ->post_title );
29
- $ pattern ->slug = wp_get_theme ()->get ( 'TextDomain ' ) . '/ ' . $ pattern ->name ;
30
- $ pattern_category_list = get_the_terms ( $ pattern ->id , 'wp_pattern_category ' );
31
- $ pattern ->categories = ! empty ( $ pattern_category_list ) ? join ( ', ' , wp_list_pluck ( $ pattern_category_list , 'name ' ) ) : '' ;
32
- $ pattern ->sync_status = get_post_meta ( $ pattern ->id , 'wp_pattern_sync_status ' , true );
33
- $ pattern ->content = <<<PHP
34
- <?php
35
- /**
36
- * Title: {$ pattern ->title }
37
- * Slug: {$ pattern ->slug }
38
- * Categories: {$ pattern ->categories }
39
- */
40
- ?>
41
- {$ pattern_post ->post_content }
42
- PHP ;
43
-
44
- return $ pattern ;
45
- }
46
-
47
24
public static function escape_alt_for_pattern ( $ html ) {
48
25
if ( empty ( $ html ) ) {
49
26
return $ html ;
@@ -71,132 +48,4 @@ public static function create_pattern_link( $attributes ) {
71
48
return '<!-- wp:pattern ' . $ attributes_json . ' /--> ' ;
72
49
}
73
50
74
- public static function replace_local_pattern_references ( $ pattern ) {
75
- // Find any references to pattern in templates
76
- $ templates_to_update = array ();
77
- $ args = array (
78
- 'post_type ' => array ( 'wp_template ' , 'wp_template_part ' ),
79
- 'posts_per_page ' => -1 ,
80
- 's ' => 'wp:block {"ref": ' . $ pattern ->id . '} ' ,
81
- );
82
- $ find_pattern_refs = new WP_Query ( $ args );
83
- if ( $ find_pattern_refs ->have_posts () ) {
84
- foreach ( $ find_pattern_refs ->posts as $ post ) {
85
- $ slug = $ post ->post_name ;
86
- array_push ( $ templates_to_update , $ slug );
87
- }
88
- }
89
- $ templates_to_update = array_unique ( $ templates_to_update );
90
-
91
- // Only update templates that reference the pattern
92
- CBT_Theme_Templates::add_templates_to_local ( 'all ' , null , null , $ options , $ templates_to_update );
93
-
94
- // List all template and pattern files in the theme
95
- $ base_dir = get_stylesheet_directory ();
96
- $ patterns = glob ( $ base_dir . DIRECTORY_SEPARATOR . 'patterns ' . DIRECTORY_SEPARATOR . '*.php ' );
97
- $ templates = glob ( $ base_dir . DIRECTORY_SEPARATOR . 'templates ' . DIRECTORY_SEPARATOR . '*.html ' );
98
- $ template_parts = glob ( $ base_dir . DIRECTORY_SEPARATOR . 'template-parts ' . DIRECTORY_SEPARATOR . '*.html ' );
99
-
100
- // Replace references to the local patterns in the theme
101
- foreach ( array_merge ( $ patterns , $ templates , $ template_parts ) as $ file ) {
102
- $ file_content = file_get_contents ( $ file );
103
- $ file_content = str_replace ( 'wp:block {"ref": ' . $ pattern ->id . '} ' , 'wp:pattern {"slug":" ' . $ pattern ->slug . '"} ' , $ file_content );
104
- file_put_contents ( $ file , $ file_content );
105
- }
106
-
107
- CBT_Theme_Templates::clear_user_templates_customizations ();
108
- CBT_Theme_Templates::clear_user_template_parts_customizations ();
109
- }
110
-
111
- public static function prepare_pattern_for_export ( $ pattern , $ options = null ) {
112
- if ( ! $ options ) {
113
- $ options = array (
114
- 'localizeText ' => false ,
115
- 'removeNavRefs ' => true ,
116
- 'localizeImages ' => true ,
117
- );
118
- }
119
-
120
- $ pattern = CBT_Theme_Templates::eliminate_environment_specific_content ( $ pattern , $ options );
121
-
122
- if ( array_key_exists ( 'localizeText ' , $ options ) && $ options ['localizeText ' ] ) {
123
- $ pattern = CBT_Theme_Templates::escape_text_in_template ( $ pattern );
124
- }
125
-
126
- if ( array_key_exists ( 'localizeImages ' , $ options ) && $ options ['localizeImages ' ] ) {
127
- $ pattern = CBT_Theme_Media::make_template_images_local ( $ pattern );
128
-
129
- // Write the media assets if there are any
130
- if ( $ pattern ->media ) {
131
- CBT_Theme_Media::add_media_to_local ( $ pattern ->media );
132
- }
133
- }
134
-
135
- return $ pattern ;
136
- }
137
-
138
- /**
139
- * Copy the local patterns as well as any media to the theme filesystem.
140
- */
141
- public static function add_patterns_to_theme ( $ options = null ) {
142
- $ base_dir = get_stylesheet_directory ();
143
- $ patterns_dir = $ base_dir . DIRECTORY_SEPARATOR . 'patterns ' ;
144
-
145
- $ pattern_query = new WP_Query (
146
- array (
147
- 'post_type ' => 'wp_block ' ,
148
- 'posts_per_page ' => -1 ,
149
- )
150
- );
151
-
152
- if ( $ pattern_query ->have_posts () ) {
153
- // If there is no patterns folder, create it.
154
- if ( ! is_dir ( $ patterns_dir ) ) {
155
- wp_mkdir_p ( $ patterns_dir );
156
- }
157
-
158
- foreach ( $ pattern_query ->posts as $ pattern ) {
159
- $ pattern = self ::pattern_from_wp_block ( $ pattern );
160
- $ pattern = self ::prepare_pattern_for_export ( $ pattern , $ options );
161
- $ pattern_exists = false ;
162
-
163
- // Check pattern is synced before adding to theme.
164
- if ( 'unsynced ' !== $ pattern ->sync_status ) {
165
- // Check pattern name doesn't already exist before creating the file.
166
- $ existing_patterns = glob ( $ patterns_dir . DIRECTORY_SEPARATOR . '*.php ' );
167
- foreach ( $ existing_patterns as $ existing_pattern ) {
168
- if ( strpos ( $ existing_pattern , $ pattern ->name . '.php ' ) !== false ) {
169
- $ pattern_exists = true ;
170
- }
171
- }
172
-
173
- if ( $ pattern_exists ) {
174
- return new WP_Error (
175
- 'pattern_already_exists ' ,
176
- sprintf (
177
- /* Translators: Pattern name. */
178
- __ (
179
- 'A pattern with this name already exists: "%s". ' ,
180
- 'create-block-theme '
181
- ),
182
- $ pattern ->name
183
- )
184
- );
185
- }
186
-
187
- // Create the pattern file.
188
- $ pattern_file = $ patterns_dir . $ pattern ->name . '.php ' ;
189
- file_put_contents (
190
- $ patterns_dir . DIRECTORY_SEPARATOR . $ pattern ->name . '.php ' ,
191
- $ pattern ->content
192
- );
193
-
194
- self ::replace_local_pattern_references ( $ pattern );
195
-
196
- // Remove it from the database to ensure that these patterns are loaded from the theme.
197
- wp_delete_post ( $ pattern ->id , true );
198
- }
199
- }
200
- }
201
- }
202
51
}
0 commit comments