Completed
Pull Request — develop (#1499)
by Zack
27:50 queued 07:53
created

modify_visibility_caps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 5
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * GravityView Delete Entry - Admin logic
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    GravityView <[email protected]>
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 */
11
12
if ( ! defined( 'WPINC' ) ) {
13
	die;
14
}
15
16
17
class GravityView_Delete_Entry_Admin {
18
19
	protected $loader;
20
21
	public function __construct( GravityView_Delete_Entry $loader ) {
22
		$this->loader = $loader;
23
	}
24
25
	public function load() {
26
27
		if ( ! is_admin() ) {
28
			return;
29
		}
30
31
		// Add Delete Entry settings to View Settings Metabox.
32
		add_action( 'gravityview/metaboxes/delete_entry', array( $this, 'view_settings_metabox' ) );
33
34
		// For the Delete Entry Link, you don't want visible to all users.
35
		add_filter( 'gravityview_field_visibility_caps', array( $this, 'modify_visibility_caps' ), 10, 5 );
36
37
		// Modify the field options based on the name of the field type
38
		add_filter( 'gravityview_template_delete_link_options', array( $this, 'delete_link_field_options' ), 10, 5 );
39
40
		// Add Delete Entry settings to View Settings
41
		add_action( 'gravityview/metaboxes/delete_entry', array( $this, 'view_settings_delete_entry_metabox' ), 7 );
42
43
		// Add Delete Link as a default field, outside those set in the Gravity Form form
44
		add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
45
	}
46
47
	/**
48
	 * Render Delete Entry View metabox settings
49
	 *
50
	 * @since 2.9.1
51
	 *
52
	 * @param $current_settings
53
	 *
54
	 * @return void
55
	 */
56
	public function view_settings_metabox( $current_settings ) {
57
58
		GravityView_Render_Settings::render_setting_row( 'delete_redirect', $current_settings );
59
60
		GravityView_Render_Settings::render_setting_row( 'delete_redirect_url', $current_settings );
61
	}
62
63
	/**
64
	 * Change wording for the Edit context to read Entry Creator
65
	 *
66
	 * @since 1.5.1
67
	 * @since 2.9.2 Moved here from GravityView_Delete_Entry
68
	 *
69
	 * @param array $visibility_caps Array of capabilities to display in field dropdown.
70
	 * @param string $field_type Type of field options to render (`field` or `widget`)
0 ignored issues
show
Bug introduced by
There is no parameter named $field_type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
71
	 * @param string $template_id Table slug
72
	 * @param float $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
73
	 * @param string $context What context are we in? Example: `single` or `directory`
74
	 * @param string $input_type (textarea, list, select, etc.)
75
	 *
76
	 * @return array                   Array of field options with `label`, `value`, `type`, `default` keys
77
	 */
78
	public function modify_visibility_caps( $visibility_caps = array(), $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $input_type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
80
		$caps = $visibility_caps;
81
82
		// If we're configuring fields in the edit context, we want a limited selection
83
		if ( $field_id === 'delete_link' ) {
84
85
			// Remove other built-in caps.
86
			unset( $caps['publish_posts'], $caps['gravityforms_view_entries'], $caps['delete_others_posts'] );
87
88
			$caps['read'] = _x( 'Entry Creator', 'User capability', 'gravityview' );
89
		}
90
91
		return $caps;
92
	}
93
94
	/**
95
	 * Add "Delete Link Text" setting to the edit_link field settings
96
	 *
97
	 * @since 1.5.1
98
	 * @since 2.9.2 Moved here from GravityView_Delete_Entry
99
	 *
100
	 * @param array $field_options
101
	 * @param string $template_id
102
	 * @param string $field_id
103
	 * @param string $context
104
	 * @param string $input_type
105
	 *
106
	 * @return array $field_options, with "Delete Link Text" and "Allow the following users to delete the entry:" field options.
107
	 */
108
	public function delete_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
109
110
		// Always a link, never a filter
111
		unset( $field_options['show_as_link'], $field_options['search_filter'] );
112
113
		// Delete Entry link should only appear to visitors capable of editing entries
114
		unset( $field_options['only_loggedin'], $field_options['only_loggedin_cap'] );
115
116
		$add_option['delete_link'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$add_option was never initialized. Although not strictly required by PHP, it is generally a good practice to add $add_option = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
117
			'type'       => 'text',
118
			'label'      => __( 'Delete Link Text', 'gravityview' ),
119
			'desc'       => null,
120
			'value'      => __( 'Delete Entry', 'gravityview' ),
121
			'merge_tags' => true,
122
		);
123
124
		$field_options['allow_edit_cap'] = array(
125
			'type'    => 'select',
126
			'label'   => __( 'Allow the following users to delete the entry:', 'gravityview' ),
127
			'choices' => GravityView_Render_Settings::get_cap_choices( $template_id, $field_id, $context, $input_type ),
128
			'tooltip' => 'allow_edit_cap',
129
			'class'   => 'widefat',
130
			'value'   => 'read', // Default: entry creator
131
		);
132
133
		return array_merge( $add_option, $field_options );
134
	}
135
136
137
	/**
138
	 * Add Delete Entry Link to the Add Field dialog
139
	 *
140
	 * @since 1.5.1
141
	 * @since 2.9.2 Moved here from GravityView_Delete_Entry
142
	 *
143
	 * @param array $available_fields
144
	 *
145
	 * @return array
146
	 */
147
	public function add_available_field( $available_fields = array() ) {
148
149
		$available_fields['delete_link'] = array(
150
			'label_text'    => __( 'Delete Entry', 'gravityview' ),
151
			'field_id'      => 'delete_link',
152
			'label_type'    => 'field',
153
			'input_type'    => 'delete_link',
154
			'field_options' => null,
155
			'icon'          => 'dashicons-trash',
156
		);
157
158
		return $available_fields;
159
	}
160
161
	/**
162
	 * Render Delete Entry Permissions settings
163
	 *
164
	 * @since 2.9.2
165
	 *
166
	 * @param $current_settings
167
	 *
168
	 * @return void
169
	 */
170
	public function view_settings_delete_entry_metabox( $current_settings ) {
171
172
		GravityView_Render_Settings::render_setting_row( 'user_delete', $current_settings );
173
174
	}
175
176
	/**
177
	 * Add Edit Link as a default field, outside those set in the Gravity Form form
178
	 *
179
	 * @since 1.5.1
180
	 * @since 2.9.2 Moved here from GravityView_Delete_Entry
181
	 *
182
	 * @param array $entry_default_fields Existing fields
183
	 * @param string|array $form form_ID or form object
184
	 * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer'
185
	 *
186
	 * @return array
187
	 */
188
	public function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) {
189
190
		if ( 'edit' !== $zone ) {
191
			$entry_default_fields['delete_link'] = array(
192
				'label' => __( 'Delete Entry', 'gravityview' ),
193
				'type'  => 'delete_link',
194
				'desc'  => __( 'A link to delete the entry. Respects the Delete Entry permissions.', 'gravityview' ),
195
			);
196
		}
197
198
		return $entry_default_fields;
199
	}
200
}
201