| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Admin\Theme_Builder; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
// The free and pro plugins are built from a shared source and can both |
| 8 |
// ship this file. Guard the declaration to avoid a fatal "Cannot declare |
| 9 |
// class ... already in use" error when both plugins are active. |
| 10 |
if (!class_exists(__NAMESPACE__ . '\\Loader')) { |
| 11 |
|
| 12 |
class Loader |
| 13 |
{ |
| 14 |
private static $_instance = null; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
// Load the main Theme_Builder class via autoloader |
| 19 |
Theme_Builder::get_instance(); |
| 20 |
|
| 21 |
// Handle admin redirects |
| 22 |
add_action('admin_init', [$this, 'handle_admin_redirects']); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Handle redirects for master_template post type |
| 27 |
*/ |
| 28 |
public function handle_admin_redirects() |
| 29 |
{ |
| 30 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only checks of WordPress-set admin query vars for redirect flow; no form data is processed. |
| 31 |
global $pagenow; |
| 32 |
$target_post_type = 'master_template'; |
| 33 |
$redirect_url = admin_url('edit.php?post_type=master_template'); |
| 34 |
|
| 35 |
if ('post.php' === $pagenow && isset($_GET['post'])) { |
| 36 |
if (isset($_GET['action']) && in_array($_GET['action'], ['elementor', 'trash', 'delete', 'restore', 'untrash'], true)) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$post_id = absint($_GET['post']); |
| 41 |
if ($post_id) { |
| 42 |
$current_post_type = get_post_type($post_id); |
| 43 |
|
| 44 |
if ($target_post_type === $current_post_type) { |
| 45 |
$master_template_type = get_post_meta($post_id, 'master_template_type', true); |
| 46 |
if (!empty($master_template_type)) { |
| 47 |
$redirect_url .= '&master_template_type_filter=' . $master_template_type; |
| 48 |
} |
| 49 |
wp_safe_redirect($redirect_url); |
| 50 |
exit; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ('post-new.php' === $pagenow && isset($_GET['post_type'])) { |
| 56 |
$current_post_type = sanitize_key($_GET['post_type']); |
| 57 |
|
| 58 |
if ($target_post_type === $current_post_type) { |
| 59 |
wp_safe_redirect($redirect_url); |
| 60 |
exit; |
| 61 |
} |
| 62 |
} |
| 63 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 64 |
} |
| 65 |
|
| 66 |
public static function get_instance() |
| 67 |
{ |
| 68 |
if (is_null(self::$_instance)) { |
| 69 |
self::$_instance = new self(); |
| 70 |
} |
| 71 |
return self::$_instance; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|