Guaranteed Save Test
';
$passed = true;
$start = microtime(true);
// STEP 1: Load classes
echo 'STEP 1: Loading plugin classes...
';
$plugin_dir = dirname(__FILE__) . '/';
try {
require_once $plugin_dir . 'includes/class-database.php';
echo '✓ class-database.php loaded
';
require_once $plugin_dir . 'includes/class-snippet.php';
echo '✓ class-snippet.php loaded
';
} catch (Exception $e) {
echo '✗ Failed to load classes: ' . $e->getMessage() . '
';
$passed = false;
}
// STEP 2: Create table
echo 'STEP 2: Creating database table...
';
try {
global $wpdb;
PC_HFAP_Database::create_tables();
$table = PC_HFAP_Database::get_table_name();
$exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
if ($table === $exists) {
echo '✓ Table exists: ' . $table . '
';
} else {
echo '✗ Table NOT created!
';
$passed = false;
}
} catch (Exception $e) {
echo '✗ Database error: ' . $e->getMessage() . '
';
$passed = false;
}
// STEP 3: Save snippet
echo 'STEP 3: Creating a test snippet...
';
$snippet_id = 0;
try {
$data = array(
'title' => 'GUARANTEED TEST ' . date('Y-m-d H:i:s'),
'location' => 'header',
'code' => ''
);
$snippet = new PC_HFAP_Snippet($data);
$result = $snippet->save();
if ($result) {
$snippet_id = $result;
echo '✓ Snippet SAVED! ID = ' . $result . '
';
} else {
echo '✗ Save FAILED! $result = false
';
echo 'WordPress error: ' . $wpdb->last_error . '
';
$passed = false;
}
} catch (Exception $e) {
echo '✗ Exception: ' . $e->getMessage() . '
';
$passed = false;
}
// STEP 4: Read it back
echo 'STEP 4: Reading snippet back...
';
if ($snippet_id > 0) {
try {
$retrieved = PC_HFAP_Snippet::get_by_id($snippet_id);
if ($retrieved && $retrieved->get_id()) {
echo '✓ Snippet retrieved!
';
echo '';
echo 'ID: ' . $retrieved->get_id() . "\n";
echo 'Title: ' . $retrieved->get_title() . "\n";
echo 'Location: ' . $retrieved->get_location() . "\n";
echo 'Code: ' . $retrieved->get_code() . "\n";
echo '';
} else {
echo '✗ Could not retrieve snippet
';
$passed = false;
}
} catch (Exception $e) {
echo '✗ Retrieve error: ' . $e->getMessage() . '
';
$passed = false;
}
}
// STEP 5: Delete it
echo 'STEP 5: Deleting test snippet...
';
if ($snippet_id > 0) {
try {
$snippet = PC_HFAP_Snippet::get_by_id($snippet_id);
if ($snippet) {
$delete_result = $snippet->delete();
if ($delete_result) {
echo '✓ Snippet deleted!
';
} else {
echo '✗ Delete failed
';
$passed = false;
}
}
} catch (Exception $e) {
echo '✗ Delete error: ' . $e->getMessage() . '
';
$passed = false;
}
}
// FINAL
$time = round((microtime(true) - $start) * 1000, 2);
echo '
';
echo 'RESULT
';
if ($passed) {
echo '✓✓✓ ALL STEPS PASSED! ✓✓✓
';
echo 'The plugin CAN save snippets to the database.
';
echo 'If the admin form is not working, the issue is with the form submission process, not the save function.
';
} else {
echo '✗ SOME STEPS FAILED
';
echo 'Please check the errors above.
';
}
echo 'Test completed in ' . $time . 'ms
';
echo 'Next Actions
';
echo '';
echo '