| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Plugin Name: Blockenberg | |
| 4 | - * Description: Advanced Gutenberg Blocks for WordPress Block Editor | |
| 5 | - * Version: 2.0.5 | |
| 3 | + * Plugin Name: Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor | |
| 4 | + * Description: Advanced Gutenberg Blocks and an AI Agent for the WordPress Block Editor | |
| 5 | + * Version: 2.0.14 | |
| 6 | 6 | * Author: Blockenberg |
| 7 | 7 | * Text Domain: blockenberg |
| 8 | 8 | * Domain Path: /languages |
| 9 | 9 | * License: GPLv2 or later |
| @@ -12,8 +12,46 @@ | ||
| 12 | 12 | |
| 13 | 13 | defined( 'ABSPATH' ) || exit; |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | + * Ensure enough PHP memory to register 600+ blocks with layout attributes. | |
| 17 | + * The register_block_type_args filter adds ~400 extra attributes per block, | |
| 18 | + * which requires significantly more memory than the WordPress default 128M. | |
| 19 | + */ | |
| 20 | +@ini_set( 'memory_limit', '512M' ); | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Google Fonts list for the Typography Control. | |
| 24 | + */ | |
| 25 | +require_once __DIR__ . '/assets/php/google-fonts.php'; | |
| 26 | + | |
| 27 | +// Editable Gutenberg template pilot and shared opt-in motion settings. | |
| 28 | +require_once __DIR__ . '/assets/php/motion.php'; | |
| 29 | +require_once __DIR__ . '/assets/php/template-catalog.php'; | |
| 30 | +require_once __DIR__ . '/assets/php/template-components.php'; | |
| 31 | +require_once __DIR__ . '/assets/php/template-dynamics.php'; | |
| 32 | +require_once __DIR__ . '/assets/php/template-preview.php'; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * User Field block — dynamic PHP render for logged-in profile values. | |
| 36 | + */ | |
| 37 | +require_once __DIR__ . '/blocks/user-field/render.php'; | |
| 38 | + | |
| 39 | +/** | |
| 40 | + * AI Agent — OpenRouter-powered chat panel inside the block editor. | |
| 41 | + */ | |
| 42 | +require_once __DIR__ . '/assets/php/ai-assistant.php'; | |
| 43 | +require_once __DIR__ . '/assets/php/ai-external.php'; | |
| 44 | + | |
| 45 | +/** | |
| 46 | + * Admin dashboard — Block Manager (enable / disable blocks). | |
| 47 | + */ | |
| 48 | +if ( is_admin() ) { | |
| 49 | + require_once __DIR__ . '/assets/php/admin-dashboard.php'; | |
| 50 | + require_once __DIR__ . '/assets/php/admin-notices.php'; | |
| 51 | +} | |
| 52 | + | |
| 53 | +/** | |
| 16 | 54 | * Enqueue common editor styles and scripts for all Blockenberg blocks |
| 17 | 55 | */ |
| 18 | 56 | add_action( 'enqueue_block_editor_assets', function() { |
| 19 | 57 | wp_enqueue_style( 'dashicons' ); |
| @@ -42,11 +80,61 @@ | ||
| 42 | 80 | filemtime( $inspector_tabs_js ), |
| 43 | 81 | true |
| 44 | 82 | ); |
| 45 | 83 | } |
| 84 | + | |
| 85 | + // Typography Control — shared Elementor-like popover for all blocks | |
| 86 | + // Registered on init (so block scripts can safely depend on it). | |
| 87 | + wp_enqueue_script( 'bkbg-typography-control' ); | |
| 46 | 88 | }); |
| 47 | 89 | |
| 48 | 90 | /** |
| 91 | + * Register shared editor scripts early (so other scripts can list them as deps). | |
| 92 | + */ | |
| 93 | +add_action( 'init', function () { | |
| 94 | + $typo_js = __DIR__ . '/assets/js/typography-control.js'; | |
| 95 | + if ( ! file_exists( $typo_js ) ) { | |
| 96 | + return; | |
| 97 | + } | |
| 98 | + | |
| 99 | + wp_register_script( | |
| 100 | + 'bkbg-typography-control', | |
| 101 | + plugins_url( 'assets/js/typography-control.js', __FILE__ ), | |
| 102 | + array( 'wp-element', 'wp-components', 'wp-i18n' ), | |
| 103 | + filemtime( $typo_js ), | |
| 104 | + true | |
| 105 | + ); | |
| 106 | + | |
| 107 | + // Pass Google Fonts list to JS as window.bkbgGoogleFonts | |
| 108 | + wp_localize_script( | |
| 109 | + 'bkbg-typography-control', | |
| 110 | + 'bkbgGoogleFonts', | |
| 111 | + function_exists( 'bkbg_google_fonts_list' ) ? bkbg_google_fonts_list() : array() | |
| 112 | + ); | |
| 113 | + | |
| 114 | + // Icon Picker — shared icon type selector + dashicon picker for all blocks | |
| 115 | + $icon_picker_js = __DIR__ . '/assets/js/icon-picker.js'; | |
| 116 | + if ( file_exists( $icon_picker_js ) ) { | |
| 117 | + // Editor handle (needs WP component deps for UI) | |
| 118 | + wp_register_script( | |
| 119 | + 'bkbg-icon-picker', | |
| 120 | + plugins_url( 'assets/js/icon-picker.js', __FILE__ ), | |
| 121 | + array( 'wp-element', 'wp-components', 'wp-i18n' ), | |
| 122 | + filemtime( $icon_picker_js ), | |
| 123 | + true | |
| 124 | + ); | |
| 125 | + // Frontend handle (same file, no WP deps — only data + DOM builder) | |
| 126 | + wp_register_script( | |
| 127 | + 'bkbg-icon-picker-frontend', | |
| 128 | + plugins_url( 'assets/js/icon-picker.js', __FILE__ ), | |
| 129 | + array(), | |
| 130 | + filemtime( $icon_picker_js ), | |
| 131 | + true | |
| 132 | + ); | |
| 133 | + } | |
| 134 | +} ); | |
| 135 | + | |
| 136 | +/** | |
| 49 | 137 | * Enqueue editor styles in a way compatible with the iframe-based editor canvas. |
| 50 | 138 | */ |
| 51 | 139 | add_action( 'enqueue_block_assets', function () { |
| 52 | 140 | // Avoid loading editor-only CSS on the frontend. |
| @@ -90,9 +178,12 @@ | ||
| 90 | 178 | 'wp-i18n', |
| 91 | 179 | 'wp-block-editor', |
| 92 | 180 | 'wp-components', |
| 93 | 181 | 'wp-dom-ready', |
| 94 | - 'wp-data' | |
| 182 | + 'wp-data', | |
| 183 | + 'bkbg-inspector-tabs', | |
| 184 | + 'bkbg-typography-control', | |
| 185 | + 'bkbg-icon-picker' | |
| 95 | 186 | ); |
| 96 | 187 | |
| 97 | 188 | // Standard WordPress style dependencies for blocks |
| 98 | 189 | $style_dependencies = array( |
| @@ -109,11 +200,23 @@ | ||
| 109 | 200 | filemtime( $layout_css ) |
| 110 | 201 | ); |
| 111 | 202 | } |
| 112 | 203 | |
| 204 | + // Get disabled blocks list to skip asset registration. | |
| 205 | + $disabled_blocks = get_option( 'blockenberg_disabled_blocks', array() ); | |
| 206 | + if ( ! is_array( $disabled_blocks ) ) { | |
| 207 | + $disabled_blocks = array(); | |
| 208 | + } | |
| 209 | + | |
| 113 | 210 | // Automatically register scripts for all blocks |
| 114 | 211 | foreach ( glob( $blocks_dir . '*', GLOB_ONLYDIR ) as $block_dir ) { |
| 115 | 212 | $block_name = basename( $block_dir ); |
| 213 | + | |
| 214 | + // Skip disabled blocks. | |
| 215 | + if ( in_array( 'blockenberg/' . $block_name, $disabled_blocks, true ) ) { | |
| 216 | + continue; | |
| 217 | + } | |
| 218 | + | |
| 116 | 219 | $script_file = $block_dir . '/index.js'; |
| 117 | 220 | |
| 118 | 221 | $style_file = $block_dir . '/style.css'; |
| 119 | 222 | $frontend_file = $block_dir . '/frontend.js'; |
| @@ -149,9 +252,9 @@ | ||
| 149 | 252 | if ( file_exists( $frontend_file ) ) { |
| 150 | 253 | wp_register_script( |
| 151 | 254 | 'bkbg-' . $block_name . '-frontend', |
| 152 | 255 | plugins_url( 'blocks/' . $block_name . '/frontend.js', __FILE__ ), |
| 153 | - array(), | |
| 256 | + array( 'wp-dom-ready', 'bkbg-icon-picker-frontend' ), | |
| 154 | 257 | filemtime( $frontend_file ), |
| 155 | 258 | true |
| 156 | 259 | ); |
| 157 | 260 | } |
| @@ -157,9 +260,24 @@ | ||
| 157 | 260 | } |
| 158 | 261 | } |
| 159 | 262 | |
| 160 | 263 | // Automatically register all blocks in the /blocks directory |
| 264 | + // Skip blocks the admin has disabled via the Blockenberg dashboard. | |
| 265 | + $disabled_blocks = get_option( 'blockenberg_disabled_blocks', array() ); | |
| 266 | + if ( ! is_array( $disabled_blocks ) ) { | |
| 267 | + $disabled_blocks = array(); | |
| 268 | + } | |
| 269 | + | |
| 161 | 270 | foreach ( glob( __DIR__ . '/blocks/*/block.json' ) as $metadata ) { |
| 271 | + // Read block name from block.json to check against disabled list. | |
| 272 | + $raw_json = file_get_contents( $metadata ); | |
| 273 | + $block_meta = $raw_json ? json_decode( $raw_json, true ) : null; | |
| 274 | + $block_name = is_array( $block_meta ) && isset( $block_meta['name'] ) ? $block_meta['name'] : ''; | |
| 275 | + | |
| 276 | + if ( '' !== $block_name && in_array( $block_name, $disabled_blocks, true ) ) { | |
| 277 | + continue; // Block is disabled — skip registration. | |
| 278 | + } | |
| 279 | + | |
| 162 | 280 | register_block_type( dirname( $metadata ) ); |
| 163 | 281 | } |
| 164 | 282 | } ); |
| 165 | 283 | |
| @@ -164,18 +282,84 @@ | ||
| 164 | 282 | } ); |
| 165 | 283 | |
| 166 | 284 | // Register custom block category and ensure Blockenberg blocks appear in it. |
| 167 | 285 | add_filter( 'block_categories_all', function( $categories, $block_editor_context ) { |
| 168 | - // Prepend our custom category so it appears first. | |
| 169 | - array_unshift( $categories, array( | |
| 170 | - 'slug' => 'blockenberg', | |
| 171 | - 'title' => __( 'Blockenberg Blocks', 'blockenberg' ), | |
| 172 | - 'icon' => null, | |
| 173 | - ) ); | |
| 286 | + // Prepend Blockenberg sub-categories in reverse order so they appear in the right order. | |
| 287 | + $bkbg_categories = array( | |
| 288 | + array( 'slug' => 'blockenberg', 'title' => __( 'General (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 289 | + array( 'slug' => 'bkbg-layout', 'title' => __( 'Layout & Structure (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 290 | + array( 'slug' => 'bkbg-content', 'title' => __( 'Content & Typography (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 291 | + array( 'slug' => 'bkbg-media', 'title' => __( 'Media & Images (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 292 | + array( 'slug' => 'bkbg-marketing', 'title' => __( 'Marketing & Conversion (Blockenberg)','blockenberg' ), 'icon' => null ), | |
| 293 | + array( 'slug' => 'bkbg-business', 'title' => __( 'Business & Services (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 294 | + array( 'slug' => 'bkbg-blog', 'title' => __( 'Blog & Editorial (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 295 | + array( 'slug' => 'bkbg-interactive', 'title' => __( 'Interactive & Games (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 296 | + array( 'slug' => 'bkbg-charts', 'title' => __( 'Charts & Data (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 297 | + array( 'slug' => 'bkbg-calculators', 'title' => __( 'Calculators & Tools (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 298 | + array( 'slug' => 'bkbg-effects', 'title' => __( 'Effects & Animation (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 299 | + array( 'slug' => 'bkbg-dev', 'title' => __( 'Developer Tools (Blockenberg)', 'blockenberg' ), 'icon' => null ), | |
| 300 | + ); | |
| 301 | + foreach ( array_reverse( $bkbg_categories ) as $cat ) { | |
| 302 | + array_unshift( $categories, $cat ); | |
| 303 | + } | |
| 174 | 304 | return $categories; |
| 175 | 305 | }, 10, 2 ); |
| 176 | 306 | |
| 177 | 307 | /** |
| 308 | + * Load Google Fonts on the frontend for Blockenberg blocks. | |
| 309 | + * Scans block attributes for 'headerTypo', 'contentTypo', and any other | |
| 310 | + * attribute ending in 'Typo' that contains a non-empty 'family' key. | |
| 311 | + */ | |
| 312 | +add_action( 'wp_enqueue_scripts', function () { | |
| 313 | + if ( ! is_singular() ) { | |
| 314 | + return; | |
| 315 | + } | |
| 316 | + $post = get_post(); | |
| 317 | + if ( ! $post || ! has_blocks( $post->post_content ) ) { | |
| 318 | + return; | |
| 319 | + } | |
| 320 | + | |
| 321 | + $system_fonts = array( 'Arial', 'Georgia', 'Helvetica', 'Tahoma', 'Times New Roman', 'Trebuchet MS', 'Verdana' ); | |
| 322 | + $queued = array(); | |
| 323 | + | |
| 324 | + $blocks = parse_blocks( $post->post_content ); | |
| 325 | + | |
| 326 | + // Recursive walker for nested blocks | |
| 327 | + $collect = null; | |
| 328 | + $collect = function ( $blocks ) use ( &$collect, $system_fonts, &$queued ) { | |
| 329 | + foreach ( $blocks as $block ) { | |
| 330 | + if ( strpos( (string) $block['blockName'], 'blockenberg/' ) !== 0 ) { | |
| 331 | + if ( ! empty( $block['innerBlocks'] ) ) { | |
| 332 | + $collect( $block['innerBlocks'] ); | |
| 333 | + } | |
| 334 | + continue; | |
| 335 | + } | |
| 336 | + $attrs = $block['attrs'] ?? array(); | |
| 337 | + foreach ( $attrs as $key => $val ) { | |
| 338 | + // Any typography attribute (legacy *Typo suffix OR new typo* prefix) | |
| 339 | + // that is an array with a non-empty 'family' key. | |
| 340 | + $is_typo_key = ( substr( $key, -4 ) === 'Typo' ) || ( strpos( $key, 'typo' ) === 0 ); | |
| 341 | + if ( $is_typo_key && is_array( $val ) && ! empty( $val['family'] ) ) { | |
| 342 | + $family = sanitize_text_field( $val['family'] ); | |
| 343 | + if ( ! in_array( $family, $system_fonts, true ) && ! isset( $queued[ $family ] ) ) { | |
| 344 | + $queued[ $family ] = true; | |
| 345 | + $handle = 'bkbg-gf-' . sanitize_title( $family ); | |
| 346 | + $url = 'https://fonts.googleapis.com/css2?family=' . | |
| 347 | + urlencode( $family ) . | |
| 348 | + ':wght@300;400;500;600;700;800;900&display=swap'; | |
| 349 | + wp_enqueue_style( $handle, $url, array(), null ); | |
| 350 | + } | |
| 351 | + } | |
| 352 | + } | |
| 353 | + if ( ! empty( $block['innerBlocks'] ) ) { | |
| 354 | + $collect( $block['innerBlocks'] ); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + }; | |
| 358 | + $collect( $blocks ); | |
| 359 | +} ); | |
| 360 | + | |
| 361 | +/** | |
| 178 | 362 | * Register advanced layout attributes on the SERVER side for every Blockenberg block. |
| 179 | 363 | * Without this, WordPress strips unknown attributes during server-side parsing |
| 180 | 364 | * (array_intersect_key in WP_Block_Type::prepare_attributes_for_render). |
| 181 | 365 | */ |
| @@ -183,8 +367,17 @@ | ||
| 183 | 367 | if ( strpos( $block_type, 'blockenberg/' ) !== 0 ) { |
| 184 | 368 | return $args; |
| 185 | 369 | } |
| 186 | 370 | |
| 371 | + // Blockenberg has its own Advanced spacing controls. | |
| 372 | + // Disable core Gutenberg "Dimensions" (spacing) UI to avoid duplicates. | |
| 373 | + if ( isset( $args['supports'] ) && is_array( $args['supports'] ) ) { | |
| 374 | + unset( $args['supports']['spacing'] ); | |
| 375 | + unset( $args['supports']['__experimentalSpacing'] ); | |
| 376 | + unset( $args['supports']['dimensions'] ); | |
| 377 | + unset( $args['supports']['__experimentalDimensions'] ); | |
| 378 | + } | |
| 379 | + | |
| 187 | 380 | $sides = array( 'Top', 'Right', 'Bottom', 'Left' ); |
| 188 | 381 | $devices = array( '', 'Tablet', 'Mobile' ); |
| 189 | 382 | $extra = array(); |
| 190 | 383 | |
| @@ -917,5 +1110,193 @@ | ||
| 917 | 1110 | } |
| 918 | 1111 | return rest_ensure_response( array( 'ok' => true ) ); |
| 919 | 1112 | }, |
| 920 | 1113 | ) ); |
| 921 | -} ); | |
| 1114 | +} ); | |
| 1115 | + | |
| 1116 | +/** | |
| 1117 | + * Strip CR/LF and related sequences so values cannot inject mail headers. | |
| 1118 | + * | |
| 1119 | + * @param string $value Raw header fragment. | |
| 1120 | + * @return string | |
| 1121 | + */ | |
| 1122 | +function bkbg_sanitize_mail_header( $value ) { | |
| 1123 | + $value = (string) $value; | |
| 1124 | + $value = str_replace( array( "\r", "\n", '%0a', '%0d', '%0A', '%0D' ), '', $value ); | |
| 1125 | + return trim( sanitize_text_field( $value ) ); | |
| 1126 | +} | |
| 1127 | + | |
| 1128 | +/** | |
| 1129 | + * HMAC signature for a contact-form recipient email. | |
| 1130 | + * Prevents open-relay abuse: the client may only use a recipient that was | |
| 1131 | + * signed server-side when the block was rendered. | |
| 1132 | + * | |
| 1133 | + * @param string $email Recipient email. | |
| 1134 | + * @return string Hex HMAC or empty string if invalid. | |
| 1135 | + */ | |
| 1136 | +function bkbg_contact_recipient_sig( $email ) { | |
| 1137 | + $email = strtolower( sanitize_email( (string) $email ) ); | |
| 1138 | + if ( ! is_email( $email ) ) { | |
| 1139 | + return ''; | |
| 1140 | + } | |
| 1141 | + return hash_hmac( 'sha256', $email, wp_salt( 'auth' ) ); | |
| 1142 | +} | |
| 1143 | + | |
| 1144 | +/** | |
| 1145 | + * Verify a contact-form recipient signature. | |
| 1146 | + * | |
| 1147 | + * @param string $email Recipient email from the client. | |
| 1148 | + * @param string $sig HMAC from data-recipient-sig. | |
| 1149 | + * @return bool | |
| 1150 | + */ | |
| 1151 | +function bkbg_verify_contact_recipient( $email, $sig ) { | |
| 1152 | + $email = strtolower( sanitize_email( (string) $email ) ); | |
| 1153 | + if ( ! is_email( $email ) || ! is_string( $sig ) || '' === $sig ) { | |
| 1154 | + return false; | |
| 1155 | + } | |
| 1156 | + $expected = bkbg_contact_recipient_sig( $email ); | |
| 1157 | + return ( '' !== $expected && hash_equals( $expected, $sig ) ); | |
| 1158 | +} | |
| 1159 | + | |
| 1160 | +/** | |
| 1161 | + * Inject a server-signed recipient HMAC into Contact Form markup on render. | |
| 1162 | + * Existing posts do not need to be re-saved — the signature is added at runtime. | |
| 1163 | + */ | |
| 1164 | +add_filter( 'render_block', function ( $block_content, $block ) { | |
| 1165 | + if ( empty( $block['blockName'] ) || 'blockenberg/contact-form' !== $block['blockName'] ) { | |
| 1166 | + return $block_content; | |
| 1167 | + } | |
| 1168 | + if ( ! is_string( $block_content ) || '' === $block_content ) { | |
| 1169 | + return $block_content; | |
| 1170 | + } | |
| 1171 | + | |
| 1172 | + $attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array(); | |
| 1173 | + $recipient = isset( $attrs['recipientEmail'] ) ? sanitize_email( (string) $attrs['recipientEmail'] ) : ''; | |
| 1174 | + if ( ! is_email( $recipient ) ) { | |
| 1175 | + return $block_content; | |
| 1176 | + } | |
| 1177 | + | |
| 1178 | + $sig = bkbg_contact_recipient_sig( $recipient ); | |
| 1179 | + if ( '' === $sig ) { | |
| 1180 | + return $block_content; | |
| 1181 | + } | |
| 1182 | + | |
| 1183 | + // Replace existing sig if present, otherwise inject onto the first opening tag. | |
| 1184 | + if ( false !== strpos( $block_content, 'data-recipient-sig=' ) ) { | |
| 1185 | + $block_content = preg_replace( | |
| 1186 | + '/\sdata-recipient-sig=(["\'])[^"\']*\1/', | |
| 1187 | + ' data-recipient-sig="' . esc_attr( $sig ) . '"', | |
| 1188 | + $block_content, | |
| 1189 | + 1 | |
| 1190 | + ); | |
| 1191 | + } else { | |
| 1192 | + $block_content = preg_replace( | |
| 1193 | + '/^\s*(<[a-zA-Z][^>]*)/', | |
| 1194 | + '$1 data-recipient-sig="' . esc_attr( $sig ) . '"', | |
| 1195 | + $block_content, | |
| 1196 | + 1 | |
| 1197 | + ); | |
| 1198 | + } | |
| 1199 | + | |
| 1200 | + return $block_content; | |
| 1201 | +}, 10, 2 ); | |
| 1202 | + | |
| 1203 | +/** | |
| 1204 | + * Contact Form endpoint — POST /wp-json/blockenberg/v1/contact | |
| 1205 | + * Sends an email via wp_mail() to the admin or a custom recipient stored in the block. | |
| 1206 | + * Custom recipients require a valid server-issued HMAC (data-recipient-sig). | |
| 1207 | + */ | |
| 1208 | +add_action( 'rest_api_init', function () { | |
| 1209 | + register_rest_route( 'blockenberg/v1', '/contact', array( | |
| 1210 | + 'methods' => 'POST', | |
| 1211 | + 'permission_callback' => '__return_true', | |
| 1212 | + 'callback' => function ( WP_REST_Request $request ) { | |
| 1213 | + $payload = $request->get_json_params(); | |
| 1214 | + if ( ! is_array( $payload ) ) { | |
| 1215 | + $payload = array(); | |
| 1216 | + } | |
| 1217 | + | |
| 1218 | + // Rate-limit by IP: max 5 requests per 60 s. | |
| 1219 | + $ip = ''; | |
| 1220 | + if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { | |
| 1221 | + $ip = sanitize_text_field( wp_unslash( (string) $_SERVER['REMOTE_ADDR'] ) ); | |
| 1222 | + } | |
| 1223 | + if ( '' !== $ip ) { | |
| 1224 | + $rl_key = 'bkbg_contact_' . md5( $ip ); | |
| 1225 | + $rl = get_transient( $rl_key ); | |
| 1226 | + if ( is_array( $rl ) && isset( $rl['count'] ) && $rl['count'] >= 5 ) { | |
| 1227 | + return new WP_Error( 'rate_limited', __( 'Too many requests. Please try again later.', 'blockenberg' ), array( 'status' => 429 ) ); | |
| 1228 | + } | |
| 1229 | + if ( is_array( $rl ) ) { | |
| 1230 | + $rl['count']++; | |
| 1231 | + set_transient( $rl_key, $rl, 60 ); | |
| 1232 | + } else { | |
| 1233 | + set_transient( $rl_key, array( 'count' => 1 ), 60 ); | |
| 1234 | + } | |
| 1235 | + } | |
| 1236 | + | |
| 1237 | + // Honeypot check (bots fill the hidden website field). | |
| 1238 | + $honeypot = isset( $payload['website'] ) ? sanitize_text_field( (string) $payload['website'] ) : ''; | |
| 1239 | + if ( '' !== $honeypot ) { | |
| 1240 | + return rest_ensure_response( array( 'ok' => true ) ); // silently accept. | |
| 1241 | + } | |
| 1242 | + | |
| 1243 | + // Validate required fields. | |
| 1244 | + $name = isset( $payload['name'] ) ? sanitize_text_field( (string) $payload['name'] ) : ''; | |
| 1245 | + $email = isset( $payload['email'] ) ? sanitize_email( (string) $payload['email'] ) : ''; | |
| 1246 | + $phone = isset( $payload['phone'] ) ? sanitize_text_field( (string) $payload['phone'] ) : ''; | |
| 1247 | + $message = isset( $payload['message'] ) ? sanitize_textarea_field( (string) $payload['message'] ) : ''; | |
| 1248 | + | |
| 1249 | + if ( ! is_email( $email ) ) { | |
| 1250 | + return new WP_Error( 'invalid_email', __( 'Invalid email address.', 'blockenberg' ), array( 'status' => 400 ) ); | |
| 1251 | + } | |
| 1252 | + if ( empty( $message ) ) { | |
| 1253 | + return new WP_Error( 'empty_message', __( 'Message is required.', 'blockenberg' ), array( 'status' => 400 ) ); | |
| 1254 | + } | |
| 1255 | + | |
| 1256 | + // Recipient: only accept a client-supplied address when it carries a | |
| 1257 | + // valid server HMAC. Otherwise always fall back to admin_email (no open relay). | |
| 1258 | + $admin_email = sanitize_email( (string) get_option( 'admin_email' ) ); | |
| 1259 | + $recipient = $admin_email; | |
| 1260 | + $requested = isset( $payload['recipient'] ) ? sanitize_email( (string) $payload['recipient'] ) : ''; | |
| 1261 | + $sig = isset( $payload['recipientSig'] ) ? (string) $payload['recipientSig'] : ''; | |
| 1262 | + if ( is_email( $requested ) && bkbg_verify_contact_recipient( $requested, $sig ) ) { | |
| 1263 | + $recipient = strtolower( $requested ); | |
| 1264 | + } | |
| 1265 | + | |
| 1266 | + $subject = isset( $payload['subject'] ) ? bkbg_sanitize_mail_header( (string) $payload['subject'] ) : ''; | |
| 1267 | + if ( '' === $subject ) { | |
| 1268 | + $subject = __( 'New Contact Form Submission', 'blockenberg' ); | |
| 1269 | + } | |
| 1270 | + | |
| 1271 | + // Build email body. | |
| 1272 | + $body = "Name: {$name}\n"; | |
| 1273 | + $body .= "Email: {$email}\n"; | |
| 1274 | + if ( ! empty( $phone ) ) { | |
| 1275 | + $body .= "Phone: {$phone}\n"; | |
| 1276 | + } | |
| 1277 | + $body .= "\nMessage:\n{$message}\n"; | |
| 1278 | + | |
| 1279 | + // Reply-To: strip CR/LF from name; never allow header injection. | |
| 1280 | + $safe_name = bkbg_sanitize_mail_header( $name ); | |
| 1281 | + $safe_name = str_replace( array( '"', '<', '>' ), '', $safe_name ); | |
| 1282 | + if ( '' !== $safe_name ) { | |
| 1283 | + $reply_to = sprintf( 'Reply-To: %s <%s>', $safe_name, $email ); | |
| 1284 | + } else { | |
| 1285 | + $reply_to = 'Reply-To: ' . $email; | |
| 1286 | + } | |
| 1287 | + | |
| 1288 | + $headers = array( | |
| 1289 | + 'Content-Type: text/plain; charset=UTF-8', | |
| 1290 | + $reply_to, | |
| 1291 | + ); | |
| 1292 | + | |
| 1293 | + $sent = wp_mail( $recipient, $subject, $body, $headers ); | |
| 1294 | + | |
| 1295 | + if ( ! $sent ) { | |
| 1296 | + return new WP_Error( 'mail_failed', __( 'Failed to send email. Please try again.', 'blockenberg' ), array( 'status' => 500 ) ); | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + return rest_ensure_response( array( 'ok' => true ) ); | |
| 1300 | + }, | |
| 1301 | + ) ); | |
| 1302 | +} ); | |