| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Admin; |
| 4 |
|
| 5 |
use MasterAddons\Inc\Classes\Helper; |
| 6 |
use MasterAddons\Inc\Classes\Assets_Manager; |
| 7 |
|
| 8 |
/** |
| 9 |
* JLTMA Page Importer |
| 10 |
* Adds an "Import Pages" button on the WordPress Pages list |
| 11 |
* that opens the existing Template Library in a modal popup |
| 12 |
*/ |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
|
| 16 |
final class Page_Importer { |
| 17 |
|
| 18 |
private static $instance = null; |
| 19 |
const HANDLE = 'jltma-page-importer'; |
| 20 |
|
| 21 |
public static function get_instance() { |
| 22 |
if (!self::$instance) { |
| 23 |
self::$instance = new self(); |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); |
| 33 |
add_filter('views_edit-page', array($this, 'add_import_pages_tab')); |
| 34 |
add_filter('admin_body_class', array($this, 'add_body_class')); |
| 35 |
add_action('pre_get_posts', array($this, 'filter_imported_pages')); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Enqueue scripts only on Pages list screen |
| 40 |
*/ |
| 41 |
public function enqueue_scripts($hook_suffix) { |
| 42 |
// Only load on Pages list screen |
| 43 |
$screen = get_current_screen(); |
| 44 |
|
| 45 |
if (!$screen || $screen->id !== 'edit-page' || $screen->post_type !== 'page') { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Enqueue WordPress React (required for template library) |
| 50 |
wp_enqueue_script('wp-element'); |
| 51 |
wp_enqueue_script('wp-components'); |
| 52 |
wp_enqueue_script('wp-api-fetch'); |
| 53 |
|
| 54 |
// Enqueue all page importer assets via Assets_Manager |
| 55 |
// This includes: macy, template-library, widget-builder, page-importer |
| 56 |
Assets_Manager::enqueue('page-importer'); |
| 57 |
|
| 58 |
// Resolve plugin URL — use JLTMA_PRO_URL fallback for pro-only builds |
| 59 |
$plugin_url = defined('JLTMA_URL') ? JLTMA_URL : (defined('JLTMA_PRO_URL') ? untrailingslashit(JLTMA_PRO_URL) : ''); |
| 60 |
|
| 61 |
// Localize Template Library (required by React app) |
| 62 |
wp_localize_script('jltma-template-library', 'JLTMATemplateLibrary', array( |
| 63 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 64 |
'nonce' => wp_create_nonce('jltma_template_library_nonce'), |
| 65 |
'restUrl' => get_rest_url(), |
| 66 |
'restNonce' => wp_create_nonce('wp_rest'), |
| 67 |
'pluginUrl' => $plugin_url, |
| 68 |
'assetsUrl' => $plugin_url . '/assets/', |
| 69 |
'isProActive' => Helper::jltma_premium(), |
| 70 |
'strings' => array( |
| 71 |
'searchPlaceholder' => __('Search templates...', 'master-addons'), |
| 72 |
'importTemplate' => __('Import', 'master-addons'), |
| 73 |
'previewTemplate' => __('Preview', 'master-addons'), |
| 74 |
'loadingTemplates' => __('Loading templates...', 'master-addons'), |
| 75 |
'noTemplatesFound' => __('No templates found', 'master-addons'), |
| 76 |
'importSuccess' => __('Template imported successfully!', 'master-addons'), |
| 77 |
'importError' => __('Failed to import template', 'master-addons') |
| 78 |
) |
| 79 |
)); |
| 80 |
|
| 81 |
// Localize Template Kits config (for Template Kits dropdown option) |
| 82 |
wp_localize_script('jltma-template-library', 'JLTMATemplatesKit', array( |
| 83 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 84 |
'nonce' => wp_create_nonce('jltma_template_kits_nonce_action'), |
| 85 |
'is_pro' => Helper::jltma_premium(), |
| 86 |
'pluginUrl' => $plugin_url, |
| 87 |
'strings' => array( |
| 88 |
'importSuccess' => __('Template Kit imported successfully!', 'master-addons'), |
| 89 |
'importError' => __('Failed to import template kit.', 'master-addons'), |
| 90 |
'previewTemplate' => __('Preview', 'master-addons'), |
| 91 |
'importTemplate' => __('Import Kit', 'master-addons'), |
| 92 |
'searchPlaceholder' => __('Search template kits...', 'master-addons'), |
| 93 |
) |
| 94 |
)); |
| 95 |
|
| 96 |
// Localize script data for page importer |
| 97 |
wp_localize_script( |
| 98 |
'jltma-page-importer', |
| 99 |
'JLTMA_PAGE_IMPORTER', |
| 100 |
array( |
| 101 |
'logo_url' => $plugin_url . '/assets/images/logo.svg', |
| 102 |
'red_logo_url' => $plugin_url . '/assets/images/red-logo.svg', |
| 103 |
'library_url' => admin_url('admin.php?page=jltma-template-library'), |
| 104 |
'template_kits_url' => $plugin_url . '/assets/js/admin/template-kits-app.js', |
| 105 |
'brand_color' => '#6814cd', |
| 106 |
'nonce' => wp_create_nonce('jltma_page_importer_nonce'), |
| 107 |
'i18n' => array( |
| 108 |
'import_pages' => __('Import Pages', 'master-addons'), |
| 109 |
'close' => __('Close', 'master-addons'), |
| 110 |
'loading' => __('Loading Template Library...', 'master-addons'), |
| 111 |
), |
| 112 |
) |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Add body class to Pages list screen |
| 118 |
* This helps with styling and JavaScript targeting |
| 119 |
*/ |
| 120 |
public function add_body_class($classes) { |
| 121 |
$screen = get_current_screen(); |
| 122 |
|
| 123 |
// Only add class on Pages list screen |
| 124 |
if ($screen && $screen->id === 'edit-page' && $screen->post_type === 'page') { |
| 125 |
$classes .= ' jltma-page-importer-btn-active'; |
| 126 |
} |
| 127 |
|
| 128 |
return $classes; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Add custom tab to Pages list view (optional enhancement) |
| 133 |
* This adds a tab showing imported pages |
| 134 |
*/ |
| 135 |
public function add_import_pages_tab($views) { |
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
// Count pages imported via Master Addons |
| 139 |
// Check both meta keys: _jltma_demo_import_item (kit imports) and _jltma_imported_template (single imports) |
| 140 |
$count = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for counting imported pages by meta key; query is fully static with no user-supplied values |
| 141 |
"SELECT COUNT(*) FROM {$wpdb->posts} |
| 142 |
WHERE post_type = 'page' |
| 143 |
AND post_status = 'publish' |
| 144 |
AND ID IN ( |
| 145 |
SELECT post_id FROM {$wpdb->postmeta} |
| 146 |
WHERE meta_key IN ('_jltma_demo_import_item', '_jltma_imported_template') |
| 147 |
)" |
| 148 |
); |
| 149 |
|
| 150 |
if ($count > 0) { |
| 151 |
$class = (isset($_GET['jltma_imported']) && $_GET['jltma_imported'] == '1') ? 'current' : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only view filter, no data processed |
| 152 |
$url = add_query_arg('jltma_imported', '1', admin_url('edit.php?post_type=page')); |
| 153 |
$views['jltma_imported'] = sprintf( |
| 154 |
'<a href="%s" class="%s" style="color: %s; font-weight: 500">%s <span class="count">(%s)</span></a>', |
| 155 |
$url, |
| 156 |
$class, |
| 157 |
'#6814cd', |
| 158 |
__('MA Imported', 'master-addons'), |
| 159 |
$count |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
return $views; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Filter pages list to show only MA imported pages |
| 168 |
*/ |
| 169 |
public function filter_imported_pages($query) { |
| 170 |
if (!is_admin() || !$query->is_main_query()) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$screen = get_current_screen(); |
| 175 |
if (!$screen || $screen->id !== 'edit-page') { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if (isset($_GET['jltma_imported']) && $_GET['jltma_imported'] === '1') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only query filter, no data processed |
| 180 |
$query->set('meta_query', [ |
| 181 |
'relation' => 'OR', |
| 182 |
[ |
| 183 |
'key' => '_jltma_demo_import_item', |
| 184 |
'compare' => 'EXISTS', |
| 185 |
], |
| 186 |
[ |
| 187 |
'key' => '_jltma_imported_template', |
| 188 |
'compare' => 'EXISTS', |
| 189 |
], |
| 190 |
]); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|