init_hooks(); } private function init_hooks() { add_action('init', [$this, 'register_post_type']); add_filter('manage_jltma_widget_posts_columns', [$this, 'set_custom_columns']); add_action('manage_jltma_widget_posts_custom_column', [$this, 'custom_column_content'], 10, 2); add_filter('manage_edit-jltma_widget_sortable_columns', [$this, 'set_sortable_columns']); add_action('save_post', [$this, 'save_meta_data']); add_filter('post_row_actions', [$this, 'modify_row_actions'], 10, 2); add_action('admin_head', [$this, 'admin_head_css']); add_action('admin_footer', [$this, 'admin_footer_js']); add_action('wp_ajax_jltma_widget_get_shortcode', [$this, 'get_shortcode_ajax']); add_action('wp_ajax_jltma_add_widget_category', [$this, 'add_widget_category_ajax']); add_action('template_redirect', [$this, 'handle_widget_preview']); } public function register_post_type() { $labels = [ 'name' => _x('Master Addons Widgets', 'Post Type General Name', 'master-addons'), 'singular_name' => _x('Widget', 'Post Type Singular Name', 'master-addons'), 'menu_name' => __('Widgets', 'master-addons'), 'name_admin_bar' => __('Widget', 'master-addons'), 'archives' => __('Widget Archives', 'master-addons'), 'attributes' => __('Widget Attributes', 'master-addons'), 'parent_item_colon' => __('Parent Widget:', 'master-addons'), 'all_items' => __('All Widgets', 'master-addons'), 'add_new_item' => __('Add New Widget', 'master-addons'), 'add_new' => __('Add New', 'master-addons'), 'new_item' => __('New Widget', 'master-addons'), 'edit_item' => __('Edit Widget', 'master-addons'), 'update_item' => __('Update Widget', 'master-addons'), 'view_item' => __('View Widget', 'master-addons'), 'view_items' => __('View Widgets', 'master-addons'), 'search_items' => __('Search Widget', 'master-addons'), 'not_found' => __('Not found', 'master-addons'), 'not_found_in_trash' => __('Not found in Trash', 'master-addons'), 'featured_image' => __('Featured Image', 'master-addons'), 'set_featured_image' => __('Set featured image', 'master-addons'), 'remove_featured_image' => __('Remove featured image', 'master-addons'), 'use_featured_image' => __('Use as featured image', 'master-addons'), 'insert_into_item' => __('Insert into widget', 'master-addons'), 'uploaded_to_this_item' => __('Uploaded to this widget', 'master-addons'), 'items_list' => __('Widgets list', 'master-addons'), 'items_list_navigation' => __('Widgets list navigation', 'master-addons'), 'filter_items_list' => __('Filter widgets list', 'master-addons'), ]; $args = [ 'label' => __('Widget', 'master-addons'), 'description' => __('Master Addons Custom Widgets', 'master-addons'), 'labels' => $labels, 'supports' => ['title', 'editor'], 'public' => false, 'show_ui' => true, 'show_in_menu' => false, // We'll add it to our custom menu 'menu_position' => 20, 'show_in_admin_bar' => true, 'show_in_nav_menus' => false, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'capability_type' => 'post', 'show_in_rest' => true, 'rewrite' => false, ]; register_post_type($this->post_type, $args); } public function set_custom_columns($columns) { $new_columns = []; $new_columns['cb'] = $columns['cb']; $new_columns['title'] = __('Widget Name', 'master-addons'); $new_columns['jltma_widget_category'] = __('Widget Category', 'master-addons'); $new_columns['author'] = __('Author', 'master-addons'); $new_columns['jltma_widget_shortcode'] = __('Shortcode', 'master-addons'); return $new_columns; } public function custom_column_content($column, $post_id) { switch ($column) { case 'jltma_widget_category': $this->render_category_column($post_id); break; case 'jltma_widget_shortcode': $this->render_shortcode_column($post_id); break; } } private function render_category_column($post_id) { $category = get_post_meta($post_id, '_jltma_widget_category', true); if (empty($category)) { $category = 'general'; } $category_name = ucfirst(str_replace('_', ' ', $category)); echo '' . esc_html($category_name) . ''; echo '
'; echo ' ' . esc_html__('Edit Category', 'master-addons'); echo ''; } private function render_shortcode_column($post_id) { // Use the new shortcode format with widget ID $shortcode = '[jltma_widget_' . $post_id . ']'; echo '
'; echo ''; echo ''; echo '
'; } public function set_sortable_columns($columns) { $columns['jltma_widget_category'] = 'jltma_widget_category'; return $columns; } public function modify_row_actions($actions, $post) { if ($post->post_type === $this->post_type) { // Remove default actions unset($actions['inline hide-if-no-js']); unset($actions['view']); // Replace default edit with link to widget editor page $editor_url = admin_url('admin.php?page=jltma-widget-editor&widget_id=' . $post->ID); $actions['edit'] = sprintf( '%s', esc_url($editor_url), __('Edit', 'master-addons') ); } return $actions; } public function save_meta_data($post_id) { if (!isset($_POST['jltma_widget_meta_nonce_field']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['jltma_widget_meta_nonce_field'] ) ), 'jltma_widget_meta_nonce')) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!current_user_can('edit_post', $post_id)) { return; } // Save widget name if (isset($_POST['jltma_widget_name'])) { $widget_name = sanitize_title( wp_unslash( $_POST['jltma_widget_name'] ) ); update_post_meta($post_id, '_jltma_widget_name', $widget_name); } // Save category if (isset($_POST['jltma_widget_category'])) { update_post_meta($post_id, '_jltma_widget_category', sanitize_text_field( wp_unslash( $_POST['jltma_widget_category'] ) )); } } public function admin_head_css() { global $current_screen; if (!$current_screen || ($current_screen->post_type !== $this->post_type && $current_screen->id !== 'admin_page_jltma-widget-editor')) { return; } ?> id, ['edit-jltma_widget', 'jltma_widget', 'admin_page_jltma-widget-editor'])) { return; } ?>

'You do not have permission to perform this action.'], 403); } check_ajax_referer('jltma_widget_nonce', '_nonce'); $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; // Use the new shortcode format with widget ID $shortcode = '[jltma_widget_' . $post_id . ']'; wp_send_json_success(['shortcode' => $shortcode]); } public function add_widget_category_ajax() { // Verify nonce check_ajax_referer('jltma_add_category_nonce', '_nonce'); // Check user capabilities if (!current_user_can('manage_options')) { wp_send_json_error([ 'message' => __('You do not have permission to add categories.', 'master-addons') ]); } // Get category name $category_name = isset($_POST['category_name']) ? sanitize_text_field( wp_unslash( $_POST['category_name'] ) ) : ''; if (empty($category_name)) { wp_send_json_error([ 'message' => __('Category name is required.', 'master-addons') ]); } // Generate slug from name $category_slug = sanitize_title($category_name); // Get existing categories $categories = get_option('jltma_widget_categories', []); // Check if category already exists if (isset($categories[$category_slug])) { wp_send_json_error([ 'message' => __('A category with this name already exists.', 'master-addons') ]); } // Add new category $categories[$category_slug] = $category_name; // Save categories update_option('jltma_widget_categories', $categories); // Return success wp_send_json_success([ /* translators: %s is the category name. */ 'message' => sprintf(__('Category "%s" added successfully!', 'master-addons'), $category_name), 'slug' => $category_slug, 'name' => $category_name ]); } public function handle_widget_preview() { // Check if this is a preview request if (!isset($_GET['jltma_widget_preview']) || $_GET['jltma_widget_preview'] != '1') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only preview check, no nonce available return; } // Verify user permissions if (!current_user_can('edit_posts')) { wp_die(esc_html__('You do not have permission to preview widgets.', 'master-addons')); } $widget_id = isset($_GET['widget_id']) ? intval($_GET['widget_id']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce not available for preview URL $shortcode = isset($_GET['shortcode']) ? sanitize_text_field( wp_unslash( $_GET['shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce not available for preview URL if (!$widget_id || !$shortcode) { wp_die(esc_html__('Invalid widget preview request.', 'master-addons')); } // Check if widget exists $widget_post = get_post($widget_id); if (!$widget_post || $widget_post->post_type !== $this->post_type) { wp_die(esc_html__('Widget not found.', 'master-addons')); } // Render preview template $this->render_preview_template($widget_id, $shortcode); exit; } private function render_preview_template($widget_id, $shortcode) { ?> > <?php esc_html_e('Widget Preview', 'master-addons'); ?>
post_type; } }