|
<?php |
|
/* |
|
Plugin Name: CORS Shortcode Plugin |
|
Description: A plugin to display CORS data using a shortcode. |
|
Version: 1.2 |
|
Author: Your Name |
|
*/ |
|
|
|
// Enqueue jQuery |
|
function cors_enqueue_scripts() |
|
{ |
|
wp_enqueue_script('jquery'); |
|
} |
|
add_action('wp_enqueue_scripts', 'cors_enqueue_scripts'); |
|
|
|
// Register the shortcode |
|
function cors_shortcode($atts) |
|
{ |
|
// Extract shortcode attributes |
|
$atts = shortcode_atts( |
|
[ |
|
'base_url' => site_url(), // Default to the site's URL |
|
], |
|
$atts |
|
); |
|
|
|
$base_url = esc_url($atts['base_url']); |
|
|
|
ob_start(); |
|
?> |
|
<div id="cors-results"></div> |
|
<script> |
|
if (typeof jQuery !== 'undefined') { |
|
const $ = jQuery; |
|
jQuery(document).ready(function ($) { |
|
const $apiResults = $('<div><h3>REST API Responses</h3><table><thead><tr><th>Method</th><th>Response</th></tr></thead><tbody id="api-results-body"></tbody></table></div>'); |
|
const $ajaxResults = $('<div><h3>AJAX Responses</h3><table><thead><tr><th>Method</th><th>Response</th></tr></thead><tbody id="ajax-results-body"></tbody></table></div>'); |
|
|
|
$('#cors-results').append($apiResults, $ajaxResults); |
|
|
|
function appendMessage(tableId, method, message, type) { |
|
const color = type === 'success' ? 'green' : 'red'; |
|
$('#' + tableId).append(`<tr style="color:${color};"><td>${method}</td><td>${message}</td></tr>`); |
|
} |
|
|
|
const apiUrl = '<?php echo $base_url ?>' + '/wp-json/enable_cors/v1/currenttime'; |
|
const ajaxUrl = '<?php echo $base_url ?>' + '/wp-admin/admin-ajax.php'; |
|
|
|
// Function to make API Calls |
|
function makeApiCall(method, data = {}) { |
|
$.ajax({ |
|
url: apiUrl, |
|
type: method, |
|
data: data, |
|
success: function (data) { |
|
appendMessage('api-results-body', method, data.message || data, 'success'); |
|
}, |
|
error: function () { |
|
appendMessage('api-results-body', method, 'Failed', 'error'); |
|
} |
|
}); |
|
} |
|
|
|
// Function to make AJAX Calls |
|
function makeAjaxCall(method, data = {}) { |
|
$.ajax({ |
|
url: ajaxUrl, |
|
type: method, |
|
data: { action: 'enable_cors_ajax_action', ...data }, |
|
success: function (data) { |
|
appendMessage('ajax-results-body', method, data.message || data, 'success'); |
|
}, |
|
error: function () { |
|
appendMessage('ajax-results-body', method, 'Failed', 'error'); |
|
} |
|
}); |
|
} |
|
|
|
// REST API Calls |
|
makeApiCall('GET'); |
|
makeApiCall('POST', { data: 'Hello from API POST!' }); |
|
makeApiCall('PUT', { data: 'Hello from API PUT!' }); |
|
makeApiCall('DELETE', { data: 'Hello from API DELETE!' }); |
|
makeApiCall('PATCH', { data: 'Hello from API PATCH!' }); |
|
|
|
// AJAX Calls |
|
makeAjaxCall('GET'); |
|
makeAjaxCall('POST', { data: 'Hello from AJAX POST!' }); |
|
}); |
|
|
|
} |
|
</script> |
|
<?php |
|
return ob_get_clean(); |
|
} |
|
add_shortcode('cors_display', 'cors_shortcode'); |
|
|
|
// Create a page when the plugin is activated |
|
function cors_plugin_activate() |
|
{ |
|
$page_title = 'CORS API Page'; |
|
$page_content = '[cors_display base_url="' . esc_url(site_url()) . '"]'; |
|
$page_slug = 'cors-api-page'; |
|
|
|
// Check if the page already exists |
|
$existing_page = get_page_by_path($page_slug); |
|
if (!$existing_page) { |
|
// Create the page |
|
wp_insert_post([ |
|
'post_title' => $page_title, |
|
'post_content' => $page_content, |
|
'post_status' => 'publish', |
|
'post_type' => 'page', |
|
'post_name' => $page_slug, |
|
]); |
|
} |
|
} |
|
register_activation_hook(__FILE__, 'cors_plugin_activate'); |
|
|
|
// Delete the page when the plugin is uninstalled |
|
function cors_plugin_uninstall() |
|
{ |
|
$page_slug = 'cors-api-page'; |
|
|
|
// Get the page by slug |
|
$page = get_page_by_path($page_slug); |
|
if ($page) { |
|
// Delete the page |
|
wp_delete_post($page->ID, true); |
|
} |
|
} |
|
register_deactivation_hook(__FILE__, 'cors_plugin_uninstall'); |