rn; } foreach ( $this->behaviors as $behavior ) { $ref_class = new ReflectionClass( $behavior ); if ( $ref_class->hasProperty( $name ) ) { $ref_class->getProperty( $name )->setValue( $behavior, $value ); return; } } throw new Exception( sprintf( 'Setting unknown property: %s::%s', esc_attr( get_class( $this ) ), esc_attr( $name ) ) ); } /** * It iterates over the properties of the class and checks if the property has a docblock containing the * "@defender_property" tag. If the property has the tag, it extracts the type, sanitize, and rule information from * the docblock and stores it in the "annotations" array. * * The list should be * - type: for casting, * - sanitize_*: the list of sanitize_ functions, which should be run on this property, * - rule: the rule that we use for validation. */ protected function parse_annotations() { $class = new ReflectionClass( static::class ); $properties = $class->getProperties( ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED ); foreach ( $properties as $property ) { $doc_block = $property->getDocComment(); if ( ! stristr( $doc_block, '@defender_property' ) ) { continue; } $this->annotations[ $property->getName() ] = array( 'type' => $this->parse_annotations_var( $doc_block ), 'sanitize' => $this->parse_annotation_sanitize( $doc_block ), 'rule' => $this->parse_annotation_rule( $doc_block ), ); } } /** * Parses the variable type from a docblock. * * @param mixed $docblock The docblock to parse. * * @return string|bool The variable type if found, false otherwise. */ private function parse_annotations_var( $docblock ) { $pattern = '/@var\s(.+)/'; if ( preg_match( $pattern, $docblock, $matches ) ) { $type = trim( $matches[1] ); // Only allow right type. if ( in_array( $type, array( 'boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array', 'object' ), true ) ) { return $type; } } return false; } /** * Get the sanitize function. * * @param mixed $docblock The docblock to parse. * * @return bool|string */ private function parse_annotation_sanitize( $docblock ) { $pattern = '/@(sanitize_.+)/'; if ( preg_match( $pattern, $docblock, $matches ) ) { return trim( $matches[1] ); } return false; } /** * Get the validation rule. * * @param mixed $docblock The docblock to parse. * * @return bool|string */ private function parse_annotation_rule( $docblock ) { $pattern = '/@(rule_.+)/'; if ( preg_match( $pattern, $docblock, $matches ) ) { return trim( $matches[1] ); } return false; } /** * Logs a message with an optional category. * * @param mixed $message The message to log. If it is not a string, array, or object, it will be converted to a * string using print_r(). * @param string $category Optional. The category of the log. If provided, the log will be saved to a file with the * category as the filename. If not provided, the log will not be saved to a file. * * @return void */ protected function log( $message, $category = '' ): void { global $wp_filesystem; // Initialize the WP filesystem, no more using 'file-put-contents' function. if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } if ( ! is_string( $message ) || is_array( $message ) || is_object( $message ) ) { $message = wp_json_encode( $message, JSON_PRETTY_PRINT ); } $this->internal_logging[] = wp_date( 'Y-m-d H:i:s' ) . ' ' . $message; /** * Uncomment it for detailed logging on wp cli. * if ( 'cli' === PHP_SAPI ) { * echo $message . PHP_EOL; * } */ $message = '[' . wp_date( 'c' ) . '] ' . $message . PHP_EOL; if ( $this->has_method( 'get_log_path' ) ) { if ( '' !== $category && 0 === preg_match( '/\.log$/', $category ) ) { $category .= '.log'; } $file_path = $this->get_log_path( $category ); $dir_name = pathinfo( $file_path, PATHINFO_DIRNAME ); if ( $wp_filesystem->exists( $file_path ) ) { $message = $wp_filesystem->get_contents( $file_path ) . $message; } elseif ( ! is_dir( $dir_name ) ) { wp_mkdir_p( $dir_name ); } if ( $wp_filesystem->is_writable( $dir_name ) ) { $wp_filesystem->put_contents( $file_path, $message ); } } } }