'', 'default' => '', 'format' => '', 'size' => '', 'text' => '', 'link' => '']; /** * Register on init. Not when Pro has taken over: it registers the shortcode itself. */ public static function register(): void { if (Plugin::isDisabled()) { return; } add_shortcode(self::TAG, [self::class, 'render']); } /** * @param array|string $atts As WordPress passes them (a string when there are none). */ public static function render($atts): string { $atts = shortcode_atts(self::ATTRIBUTES, is_array($atts) ? $atts : [], self::TAG); $atts = array_map(function ($value) { return is_scalar($value) ? (string) $value : ''; }, $atts); $name = $atts['name']; if ($name === '' || PublicFields::get($name) === null) { // Never the default here: the output must not tell whether an option exists. return self::explain($name); } $args = array_filter(array_diff_key($atts, ['name' => true, 'default' => true]), 'strlen'); $html = Output::html($name, $args); return $html !== '' ? $html : esc_html($atts['default']); } /** * Why nothing is shown, as an HTML comment for someone who may see the options (manage_options); nothing for * everyone else. */ private static function explain(string $name): string { if (!current_user_can('manage_options')) { return ''; } if ($name === '') { $reason = __('the shortcode has no name="..."', 'admin-options-pages'); } elseif (in_array($name, (array) get_option(Plugin::PREFIX_ . 'option_names', []), true)) { $reason = __('"Use in shortcode and blocks" is off for this field, or its type cannot be shown', 'admin-options-pages'); } else { $reason = __('this is not an option of Admin Options Pages', 'admin-options-pages'); } // "--" would end the comment early. return sprintf('', str_replace('--', '- -', esc_html($name)), str_replace('--', '- -', esc_html($reason))); } }