| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\User; |
| 4 |
|
| 5 |
use AgeGate\Utility\Cookie; |
| 6 |
use AgeGate\Common\Settings; |
| 7 |
use AgeGate\Common\Admin\AbstractController; |
| 8 |
|
| 9 |
/** |
| 10 |
* Determine placement of toolbar link |
| 11 |
* |
| 12 |
* @package AgeGate |
| 13 |
* @copyright 2022 Phil Baker |
| 14 |
*/ |
| 15 |
class Toolbar extends AbstractController |
| 16 |
{ |
| 17 |
protected const INIT = false; |
| 18 |
|
| 19 |
public function register(): void |
| 20 |
{ |
| 21 |
add_action('admin_bar_menu', [$this, 'toolbarLink'], 1000); |
| 22 |
add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 23 |
} |
| 24 |
|
| 25 |
public function required(): bool |
| 26 |
{ |
| 27 |
$settings = Settings::getInstance(); |
| 28 |
|
| 29 |
return !is_admin() && $settings->toolbar; |
| 30 |
} |
| 31 |
|
| 32 |
public function data(): array |
| 33 |
{ |
| 34 |
return []; |
| 35 |
} |
| 36 |
|
| 37 |
public function fields(): array |
| 38 |
{ |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
public function enqueue(): void |
| 43 |
{ |
| 44 |
if (is_admin_bar_showing()) { |
| 45 |
$settings = Settings::getInstance(); |
| 46 |
$path = sprintf('%s%s%s', AGE_GATE_URL, 'dist', ($settings->rawAssets ? '/raw' : '')); |
| 47 |
wp_enqueue_script('age-gate-toolbar', $path . '/toolbar.js', [], AGE_GATE_VERSION, true); |
| 48 |
wp_enqueue_style('age-gate-toolbar', $path . '/toolbar.css', [], AGE_GATE_VERSION); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function toolbarLink($adminBar) |
| 53 |
{ |
| 54 |
if (!is_admin_bar_showing()) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
$settings = Settings::getInstance(); |
| 60 |
echo sprintf('<script>var ag_cookie_domain = "%s"; var ag_cookie_name = "%s"</script>', esc_attr(Cookie::getDomain()), esc_attr($settings->getCookieName())); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 61 |
|
| 62 |
$parent = [ |
| 63 |
'id' => 'age-gate', |
| 64 |
'title' => '<span class="screen-reader-text">' . __('Age Gate', 'age-gate') . '</span>', |
| 65 |
'href' => esc_url( |
| 66 |
add_query_arg( |
| 67 |
[ |
| 68 |
'page' => 'age-gate', |
| 69 |
], |
| 70 |
admin_url('admin.php') |
| 71 |
) |
| 72 |
) |
| 73 |
]; |
| 74 |
|
| 75 |
$adminBar->add_node($parent); |
| 76 |
|
| 77 |
|
| 78 |
$child = [ |
| 79 |
'id' => 'age-gate-toggle', |
| 80 |
'title' => 'Toggle', |
| 81 |
'parent' => 'age-gate', |
| 82 |
'href' => esc_url( |
| 83 |
add_query_arg( |
| 84 |
[ |
| 85 |
'page' => 'age-gate', |
| 86 |
'ag_switch' => '1', |
| 87 |
'_wpnonce' => wp_create_nonce('age-gate-toggle'), |
| 88 |
'ls' => $settings->useLocalStorage, |
| 89 |
], |
| 90 |
admin_url('admin.php') |
| 91 |
) |
| 92 |
), |
| 93 |
'meta' => [ |
| 94 |
'title' => $settings->cookieDomain |
| 95 |
] |
| 96 |
|
| 97 |
]; |
| 98 |
|
| 99 |
$adminBar->add_node($child); |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|