*/ private $defs = array(); /** * Whether the lazy load ran. * * @var bool */ private $loaded = false; /** * True while free registers its own conditions. * * @var bool */ private $registering_core = false; /** * Diagnostics log of refused or adjusted registrations. * * @var array */ private $log = array(); /** * Get the shared registry. * * @return Registry */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Drop the shared instance (tests, and network tools after switch_to_blog). */ public static function reset() { self::$instance = null; } /** * Load conditions once. Free registers its own conditions first, then * fires gtfg_register_conditions. Never runs before init (F11). * * @return bool True when the registry is loaded. */ public function ensure_loaded() { if ( $this->loaded ) { return true; } if ( function_exists( 'did_action' ) && ! did_action( 'init' ) && ! doing_action( 'init' ) ) { return false; } $this->loaded = true; $this->registering_core = true; Conditions_Core::register( $this ); Conditions_Woocommerce::register( $this ); Conditions_Schedule::register( $this ); $this->registering_core = false; if ( function_exists( 'do_action' ) ) { do_action( 'gtfg_register_conditions', $this ); } return true; } /** * Register a condition. * * @param string $name Lower-case identifier. * @param array $def Definition. * @return bool True when registered. */ public function register( $name, array $def ) { $name = is_string( $name ) ? strtolower( $name ) : ''; if ( ! preg_match( '/^[a-z_][a-z0-9_]*$/', $name ) ) { $this->note( $name, 'invalid_name' ); return false; } if ( isset( $this->defs[ $name ] ) ) { $this->note( $name, 'duplicate_refused' ); return false; } if ( ! isset( $def['callback'] ) || ! is_callable( $def['callback'] ) ) { $this->note( $name, 'no_callback' ); return false; } $params = self::normalize_params( isset( $def['args'] ) ? $def['args'] : 'none' ); if ( null === $params ) { $this->note( $name, 'bad_args_schema' ); return false; } if ( $this->registering_core ) { $source = 'core'; } else { $source = ( isset( $def['source'] ) && 'pro' === $def['source'] ) ? 'pro' : 'custom'; } $risk = ( isset( $def['risk'] ) && 'location' === $def['risk'] ) ? 'location' : 'audience'; if ( 'core' !== $source && 'location' === $risk && Risk_Classifier::name_is_audience( $name ) ) { $risk = 'audience'; $this->note( $name, 'location_claim_refused' ); } $allowed = null; if ( 'custom' === $source ) { // F5: custom conditions are always restricted to exact argument tuples. if ( empty( $params ) ) { $allowed = array( array() ); } elseif ( isset( $def['allowed_args'] ) && is_array( $def['allowed_args'] ) && ! empty( $def['allowed_args'] ) ) { $allowed = array_values( array_filter( $def['allowed_args'], 'is_array' ) ); } else { $this->note( $name, 'custom_without_allowed_args_refused' ); return false; } } $default_cap = ( 'custom' === $source ) ? 'edit_theme_options' : ''; $this->defs[ $name ] = array( 'name' => $name, 'params' => $params, 'callback' => $def['callback'], 'label' => isset( $def['label'] ) ? (string) $def['label'] : $name, 'help' => isset( $def['help'] ) ? (string) $def['help'] : '', 'group' => isset( $def['group'] ) ? (string) $def['group'] : 'Custom', 'risk' => $risk, 'available' => ( isset( $def['available'] ) && is_callable( $def['available'] ) ) ? $def['available'] : null, 'entity' => isset( $def['entity'] ) ? (string) $def['entity'] : '', 'source' => $source, 'min_cap' => isset( $def['min_cap'] ) ? (string) $def['min_cap'] : $default_cap, 'simulatable' => ! isset( $def['simulatable'] ) || (bool) $def['simulatable'], 'allowed_args' => $allowed, ); return true; } /** * Get one definition. * * @param string $name Name. * @return array|null */ public function get( $name ) { $this->ensure_loaded(); $name = is_string( $name ) ? strtolower( $name ) : ''; return isset( $this->defs[ $name ] ) ? $this->defs[ $name ] : null; } /** * All definitions. * * @return array */ public function all() { $this->ensure_loaded(); return $this->defs; } /** * Diagnostics log. * * @return array */ public function log() { return $this->log; } /** * Validate arguments against a condition's schema. * * @param string $name Condition name. * @param mixed $args Arguments (list). * @return true|string True or a reason code. */ public function validate_args( $name, $args ) { $def = $this->get( $name ); if ( null === $def ) { return 'condition_unavailable'; } if ( null === $args ) { $args = array(); } if ( ! is_array( $args ) || array_values( $args ) !== $args ) { return 'bad_arguments'; } $params = $def['params']; $count = count( $args ); $min = 0; $max = count( $params ); foreach ( $params as $p ) { if ( ! $p['optional'] && ! $p['variadic'] ) { ++$min; } if ( $p['variadic'] ) { $max = PHP_INT_MAX; } } if ( $count < $min || $count > $max ) { return 'bad_arguments'; } foreach ( $args as $i => $value ) { $p = isset( $params[ $i ] ) ? $params[ $i ] : end( $params ); if ( is_array( $value ) && count( $value ) > self::MAX_LIST_ITEMS ) { return 'too_large'; } if ( ! self::type_ok( $p['type'], $value ) ) { return 'bad_arguments'; } } if ( null !== $def['allowed_args'] && ! in_array( $args, $def['allowed_args'], true ) ) { return 'bad_arguments'; } return true; } /** * Check one value against a type. * * @param string $type Type. * @param mixed $value Value. * @return bool */ public static function type_ok( $type, $value ) { switch ( $type ) { case 'int': return is_int( $value ); case 'string': return is_string( $value ); case 'bool': return is_bool( $value ); case 'scalar': return null === $value || ( is_scalar( $value ) && ! is_float( $value ) ); case 'id_or_string': return is_int( $value ) || is_string( $value ); case 'date': return is_string( $value ) && (bool) preg_match( '/^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$/', $value ); case 'string_list': if ( is_string( $value ) ) { return true; } return self::flat_list( $value, array( 'string' ) ); case 'id_or_string_list': if ( is_int( $value ) || is_string( $value ) ) { return true; } return self::flat_list( $value, array( 'int', 'string' ) ); } return false; } /** * Is the value a flat list of the given scalar kinds. * * @param mixed $value Value. * @param array $kinds Allowed kinds. * @return bool */ private static function flat_list( $value, array $kinds ) { if ( ! is_array( $value ) || array_values( $value ) !== $value || count( $value ) > self::MAX_LIST_ITEMS ) { return false; } foreach ( $value as $v ) { $ok = ( in_array( 'int', $kinds, true ) && is_int( $v ) ) || ( in_array( 'string', $kinds, true ) && is_string( $v ) ); if ( ! $ok ) { return false; } } return true; } /** * Normalise an args schema to a list of parameter specs. * * @param mixed $args Schema: 'none', a type string, or a list of specs. * @return array|null */ private static function normalize_params( $args ) { if ( 'none' === $args || array() === $args ) { return array(); } if ( is_string( $args ) ) { $args = array( $args ); } if ( ! is_array( $args ) ) { return null; } $out = array(); foreach ( $args as $spec ) { if ( is_string( $spec ) ) { $optional = '?' === substr( $spec, -1 ); $variadic = '*' === substr( $spec, -1 ); $spec = array( 'type' => rtrim( $spec, '?*' ), 'optional' => $optional, 'variadic' => $variadic, ); } if ( ! is_array( $spec ) || ! isset( $spec['type'] ) || ! in_array( $spec['type'], self::TYPES, true ) ) { return null; } $out[] = array( 'type' => $spec['type'], 'optional' => ! empty( $spec['optional'] ), 'variadic' => ! empty( $spec['variadic'] ), ); } return $out; } /** * Add a diagnostics log entry. * * @param string $name Name. * @param string $reason Reason. */ private function note( $name, $reason ) { $this->log[] = array( 'name' => (string) $name, 'reason' => $reason, ); } }