Responsive breakpoints CSS -> Generate * */ if ( ! defined( 'ABSPATH' ) ) exit; /** * Apply stored mobile/tablet breakpoint widths to compiled CSS * Uses temporary markers so tablet_px equal to 480 does not collide with mobile widths */ function tbkkt_responsive_breakpoints_css_apply_breakpoint_widths_to_css( string $css, int $mobile_px, int $tablet_px ): string { $mobile_px = (int) $mobile_px; $tablet_px = (int) $tablet_px; $mark_m = '__tbkkt_BP_MOBILE_PX__'; $mark_t = '__tbkkt_BP_TABLET_PX__'; $css = str_replace( '480px', $mark_m, $css ); $css = str_replace( '782px', $mark_t, $css ); $css = str_replace( $mark_t, (string) $tablet_px . 'px', $css ); return str_replace( $mark_m, (string) $mobile_px . 'px', $css ); } /** * Write generated CSS into the uploads directory when custom breakpoint CSS is enabled */ function tbkkt_responsive_breakpoints_css_generate( array $breakpoints ): bool { if ( empty( $breakpoints['enabled'] ) ) return true; $mobile = isset( $breakpoints['mobile_px'] ) ? (int) $breakpoints['mobile_px'] : tbkkt_responsive_breakpoints_css_get_default_mobile_px(); $tablet = isset( $breakpoints['tablet_px'] ) ? (int) $breakpoints['tablet_px'] : tbkkt_responsive_breakpoints_css_get_default_tablet_px(); $dest_root = tbkkt_responsive_breakpoints_css_get_generated_base_path(); if ( $dest_root === '' ) : return false; endif; if ( ! wp_mkdir_p( $dest_root ) ) : return false; endif; $pairs = tbkkt_responsive_breakpoints_css_get_generated_file_pairs(); $success = true; foreach ( $pairs as $pair ) : $source_relative = isset( $pair['source'] ) ? (string) $pair['source'] : ''; $output_name = isset( $pair['output_name'] ) ? (string) $pair['output_name'] : ''; if ( $source_relative === '' || $output_name === '' ) continue; $src_path = TBKKT_PLUGIN_PATH . ltrim( $source_relative, '/' ); $dst_path = $dest_root . '/' . $output_name; if ( ! is_readable( $src_path ) ) : $success = false; continue; endif; // Create subdirectory if needed $output_dir = dirname( $dst_path ); if ( ! is_dir( $output_dir ) ) : if ( ! wp_mkdir_p( $output_dir ) ) : $success = false; continue; endif; endif; $css = file_get_contents( $src_path ); if ( false === $css ) : $success = false; continue; endif; $css = tbkkt_responsive_breakpoints_css_apply_breakpoint_widths_to_css( $css, $mobile, $tablet ); if ( false === file_put_contents( $dst_path, $css, LOCK_EX ) ) : $success = false; endif; endforeach; if ( $success ) : tbkkt_responsive_breakpoints_css_write_uploads_index_if_missing(); endif; return $success; } /** * Run generation from the full plugin option array */ function tbkkt_responsive_breakpoints_css_generate_from_option( array $option_value ): void { $key = tbkkt_responsive_breakpoints_css_get_option_key(); $defaults = tbkkt_responsive_breakpoints_css_get_settings_defaults(); $branch = isset( $option_value[ $key ] ) && is_array( $option_value[ $key ] ) ? $option_value[ $key ] : $defaults; $breakpoints = array( 'enabled' => ! empty( $branch['enabled'] ), 'mobile_px' => isset( $branch['mobile_px'] ) ? (int) $branch['mobile_px'] : (int) $defaults['mobile_px'], 'tablet_px' => isset( $branch['tablet_px'] ) ? (int) $branch['tablet_px'] : (int) $defaults['tablet_px'], ); if ( ! $breakpoints['enabled'] ) : tbkkt_responsive_breakpoints_css_delete_generated_uploads(); return; endif; $generated = tbkkt_responsive_breakpoints_css_generate( $breakpoints ); $from_settings_tab = tbkkt_settings_get_submit_tab_from_request() === 'settings'; if ( ! $generated ) : if ( $from_settings_tab ) : /* translators: Settings API notice when regenerating breakpoint CSS files failed. */ add_settings_error( TBKKT_SETTINGS_GROUP, 'tbkkt_rbcss_regen_fail', __( 'Could not write responsive CSS files under uploads. Check file permissions and the server error log.', 'tms-block-kit' ), 'error' ); endif; return; endif; if ( $from_settings_tab ) : /* translators: Settings API notice after breakpoint CSS was rebuilt under uploads. */ add_settings_error( TBKKT_SETTINGS_GROUP, 'tbkkt_rbcss_regen_ok', __( 'Responsive CSS files were regenerated from bundled plugin styles.', 'tms-block-kit' ), 'success' ); endif; } /** * Keep generated CSS in sync when the plugin option is created (`added_option`) or updated (`updated_option`) */ add_action( 'updated_option', function ( string $option, $old_value, $value ): void { if ( $option !== TBKKT_OPTION_NAME || ! is_array( $value ) ) return; tbkkt_responsive_breakpoints_css_generate_from_option( $value ); }, 10, 3 ); add_action( 'added_option', function ( string $option, $value ): void { if ( $option !== TBKKT_OPTION_NAME || ! is_array( $value ) ) return; tbkkt_responsive_breakpoints_css_generate_from_option( $value ); }, 10, 2 );