-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonemeta.php
More file actions
280 lines (240 loc) · 6.94 KB
/
onemeta.php
File metadata and controls
280 lines (240 loc) · 6.94 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Plugin Name: OneMeta - Custom Meta Fields
* Plugin URI: https://fronttheme.com/products/onemeta
* Description: Build powerful custom fields with a visual builder. Export as PHP code or use directly in WordPress.
* Version: 1.0.0
* Author: Faruk Ahmed
* Author URI: https://fronttheme.com
* Text Domain: onemeta
* Domain Path: /languages
* Requires at least: 6.8
* Requires PHP: 8.2
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package OneMeta
*/
use OneMeta\Admin\Admin;
use OneMeta\API\RestAPI;
use OneMeta\Core\Engine;
use OneMeta\Core\Renderer;
use OneMeta\Core\Sanitizer;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Define plugin constants
define( 'ONEMETA_VERSION', '1.0.0' );
define( 'ONEMETA_PLUGIN_FILE', __FILE__ );
define( 'ONEMETA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'ONEMETA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ONEMETA_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/**
* PSR-4 Autoloader for OneMeta
*/
spl_autoload_register( function ( $class ) {
$prefix = 'OneMeta\\';
$base_dir = ONEMETA_PLUGIN_DIR . 'includes/';
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
return;
}
$relative_class = substr( $class, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
if ( file_exists( $file ) ) {
require $file;
}
} );
/**
* Main OneMeta Plugin Class
*/
final class OneMeta {
/**
* Plugin instance
*
* @var OneMeta|null
*/
private static ?OneMeta $instance = null;
/**
* Get plugin instance
*
* @return OneMeta|null
*/
public static function instance(): ?OneMeta {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->init_hooks();
}
/**
* Initialize hooks
*/
private function init_hooks(): void {
register_activation_hook( ONEMETA_PLUGIN_FILE, [ $this, 'activate' ] );
register_deactivation_hook( ONEMETA_PLUGIN_FILE, [ $this, 'deactivate' ] );
add_action( 'plugins_loaded', [ $this, 'init' ] );
add_action( 'init', [ $this, 'load_textdomain' ] );
}
/**
* Plugin activation
*/
public function activate(): void {
// Create database tables if needed
$this->create_tables();
// Set default options
if ( ! get_option( 'onemeta_version' ) ) {
update_option( 'onemeta_version', ONEMETA_VERSION );
}
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation
*/
public function deactivate(): void {
flush_rewrite_rules();
}
/**
* Create database tables
*/
private function create_tables(): void {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'onemeta_field_groups';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
group_key varchar(100) NOT NULL,
title varchar(255) NOT NULL,
config longtext NOT NULL,
status varchar(20) DEFAULT 'active',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY group_key (group_key)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Initialize plugin
*/
public function init(): void {
// Initialize core components
Renderer::instance();
Sanitizer::instance();
Engine::instance();
Admin::instance();
RestAPI::instance();
}
/**
* Load plugin text domain
*/
public function load_textdomain(): void {
load_plugin_textdomain(
'onemeta',
false,
dirname( ONEMETA_PLUGIN_BASENAME ) . '/languages'
);
}
}
/**
* Initialize OneMeta
*/
function onemeta(): ?OneMeta {
return OneMeta::instance();
}
onemeta();
/**
* Helper function to get post meta value
*
* @param int $post_id Post ID
* @param string $key Meta key (without onemeta_ prefix)
* @param mixed $default Default value if meta doesn't exist
*
* @return mixed Meta value or default
*/
function onemeta_get_meta( int $post_id, string $key, mixed $default = '' ): mixed {
$meta_key = 'onemeta_' . $key;
if ( ! metadata_exists( 'post', $post_id, $meta_key ) ) {
return $default;
}
$value = get_post_meta( $post_id, $meta_key, true );
if ( $value === '' || $value === null ) {
return $default;
}
// Resolve return format for image/gallery/file fields
return \OneMeta\Core\FieldValueResolver::instance()->resolve( $value, $key, 'post' );
}
/**
* Helper function to get user meta value
*
* @param int $user_id User ID
* @param string $key Meta key (without onemeta_ prefix)
* @param mixed $default Default value if meta doesn't exist
*
* @return mixed Meta value or default
*/
function onemeta_get_user_meta( int $user_id, string $key, mixed $default = '' ): mixed {
$meta_key = 'onemeta_' . $key;
if ( ! metadata_exists( 'user', $user_id, $meta_key ) ) {
return $default;
}
$value = get_user_meta( $user_id, $meta_key, true );
if ( $value === '' || $value === null ) {
return $default;
}
// Resolve return format for image/gallery/file fields
return \OneMeta\Core\FieldValueResolver::instance()->resolve( $value, $key, 'user' );
}
/**
* Helper function to update post meta value
*
* @param int $post_id Post ID
* @param string $key Meta key (without onemeta_ prefix)
* @param mixed $value Value to update
*
* @return int|bool Meta ID on success, false on failure
*/
function onemeta_update_meta( int $post_id, string $key, mixed $value ): bool|int {
return update_post_meta( $post_id, 'onemeta_' . $key, $value );
}
/**
* Helper function to update user meta value
*
* @param int $user_id User ID
* @param string $key Meta key (without onemeta_ prefix)
* @param mixed $value Value to update
*
* @return int|bool Meta ID on success, false on failure
*/
function onemeta_update_user_meta( int $user_id, string $key, mixed $value ): bool|int {
return update_user_meta( $user_id, 'onemeta_' . $key, $value );
}
/**
* Helper function to delete post meta value
*
* @param int $post_id Post ID
* @param string $key Meta key (without onemeta_ prefix)
*
* @return bool True on success, false on failure
*/
function onemeta_delete_meta( int $post_id, string $key ): bool {
return delete_post_meta( $post_id, 'onemeta_' . $key );
}
/**
* Helper function to delete user meta value
*
* @param int $user_id User ID
* @param string $key Meta key (without onemeta_ prefix)
*
* @return bool True on success, false on failure
*/
function onemeta_delete_user_meta( int $user_id, string $key ): bool {
return delete_user_meta( $user_id, 'onemeta_' . $key );
}