post_type && current_user_can( 'edit_post', $post->ID ) ) { $actions['bpmgr_duplicate'] = sprintf( '%2$s', absint( $post->ID ), esc_html__( 'Duplicate', 'onceblock' ) ); } return $actions; } /** * Load and validate the preset targeted by an AJAX request * * Verifies the nonce, the post type and the current user's capability to edit it. * Sends a JSON error and exits when any check fails. * * @return WP_Post */ private function get_ajax_preset() { check_ajax_referer( 'bpmgr_admin_nonce', 'nonce' ); $block_id = isset( $_POST['block_id'] ) ? absint( wp_unslash( $_POST['block_id'] ) ) : 0; $preset = $block_id ? get_post( $block_id ) : null; if ( ! $preset || 'bpmgr_block_preset' !== $preset->post_type ) { wp_send_json_error( __( 'Block preset not found.', 'onceblock' ), 404 ); } if ( ! current_user_can( 'edit_post', $preset->ID ) ) { wp_send_json_error( __( 'You are not allowed to do that.', 'onceblock' ), 403 ); } return $preset; } /** * AJAX: duplicate a block preset as a draft copy */ public function ajax_duplicate_block() { $preset = $this->get_ajax_preset(); $post_type = get_post_type_object( 'bpmgr_block_preset' ); if ( ! $post_type || ! current_user_can( $post_type->cap->create_posts ) ) { wp_send_json_error( __( 'You are not allowed to do that.', 'onceblock' ), 403 ); } $new_id = wp_insert_post( wp_slash( array( 'post_type' => 'bpmgr_block_preset', /* translators: %s: Title of the original block preset. */ 'post_title' => sprintf( __( '%s (Copy)', 'onceblock' ), $preset->post_title ), 'post_content' => $preset->post_content, 'post_excerpt' => $preset->post_excerpt, 'post_status' => 'draft', 'post_author' => get_current_user_id(), ) ), true ); if ( is_wp_error( $new_id ) || ! $new_id ) { wp_send_json_error( __( 'Duplication failed.', 'onceblock' ), 500 ); } // Copy taxonomy terms. foreach ( get_object_taxonomies( 'bpmgr_block_preset' ) as $taxonomy ) { $term_ids = wp_get_object_terms( $preset->ID, $taxonomy, array( 'fields' => 'ids' ) ); if ( ! is_wp_error( $term_ids ) && ! empty( $term_ids ) ) { wp_set_object_terms( $new_id, $term_ids, $taxonomy ); } } // Copy custom fields, skipping WordPress-internal and migration bookkeeping keys. foreach ( get_post_meta( $preset->ID ) as $meta_key => $meta_values ) { if ( 0 === strpos( $meta_key, '_edit_' ) || 0 === strpos( $meta_key, '_wp_' ) || '_bpmgr_migrated_from' === $meta_key ) { continue; } foreach ( $meta_values as $meta_value ) { add_post_meta( $new_id, $meta_key, wp_slash( maybe_unserialize( $meta_value ) ) ); } } wp_send_json_success( array( 'new_id' => $new_id ) ); } /** * AJAX: render a block preset for the admin preview container */ public function ajax_preview_block() { $preset = $this->get_ajax_preset(); // Render through the core 'the_content' filter so blocks and shortcodes behave as in normal content. $content = apply_filters( 'the_content', $preset->post_content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- core filter, not a plugin hook. wp_send_json_success( array( 'content' => wp_kses_post( $content ) ) ); } /** * Add admin menus */ public function add_admin_menus() { // Settings submenu under Block Presets (main location) add_submenu_page( 'edit.php?post_type=bpmgr_block_preset', __( 'Block Presets Settings', 'onceblock' ), __( 'Settings', 'onceblock' ), 'manage_options', 'bpmgr-settings', array( $this, 'settings_page' ) ); // Tools submenu under Block Presets add_submenu_page( 'edit.php?post_type=bpmgr_block_preset', __( 'Block Presets Tools', 'onceblock' ), __( 'Tools', 'onceblock' ), 'manage_options', 'bpmgr-tools', array( $this, 'tools_page' ) ); // Usage Statistics submenu add_submenu_page( 'edit.php?post_type=bpmgr_block_preset', __( 'Usage Statistics', 'onceblock' ), __( 'Statistics', 'onceblock' ), 'manage_options', 'bpmgr-statistics', array( $this, 'statistics_page' ) ); // Also add to Settings menu for discoverability add_options_page( __( 'OnceBlock Settings', 'onceblock' ), __( 'Blocks Presets', 'onceblock' ), 'manage_options', 'onceblock', array( $this, 'settings_page' ) ); } /** * Handle the Tools page "Export Block Presets" action, if requested * * Sends the export as a downloadable JSON file, so it must run before WordPress * outputs any admin page HTML - which has already started by the time the Tools * page's own render callback runs. Hooking this to 'admin_init' instead (which * fires before that output begins) is what makes a clean file download possible. */ public function maybe_export_presets() { if ( ! isset( $_POST['bpmgr_action'], $_POST['bpmgr_nonce'] ) || 'export_presets' !== $_POST['bpmgr_action'] || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bpmgr_nonce'] ) ), 'bpmgr_tools_action' ) || ! current_user_can( 'manage_options' ) ) { return; } $export_data = bpmgr_export_presets(); if ( ! $export_data ) { set_transient( 'bpmgr_export_error', 1, 30 ); return; } $filename = 'bpmgr-block-presets-' . gmdate( 'Y-m-d-H-i-s' ) . '.json'; nocache_headers(); header( 'Content-Type: application/json' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); echo wp_json_encode( $export_data, JSON_PRETTY_PRINT ); exit; } /** * Initialize admin settings */ public function admin_init() { register_setting( 'bpmgr_settings_group', 'bpmgr_settings', array( 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); // General Settings Section add_settings_section( 'bpmgr_general_settings', __( 'General Settings', 'onceblock' ), array( $this, 'general_settings_callback' ), 'onceblock' ); add_settings_field( 'enable_acf_blocks', __( 'Enable ACF Blocks (Optional)', 'onceblock' ), array( $this, 'checkbox_field_callback' ), 'onceblock', 'bpmgr_general_settings', array( 'field' => 'enable_acf_blocks', 'description' => __( 'Enable automatic ACF blocks registration from theme folders. Requires Advanced Custom Fields Pro plugin.', 'onceblock' ), ) ); add_settings_field( 'enable_block_presets', __( 'Enable Block Presets', 'onceblock' ), array( $this, 'checkbox_field_callback' ), 'onceblock', 'bpmgr_general_settings', array( 'field' => 'enable_block_presets', 'description' => __( 'Enable the Block Presets custom post type.', 'onceblock' ), ) ); add_settings_field( 'enable_debug_logging', __( 'Enable Test Log Mode', 'onceblock' ), array( $this, 'checkbox_field_callback' ), 'onceblock', 'bpmgr_general_settings', array( 'field' => 'enable_debug_logging', 'description' => __( 'Write diagnostic logs to debug.log for troubleshooting. Disable in production.', 'onceblock' ), ) ); // Block Folders Section add_settings_section( 'bpmgr_folder_settings', __( 'Block Folders', 'onceblock' ), array( $this, 'folder_settings_callback' ), 'onceblock' ); add_settings_field( 'block_folders', __( 'ACF Block Folders', 'onceblock' ), array( $this, 'textarea_field_callback' ), 'onceblock', 'bpmgr_folder_settings', array( 'field' => 'block_folders', 'description' => __( 'Enter theme folder paths (relative to theme root) where ACF blocks are located. One per line.', 'onceblock' ), 'placeholder' => "general/acf-blocks\npublic/acf-blocks\nlogin-register/acf-blocks\nmembers/acf-blocks", ) ); } /** * Enqueue admin scripts and styles * * @param string $hook Current admin page hook suffix. */ public function admin_scripts( $hook ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $is_bpmgr_screen = ( $screen && isset( $screen->post_type ) && 'bpmgr_block_preset' === $screen->post_type ); if ( false !== strpos( $hook, 'onceblock' ) || false !== strpos( $hook, 'bpmgr-' ) || $is_bpmgr_screen ) { wp_enqueue_style( 'bpmgr-admin', BPMGR_PLUGIN_URL . 'admin/css/admin.css', array(), BPMGR_VERSION ); wp_enqueue_script( 'bpmgr-admin', BPMGR_PLUGIN_URL . 'admin/js/admin.js', array( 'jquery' ), BPMGR_VERSION, true ); wp_localize_script( 'bpmgr-admin', 'bpmgr_admin', array( 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), 'nonce' => wp_create_nonce( 'bpmgr_admin_nonce' ), 'strings' => array( 'confirm_delete' => __( 'Are you sure you want to delete this item?', 'onceblock' ), 'processing' => __( 'Processing...', 'onceblock' ), 'loading' => __( 'Loading...', 'onceblock' ), 'preview_failed' => __( 'Preview failed to load.', 'onceblock' ), 'confirm_duplicate' => __( 'Duplicate this block preset?', 'onceblock' ), 'invalid_path' => __( 'Invalid path detected. Paths cannot contain "..".', 'onceblock' ), 'duplication_failed' => __( 'Duplication failed.', 'onceblock' ), ), ) ); } } /** * Add meta boxes */ public function add_meta_boxes() { add_meta_box( 'bpmgr_block_info', __( 'Block Information', 'onceblock' ), array( $this, 'block_info_meta_box' ), 'bpmgr_block_preset', 'side', 'high' ); add_meta_box( 'bpmgr_block_usage', __( 'Usage Statistics', 'onceblock' ), array( $this, 'block_usage_meta_box' ), 'bpmgr_block_preset', 'side', 'default' ); add_meta_box( 'bpmgr_block_shortcode', __( 'Shortcode & Functions', 'onceblock' ), array( $this, 'block_shortcode_meta_box' ), 'bpmgr_block_preset', 'side', 'default' ); } /** * Block information meta box * * @param WP_Post $post Current post object. */ public function block_info_meta_box( $post ) { wp_nonce_field( 'bpmgr_meta_box_nonce', 'bpmgr_meta_box_nonce' ); $block_type = get_post_meta( $post->ID, '_bpmgr_block_type', true ); $block_description = get_post_meta( $post->ID, '_bpmgr_block_description', true ); ?>
' . esc_html( __( 'This block preset has not been used yet.', 'onceblock' ) ) . '
'; } } /** * Block shortcode meta box * * @param WP_Post $post Current post object. */ public function block_shortcode_meta_box( $post ) { ?> $title ) { $new_columns[ $key ] = $title; if ( 'title' === $key ) { $new_columns['bpmgr_usage'] = __( 'Usage Count', 'onceblock' ); $new_columns['bpmgr_shortcode'] = __( 'Shortcode', 'onceblock' ); } } return $new_columns; } /** * Render custom columns * * @param string $column Column key being rendered. * @param int $post_id Post ID for the current row. */ public function render_custom_columns( $column, $post_id ) { switch ( $column ) { case 'bpmgr_usage': global $wpdb; // Table name is built from $wpdb->prefix (not user input), so it is safe to interpolate; // $wpdb->prepare() does not support table/column identifiers as placeholders. $table_name = $wpdb->prefix . 'bpmgr_block_usage'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $table_name is $wpdb->prefix plus a literal, never user input. $total_usage = $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT SUM(usage_count) FROM $table_name WHERE block_id = %d", $post_id ) ); echo esc_html( $total_usage ? $total_usage : '0' ); break; case 'bpmgr_shortcode': echo '[bpmgr_block id="' . esc_attr( $post_id ) . '"]';
break;
}
}
/**
* Settings page
*/
public function settings_page() {
include BPMGR_PLUGIN_DIR . 'admin/settings-page.php';
}
/**
* Tools page
*/
public function tools_page() {
include BPMGR_PLUGIN_DIR . 'admin/tools-page.php';
}
/**
* Statistics page
*/
public function statistics_page() {
include BPMGR_PLUGIN_DIR . 'admin/statistics-page.php';
}
/**
* General settings section callback
*/
public function general_settings_callback() {
echo '' . esc_html( __( 'Configure general settings for the OnceBlock plugin. The plugin works with native WordPress functionality - ACF integration is optional.', 'onceblock' ) ) . '
'; } /** * Folder settings section callback */ public function folder_settings_callback() { echo '' . esc_html( __( 'Configure which theme folders should be scanned for ACF blocks.', 'onceblock' ) ) . '
'; } /** * Checkbox field callback * * @param array $args Field arguments: 'field' key name, 'description' help text. */ public function checkbox_field_callback( $args ) { $settings = get_option( 'bpmgr_settings', array() ); $value = isset( $settings[ $args['field'] ] ) ? $settings[ $args['field'] ] : false; echo ''; } /** * Textarea field callback * * @param array $args Field arguments: 'field' key name, 'description' help text, 'placeholder' text. */ public function textarea_field_callback( $args ) { $settings = get_option( 'bpmgr_settings', array() ); $value = isset( $settings[ $args['field'] ] ) ? $settings[ $args['field'] ] : ''; if ( is_array( $value ) ) { $value = implode( "\n", $value ); } echo ''; if ( isset( $args['description'] ) ) { echo '' . esc_html( $args['description'] ) . '
'; } } /** * Sanitize settings * * @param array $input Raw settings input. * @return array Sanitized settings. */ public function sanitize_settings( $input ) { $sanitized = array(); // Checkboxes are absent from $_POST entirely when unchecked, so these must be written // unconditionally - an isset() guard would drop the key (and the "off" state) rather // than storing false, making the setting impossible to turn off once enabled. $sanitized['enable_acf_blocks'] = ! empty( $input['enable_acf_blocks'] ); $sanitized['enable_block_presets'] = ! empty( $input['enable_block_presets'] ); $sanitized['enable_debug_logging'] = ! empty( $input['enable_debug_logging'] ); if ( isset( $input['block_folders'] ) ) { $folders = explode( "\n", $input['block_folders'] ); $folders = array_map( 'trim', $folders ); $folders = array_map( 'sanitize_text_field', $folders ); // Only allow safe relative folder paths (letters, numbers, hyphens, underscores, // forward slashes) so these values can't be used for directory traversal when // later passed to get_theme_file_path() in BPMGR_ACF_Blocks. $folders = array_filter( $folders, static function ( $folder ) { return '' !== $folder && preg_match( '/^[a-zA-Z0-9\-_\/]+$/', $folder ); } ); $sanitized['block_folders'] = array_values( $folders ); } return $sanitized; } }