-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcover3d.php
More file actions
123 lines (108 loc) · 4.12 KB
/
Copy pathcover3d.php
File metadata and controls
123 lines (108 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Cover3D
*
* @package Cover3D
* @author Márcio Duarte
* @copyright 2025 Épico Studio
* @license GPL v2 or later
*
* Plugin Name: Cover3D
* Plugin URI: https://github.com/EpicoStudio/cover3d
* Description: A block that displays a 3D book cover, animated when you mouse over it.
* Version: 1.0.0
* Requires at least: 6.1
* Requires PHP: 7.4
* Tested up to: 6.9
* Author: Márcio Duarte
* Author URI: https://epico.studio
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: cover3d
* Domain Path: /languages
*/
// Prevent direct access to the plugin file.
defined( 'ABSPATH' ) || exit;
// Define constants.
if ( ! defined( 'COVER3D_URL' ) ) {
define( 'COVER3D_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
}
if ( ! defined( 'COVER3D_PATH' ) ) {
define( 'COVER3D_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
}
/**
* Registers the Cover3D block using the block.json metadata.
*
* Uses wp_register_block_types_from_metadata_collection() for WordPress 6.7+
* with fallback to wp_register_block_metadata_collection() for older versions.
*
* @link https://developer.wordpress.org/reference/functions/register_block_type/
* @link https://developer.wordpress.org/reference/functions/wp_register_block_metadata_collection/
* @link https://developer.wordpress.org/reference/functions/wp_register_block_types_from_metadata_collection/
*
* @return void
*/
function cover3d_register_block() {
if ( ! function_exists( 'register_block_type' ) ) {
// The block editor is not available.
return;
}
// WordPress 6.7+ has the most efficient registration method.
if ( function_exists( 'wp_register_block_types_from_metadata_collection' ) ) {
wp_register_block_types_from_metadata_collection( COVER3D_PATH . '/build', COVER3D_PATH . '/build/blocks-manifest.php' );
} else {
// Fallback for WordPress 6.4-6.6.
if ( function_exists( 'wp_register_block_metadata_collection' ) ) {
wp_register_block_metadata_collection( COVER3D_PATH . '/build', COVER3D_PATH . '/build/blocks-manifest.php' );
}
// Iterate over blocks in the manifest and register each one.
$cover3d_manifest_data = require COVER3D_PATH . '/build/blocks-manifest.php';
foreach ( array_keys( $cover3d_manifest_data ) as $cover3d_block_type ) {
register_block_type( COVER3D_PATH . "/build/{$cover3d_block_type}" );
}
}
// Load available translations for the editor script.
wp_set_script_translations( 'cover3d-book-editor-script', 'cover3d', COVER3D_PATH . '/languages' );
}
add_action( 'init', 'cover3d_register_block' );
/**
* Sanitizes a color value.
*
* Accepts hex colors with or without hash, rgb(), rgba(), hsl(), hsla(), and named colors.
*
* @param string $color The color value to sanitize.
* @return string The sanitized color value.
*/
function cover3d_sanitize_color( $color ) {
// Return empty string for empty values.
if ( empty( $color ) ) {
return '';
}
// Trim whitespace.
$color = trim( $color );
// Check for hex color (with or without hash, 3, 4, 6, or 8 digits).
if ( preg_match( '/^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{3})$/', $color ) ) {
// Add hash if missing.
return '#' . ltrim( $color, '#' );
}
// Check for rgb/rgba colors.
if ( preg_match( '/^rgba?\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(0|1|0?\.\d+))?\s*\)$/i', $color ) ) {
return $color;
}
// Check for hsl/hsla colors.
if ( preg_match( '/^hsla?\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*(,\s*(0|1|0?\.\d+))?\s*\)$/i', $color ) ) {
return $color;
}
// Check for valid named colors.
$named_colors = array(
'transparent', 'currentcolor', 'inherit',
'black', 'white', 'red', 'green', 'blue', 'yellow', 'orange',
'purple', 'pink', 'gray', 'grey', 'brown', 'cyan', 'magenta', 'lime', 'navy',
'teal', 'olive', 'maroon', 'aqua', 'silver', 'fuchsia',
);
if ( in_array( strtolower( $color ), $named_colors, true ) ) {
return strtolower( $color );
}
// Return a safe default if nothing matches.
return '#ffffff';
}