File: /wordpress/plugins/wp-cloud-client/1.1.1/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 '';
}
}
}