| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Hooks; |
| 6 |
|
| 7 |
/** |
| 8 |
* Utility Hooks |
| 9 |
* |
| 10 |
* Handles various utility functionality like admin bar modifications |
| 11 |
*/ |
| 12 |
class UtilsHooks |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Initialize utility hooks |
| 16 |
*/ |
| 17 |
public static function init(): void |
| 18 |
{ |
| 19 |
// Add "Edit Trip" to admin bar on single trip page |
| 20 |
add_action('admin_bar_menu', [self::class, 'addEditTripAdminBarLink'], 80); |
| 21 |
|
| 22 |
// Add admin bar CSS for trip edit link |
| 23 |
add_action('wp_head', [self::class, 'addAdminBarTripEditCSS']); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Add "Edit Trip" link to admin bar on single trip page |
| 28 |
* Add "Edit Destination/Activity/Category" links on taxonomy pages |
| 29 |
* |
| 30 |
* @param \WP_Admin_Bar $admin_bar |
| 31 |
*/ |
| 32 |
public static function addEditTripAdminBarLink($admin_bar): void |
| 33 |
{ |
| 34 |
// Debug logging at function start |
| 35 |
|
| 36 |
// Only for logged-in users who can edit Yatra items |
| 37 |
if (!is_user_logged_in() || (!current_user_can('manage_options'))) { |
| 38 |
|
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// Check if we're on a single trip page (use query var, not global $trip — |
| 43 |
// the booking template also assigns $trip at global scope, which would |
| 44 |
// otherwise leak this menu into the booking page admin bar). |
| 45 |
global $trip; |
| 46 |
if (function_exists('yatra_is_single_trip') && yatra_is_single_trip() && !empty($trip) && !empty($trip->id)) { |
| 47 |
// Build the edit URL for the trip |
| 48 |
$edit_url = admin_url('admin.php?page=yatra&subpage=trips&action=edit&id=' . (int) $trip->id); |
| 49 |
|
| 50 |
// Add the Edit Trip node |
| 51 |
$admin_bar->add_node([ |
| 52 |
'id' => 'yatra-edit-trip', |
| 53 |
'title' => __('Edit Trip', 'yatra'), |
| 54 |
'href' => $edit_url, |
| 55 |
'meta' => [ |
| 56 |
'title' => __('Edit this trip in Yatra admin', 'yatra'), |
| 57 |
], |
| 58 |
]); |
| 59 |
} |
| 60 |
|
| 61 |
// Check if we're on a taxonomy page |
| 62 |
$taxonomy_data = $GLOBALS['yatra_taxonomy_data'] ?? null; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if (!empty($taxonomy_data) && !empty($taxonomy_data->id)) { |
| 67 |
// Build the edit URL based on taxonomy type |
| 68 |
$type = $taxonomy_data->type ?? ''; |
| 69 |
$id = (int) $taxonomy_data->id; |
| 70 |
|
| 71 |
$tabs = ''; |
| 72 |
$title = ''; |
| 73 |
|
| 74 |
switch ($type) { |
| 75 |
case 'destination': |
| 76 |
$tabs = 'destinations'; |
| 77 |
$title = __('Edit Destination', 'yatra'); |
| 78 |
break; |
| 79 |
case 'activity': |
| 80 |
$tabs = 'activities'; |
| 81 |
$title = __('Edit Activity', 'yatra'); |
| 82 |
break; |
| 83 |
case 'category': |
| 84 |
$tabs = 'categories'; |
| 85 |
$title = __('Edit Category', 'yatra'); |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
if ($tabs && $title) { |
| 90 |
$edit_url = admin_url('admin.php?page=yatra&subpage=trips&tab=' . $tabs . '&action=edit&id=' . $id); |
| 91 |
|
| 92 |
$node_id = 'yatra-edit-' . $type; |
| 93 |
|
| 94 |
|
| 95 |
$admin_bar->add_node([ |
| 96 |
'id' => $node_id, |
| 97 |
'title' => $title, |
| 98 |
'href' => $edit_url, |
| 99 |
'meta' => [ |
| 100 |
/* translators: %s: object type label (e.g. trip, booking). */ |
| 101 |
'title' => sprintf(__('Edit this %s in Yatra admin', 'yatra'), $type), |
| 102 |
], |
| 103 |
]); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Add CSS for the Edit Trip/Taxonomy admin bar links |
| 110 |
*/ |
| 111 |
public static function addAdminBarTripEditCSS(): void |
| 112 |
{ |
| 113 |
// Only output CSS if user can see admin bar |
| 114 |
if (!is_user_logged_in() || !is_admin_bar_showing()) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Check if we're on a trip page or taxonomy page |
| 119 |
global $trip; |
| 120 |
$taxonomy_data = $GLOBALS['yatra_taxonomy_data'] ?? null; |
| 121 |
|
| 122 |
if (empty($trip) && empty($taxonomy_data)) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
echo '<style> |
| 127 |
#wpadminbar #wp-admin-bar-yatra-edit-trip > .ab-item:before, |
| 128 |
#wpadminbar #wp-admin-bar-yatra-edit-destination > .ab-item:before, |
| 129 |
#wpadminbar #wp-admin-bar-yatra-edit-activity > .ab-item:before, |
| 130 |
#wpadminbar #wp-admin-bar-yatra-edit-category > .ab-item:before { |
| 131 |
content: "\f464"; |
| 132 |
font-family: dashicons; |
| 133 |
margin-right: 5px; |
| 134 |
} |
| 135 |
</style>'; |
| 136 |
} |
| 137 |
} |
| 138 |
|