Completed
Pull Request — master (#1461)
by Zack
17:37 queued 14:56
created

GravityView_Edit_Entry_Admin::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 24
ccs 0
cts 11
cp 0
crap 6
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * GravityView Edit Entry - Admin logic
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
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_Edit_Entry_Admin {
18
19
    protected $loader;
20
21
    function __construct( GravityView_Edit_Entry $loader ) {
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...
22
        $this->loader = $loader;
23
    }
24
25
    function load() {
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...
26
27
        if( !is_admin() ) {
28
            return;
29
        }
30
31
        // Add Edit Link as a default field, outside those set in the Gravity Form form
32
        add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
33
34
        // For the Edit 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_edit_link_options', array( $this, 'edit_link_field_options' ), 10, 5 );
39
40
        // add tooltips
41
        add_filter( 'gravityview/metaboxes/tooltips', array( $this, 'tooltips') );
42
43
        // custom fields' options for zone EDIT
44
        add_filter( 'gravityview_template_field_options', array( $this, 'field_options' ), 10, 6 );
45
46
        // Add Edit Entry settings to View Settings
47
        add_action( 'gravityview/metaboxes/edit_entry', array( $this, 'view_settings_metabox' ) );
48
    }
49
50
	/**
51
	 * Render Edit Entry View metabox settings
52
	 *
53
	 * @since 2.9
54
	 *
55
	 * @param $current_settings
56
	 *
57
	 * @return void
58
	 */
59
	public function view_settings_metabox( $current_settings ) {
60
61
		GravityView_Render_Settings::render_setting_row( 'edit_locking', $current_settings );
62
63
		GravityView_Render_Settings::render_setting_row( 'user_edit', $current_settings );
64
65
		GravityView_Render_Settings::render_setting_row( 'unapprove_edit', $current_settings );
66
67
		GravityView_Render_Settings::render_setting_row( 'edit_redirect', $current_settings );
68
69
		GravityView_Render_Settings::render_setting_row( 'edit_redirect_url', $current_settings );
70
	}
71
72
    /**
73
     * Add Edit Link as a default field, outside those set in the Gravity Form form
74
     * @param array $entry_default_fields Existing fields
75
     * @param  string|array $form form_ID or form object
76
     * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
77
     */
78
    function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) {
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...
79
80
        if( $zone !== 'edit' ) {
81
82
            $entry_default_fields['edit_link'] = array(
83
                'label' => __('Edit Entry', 'gravityview'),
84
                'type' => 'edit_link',
85
                'desc'	=> __('A link to edit the entry. Visible based on View settings.', 'gravityview'),
86
            );
87
88
        }
89
90
        return $entry_default_fields;
91
    }
92
93
    /**
94
     * Change wording for the Edit context to read Entry Creator
95
     *
96
     * @param  array 	   $visibility_caps        Array of capabilities to display in field dropdown.
97
     * @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...
98
     * @param  string      $template_id Table slug
99
     * @param  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
100
     * @param  string      $context     What context are we in? Example: `single` or `directory`
101
     * @param  string      $input_type  (textarea, list, select, etc.)
102
     * @return array                   Array of field options with `label`, `value`, `type`, `default` keys
103
     */
104
    function modify_visibility_caps( $visibility_caps = array(), $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
0 ignored issues
show
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...
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...
105
106
        $caps = $visibility_caps;
107
108
        // If we're configuring fields in the edit context, we want a limited selection
109
        if( $context === 'edit' ) {
110
111
            // Remove other built-in caps.
112
            unset( $caps['publish_posts'], $caps['gravityforms_view_entries'], $caps['delete_others_posts'] );
113
114
            $caps['read'] = _x('Entry Creator','User capability', 'gravityview');
115
        }
116
117
        return $caps;
118
    }
119
120
    /**
121
     * Add "Edit Link Text" setting to the edit_link field settings
122
     * @param  [type] $field_options [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
123
     * @param  [type] $template_id   [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
124
     * @param  [type] $field_id      [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     * @param  [type] $context       [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
     * @param  [type] $input_type    [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
127
     * @return [type]                [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
128
     */
129
    function edit_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
0 ignored issues
show
Unused Code introduced by
The parameter $template_id 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 $field_id 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 $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...
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...
130
131
        // Always a link, never a filter
132
        unset( $field_options['show_as_link'], $field_options['search_filter'] );
133
134
        // Edit Entry link should only appear to visitors capable of editing entries
135
        unset( $field_options['only_loggedin'], $field_options['only_loggedin_cap'] );
136
137
        $add_option['edit_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...
138
            'type' => 'text',
139
            'label' => __( 'Edit Link Text', 'gravityview' ),
140
            'desc' => NULL,
141
            'value' => __('Edit Entry', 'gravityview'),
142
            'merge_tags' => true,
143
        );
144
145
        return array_merge( $add_option, $field_options );
146
    }
147
148
    /**
149
     * Add tooltips
150
     * @param  array $tooltips Existing tooltips
151
     * @return array           Modified tooltips
152
     */
153
    function tooltips( $tooltips ) {
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...
154
155
        $return = $tooltips;
156
157
        $return['allow_edit_cap'] = array(
158
            'title' => __('Limiting Edit Access', 'gravityview'),
159
            'value' => __('Change this setting if you don\'t want the user who created the entry to be able to edit this field.', 'gravityview'),
160
        );
161
162
        return $return;
163
    }
164
165
    /**
166
     * Manipulate the fields' options for the EDIT ENTRY screen
167
     * @param  [type] $field_options [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
168
     * @param  [type] $template_id   [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
169
     * @param  [type] $field_id      [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
170
     * @param  [type] $context       [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
171
     * @param  [type] $input_type    [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
172
     * @return [type]                [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
173
     */
174
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id 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...
175
176
        // We only want to modify the settings for the edit context
177
        if( 'edit' !== $context ) {
178
179
            /**
180
             * @since 1.8.4
181
             */
182
            $field_options['new_window'] = array(
183
                'type' => 'checkbox',
184
                'label' => __( 'Open link in a new tab or window?', 'gravityview' ),
185
                'value' => false,
186
            );
187
188
            return $field_options;
189
        }
190
191
        //  Entry field is only for logged in users
192
        unset( $field_options['only_loggedin'], $field_options['only_loggedin_cap'] );
193
194
        $add_options = array(
195
            'allow_edit_cap' => array(
196
                'type' => 'select',
197
                'label' => __( 'Make field editable to:', 'gravityview' ),
198
                'choices' => GravityView_Render_Settings::get_cap_choices( $template_id, $field_id, $context, $input_type ),
199
                'tooltip' => 'allow_edit_cap',
200
                'class' => 'widefat',
201
                'value' => 'read', // Default: entry creator
202
            ),
203
        );
204
205
        return array_merge( $field_options, $add_options );
206
    }
207
208
209
} // end class
210