Accordion
1 year ago
AddToCartWithOptions
1 week ago
OrderConfirmation
2 months ago
ProductCollection
3 weeks ago
Reviews
2 months ago
AbstractBlock.php
4 weeks ago
AbstractDynamicBlock.php
2 years ago
AbstractInnerBlock.php
1 year ago
AbstractProductGrid.php
3 months ago
ActiveFilters.php
2 years ago
AddToCartForm.php
4 weeks ago
AddToWishlistButton.php
4 weeks ago
AllProducts.php
2 years ago
AllReviews.php
2 years ago
AtomicBlock.php
2 years ago
AttributeFilter.php
1 year ago
Breadcrumbs.php
1 year ago
Cart.php
2 months ago
CartAcceptedPaymentMethodsBlock.php
2 years ago
CartCrossSellsBlock.php
2 years ago
CartCrossSellsProductsBlock.php
2 years ago
CartExpressPaymentBlock.php
1 year ago
CartItemsBlock.php
2 years ago
CartLineItemsBlock.php
2 years ago
CartLink.php
2 months ago
CartOrderSummaryBlock.php
2 years ago
CartOrderSummaryCouponFormBlock.php
2 years ago
CartOrderSummaryDiscountBlock.php
2 years ago
CartOrderSummaryFeeBlock.php
2 years ago
CartOrderSummaryHeadingBlock.php
2 years ago
CartOrderSummaryShippingBlock.php
2 years ago
CartOrderSummarySubtotalBlock.php
2 years ago
CartOrderSummaryTaxesBlock.php
2 years ago
CartOrderSummaryTotalsBlock.php
2 years ago
CartTotalsBlock.php
2 years ago
CatalogSorting.php
4 weeks ago
CategoryDescription.php
2 months ago
CategoryTitle.php
2 months ago
Checkout.php
2 months ago
CheckoutActionsBlock.php
1 year ago
CheckoutAdditionalInformationBlock.php
2 years ago
CheckoutBillingAddressBlock.php
2 years ago
CheckoutContactInformationBlock.php
2 years ago
CheckoutExpressPaymentBlock.php
9 months ago
CheckoutFieldsBlock.php
2 years ago
CheckoutOrderNoteBlock.php
2 years ago
CheckoutOrderSummaryBlock.php
2 years ago
CheckoutOrderSummaryCartItemsBlock.php
2 years ago
CheckoutOrderSummaryCouponFormBlock.php
2 years ago
CheckoutOrderSummaryDiscountBlock.php
2 years ago
CheckoutOrderSummaryFeeBlock.php
2 years ago
CheckoutOrderSummaryShippingBlock.php
2 years ago
CheckoutOrderSummarySubtotalBlock.php
2 years ago
CheckoutOrderSummaryTaxesBlock.php
2 years ago
CheckoutOrderSummaryTotalsBlock.php
2 years ago
CheckoutPaymentBlock.php
2 years ago
CheckoutPickupOptionsBlock.php
2 years ago
CheckoutShippingAddressBlock.php
2 years ago
CheckoutShippingMethodBlock.php
2 years ago
CheckoutShippingMethodsBlock.php
2 years ago
CheckoutTermsBlock.php
2 years ago
CheckoutTotalsBlock.php
2 years ago
ClassicShortcode.php
1 year ago
ClassicTemplate.php
4 months ago
ComingSoon.php
1 year ago
CouponCode.php
2 months ago
CustomerAccount.php
3 months ago
Dropdown.php
4 weeks ago
EmailContent.php
5 months ago
EmptyCartBlock.php
2 years ago
EmptyMiniCartContentsBlock.php
11 months ago
EnableBlockJsonAssetsTrait.php
1 year ago
FeaturedCategory.php
7 months ago
FeaturedItem.php
7 months ago
FeaturedProduct.php
7 months ago
FilledCartBlock.php
2 years ago
FilledMiniCartContentsBlock.php
4 months ago
FilterWrapper.php
2 years ago
HandpickedProducts.php
1 year ago
MiniCart.php
4 weeks ago
MiniCartCartButtonBlock.php
11 months ago
MiniCartCheckoutButtonBlock.php
11 months ago
MiniCartContents.php
3 months ago
MiniCartFooterBlock.php
4 weeks ago
MiniCartItemsBlock.php
11 months ago
MiniCartProductsTableBlock.php
4 weeks ago
MiniCartShoppingButtonBlock.php
9 months ago
MiniCartTitleBlock.php
11 months ago
MiniCartTitleItemsCounterBlock.php
11 months ago
MiniCartTitleLabelBlock.php
11 months ago
NextPreviousButtons.php
5 months ago
PageContentWrapper.php
2 years ago
PaymentMethodIcons.php
11 months ago
PriceFilter.php
1 year ago
ProceedToCheckoutBlock.php
2 years ago
ProductAverageRating.php
1 year ago
ProductBestSellers.php
2 years ago
ProductButton.php
2 months ago
ProductCategories.php
4 weeks ago
ProductCategory.php
2 years ago
ProductDescription.php
9 months ago
ProductDetails.php
5 months ago
ProductFilterActive.php
4 weeks ago
ProductFilterAttribute.php
4 weeks ago
ProductFilterCheckboxList.php
4 weeks ago
ProductFilterChips.php
4 weeks ago
ProductFilterClearButton.php
4 weeks ago
ProductFilterPrice.php
4 weeks ago
ProductFilterPriceSlider.php
4 weeks ago
ProductFilterRating.php
4 weeks ago
ProductFilterRemovableChips.php
4 weeks ago
ProductFilterStatus.php
4 weeks ago
ProductFilterTaxonomy.php
4 weeks ago
ProductFilters.php
3 weeks ago
ProductGallery.php
4 weeks ago
ProductGalleryLargeImage.php
4 weeks ago
ProductGalleryThumbnails.php
4 weeks ago
ProductImage.php
4 weeks ago
ProductImageGallery.php
7 months ago
ProductMeta.php
1 year ago
ProductNew.php
2 years ago
ProductOnSale.php
2 years ago
ProductPrice.php
2 months ago
ProductQuery.php
4 weeks ago
ProductRating.php
1 year ago
ProductRatingCounter.php
1 year ago
ProductRatingStars.php
1 year ago
ProductResultsCount.php
2 months ago
ProductSKU.php
2 months ago
ProductSaleBadge.php
1 year ago
ProductSearch.php
10 months ago
ProductSpecifications.php
2 months ago
ProductStockIndicator.php
2 months ago
ProductSummary.php
7 months ago
ProductTag.php
2 years ago
ProductTemplate.php
2 months ago
ProductTitle.php
1 year ago
ProductTopRated.php
2 years ago
ProductsByAttribute.php
2 years ago
RatingFilter.php
2 years ago
RelatedProducts.php
3 months ago
ReviewsByCategory.php
2 years ago
ReviewsByProduct.php
2 years ago
SavedForLater.php
4 weeks ago
SingleProduct.php
2 months ago
StockFilter.php
2 years ago
StoreNotices.php
1 year ago
Wishlist.php
4 weeks ago
ClassicTemplate.php
451 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate; |
| 5 | use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate; |
| 6 | use Automattic\WooCommerce\Blocks\Templates\ProductCategoryTemplate; |
| 7 | use Automattic\WooCommerce\Blocks\Templates\ProductTagTemplate; |
| 8 | use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate; |
| 9 | use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate; |
| 10 | use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; |
| 11 | use WC_Shortcode_Checkout; |
| 12 | use WC_Frontend_Scripts; |
| 13 | |
| 14 | /** |
| 15 | * Classic Template class |
| 16 | * |
| 17 | * @internal |
| 18 | */ |
| 19 | class ClassicTemplate extends AbstractDynamicBlock { |
| 20 | /** |
| 21 | * Block name. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $block_name = 'legacy-template'; |
| 26 | |
| 27 | /** |
| 28 | * API version. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $api_version = '3'; |
| 33 | |
| 34 | /** |
| 35 | * Initialize this block. |
| 36 | */ |
| 37 | protected function initialize() { |
| 38 | parent::initialize(); |
| 39 | add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); |
| 40 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Extra data passed through from server to client for block. |
| 45 | * |
| 46 | * @param array $attributes Any attributes that currently are available from the block. |
| 47 | * Note, this will be empty in the editor context when the block is |
| 48 | * not in the post content on editor load. |
| 49 | */ |
| 50 | protected function enqueue_data( array $attributes = array() ) { |
| 51 | parent::enqueue_data( $attributes ); |
| 52 | |
| 53 | // Disable client-side navigation so that interactivity powered |
| 54 | // components fall back to full page reload. |
| 55 | wp_interactivity_config( |
| 56 | 'core/router', |
| 57 | array( |
| 58 | 'clientNavigationDisabled' => true, |
| 59 | ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Enqueue assets used for rendering the block in editor context. |
| 65 | * |
| 66 | * This is needed if a block is not yet within the post content--`render` and `enqueue_assets` may not have ran. |
| 67 | */ |
| 68 | public function enqueue_block_assets() { |
| 69 | // Ensures frontend styles for blocks exist in the site editor iframe. |
| 70 | if ( class_exists( 'WC_Frontend_Scripts' ) && is_admin() ) { |
| 71 | $frontend_scripts = new WC_Frontend_Scripts(); |
| 72 | $styles = $frontend_scripts::get_styles(); |
| 73 | |
| 74 | foreach ( $styles as $handle => $style ) { |
| 75 | wp_enqueue_style( |
| 76 | $handle, |
| 77 | set_url_scheme( $style['src'] ), |
| 78 | $style['deps'], |
| 79 | $style['version'], |
| 80 | $style['media'] |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Enqueue assets specific to this block. |
| 88 | * |
| 89 | * @param array $attributes Block attributes. |
| 90 | * @param string $content Block content. |
| 91 | * @param WP_Block $block Block instance. |
| 92 | */ |
| 93 | protected function enqueue_assets( $attributes, $content, $block ) { |
| 94 | parent::enqueue_assets( $attributes, $content, $block ); |
| 95 | |
| 96 | if ( is_product() ) { |
| 97 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_legacy_assets' ], 20 ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Enqueue legacy assets when this block is used as we don't enqueue them for block themes anymore. |
| 103 | * |
| 104 | * Note: This enqueue logic is intentionally duplicated in ProductImageGallery.php |
| 105 | * to keep legacy blocks independent and allow for separate deprecation paths. |
| 106 | * |
| 107 | * @see https://github.com/woocommerce/woocommerce/pull/60223 |
| 108 | */ |
| 109 | public function enqueue_legacy_assets() { |
| 110 | // Legacy script dependencies for backward compatibility. |
| 111 | if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { |
| 112 | wp_enqueue_script( 'wc-zoom' ); |
| 113 | } |
| 114 | |
| 115 | if ( current_theme_supports( 'wc-product-gallery-slider' ) ) { |
| 116 | wp_enqueue_script( 'wc-flexslider' ); |
| 117 | } |
| 118 | |
| 119 | if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { |
| 120 | wp_enqueue_script( 'wc-photoswipe-ui-default' ); |
| 121 | wp_enqueue_style( 'photoswipe-default-skin' ); |
| 122 | add_action( |
| 123 | 'wp_footer', |
| 124 | function () { |
| 125 | wc_get_template( 'single-product/photoswipe.php' ); |
| 126 | } |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | wp_enqueue_script( 'wc-single-product' ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Render method for the Classic Template block. This method will determine which template to render. |
| 136 | * |
| 137 | * @param array $attributes Block attributes. |
| 138 | * @param string $content Block content. |
| 139 | * @param WP_Block $block Block instance. |
| 140 | * @return string | void Rendered block type output. |
| 141 | */ |
| 142 | protected function render( $attributes, $content, $block ) { |
| 143 | if ( ! isset( $attributes['template'] ) ) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * We need to load the scripts here because when using block templates wp_head() gets run after the block |
| 149 | * template. As a result we are trying to enqueue required scripts before we have even registered them. |
| 150 | * |
| 151 | * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447 |
| 152 | */ |
| 153 | if ( class_exists( 'WC_Frontend_Scripts' ) ) { |
| 154 | $frontend_scripts = new WC_Frontend_Scripts(); |
| 155 | $frontend_scripts::load_scripts(); |
| 156 | } |
| 157 | |
| 158 | if ( OrderConfirmationTemplate::SLUG === $attributes['template'] ) { |
| 159 | return $this->render_order_received(); |
| 160 | } |
| 161 | |
| 162 | if ( is_product() ) { |
| 163 | add_filter( 'woocommerce_single_product_zoom_enabled', '__return_true' ); |
| 164 | add_filter( 'woocommerce_single_product_photoswipe_enabled', '__return_true' ); |
| 165 | add_filter( 'woocommerce_single_product_flexslider_enabled', '__return_true' ); |
| 166 | |
| 167 | return $this->render_single_product(); |
| 168 | } |
| 169 | |
| 170 | $valid = false; |
| 171 | $archive_templates = array( |
| 172 | ProductCatalogTemplate::SLUG, |
| 173 | ProductCategoryTemplate::SLUG, |
| 174 | ProductTagTemplate::SLUG, |
| 175 | ProductAttributeTemplate::SLUG, |
| 176 | ProductSearchResultsTemplate::SLUG, |
| 177 | ); |
| 178 | |
| 179 | // Set selected template when we directly find template base slug. |
| 180 | if ( in_array( $attributes['template'], $archive_templates, true ) ) { |
| 181 | $valid = true; |
| 182 | } |
| 183 | |
| 184 | // Set selected template when we find template base slug as prefix for a specific term. |
| 185 | foreach ( $archive_templates as $template ) { |
| 186 | if ( 0 === strpos( $attributes['template'], $template ) ) { |
| 187 | $valid = true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if ( $valid ) { |
| 192 | // Set this so that our product filters can detect if it's a PHP template. |
| 193 | $this->asset_data_registry->add( 'isRenderingPhpTemplate', true ); |
| 194 | |
| 195 | // Set this so filter blocks being used as widgets know when to render. |
| 196 | $this->asset_data_registry->add( 'hasFilterableProducts', true ); |
| 197 | |
| 198 | $this->asset_data_registry->add( |
| 199 | 'pageUrl', |
| 200 | html_entity_decode( get_pagenum_link() ) |
| 201 | ); |
| 202 | |
| 203 | return $this->render_archive_product(); |
| 204 | } |
| 205 | |
| 206 | ob_start(); |
| 207 | |
| 208 | echo "You're using the ClassicTemplate block"; |
| 209 | |
| 210 | wp_reset_postdata(); |
| 211 | |
| 212 | return ob_get_clean(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Render method for rendering the order confirmation template. |
| 217 | * |
| 218 | * @return string Rendered block type output. |
| 219 | */ |
| 220 | protected function render_order_received() { |
| 221 | ob_start(); |
| 222 | |
| 223 | echo '<div class="wp-block-group">'; |
| 224 | |
| 225 | printf( |
| 226 | '<%1$s %2$s>%3$s</%1$s>', |
| 227 | 'h1', |
| 228 | get_block_wrapper_attributes(), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 229 | esc_html__( 'Order confirmation', 'woocommerce' ) |
| 230 | ); |
| 231 | |
| 232 | WC_Shortcode_Checkout::output( array() ); |
| 233 | |
| 234 | echo '</div>'; |
| 235 | |
| 236 | return ob_get_clean(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Render method for the single product template and parts. |
| 241 | * |
| 242 | * @return string Rendered block type output. |
| 243 | */ |
| 244 | protected function render_single_product() { |
| 245 | ob_start(); |
| 246 | |
| 247 | /** |
| 248 | * Hook: woocommerce_before_main_content |
| 249 | * |
| 250 | * Called before rendering the main content for a product. |
| 251 | * |
| 252 | * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
| 253 | * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
| 254 | * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
| 255 | * |
| 256 | * @since 6.3.0 |
| 257 | */ |
| 258 | do_action( 'woocommerce_before_main_content' ); |
| 259 | |
| 260 | $product_query = new \WP_Query( |
| 261 | array( |
| 262 | 'post_type' => 'product', |
| 263 | 'p' => get_the_ID(), |
| 264 | ) |
| 265 | ); |
| 266 | |
| 267 | while ( $product_query->have_posts() ) : |
| 268 | |
| 269 | $product_query->the_post(); |
| 270 | wc_get_template_part( 'content', 'single-product' ); |
| 271 | |
| 272 | endwhile; |
| 273 | |
| 274 | /** |
| 275 | * Hook: woocommerce_after_main_content |
| 276 | * |
| 277 | * Called after rendering the main content for a product. |
| 278 | * |
| 279 | * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
| 280 | * |
| 281 | * @since 6.3.0 |
| 282 | */ |
| 283 | do_action( 'woocommerce_after_main_content' ); |
| 284 | |
| 285 | wp_reset_postdata(); |
| 286 | |
| 287 | return ob_get_clean(); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Render method for the archive product template and parts. |
| 292 | * |
| 293 | * @return string Rendered block type output. |
| 294 | */ |
| 295 | protected function render_archive_product() { |
| 296 | ob_start(); |
| 297 | |
| 298 | /** |
| 299 | * Hook: woocommerce_before_main_content |
| 300 | * |
| 301 | * Called before rendering the main content for a product. |
| 302 | * |
| 303 | * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
| 304 | * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
| 305 | * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
| 306 | * |
| 307 | * @since 6.3.0 |
| 308 | */ |
| 309 | do_action( 'woocommerce_before_main_content' ); |
| 310 | |
| 311 | ?> |
| 312 | <header class="woocommerce-products-header"> |
| 313 | <?php |
| 314 | /** |
| 315 | * Hook: woocommerce_show_page_title |
| 316 | * |
| 317 | * Allows controlling the display of the page title. |
| 318 | * |
| 319 | * @since 6.3.0 |
| 320 | */ |
| 321 | if ( apply_filters( 'woocommerce_show_page_title', true ) ) { |
| 322 | ?> |
| 323 | <h1 class="woocommerce-products-header__title page-title"> |
| 324 | <?php |
| 325 | woocommerce_page_title(); |
| 326 | ?> |
| 327 | </h1> |
| 328 | <?php |
| 329 | } |
| 330 | /** |
| 331 | * Hook: woocommerce_archive_description. |
| 332 | * |
| 333 | * @see woocommerce_taxonomy_archive_description() Renders the taxonomy archive description (priority 10) |
| 334 | * @see woocommerce_product_archive_description() Renders the product archive description (priority 10) |
| 335 | * |
| 336 | * @since 6.3.0 |
| 337 | */ |
| 338 | do_action( 'woocommerce_archive_description' ); |
| 339 | ?> |
| 340 | </header> |
| 341 | <?php |
| 342 | if ( woocommerce_product_loop() ) { |
| 343 | |
| 344 | /** |
| 345 | * Hook: woocommerce_before_shop_loop. |
| 346 | * |
| 347 | * @see woocommerce_output_all_notices() Render error notices (priority 10) |
| 348 | * @see woocommerce_result_count() Show number of results found (priority 20) |
| 349 | * @see woocommerce_catalog_ordering() Show form to control sort order (priority 30) |
| 350 | * |
| 351 | * @since 6.3.0 |
| 352 | */ |
| 353 | do_action( 'woocommerce_before_shop_loop' ); |
| 354 | |
| 355 | woocommerce_product_loop_start(); |
| 356 | |
| 357 | if ( wc_get_loop_prop( 'total' ) ) { |
| 358 | while ( have_posts() ) { |
| 359 | the_post(); |
| 360 | |
| 361 | /** |
| 362 | * Hook: woocommerce_shop_loop. |
| 363 | * |
| 364 | * @since 6.3.0 |
| 365 | */ |
| 366 | do_action( 'woocommerce_shop_loop' ); |
| 367 | |
| 368 | wc_get_template_part( 'content', 'product' ); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | woocommerce_product_loop_end(); |
| 373 | |
| 374 | /** |
| 375 | * Hook: woocommerce_after_shop_loop. |
| 376 | * |
| 377 | * @see woocommerce_pagination() Renders pagination (priority 10) |
| 378 | * |
| 379 | * @since 6.3.0 |
| 380 | */ |
| 381 | do_action( 'woocommerce_after_shop_loop' ); |
| 382 | } else { |
| 383 | /** |
| 384 | * Hook: woocommerce_no_products_found. |
| 385 | * |
| 386 | * @see wc_no_products_found() Default no products found content (priority 10) |
| 387 | * |
| 388 | * @since 6.3.0 |
| 389 | */ |
| 390 | do_action( 'woocommerce_no_products_found' ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Hook: woocommerce_after_main_content |
| 395 | * |
| 396 | * Called after rendering the main content for a product. |
| 397 | * |
| 398 | * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
| 399 | * |
| 400 | * @since 6.3.0 |
| 401 | */ |
| 402 | do_action( 'woocommerce_after_main_content' ); |
| 403 | |
| 404 | wp_reset_postdata(); |
| 405 | return ob_get_clean(); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Get HTML markup with the right classes by attributes. |
| 410 | * This function appends the classname at the first element that have the class attribute. |
| 411 | * Based on the experience, all the wrapper elements have a class attribute. |
| 412 | * |
| 413 | * @param string $content Block content. |
| 414 | * @param array $block Parsed block data. |
| 415 | * @return string Rendered block type output. |
| 416 | */ |
| 417 | public function add_alignment_class_to_wrapper( string $content, array $block ) { |
| 418 | if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { |
| 419 | return $content; |
| 420 | } |
| 421 | |
| 422 | $attributes = (array) $block['attrs']; |
| 423 | |
| 424 | // Set the default alignment to wide. |
| 425 | if ( ! isset( $attributes['align'] ) ) { |
| 426 | $attributes['align'] = 'wide'; |
| 427 | } |
| 428 | |
| 429 | $align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); |
| 430 | |
| 431 | if ( ! isset( $align_class_and_style['class'] ) ) { |
| 432 | return $content; |
| 433 | } |
| 434 | |
| 435 | // Find the first tag. |
| 436 | $first_tag = '<[^<>]+>'; |
| 437 | $matches = array(); |
| 438 | preg_match( $first_tag, $content, $matches ); |
| 439 | |
| 440 | // If there is a tag, but it doesn't have a class attribute, add the class attribute. |
| 441 | if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { |
| 442 | $pattern_before_tag_closing = '/.+?(?=>)/'; |
| 443 | return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 ); |
| 444 | } |
| 445 | |
| 446 | // If there is a tag, and it has a class already, add the class attribute. |
| 447 | $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; |
| 448 | return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 ); |
| 449 | } |
| 450 | } |
| 451 |