$plugin_dir, 'active' => $active, 'plugin_file' => $plugin_file, ); } /** * Returns every plugin slug that has a /patterns directory. * * @return array */ public static function scan_plugins_with_patterns(): array { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $found = array(); foreach ( array_keys( get_plugins() ) as $rel ) { $parts = explode( '/', $rel ); $slug = $parts[0] ?? ''; if ( '' === $slug || isset( $seen[ $slug ] ) ) { continue; } $seen[ $slug ] = true; $patterns_dir = WP_PLUGIN_DIR . '/' . $slug . '/patterns'; if ( ! is_dir( $patterns_dir ) ) { continue; } $found[] = array( 'slug' => $slug, 'path' => $patterns_dir, 'active' => is_plugin_active( $rel ), ); } return $found; } // ------------------------------------------------------------------------- // Pattern file path within /patterns // ------------------------------------------------------------------------- /** * Resolves the absolute path to a pattern file inside the given * container directory's /patterns dir, refusing path escapes. * * @return string|\WP_Error */ public static function resolve_pattern_path( string $container_dir, string $filename_or_slug ) { $filename = self::sanitize_filename( $filename_or_slug ); if ( '' === $filename ) { return new \WP_Error( 'invalid_filename', __( 'Invalid pattern filename.', 'acrossai-abilities-manager' ) ); } $patterns_dir = $container_dir . '/patterns'; $abs_path = $patterns_dir . '/' . $filename; $resolved_dir = realpath( $patterns_dir ); if ( false !== $resolved_dir ) { $candidate = realpath( $abs_path ); if ( false !== $candidate && 0 !== strpos( $candidate, $resolved_dir ) ) { return new \WP_Error( 'path_escape', __( 'Pattern path escapes the /patterns directory.', 'acrossai-abilities-manager' ) ); } } return $abs_path; } /** * Normalises a value (filename OR bare slug) to a safe `slug.php` form. */ public static function sanitize_filename( string $value ): string { $value = basename( $value ); if ( ! preg_match( '/\.php$/i', $value ) ) { $value .= '.php'; } $base = preg_replace( '/\.php$/i', '', $value ); $base = sanitize_title( $base ); if ( '' === $base ) { return ''; } return $base . '.php'; } // ------------------------------------------------------------------------- // Slug validation // ------------------------------------------------------------------------- /** * Validates a bare pattern slug (the part after "/"). * Used for DB post_name and file basename. */ public static function is_valid_bare_slug( string $slug ): bool { return (bool) preg_match( '#^[a-z0-9_-]+$#', $slug ); } /** * Validates a fully-qualified slug of the form "prefix/pattern-name". * Used for the file header "Slug:" field. */ public static function is_valid_full_slug( string $slug ): bool { return (bool) preg_match( '#^[a-z0-9_-]+/[a-z0-9_-]+$#', $slug ); } /** * Builds the canonical full slug for a file-based pattern given the * container slug (theme folder or plugin folder) and the bare slug. */ public static function build_full_slug( string $container_slug, string $bare_slug ): string { return sanitize_key( $container_slug ) . '/' . sanitize_key( $bare_slug ); } // ------------------------------------------------------------------------- // Content validation // ------------------------------------------------------------------------- /** * Empty or whitespace-only content is rejected per Scenario 15. */ public static function is_valid_content( string $content ): bool { return '' !== trim( $content ); } // ------------------------------------------------------------------------- // File header parse / build // ------------------------------------------------------------------------- /** * Builds the PHP header comment block for a new pattern file. * * @param array $headers Header name => value. */ public static function build_file( array $headers, string $body ): string { $lines = array( ''; return implode( "\n", $lines ) . "\n" . $body; } /** * Parses the doc-block headers from an existing pattern file. * * @return array{headers: array, body: string} */ public static function parse_file( string $contents ): array { $headers = array(); $body = $contents; if ( preg_match( '#^\s*<\?php\s*/\*\*(.*?)\*/\s*\?>\s*#s', $contents, $m, PREG_OFFSET_CAPTURE ) ) { $header_block = $m[1][0]; $body = substr( $contents, $m[0][1] + strlen( $m[0][0] ) ); foreach ( self::HEADERS as $name ) { if ( preg_match( '/^\s*\*?\s*' . preg_quote( $name, '/' ) . '\s*:\s*(.+)$/mi', $header_block, $hm ) ) { $headers[ $name ] = trim( $hm[1] ); } } } return array( 'headers' => $headers, 'body' => ltrim( $body, "\n" ), ); } /** * Sanitize header value. * * @param string $value * @return string */ private static function sanitize_header_value( string $value ): string { $value = (string) preg_replace( '/\s+/', ' ', $value ); return trim( $value ); } /** * Maps the create/update input keys to header field names. * * @return array */ public static function input_to_header_map(): array { return array( 'title' => 'Title', 'slug_full' => 'Slug', 'description' => 'Description', 'viewport_width' => 'Viewport Width', 'inserter' => 'Inserter', 'categories' => 'Categories', 'keywords' => 'Keywords', 'block_types' => 'Block Types', 'post_types' => 'Post Types', 'template_types' => 'Template Types', ); } /** * Header fields. * * @return array */ public static function header_fields(): array { return self::HEADERS; } }