$plugin_dir, 'active' => $active, 'plugin_file' => $plugin_file, ); } /** * @return array */ public static function scan_plugins_with_theme_json(): array { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $found = array(); $seen = array(); foreach ( array_keys( get_plugins() ) as $rel ) { $parts = explode( '/', $rel ); $slug = $parts[0] ?? ''; if ( '' === $slug || isset( $seen[ $slug ] ) ) { continue; } $seen[ $slug ] = true; $json = WP_PLUGIN_DIR . '/' . $slug . '/theme.json'; if ( ! is_file( $json ) ) { continue; } $found[] = array( 'slug' => $slug, 'path' => $json, 'active' => is_plugin_active( $rel ), ); } return $found; } // ------------------------------------------------------------------------- // theme.json file paths // ------------------------------------------------------------------------- /** * Theme json path. * * @param string $container_dir * @return string */ public static function theme_json_path( string $container_dir ): string { return rtrim( $container_dir, '/' ) . '/theme.json'; } // ------------------------------------------------------------------------- // Read / write // ------------------------------------------------------------------------- /** * @return string|\WP_Error */ public static function read_file( string $abs_path ) { if ( ! is_file( $abs_path ) ) { return new \WP_Error( 'file_not_found', __( 'theme.json file not found.', 'acrossai-abilities-manager' ) ); } $contents = file_get_contents( $abs_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( false === $contents ) { return new \WP_Error( 'read_failed', __( 'Could not read theme.json file.', 'acrossai-abilities-manager' ) ); } return $contents; } /** * @return array|\WP_Error */ public static function read_json( string $abs_path ) { $raw = self::read_file( $abs_path ); if ( is_wp_error( $raw ) ) { return $raw; } if ( '' === trim( $raw ) ) { return array(); } $decoded = json_decode( $raw, true ); if ( JSON_ERROR_NONE !== json_last_error() ) { return new \WP_Error( 'invalid_json', /* translators: 1: path, 2: parse error */ sprintf( __( 'Invalid JSON in %1$s: %2$s', 'acrossai-abilities-manager' ), $abs_path, json_last_error_msg() ) ); } return is_array( $decoded ) ? $decoded : array(); } /** * @return int|\WP_Error Number of bytes written. */ public static function write_file( string $abs_path, string $content ) { $guard = File_Mods_Guard::check(); if ( is_wp_error( $guard ) ) { return $guard; } $dir = dirname( $abs_path ); if ( ! is_dir( $dir ) ) { if ( ! wp_mkdir_p( $dir ) ) { return new \WP_Error( 'mkdir_failed', __( 'Could not create directory for theme.json.', 'acrossai-abilities-manager' ) ); } } if ( file_exists( $abs_path ) && ! is_writable( $abs_path ) ) { return new \WP_Error( 'file_not_writable', /* translators: %s: absolute file path */ sprintf( __( 'theme.json is read-only: %s. Check file permissions or save to the database instead.', 'acrossai-abilities-manager' ), $abs_path ) ); } if ( ! file_exists( $abs_path ) && ! is_writable( $dir ) ) { return new \WP_Error( 'dir_not_writable', /* translators: %s: directory path */ sprintf( __( 'Theme/plugin directory is not writable: %s.', 'acrossai-abilities-manager' ), $dir ) ); } $bytes = file_put_contents( $abs_path, $content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents if ( false === $bytes ) { return new \WP_Error( 'write_failed', __( 'Could not write theme.json file.', 'acrossai-abilities-manager' ) ); } return (int) $bytes; } /** * @return int|\WP_Error */ public static function write_json( string $abs_path, array $data ) { $json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( false === $json ) { return new \WP_Error( 'json_encode_failed', __( 'Could not encode theme.json data.', 'acrossai-abilities-manager' ) ); } return self::write_file( $abs_path, (string) $json ); } /** * @return true|\WP_Error */ public static function delete_file( string $abs_path ) { $guard = File_Mods_Guard::check(); if ( is_wp_error( $guard ) ) { return $guard; } if ( ! file_exists( $abs_path ) ) { return new \WP_Error( 'file_not_found', __( 'theme.json file not found.', 'acrossai-abilities-manager' ) ); } if ( ! is_writable( $abs_path ) ) { return new \WP_Error( 'file_not_writable', /* translators: %s: absolute file path */ sprintf( __( 'Cannot delete theme.json (permission denied): %s.', 'acrossai-abilities-manager' ), $abs_path ) ); } if ( ! @unlink( $abs_path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink, WordPress.PHP.NoSilencedErrors.Discouraged return new \WP_Error( 'unlink_failed', __( 'Could not delete theme.json file.', 'acrossai-abilities-manager' ) ); } return true; } }