1: <?php
2:
3: 4: 5:
6: class Quform_Toolbar
7: {
8: 9: 10:
11: protected $options;
12:
13: 14: 15:
16: protected $repository;
17:
18: 19: 20: 21:
22: public function __construct(Quform_Options $options, Quform_Repository $repository)
23: {
24: $this->options = $options;
25: $this->repository = $repository;
26: }
27:
28: 29: 30: 31: 32:
33: public function addNodes(WP_Admin_Bar $adminBar)
34: {
35: if ( ! $this->options->get('toolbarMenu') || ! $this->userHasAnyCapability()) {
36: return;
37: }
38:
39: if (current_user_can('quform_add_forms')) {
40: $adminBar->add_node(array(
41: 'id' => 'quform-new-form',
42: 'title' => Quform::escape(__('Form', 'quform')),
43: 'href' => admin_url('admin.php?page=quform.forms&sp=add'),
44: 'parent' => 'new-content'
45: ));
46: }
47:
48: if (current_user_can('quform_edit_forms') || current_user_can('quform_view_entries')) {
49: $forms = $this->repository->getFormsByUpdatedAt();
50:
51: if (is_array($forms) && count($forms)) {
52: $adminBar->add_node(array(
53: 'id' => 'quform-recent-forms',
54: 'parent' => 'quform',
55: 'group' => true
56: ));
57:
58: $currentUserId = get_current_user_id();
59: $recentFormIds = get_user_meta($currentUserId, 'quform_recent_forms', true);
60:
61: if ( ! is_array($recentFormIds) || ! count($recentFormIds)) {
62: $recentFormIds = array();
63:
64: foreach ($forms as $form) {
65: $recentFormIds[] = $form['id'];
66: }
67:
68: if (count($recentFormIds)) {
69: update_user_meta($currentUserId, 'quform_recent_forms', array_slice($recentFormIds, 0, 8));
70: }
71: }
72:
73: if (count($recentFormIds)) {
74:
75: foreach ($recentFormIds as $recentFormId) {
76:
77: foreach ($forms as $form) {
78: if ($form['id'] == $recentFormId) {
79: $adminBar->add_node(array(
80: 'id' => sprintf('quform-form-%d', $form['id']),
81: 'title' => Quform::escape($form['name']),
82: 'href' => current_user_can('quform_edit_forms') ? admin_url(sprintf('admin.php?page=quform.forms&sp=edit&id=%d', $form['id'])) : '',
83: 'parent' => 'quform-recent-forms'
84: ));
85:
86: if (current_user_can('quform_edit_forms')) {
87: $adminBar->add_node(array(
88: 'id' => sprintf('quform-form-%d-edit', $form['id']),
89: 'title' => Quform::escape(__('Edit', 'quform')),
90: 'href' => admin_url(sprintf('admin.php?page=quform.forms&sp=edit&id=%d', $form['id'])),
91: 'parent' => sprintf('quform-form-%d', $form['id'])
92: ));
93: }
94:
95: if (current_user_can('quform_view_entries')) {
96: $adminBar->add_node(array(
97: 'id' => sprintf('quform-form-%d-entries', $form['id']),
98: 'title' => Quform::escape(__('Entries', 'quform')),
99: 'href' => admin_url(sprintf('admin.php?page=quform.entries&id=%d', $form['id'])),
100: 'parent' => sprintf('quform-form-%d', $form['id'])
101: ));
102: }
103: }
104: }
105: }
106: }
107: }
108: }
109:
110: $adminBar->add_node(array(
111: 'id' => 'quform',
112: 'title' => sprintf('<span class="quform-toolbar-icon ab-item svg"></span><span class="ab-label">%s</span>', Quform::escape(__('Forms', 'quform'))),
113: 'href' => current_user_can('quform_view_dashboard') ? admin_url('admin.php?page=quform.dashboard') : false,
114: 'parent' => false,
115: 'meta' => array('class' => 'quform-toolbar-menu')
116: ));
117:
118: if (current_user_can('quform_view_dashboard')) {
119: $adminBar->add_node(array(
120: 'id' => 'quform-dashboard',
121: 'title' => Quform::escape(__('Dashboard', 'quform')),
122: 'href' => admin_url('admin.php?page=quform.dashboard'),
123: 'parent' => 'quform'
124: ));
125: }
126:
127: if (current_user_can('quform_list_forms')) {
128: $adminBar->add_node(array(
129: 'id' => 'quform-forms',
130: 'title' => Quform::escape(__('Forms', 'quform')),
131: 'href' => admin_url('admin.php?page=quform.forms'),
132: 'parent' => 'quform'
133: ));
134: }
135:
136: if (current_user_can('quform_add_forms')) {
137: $adminBar->add_node(array(
138: 'id' => 'quform-add-new',
139: 'title' => Quform::escape(__('Add New', 'quform')),
140: 'href' => admin_url('admin.php?page=quform.forms&sp=add'),
141: 'parent' => 'quform'
142: ));
143: }
144:
145: if (current_user_can('quform_settings')) {
146: $adminBar->add_node(array(
147: 'id' => 'quform-settings',
148: 'title' => Quform::escape(__('Settings', 'quform')),
149: 'href' => admin_url('admin.php?page=quform.settings'),
150: 'parent' => 'quform'
151: ));
152: }
153: }
154:
155: 156: 157:
158: public function printStyles()
159: {
160: if ( ! $this->options->get('toolbarMenu') || ! $this->userHasAnyCapability()) {
161: return;
162: }
163:
164: ?>
165: <style>
166:
167: float: left;
168: width: 27px;
169: height: 27px;
170: background: url('<?php echo esc_html(Quform::getPluginIcon()); ?>') no-repeat 0px 7px;
171: background-size: 20px;
172: }
173: </style>
174: <?php
175: }
176:
177: 178: 179: 180: 181:
182: protected function userHasAnyCapability()
183: {
184: return
185: current_user_can('quform_view_dashboard') ||
186: current_user_can('quform_list_forms') ||
187: current_user_can('quform_add_forms') ||
188: current_user_can('quform_edit_forms') ||
189: current_user_can('quform_view_entries') ||
190: current_user_can('quform_settings');
191: }
192: }
193: