237 lines
8.9 KiB
PHP
237 lines
8.9 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: LCP Data Blocks
|
|
* Plugin URI: https://coosguide.com
|
|
* Description: A plugin for displaying various charts and graphs in Local Content Pro
|
|
* Version: 1.0.0
|
|
* Author: CoosGuide
|
|
* Author URI: https://coosguide.com
|
|
* License: GPL v2 or later
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
|
* Text Domain: lcp
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
// Include block files
|
|
require_once plugin_dir_path(__FILE__) . 'blocks/bar-graph/lcp-bar-graph.php';
|
|
require_once plugin_dir_path(__FILE__) . 'blocks/bar-graph/render.php';
|
|
|
|
/**
|
|
* Add custom category for LCP blocks
|
|
*/
|
|
function lcp_block_categories($categories) {
|
|
return array_merge(
|
|
$categories,
|
|
array(
|
|
array(
|
|
'slug' => 'lcp-blocks',
|
|
'title' => __('LCP Blocks', 'lcp'),
|
|
'icon' => 'chart-bar',
|
|
),
|
|
)
|
|
);
|
|
}
|
|
add_filter('block_categories_all', 'lcp_block_categories', 10, 1);
|
|
|
|
function create_block_lcp_data_blocks_block_init() {
|
|
register_block_type(
|
|
__DIR__ . '/blocks/bar-graph/build',
|
|
array(
|
|
'render_callback' => 'lcp_render_bar_graph_block'
|
|
)
|
|
);
|
|
}
|
|
add_action('init', 'create_block_lcp_data_blocks_block_init');
|
|
|
|
// Register the Data Collection Custom Post Type
|
|
function lcp_register_data_collection_post_type() {
|
|
$labels = array(
|
|
'name' => _x('Data Collections', 'Post type general name', 'lcp-data'),
|
|
'singular_name' => _x('Data Collection', 'Post type singular name', 'lcp-data'),
|
|
'menu_name' => _x('Data Collections', 'Admin Menu text', 'lcp-data'),
|
|
'name_admin_bar' => _x('Data Collection', 'Add New on Toolbar', 'lcp-data'),
|
|
'add_new' => __('Add New', 'lcp-data'),
|
|
'add_new_item' => __('Add New Data Collection', 'lcp-data'),
|
|
'new_item' => __('New Data Collection', 'lcp-data'),
|
|
'edit_item' => __('Edit Data Collection', 'lcp-data'),
|
|
'view_item' => __('View Data Collection', 'lcp-data'),
|
|
'all_items' => __('All Data Collections', 'lcp-data'),
|
|
'search_items' => __('Search Data Collections', 'lcp-data'),
|
|
'parent_item_colon' => __('Parent Data Collections:', 'lcp-data'),
|
|
'not_found' => __('No data collections found.', 'lcp-data'),
|
|
'not_found_in_trash' => __('No data collections found in Trash.', 'lcp-data'),
|
|
'featured_image' => _x('Data Collection Cover Image', 'Overrides the "Featured Image" phrase', 'lcp-data'),
|
|
'set_featured_image' => _x('Set cover image', 'Overrides the "Set featured image" phrase', 'lcp-data'),
|
|
'remove_featured_image'=> _x('Remove cover image', 'Overrides the "Remove featured image" phrase', 'lcp-data'),
|
|
'use_featured_image' => _x('Use as cover image', 'Overrides the "Use as featured image" phrase', 'lcp-data'),
|
|
'archives' => _x('Data Collection archives', 'The post type archive label used in nav menus', 'lcp-data'),
|
|
'insert_into_item' => _x('Insert into data collection', 'Overrides the "Insert into post" phrase', 'lcp-data'),
|
|
'uploaded_to_this_item'=> _x('Uploaded to this data collection', 'Overrides the "Uploaded to this post" phrase', 'lcp-data'),
|
|
'filter_items_list' => _x('Filter data collections list', 'Screen reader text for the filter links', 'lcp-data'),
|
|
'items_list_navigation'=> _x('Data Collections list navigation', 'Screen reader text for the pagination', 'lcp-data'),
|
|
'items_list' => _x('Data Collections list', 'Screen reader text for the items list', 'lcp-data'),
|
|
);
|
|
|
|
$args = array(
|
|
'labels' => $labels,
|
|
'public' => true,
|
|
'publicly_queryable' => true,
|
|
'show_ui' => true,
|
|
'show_in_menu' => true,
|
|
'query_var' => true,
|
|
'rewrite' => array('slug' => 'data-collection'),
|
|
'capability_type' => 'post',
|
|
'has_archive' => true,
|
|
'hierarchical' => false,
|
|
'menu_position' => null,
|
|
'menu_icon' => 'dashicons-chart-area',
|
|
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields'),
|
|
'show_in_rest' => true, // Enable Gutenberg editor
|
|
);
|
|
|
|
register_post_type('lcp-data-collection', $args);
|
|
}
|
|
add_action('init', 'lcp_register_data_collection_post_type');
|
|
|
|
// Register meta field for datasets
|
|
function lcp_register_meta_fields() {
|
|
register_post_meta('lcp-data-collection', 'lcp_datasets', array(
|
|
'show_in_rest' => array(
|
|
'schema' => array(
|
|
'type' => 'array',
|
|
'items' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'dataset_name' => array(
|
|
'type' => 'string'
|
|
),
|
|
'dataset_source' => array(
|
|
'type' => 'string'
|
|
),
|
|
'dataset_json' => array(
|
|
'type' => 'array'
|
|
)
|
|
)
|
|
)
|
|
)
|
|
),
|
|
'single' => true,
|
|
'type' => 'array',
|
|
'auth_callback' => function() {
|
|
return current_user_can('edit_posts');
|
|
}
|
|
));
|
|
}
|
|
add_action('init', 'lcp_register_meta_fields');
|
|
|
|
// Add meta box for datasets
|
|
function lcp_add_datasets_meta_box() {
|
|
add_meta_box(
|
|
'lcp_datasets_meta_box',
|
|
'Datasets',
|
|
'lcp_render_datasets_meta_box',
|
|
'lcp-data-collection',
|
|
'normal',
|
|
'high'
|
|
);
|
|
}
|
|
add_action('add_meta_boxes', 'lcp_add_datasets_meta_box');
|
|
|
|
// Render the meta box
|
|
function lcp_render_datasets_meta_box($post) {
|
|
// Get existing datasets
|
|
$datasets = get_post_meta($post->ID, 'lcp_datasets', true);
|
|
|
|
// Ensure it's an array
|
|
if (!is_array($datasets)) {
|
|
$datasets = array();
|
|
}
|
|
|
|
// Convert PHP array to JSON for the React component
|
|
$datasets_json = json_encode($datasets);
|
|
|
|
// Add nonce for verification
|
|
wp_nonce_field('lcp_datasets_meta_box', 'lcp_datasets_meta_box_nonce');
|
|
|
|
// Add hidden input for data
|
|
?>
|
|
<input type="hidden" id="lcp_datasets_data" name="lcp_datasets_data" value="<?php echo esc_attr($datasets_json); ?>" />
|
|
<div id="lcp-datasets-repeater" data-datasets="<?php echo esc_attr($datasets_json); ?>"></div>
|
|
<?php
|
|
}
|
|
|
|
// Save the meta box data
|
|
function lcp_save_datasets_meta($post_id) {
|
|
// Check if our nonce is set and verify it
|
|
if (!isset($_POST['lcp_datasets_meta_box_nonce']) ||
|
|
!wp_verify_nonce($_POST['lcp_datasets_meta_box_nonce'], 'lcp_datasets_meta_box')) {
|
|
return;
|
|
}
|
|
|
|
// If this is an autosave, don't do anything
|
|
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
|
return;
|
|
}
|
|
|
|
// Check the user's permissions
|
|
if (!current_user_can('edit_post', $post_id)) {
|
|
return;
|
|
}
|
|
|
|
// Save the data
|
|
if (isset($_POST['lcp_datasets_data'])) {
|
|
$datasets = json_decode(wp_unslash($_POST['lcp_datasets_data']), true);
|
|
if (is_array($datasets)) {
|
|
update_post_meta($post_id, 'lcp_datasets', $datasets);
|
|
}
|
|
}
|
|
}
|
|
add_action('save_post_lcp-data-collection', 'lcp_save_datasets_meta');
|
|
|
|
// Enqueue scripts and styles for the admin
|
|
function lcp_enqueue_admin_scripts($hook) {
|
|
global $post;
|
|
|
|
// Only enqueue on our custom post type edit screen
|
|
if ($hook == 'post-new.php' || $hook == 'post.php') {
|
|
if (is_object($post) && $post->post_type == 'lcp-data-collection') {
|
|
// Enqueue React and dependencies
|
|
wp_enqueue_script('wp-element');
|
|
wp_enqueue_script('wp-components');
|
|
wp_enqueue_script('wp-data');
|
|
|
|
// Enqueue our custom scripts
|
|
wp_enqueue_script(
|
|
'lcp-form-repeater',
|
|
plugins_url('build/index.js', __FILE__),
|
|
array('wp-element', 'wp-components', 'wp-data'),
|
|
filemtime(plugin_dir_path(__FILE__) . 'build/index.js'),
|
|
true
|
|
);
|
|
|
|
// Pass data to JavaScript
|
|
wp_localize_script('lcp-form-repeater', 'lcpDataSettings', array(
|
|
'root' => esc_url_raw(rest_url()),
|
|
'nonce' => wp_create_nonce('wp_rest'),
|
|
'postId' => $post->ID
|
|
));
|
|
}
|
|
}
|
|
}
|
|
add_action('admin_enqueue_scripts', 'lcp_enqueue_admin_scripts');
|
|
|
|
// Flush rewrite rules on plugin activation
|
|
function lcp_data_activate() {
|
|
lcp_register_data_collection_post_type();
|
|
flush_rewrite_rules();
|
|
}
|
|
register_activation_hook(__FILE__, 'lcp_data_activate');
|
|
|
|
// Flush rewrite rules on plugin deactivation
|
|
function lcp_data_deactivate() {
|
|
flush_rewrite_rules();
|
|
}
|
|
register_deactivation_hook(__FILE__, 'lcp_data_deactivate');
|