ID, key 0 of * that entry, read literally: when key 0 is missing no agent shows and * the post type is not consulted; * 3. else key 0 of the list for get_post_type(), read the same way. */ class Legacy_Placement { const SITEWIDE_OPTION = 'ai_botkit_chatbot_sitewide_enabled'; const PAGES_OPTION = 'ai_botkit_chatbot_enabled_pages'; const POST_TYPES_OPTION = 'ai_botkit_chatbot_enabled_post_types'; /** * The three old options, with the defaults today's footer passes. */ public static function options(): array { return [ 'sitewide' => get_option(self::SITEWIDE_OPTION), 'pages' => get_option(self::PAGES_OPTION), 'post_types' => get_option(self::POST_TYPES_OPTION, []), ]; } /** * @param array $context From Request_Context; reads legacy_post_id and legacy_post_type. * @param array $options From options(). */ public static function winner(array $context, array $options): ?string { $sitewide = isset($options['sitewide']) ? $options['sitewide'] : null; if (self::sitewide_active($sitewide)) { return self::sitewide_namespace($sitewide); } $pages = isset($options['pages']) ? $options['pages'] : null; $post_id = isset($context['legacy_post_id']) ? $context['legacy_post_id'] : null; if (is_array($pages) && null !== $post_id && isset($pages[$post_id])) { return self::first_agent($pages[$post_id]); } $post_types = isset($options['post_types']) ? $options['post_types'] : null; $post_type = array_key_exists('legacy_post_type', $context) ? $context['legacy_post_type'] : false; // A bool or int key is cast as PHP casts it today: get_post_type() === false reads key 0. if (is_array($post_types) && (is_string($post_type) || is_bool($post_type) || is_int($post_type)) && isset($post_types[$post_type]) && !empty($post_types[$post_type])) { return self::first_agent($post_types[$post_type]); } return null; } /** * Whether today's footer takes the sitewide branch: `isset($v) && $v != 0` * as PHP 8 evaluates it, on every PHP. 1.1.x only ever ran on PHP 8 (its * `AI_BotKit\Public` namespace was a parse error on 7.x), and PHP 7.4's * native `'abc' != 0` is false, which would switch sitewide off. * * @param mixed $sitewide The raw option value. */ public static function sitewide_active($sitewide): bool { if (null === $sitewide) { return false; } if (is_string($sitewide)) { // PHP 8: a numeric string compares as a number, any other string as the string "0". return is_numeric(trim($sitewide)) ? 0.0 !== (float) trim($sitewide) : true; } if (is_bool($sitewide)) { return $sitewide; } if (is_int($sitewide) || is_float($sitewide)) { return 0.0 !== (float) $sitewide; } return true; // an array or object is never equal to 0 } /** * The sitewide agent, or null when sitewide is off or names no agent. * * @param mixed $sitewide The raw option value. */ public static function sitewide_namespace($sitewide): ?string { if (self::sitewide_active($sitewide) && is_scalar($sitewide) && '' !== (string) $sitewide) { return (string) $sitewide; } return null; } /** * Key 0 of an old agent list, read literally, or null when it shows no * agent (key 0 missing or empty). A string entry, which the old admin * never wrote, yields its first character, as `$entry[0]` does today. * * @param mixed $list */ public static function first_agent($list): ?string { if (is_string($list)) { return '' === $list ? null : $list[0]; } if (!is_array($list) || !array_key_exists(0, $list) || !is_scalar($list[0]) || '' === (string) $list[0]) { return null; } return (string) $list[0]; } }