post_content, 'kofront_dashboard' ) || has_shortcode( $post->post_content, 'kofront_editor' ) ) ) { // Enqueue the native WordPress Media Uploader wp_enqueue_media(); wp_localize_script( 'kofront-script', 'kofrontAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'kofront_ajax_nonce' ) ) ); } } // --------------------------------------------------------- // 1.1. Prevent Caching on Portal Pages (Pre-render hook). // --------------------------------------------------------- add_action( 'template_redirect', 'kofront_disable_caching' ); function kofront_disable_caching() { global $post; // Trigger if the page has the dashboard or editor shortcode. if ( is_a( $post, 'WP_Post' ) && ( has_shortcode( $post->post_content, 'kofront_dashboard' ) || has_shortcode( $post->post_content, 'kofront_editor' ) ) ) { if ( ! defined( 'DONOTCACHEPAGE' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound define( 'DONOTCACHEPAGE', true ); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound do_action( 'litespeed_control_set_nocache', 'kofront-portal' ); nocache_headers(); } } // --------------------------------------------------------- // 2. AJAX listener for instant category deletion. // --------------------------------------------------------- add_action( 'wp_ajax_kofront_delete_category', 'kofront_ajax_delete_category' ); function kofront_ajax_delete_category() { check_ajax_referer( 'kofront_ajax_nonce', 'security' ); if ( ! current_user_can( 'edit_others_posts' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ) ); } $cat_id = isset( $_POST['cat_id'] ) ? intval( wp_unslash( $_POST['cat_id'] ) ) : 0; if ( $cat_id > 0 ) { $del_result = wp_delete_term( $cat_id, 'category' ); if ( is_wp_error( $del_result ) ) { wp_send_json_error( array( 'message' => $del_result->get_error_message() ) ); } else { wp_send_json_success( array( 'message' => 'Category deleted.' ) ); } } wp_send_json_error( array( 'message' => 'Invalid category ID.' ) ); } // --------------------------------------------------------- // 3. Dashboard shortcode. // --------------------------------------------------------- add_shortcode( 'kofront_dashboard', 'kofront_render_dashboard' ); function kofront_render_dashboard( $atts ) { $parsed_atts = shortcode_atts( array( 'editor_url' => get_permalink() ), $atts ); $editor_url = esc_url( $parsed_atts['editor_url'] ); if ( ! is_user_logged_in() ) { $login_url = wp_login_url( get_permalink() ); /* translators: %s: Login URL */ return '
' . sprintf( __( 'You must be logged in to access the portal. Please log in here.', 'kofront-frontend-posts' ), esc_url( $login_url ) ) . '
'; } $current_user_id = get_current_user_id(); $current_url = get_permalink(); $message = ''; // Handle post deletion. if ( isset( $_GET['kofront_action'] ) && sanitize_text_field( wp_unslash( $_GET['kofront_action'] ) ) === 'delete' && isset( $_GET['kofront_post_id'] ) ) { $del_post_id = intval( wp_unslash( $_GET['kofront_post_id'] ) ); if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'kofront_delete_post_' . $del_post_id ) ) { $del_post = get_post( $del_post_id ); if ( $del_post && ( $del_post->post_author == $current_user_id || current_user_can( 'edit_others_posts' ) ) ) { wp_trash_post( $del_post_id ); $message .= 'You haven't created any posts yet.
| Title | Status | Date | Action |
|---|---|---|---|
| post_status) ); ?> | ID ) ); ?> | post_status === 'publish' ) : ?> View Edit Delete |
' . sprintf( __( 'You must be logged in to access the portal. Please log in here.', 'kofront-frontend-posts' ), esc_url( $login_url ) ) . '
'; } $current_user_id = get_current_user_id(); $message = ''; $edit_id = isset( $_GET['kofront_edit_id'] ) ? intval( wp_unslash( $_GET['kofront_edit_id'] ) ) : 0; // Handle form submission. if ( isset( $_POST['kofront_is_submission'] ) && sanitize_text_field( wp_unslash( $_POST['kofront_is_submission'] ) ) === '1' ) { if ( ! isset( $_POST['kofront_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['kofront_nonce'] ) ), 'kofront_submit_action' ) ) { $message .= ''; } else { $post_id_to_edit = isset( $_POST['kofront_post_id'] ) ? intval( wp_unslash( $_POST['kofront_post_id'] ) ) : 0; $post_title = isset( $_POST['kofront_title'] ) ? sanitize_text_field( wp_unslash( $_POST['kofront_title'] ) ) : ''; $post_excerpt = isset( $_POST['kofront_excerpt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['kofront_excerpt'] ) ) : ''; if ( isset( $_POST['kofront_save_draft'] ) ) { $final_status = 'draft'; } else { $final_status = 'publish'; } $layout_style = isset( $_POST['kofront_layout_style'] ) ? sanitize_text_field( wp_unslash( $_POST['kofront_layout_style'] ) ) : '1-col'; $selected_cats = isset( $_POST['kofront_categories'] ) && is_array( $_POST['kofront_categories'] ) ? array_map( 'intval', wp_unslash( $_POST['kofront_categories'] ) ) : array(); if ( ! empty( $_POST['kofront_new_category'] ) ) { $new_cat_name = sanitize_text_field( wp_unslash( $_POST['kofront_new_category'] ) ); $term_info = term_exists( $new_cat_name, 'category' ); if ( ! $term_info ) { $term_info = wp_insert_term( $new_cat_name, 'category' ); } if ( ! is_wp_error( $term_info ) ) { $selected_cats[] = intval( $term_info['term_id'] ); } } $gutenberg_content = ''; $order = isset( $_POST['kofront_order'] ) && is_array( $_POST['kofront_order'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_order'] ) ) : array(); $headings = isset( $_POST['kofront_heading'] ) && is_array( $_POST['kofront_heading'] ) ? array_map( 'wp_kses_post', wp_unslash( $_POST['kofront_heading'] ) ) : array(); $heading_levels = isset( $_POST['kofront_heading_level'] ) && is_array( $_POST['kofront_heading_level'] ) ? array_map( 'intval', wp_unslash( $_POST['kofront_heading_level'] ) ) : array(); $heading_aligns = isset( $_POST['kofront_heading_align'] ) && is_array( $_POST['kofront_heading_align'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_heading_align'] ) ) : array(); $paragraphs = isset( $_POST['kofront_paragraph'] ) && is_array( $_POST['kofront_paragraph'] ) ? array_map( 'wp_kses_post', wp_unslash( $_POST['kofront_paragraph'] ) ) : array(); $toc_titles = isset( $_POST['kofront_toc_title'] ) && is_array( $_POST['kofront_toc_title'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_toc_title'] ) ) : array(); $toc_levels_arr = isset( $_POST['kofront_toc_levels'] ) && is_array( $_POST['kofront_toc_levels'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_toc_levels'] ) ) : array(); $toc_spans_arr = isset( $_POST['kofront_toc_span'] ) && is_array( $_POST['kofront_toc_span'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_toc_span'] ) ) : array(); $shortcodes = isset( $_POST['kofront_shortcode'] ) && is_array( $_POST['kofront_shortcode'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['kofront_shortcode'] ) ) : array(); $raw_blocks = isset( $_POST['kofront_raw_block'] ) && is_array( $_POST['kofront_raw_block'] ) ? array_map( 'wp_kses_post', wp_unslash( $_POST['kofront_raw_block'] ) ) : array(); $block_image_ids = array(); $blocks_array = array(); // Collect blocks for native column generation. $used_anchors = array(); // Track heading anchor IDs to prevent duplicates. $toc_items = array(); // Store headings to populate TOC blocks after loop. $toc_configs = array(); // Store TOC placeholders and titles. $toc_counter = 0; foreach ( $order as $block_type ) { $block_html = ''; if ( $block_type === 'heading' ) { $text = trim( str_replace( array( '' . $text . '
'; } } elseif ( $block_type === 'shortcode' ) { $sc_text = ! empty( $shortcodes ) ? trim( wp_strip_all_tags( (string) array_shift( $shortcodes ) ) ) : ''; if ( $sc_text !== '' ) { $block_html .= '' . $sc_text . ''; } } elseif ( $block_type === 'raw_block' ) { $raw_b = ! empty( $raw_blocks ) ? trim( (string) array_shift( $raw_blocks ) ) : ''; if ( $raw_b !== '' ) { $block_html .= $raw_b; } } elseif ( strpos( $block_type, 'image_' ) === 0 ) { $size_key = 'kofront_image_size_' . str_replace( 'image_', '', $block_type ); $img_size = isset( $_POST[ $size_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $size_key ] ) ) : 'large'; $existing_img_key = 'kofront_existing_' . $block_type; $current_img_id = isset( $_POST[ $existing_img_key ] ) ? intval( wp_unslash( $_POST[ $existing_img_key ] ) ) : 0; $span_img_key = 'kofront_span_' . $block_type; $is_span = ! empty( $_POST[ $span_img_key ] ) ? ' kofront-span-all' : ''; if ( $current_img_id > 0 ) { $img_url = wp_get_attachment_url( $current_img_id ); $block_html .= ''; $block_html .= '' . esc_html__( 'Saving and redirecting...', 'kofront-frontend-posts' ) . '