| @@ -32,9 +32,9 @@ | ||
| 32 | 32 | 'post_name__in' => array( $attributes['slug'] ), |
| 33 | 33 | 'tax_query' => array( |
| 34 | 34 | array( |
| 35 | 35 | 'taxonomy' => 'wp_theme', |
| 36 | - 'field' => 'slug', | |
| 36 | + 'field' => 'name', | |
| 37 | 37 | 'terms' => $attributes['theme'], |
| 38 | 38 | ), |
| 39 | 39 | ), |
| 40 | 40 | 'posts_per_page' => 1, |
| @@ -158,13 +158,13 @@ | ||
| 158 | 158 | return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>"; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | - * Returns an array of variation objects for the template part block. | |
| 162 | + * Returns an array of area variation objects for the template part block. | |
| 163 | 163 | * |
| 164 | 164 | * @return array Array containing the block variation objects. |
| 165 | 165 | */ |
| 166 | -function gutenberg_build_template_part_block_variations() { | |
| 166 | +function gutenberg_build_template_part_block_area_variations() { | |
| 167 | 167 | $variations = array(); |
| 168 | 168 | $defined_areas = get_allowed_block_template_part_areas(); |
| 169 | 169 | foreach ( $defined_areas as $area ) { |
| 170 | 170 | if ( 'uncategorized' !== $area['area'] ) { |
| @@ -180,8 +180,66 @@ | ||
| 180 | 180 | ); |
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | return $variations; |
| 184 | +} | |
| 185 | + | |
| 186 | +/** | |
| 187 | + * Returns an array of instance variation objects for the template part block | |
| 188 | + * | |
| 189 | + * @return array Array containing the block variation objects. | |
| 190 | + */ | |
| 191 | +function gutenberg_build_template_part_block_instance_variations() { | |
| 192 | + // Block themes are unavailable during installation. | |
| 193 | + if ( wp_installing() ) { | |
| 194 | + return array(); | |
| 195 | + } | |
| 196 | + $variations = array(); | |
| 197 | + $template_parts = get_block_templates( | |
| 198 | + array( | |
| 199 | + 'post_type' => 'wp_template_part', | |
| 200 | + ), | |
| 201 | + 'wp_template_part' | |
| 202 | + ); | |
| 203 | + | |
| 204 | + $defined_areas = get_allowed_block_template_part_areas(); | |
| 205 | + $icon_by_area = array_combine( array_column( $defined_areas, 'area' ), array_column( $defined_areas, 'icon' ) ); | |
| 206 | + | |
| 207 | + foreach ( $template_parts as $template_part ) { | |
| 208 | + $variations[] = array( | |
| 209 | + 'name' => sanitize_title( $template_part->slug ), | |
| 210 | + 'title' => $template_part->title, | |
| 211 | + // If there's no description for the template part don't show the | |
| 212 | + // block description. This is a bit hacky, but prevent the fallback | |
| 213 | + // by using a non-breaking space so that the value of description | |
| 214 | + // isn't falsey. | |
| 215 | + 'description' => $template_part->description || ' ', | |
| 216 | + 'attributes' => array( | |
| 217 | + 'slug' => $template_part->slug, | |
| 218 | + 'theme' => $template_part->theme, | |
| 219 | + 'area' => $template_part->area, | |
| 220 | + ), | |
| 221 | + 'scope' => array( 'inserter' ), | |
| 222 | + 'icon' => $icon_by_area[ $template_part->area ], | |
| 223 | + 'example' => array( | |
| 224 | + 'attributes' => array( | |
| 225 | + 'slug' => $template_part->slug, | |
| 226 | + 'theme' => $template_part->theme, | |
| 227 | + 'area' => $template_part->area, | |
| 228 | + ), | |
| 229 | + ), | |
| 230 | + ); | |
| 231 | + } | |
| 232 | + return $variations; | |
| 233 | +} | |
| 234 | + | |
| 235 | +/** | |
| 236 | + * Returns an array of all template part block variations. | |
| 237 | + * | |
| 238 | + * @return array Array containing the block variation objects. | |
| 239 | + */ | |
| 240 | +function gutenberg_build_template_part_block_variations() { | |
| 241 | + return array_merge( gutenberg_build_template_part_block_area_variations(), gutenberg_build_template_part_block_instance_variations() ); | |
| 184 | 242 | } |
| 185 | 243 | |
| 186 | 244 | /** |
| 187 | 245 | * Registers the `core/template-part` block on the server. |