HEX
Server: nginx
System: Linux pool195-106-36.bur.atomicsites.net 6.12.57+deb12-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1~bpo12+1 (2025-11-17) x86_64
User: (0)
PHP: 8.3.32
Disabled: pcntl_fork
Upload Files
File: /wordpress/plugins/wp-cloud-client/1.1.2/src/BusinessProfile/ProfileShortcode.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\BusinessProfile;

/**
 * Registers and renders the [business_profile] shortcode.
 */
final class ProfileShortcode {

	private const SHORTCODE_TAG = 'business_profile';
	private const DEFAULT_ATTR  = 'company_name';

	/**
	 * Constructor.
	 *
	 * @param ProfileRepository $repository  The profile data repository.
	 * @param FieldRenderer     $renderer    The field renderer.
	 */
	public function __construct(
		private readonly ProfileRepository $repository,
		private readonly FieldRenderer $renderer,
	) {}

	/**
	 * Register the shortcode.
	 *
	 * @return void
	 */
	public function register(): void {
		add_shortcode( self::SHORTCODE_TAG, [ $this, 'render' ] );
	}

	/**
	 * Render the shortcode output.
	 *
	 * @param array<string, string>|string $atts Shortcode attributes.
	 * @return string Rendered HTML.
	 */
	public function render( array|string $atts ): string {
		try {
			$profile = $this->repository->get();

			if ( null === $profile ) {
				return esc_html__( 'Business profile data not found', 'wp-cloud-client' );
			}

			$atts = is_array( $atts ) ? $atts : [];
			$attr = $atts['attr'] ?? self::DEFAULT_ATTR;

			return $this->renderer->renderField( $profile, $attr );
		} catch ( \Throwable $e ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
				error_log( 'wpcc [business_profile] shortcode error: ' . $e->getMessage() );
			}
			return '';
		}
	}
}