-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgrid.php
More file actions
359 lines (303 loc) · 9.24 KB
/
postgrid.php
File metadata and controls
359 lines (303 loc) · 9.24 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Plugin Name: PostGrid
* Plugin URI: https://github.com/richardbaxterseo/postgrid-wp
* Description: A lightweight posts grid block for WordPress
* Author: Richard Baxter
* Version: 0.1.15
* Author URI: https://richardbaxter.co/
* Text Domain: postgrid
* Domain Path: /languages
* Requires at least: 6.0
* Requires PHP: 7.4
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Prevent direct access with proper security headers
if ( ! defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit( 'Direct access to this file is not allowed. Please access through WordPress.' );
}
// Define plugin constants
define( 'POSTGRID_VERSION', '0.1.15' );
define( 'POSTGRID_PLUGIN_FILE', __FILE__ );
define( 'POSTGRID_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'POSTGRID_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// Check if Composer autoloader exists, fall back to custom autoloader
if ( file_exists( POSTGRID_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
require_once POSTGRID_PLUGIN_DIR . 'vendor/autoload.php';
} else {
// Fallback autoloader for development or when Composer is not available
spl_autoload_register( function ( $class ) {
$prefix = 'PostGrid\\';
$base_dir = POSTGRID_PLUGIN_DIR . 'includes/';
// Does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
return;
}
// Get the relative class name
$relative_class = substr( $class, $len );
// Handle nested namespaces properly
$parts = explode( '\\', $relative_class );
if ( count( $parts ) === 1 ) {
// Top-level class (e.g., PostGrid\Plugin)
$file = $base_dir . 'class-' . strtolower( str_replace( '_', '-', $parts[0] ) ) . '.php';
} else {
// Nested namespace (e.g., PostGrid\Core\AssetManager)
$class_name = array_pop( $parts );
$namespace_path = implode( '/', $parts );
// Convert CamelCase to kebab-case (AssetManager -> asset-manager)
$file_name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $class_name ) );
$file = $base_dir . $namespace_path . '/class-' . $file_name . '.php';
}
// If the mapped file exists, require it
if ( file_exists( $file ) ) {
require_once $file;
}
} );
}
// Initialize the plugin with proper error handling
add_action( 'plugins_loaded', function() {
try {
// Check minimum requirements first
if ( ! postgrid_check_requirements() ) {
return;
}
// Check for required build files before initialization
if ( ! postgrid_check_build_files() ) {
return;
}
// Initialize main plugin instance
PostGrid\Plugin::get_instance();
} catch ( Exception $e ) {
// Log the error
error_log( 'PostGrid: Failed to initialize plugin - ' . $e->getMessage() );
// Show admin notice
add_action( 'admin_notices', function() use ( $e ) {
?>
<div class="notice notice-error is-dismissible">
<p>
<strong><?php esc_html_e( 'PostGrid Error:', 'postgrid' ); ?></strong>
<?php echo esc_html( $e->getMessage() ); ?>
</p>
</div>
<?php
} );
// Prevent plugin execution
return;
}
}, 5 );
// Register activation hook
register_activation_hook( __FILE__, 'postgrid_activation' );
// Register deactivation hook
register_deactivation_hook( __FILE__, 'postgrid_deactivation' );
/**
* Check plugin requirements
*
* @return bool
*/
function postgrid_check_requirements() {
$errors = array();
// Check WordPress version
if ( version_compare( get_bloginfo( 'version' ), '6.0', '<' ) ) {
$errors[] = __( 'PostGrid requires WordPress 6.0 or higher.', 'postgrid' );
}
// Check PHP version
if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
$errors[] = __( 'PostGrid requires PHP 7.4 or higher.', 'postgrid' );
}
// Check if block editor is available
if ( ! function_exists( 'register_block_type' ) ) {
$errors[] = __( 'PostGrid requires the WordPress block editor.', 'postgrid' );
}
// If there are errors, display them
if ( ! empty( $errors ) ) {
add_action( 'admin_notices', function() use ( $errors ) {
?>
<div class="notice notice-error">
<p><strong><?php esc_html_e( 'PostGrid cannot be activated:', 'postgrid' ); ?></strong></p>
<ul>
<?php foreach ( $errors as $error ) : ?>
<li><?php echo esc_html( $error ); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
} );
return false;
}
return true;
}
/**
* Check if required build files exist
*
* @return bool
*/
function postgrid_check_build_files() {
$required_files = array(
'build/index.js',
'build/index.asset.php',
);
$missing_files = array();
foreach ( $required_files as $file ) {
if ( ! file_exists( POSTGRID_PLUGIN_DIR . $file ) ) {
$missing_files[] = $file;
}
}
if ( ! empty( $missing_files ) ) {
add_action( 'admin_notices', function() use ( $missing_files ) {
?>
<div class="notice notice-error is-dismissible">
<p><strong><?php esc_html_e( 'PostGrid Build Error:', 'postgrid' ); ?></strong></p>
<p><?php esc_html_e( 'The following required files are missing:', 'postgrid' ); ?></p>
<ul>
<?php foreach ( $missing_files as $file ) : ?>
<li><code><?php echo esc_html( $file ); ?></code></li>
<?php endforeach; ?>
</ul>
<p><?php esc_html_e( 'Please run: npm install && npm run build', 'postgrid' ); ?></p>
</div>
<?php
} );
// Deactivate the plugin to prevent errors
deactivate_plugins( plugin_basename( __FILE__ ) );
return false;
}
return true;
}
/**
* Plugin activation
*/
function postgrid_activation() {
// Check requirements before activation
if ( ! postgrid_check_requirements() ) {
// Deactivate immediately
deactivate_plugins( plugin_basename( __FILE__ ) );
// Redirect to plugins page with error
wp_die(
esc_html__( 'PostGrid cannot be activated. Please check the requirements.', 'postgrid' ),
esc_html__( 'Plugin Activation Error', 'postgrid' ),
array( 'back_link' => true )
);
}
// Check build files
if ( ! postgrid_check_build_files() ) {
// Log warning but allow activation for development
error_log( 'PostGrid: Build directory not found. Please run npm install && npm run build' );
}
// Create database tables if needed (for future use)
postgrid_create_tables();
// Set default options
postgrid_set_default_options();
// Flush rewrite rules
flush_rewrite_rules();
// Schedule cron events
if ( ! wp_next_scheduled( 'postgrid_daily_cleanup' ) ) {
wp_schedule_event( time(), 'daily', 'postgrid_daily_cleanup' );
}
// Set activation flag
set_transient( 'postgrid_activated', true, 5 );
}
/**
* Plugin deactivation
*/
function postgrid_deactivation() {
// Clear scheduled events
wp_clear_scheduled_hook( 'postgrid_daily_cleanup' );
// Flush cache if cache manager is available
if ( class_exists( 'PostGrid\\Core\\CacheManager' ) ) {
try {
$cache = new PostGrid\Core\CacheManager();
$cache->flush();
} catch ( Exception $e ) {
error_log( 'PostGrid: Failed to flush cache on deactivation - ' . $e->getMessage() );
}
}
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Create database tables
*/
function postgrid_create_tables() {
// Reserved for future use
// Example implementation:
/*
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}postgrid_cache (
id bigint(20) NOT NULL AUTO_INCREMENT,
cache_key varchar(255) NOT NULL,
cache_value longtext NOT NULL,
expiration datetime NOT NULL,
PRIMARY KEY (id),
KEY cache_key (cache_key),
KEY expiration (expiration)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
*/
}
/**
* Set default options
*/
function postgrid_set_default_options() {
$defaults = array(
'cache_expiration' => 300,
'enable_rest_api' => true,
'supported_post_types' => array( 'post' ),
'rate_limit' => 60,
);
foreach ( $defaults as $option => $value ) {
if ( false === get_option( 'postgrid_' . $option ) ) {
add_option( 'postgrid_' . $option, $value );
}
}
}
/**
* Get plugin instance
*
* @deprecated Use PostGrid\Plugin::get_instance() directly
* @return PostGrid\Plugin|null
*/
function postgrid() {
_deprecated_function( __FUNCTION__, '0.2.0', 'PostGrid\\Plugin::get_instance()' );
return PostGrid\Plugin::get_instance();
}
/**
* Daily cleanup cron job
*/
add_action( 'postgrid_daily_cleanup', function() {
// Clean up old transients
global $wpdb;
$expired = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_name FROM {$wpdb->options}
WHERE option_name LIKE %s
AND option_value < %d",
$wpdb->esc_like( '_transient_timeout_postgrid_' ) . '%',
time()
)
);
foreach ( $expired as $transient ) {
$key = str_replace( '_transient_timeout_', '', $transient );
delete_transient( $key );
}
// Allow other cleanup tasks
do_action( 'postgrid_daily_cleanup' );
} );
/**
* Handle plugin activation redirect
*/
add_action( 'admin_init', function() {
if ( get_transient( 'postgrid_activated' ) ) {
delete_transient( 'postgrid_activated' );
// Only redirect if not bulk activation and settings page exists
if ( ! isset( $_GET['activate-multi'] ) && menu_page_url( 'postgrid-settings', false ) ) {
wp_safe_redirect( admin_url( 'options-general.php?page=postgrid-settings' ) );
exit;
}
}
} );