Completed
Push — master ( 022e44...5d3455 )
by Zack
08:11 queued 31s
created

GravityView_Admin_Bar   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 108
rs 10
wmc 16
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add_hooks() 0 4 1
A add_links() 0 9 1
A add_edit_entry_link() 0 18 3
B add_edit_view_link() 0 25 6
A remove_links() 0 7 4
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 117.

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
	}
29
30
	/**
31
	 * Add helpful GV links to the menu bar, like Edit Entry on single entry page.
32
	 *
33
	 * @since 1.13
34
	 * @return void
35
	 */
36
	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...
37
		/** @var WP_Admin_Bar $wp_admin_bar */
38
		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...
39
40
		$this->add_edit_entry_link();
41
42
		$this->add_edit_view_link();
43
44
	}
45
46
	/**
47
	 * Add Edit Entry links when on a single entry
48
	 *
49
	 * @since 1.13
50
	 * @return void
51
	 */
52
	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...
53
		/** @var WP_Admin_Bar $wp_admin_bar */
54
		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...
55
56
		$entry_id = $this->gravityview_view->getSingleEntry();
57
58
		if ( $entry_id && GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ), $entry_id ) ) {
59
60
			$entry = $this->gravityview_view->getEntry();
61
62
			$wp_admin_bar->add_menu( array(
63
				'id' => 'edit-entry',
64
				'title' => __( 'Edit Entry', 'gravityview' ),
65
				'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'] ) ) ),
66
			) );
67
68
		}
69
	}
70
71
	/**
72
	 * Add Edit View link when in embedded View
73
	 *
74
	 * @since 1.13
75
	 * @return void
76
	 */
77
	function add_edit_view_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...
78
		/** @var WP_Admin_Bar $wp_admin_bar */
79
		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...
80
81
		if( GVCommon::has_cap('edit_gravityviews') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
82
83
			$view_data = GravityView_View_Data::getInstance();
84
85
			$views = $view_data->get_views();
86
87
			// If there is a View embed, shor Edit View link.
88
			// todo: Support multiple View embeds with a drop-down menu
89
			if ( ! $this->gravityview_view->isGravityviewPostType() && ! empty( $views ) && ! $view_data->has_multiple_views() ) {
90
				$view = reset( $views );
91
92
				if( GVCommon::has_cap( 'edit_gravityview', $view['id'] ) ) {
93
					$wp_admin_bar->add_menu( array(
94
						'id'    => 'edit-view',
95
						'title' => __( 'Edit View', 'gravityview' ),
96
						'href'  => esc_url_raw( admin_url( sprintf( 'post.php?post=%d&action=edit', $view['id'] ) ) ),
97
					) );
98
				}
99
			}
100
		}
101
	}
102
103
	/**
104
	 * Remove "Edit Page" or "Edit View" links when on single entry pages
105
	 * @since 1.13
106
	 * @return void
107
	 */
108
	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...
109
110
		// If we're on the single entry page, we don't want to cause confusion.
111
		if ( is_admin() || ( $this->gravityview_view->getSingleEntry() && ! $this->gravityview_view->isGravityviewPostType() ) ) {
112
			remove_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
113
		}
114
	}
115
}
116
117
new GravityView_Admin_Bar;