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 '';
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 '