Completed
Push — master ( 589d3f...c5163f )
by Zack
12:27 queued 08:09
created

GravityView_Admin_Bar::add_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 212.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Handle management of the Admin Bar links
5
 *
6
 * @since 1.13
7
 */
8
class GravityView_Admin_Bar {
9
10
	/**
11
	 * @var GravityView_frontend|null
12
	 */
13
	var $gravityview_view = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $gravityview_view.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
15
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 1
	function add_links() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
		/** @var WP_Admin_Bar $wp_admin_bar */
39 1
		global $wp_admin_bar;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
40
41 1
		if( is_admin() || ! GVCommon::has_cap( array( 'edit_gravityviews', 'gravityview_edit_entry', 'gravityforms_edit_forms' ) ) ) {
42
			return;
43
		}
44
45 1
		if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
46 1
			if ( ! gravityview()->views->count() ) {
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<GV\Core>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47 1
				return;
48
			}
49
		} else {
50
			/** Deprecated. Do not use GravityView_View_Data any more. See the `gravityview()->request` object instead. */
51
			$view_data = GravityView_View_Data::getInstance()->get_views();
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::get_views() has been deprecated.

This method has been deprecated.

Loading history...
52
			if ( empty( $view_data ) ) {
53
				return;
54
			}
55
		}
56
57 1
		$wp_admin_bar->add_menu( array(
58 1
			'id' => 'gravityview',
59 1
			'title' => __( 'GravityView', 'gravityview' ),
60 1
			'href' => '#',
61
		) );
62
63 1
		$this->add_edit_view_and_form_link();
64
65 1
		$this->add_edit_entry_link();
66
67 1
	}
68
69
	/**
70
	 * Add the Floaty icon to the toolbar without loading the whole icon font
71
	 *
72
	 * @since 1.17
73
	 *
74
	 * @return void
75
	 */
76
	public function add_floaty_icon() {
77
		?>
78
		<style>
79
			#wp-admin-bar-gravityview > .ab-item:before {
80
				content: '';
81
				<?php // Base64-encode so that it works in Firefox as well, even though https://css-tricks.com/probably-dont-base64-svg/ ?>
82
				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;
83
				top: 2px;
84
				width: 20px;
85
				height: 20px;
86
				display: inline-block;
87
			}
88
		</style>
89
		<?php
90
	}
91
92
	/**
93
	 * Add Edit Entry links when on a single entry
94
	 *
95
	 * @since 1.13
96
	 * @return void
97
	 */
98
	function add_edit_entry_link() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99
		/** @var WP_Admin_Bar $wp_admin_bar */
100
		global $wp_admin_bar;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
101
102
		$entry_id = $this->gravityview_view->getSingleEntry();
103
104
		if ( $entry_id && GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ), $entry_id ) ) {
105
106
			$entry = $this->gravityview_view->getEntry();
107
108
			$wp_admin_bar->add_menu( array(
109
				'id' => 'edit-entry',
110
				'parent' => 'gravityview',
111
				'title' => __( 'Edit Entry', 'gravityview' ),
112
				'meta' => array(
113
					'title' => sprintf( __( 'Edit Entry %s', 'gravityview' ), GravityView_API::get_entry_slug( $entry['id'], $entry ) ),
114
				),
115
				'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'] ) ) ),
116
			) );
117
118
		}
119
	}
120
121
	/**
122
	 * Add Edit View link when in embedded View
123
	 *
124
	 * @since 1.13
125
	 * @return void
126
	 */
127 1
	function add_edit_view_and_form_link() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
		/** @var WP_Admin_Bar $wp_admin_bar */
129 1
		global $wp_admin_bar;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
130
131 1
		if( GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview', 'gravityforms_edit_forms' ) ) ) {
132
133 1
			if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
134 1
				$views = gravityview()->views->all();
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<GV\Core>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
			} else {
136
				/** Deprecated. Use no more, please. See: `gravityview()->views`*/
137
				$view_data = GravityView_View_Data::getInstance();
138
				$views = $view_data->get_views();
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::get_views() has been deprecated.

This method has been deprecated.

Loading history...
139
			}
140
141
			// If there is a View embed, show Edit View link.
142 1
			if ( ! empty( $views ) ) {
143
144 1
				$added_forms = array();
145 1
				$added_views = array();
146
147 1
				foreach ( $views as $view ) {
148
149 1
					if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
150
						/** `$view` is now a \GV\View object, not an array. */
151 1
						$view_id = $view->ID;
152 1
						$form_id = $view->form ? $view->form->ID : null;
153
					} else {
154
						/** Deprecated. */
155
						$view_id = $view['id'];
156
						$form_id = $view['form_id'];
157
					}
158
159 1
					$edit_view_title = __( 'Edit View', 'gravityview' );
160 1
					$edit_form_title = __( 'Edit Form', 'gravityview' );
161
162 1
					if( sizeof( $views ) > 1 ) {
163 1
						$edit_view_title = sprintf( _x( 'Edit View #%d', 'Edit View with the ID of %d', 'gravityview' ), $view_id );
164 1
						$edit_form_title = sprintf( __( 'Edit Form #%d', 'Edit Form with the ID of %d', 'gravityview' ), $form_id );
165
					}
166
167 1
					if( GVCommon::has_cap( 'edit_gravityview', $view_id ) && ! in_array( $view_id, $added_views ) ) {
168
169 1
						$added_views[] = $view_id;
170
171 1
						$wp_admin_bar->add_menu( array(
172 1
							'id'    => 'edit-view-' . $view_id,
173 1
							'parent' => 'gravityview',
174 1
							'title' => $edit_view_title,
175 1
							'href'  => esc_url_raw( admin_url( sprintf( 'post.php?post=%d&action=edit', $view_id ) ) ),
176
						) );
177
					}
178
179 1
					if ( ! empty( $form_id ) && GVCommon::has_cap( array( 'gravityforms_edit_forms' ), $form_id ) && ! in_array( $form_id, $added_forms )  ) {
180
181 1
						$added_forms[] = $form_id;
182
183 1
						$wp_admin_bar->add_menu( array(
184 1
							'id'    => 'edit-form-' . $form_id,
185 1
							'parent' => 'gravityview',
186 1
							'title' => $edit_form_title,
187 1
							'href' => esc_url_raw( admin_url( sprintf( 'admin.php?page=gf_edit_forms&id=%d', $form_id ) ) ),
188
						) );
189
					}
190
				}
191
			}
192
		}
193 1
	}
194
195
	/**
196
	 * Remove "Edit Page" or "Edit View" links when on single entry.
197
	 *
198
	 * @since 1.17 Also remove when on GravityView post type; the new GravityView menu will be the one-stop shop.
199
	 * @since 1.13
200
	 *
201
	 * @return void
202
	 */
203
	function remove_links() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
204
205
		// If we're on the single entry page, we don't want to cause confusion.
206
		if ( $this->gravityview_view->getSingleEntry() || $this->gravityview_view->isGravityviewPostType() ) {
207
			remove_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
208
		}
209
	}
210
}
211
212
new GravityView_Admin_Bar;