GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-admin-bar.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Handle management of the Admin Bar links
5  *
6  * @since 1.13
7  */
9 
10  /**
11  * @var GravityView_frontend|null
12  */
13  var $gravityview_view = null;
14 
15  function __construct() {
16 
17  $this->gravityview_view = GravityView_frontend::getInstance();
18 
19  $this->add_hooks();
20  }
21 
22  /**
23  * @since 1.13
24  */
25  private function add_hooks() {
26  add_action( 'add_admin_bar_menus', array( $this, 'remove_links' ), 80 );
27  add_action( 'admin_bar_menu', array( $this, 'add_links' ), 85 );
28  add_action( 'wp_after_admin_bar_render', array( $this, 'add_floaty_icon' ) );
29  }
30 
31  /**
32  * Add helpful GV links to the menu bar, like Edit Entry on single entry page.
33  *
34  * @since 1.13
35  * @return void
36  */
37  function add_links() {
38  /** @var WP_Admin_Bar $wp_admin_bar */
39  global $wp_admin_bar;
40 
41  if( is_admin() || ! GVCommon::has_cap( array( 'edit_gravityviews', 'gravityview_edit_entry', 'gravityforms_edit_forms' ) ) ) {
42  return;
43  }
44 
45  $view_data = GravityView_View_Data::getInstance()->get_views();
46  if ( empty( $view_data ) ) {
47  return;
48  }
49 
50  $wp_admin_bar->add_menu( array(
51  'id' => 'gravityview',
52  'title' => __( 'GravityView', 'gravityview' ),
53  'href' => '#',
54  ) );
55 
56  $this->add_edit_view_and_form_link();
57 
58  $this->add_edit_entry_link();
59 
60  }
61 
62  /**
63  * Add the Floaty icon to the toolbar without loading the whole icon font
64  *
65  * @since 1.17
66  *
67  * @return void
68  */
69  public function add_floaty_icon() {
70  ?>
71  <style>
72  #wp-admin-bar-gravityview > .ab-item:before {
73  content: '';
74  <?php // Base64-encode so that it works in Firefox as well, even though https://css-tricks.com/probably-dont-base64-svg/ ?>
75  background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='20.4 27.05 20 21'%3E%3Cpath fill='none' d='M25.8 6.7c0 .7.1 1.3.4 1.9-.1-.3-.1-.6-.1-1 0-3.1 3.3-6.6 7.8-5.2-.9-.5-1.8-.8-2.9-.8-2.9-.1-5.2 2.2-5.2 5.1z'/%3E%3Cpath fill='%23F0F5FA' d='M36.23 33.77c-1.45-1.48-3.37-2.3-5.44-2.43V30.3c.6-.13 1.1-.7 1.1-1.44 0-.83-.7-1.5-1.5-1.5s-1.5.66-1.5 1.5c0 .7.43 1.3 1.1 1.48v1.03c-2.07.08-4 .92-5.47 2.37-1.57 1.5-2.43 3.57-2.43 5.75 0 .1 0 .3.04.6 0 .1.05.3.07.5.1.6.25 1.1.44 1.7l.2.5c.03.1.06.2.12.3l.2.4c.37.63.8 1.22 1.3 1.72 1.55 1.55 3.6 2.4 5.8 2.4h.1c2.15 0 4.2-.85 5.7-2.36.55-.53 1-1.1 1.36-1.7.1-.16.17-.3.23-.47l.14-.3.2-.5c.2-.57.33-1.15.42-1.74l.08-.5c.04-.2.04-.4.04-.6.02-2.16-.8-4.23-2.38-5.8zM29.2 29.2c0 .08 0 .16.03.28-.06-.17-.1-.34-.1-.53 0-.8.63-1.43 1.44-1.43.3 0 .58.1.8.25-1.25-.4-2.17.56-2.17 1.43zm1.26 2.8c3.6.04 6.6 2.58 7.3 5.98-.94-2.03-3.84-3.5-7.33-3.54-3.46 0-6.4 1.45-7.36 3.46.75-3.38 3.76-5.9 7.4-5.9zM29 43.66c-3.06-.42-5.35-2.18-5.35-4.27 0-2.4 3.04-4.4 6.78-4.3h1.03c-2.18 2.1-2.6 5.4-2.45 8.5zm8.32-1.18c-1.3 2.65-3.96 4.33-6.9 4.33-2.92 0-5.6-1.6-6.9-4.3-.3-.6-.45-1.2-.54-1.9.84 2.16 3.82 3.75 7.42 3.78 3.6 0 6.6-1.57 7.45-3.7-.1.68-.28 1.3-.53 1.88z' opacity='.6'/%3E%3C/svg%3E") 50% 50% no-repeat !important;
76  top: 2px;
77  width: 20px;
78  height: 20px;
79  display: inline-block;
80  }
81  </style>
82  <?php
83  }
84 
85  /**
86  * Add Edit Entry links when on a single entry
87  *
88  * @since 1.13
89  * @return void
90  */
91  function add_edit_entry_link() {
92  /** @var WP_Admin_Bar $wp_admin_bar */
93  global $wp_admin_bar;
94 
95  $entry_id = $this->gravityview_view->getSingleEntry();
96 
97  if ( $entry_id && GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ), $entry_id ) ) {
98 
99  $entry = $this->gravityview_view->getEntry();
100 
101  $wp_admin_bar->add_menu( array(
102  'id' => 'edit-entry',
103  'parent' => 'gravityview',
104  'title' => __( 'Edit Entry', 'gravityview' ),
105  'meta' => array(
106  'title' => sprintf( __( 'Edit Entry %s', 'gravityview' ), GravityView_API::get_entry_slug( $entry['id'], $entry ) ),
107  ),
108  'href' => esc_url_raw( admin_url( sprintf( 'admin.php?page=gf_entries&amp;screen_mode=edit&amp;view=entry&amp;id=%d&lid=%d', $entry['form_id'], $entry['id'] ) ) ),
109  ) );
110 
111  }
112  }
113 
114  /**
115  * Add Edit View link when in embedded View
116  *
117  * @since 1.13
118  * @return void
119  */
120  function add_edit_view_and_form_link() {
121  /** @var WP_Admin_Bar $wp_admin_bar */
122  global $wp_admin_bar;
123 
124  if( GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview', 'gravityforms_edit_forms' ) ) ) {
125 
126  $view_data = GravityView_View_Data::getInstance();
127  $views = $view_data->get_views();
128 
129  // If there is a View embed, show Edit View link.
130  if ( ! empty( $views ) ) {
131 
132  $added_forms = array();
133  $added_views = array();
134 
135  foreach ( $views as $view ) {
136  $view = \GV\View::by_id( $view['id'] );
137  $view_id = $view->ID;
138  $form_id = $view->form ? $view->form->ID : null;
139 
140  $edit_view_title = __( 'Edit View', 'gravityview' );
141  $edit_form_title = __( 'Edit Form', 'gravityview' );
142 
143  if( sizeof( $views ) > 1 ) {
144  $edit_view_title = sprintf( _x( 'Edit View #%d', 'Edit View with the ID of %d', 'gravityview' ), $view_id );
145  $edit_form_title = sprintf( __( 'Edit Form #%d', 'Edit Form with the ID of %d', 'gravityview' ), $form_id );
146  }
147 
148  if( GVCommon::has_cap( 'edit_gravityview', $view_id ) && ! in_array( $view_id, $added_views ) ) {
149 
150  $added_views[] = $view_id;
151 
152  $wp_admin_bar->add_menu( array(
153  'id' => 'edit-view-' . $view_id,
154  'parent' => 'gravityview',
155  'title' => $edit_view_title,
156  'href' => esc_url_raw( admin_url( sprintf( 'post.php?post=%d&action=edit', $view_id ) ) ),
157  ) );
158  }
159 
160  if ( ! empty( $form_id ) && GVCommon::has_cap( array( 'gravityforms_edit_forms' ), $form_id ) && ! in_array( $form_id, $added_forms ) ) {
161 
162  $added_forms[] = $form_id;
163 
164  $wp_admin_bar->add_menu( array(
165  'id' => 'edit-form-' . $form_id,
166  'parent' => 'gravityview',
167  'title' => $edit_form_title,
168  'href' => esc_url_raw( admin_url( sprintf( 'admin.php?page=gf_edit_forms&id=%d', $form_id ) ) ),
169  ) );
170  }
171  }
172  }
173  }
174  }
175 
176  /**
177  * Remove "Edit Page" or "Edit View" links when on single entry.
178  *
179  * @since 1.17 Also remove when on GravityView post type; the new GravityView menu will be the one-stop shop.
180  * @since 1.13
181  *
182  * @return void
183  */
184  function remove_links() {
185 
186  // If we're on the single entry page, we don't want to cause confusion.
187  if ( $this->gravityview_view->getSingleEntry() || $this->gravityview_view->isGravityviewPostType() ) {
188  remove_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
189  }
190  }
191 }
192 
static getInstance( $passed_post=NULL)
Definition: class-data.php:120
remove_links()
Remove "Edit Page" or "Edit View" links when on single entry.
Handle management of the Admin Bar links.
static by_id( $post_id)
Construct a instance from a post ID.
if(empty( $created_by)) $form_id
add_floaty_icon()
Add the Floaty icon to the toolbar without loading the whole icon font.
static get_entry_slug( $id_or_string, $entry=array())
Get the entry slug for the entry.
Definition: class-api.php:497
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
$entry
Definition: notes.php:27
static getInstance()
Get the one true instantiated self.