Completed
Push — master ( 9b310a...05ab62 )
by Zack
46:38 queued 42:54
created

Legacy_Context::thaw()   D

Complexity

Conditions 28
Paths 28

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 85
CRAP Score 28

Importance

Changes 0
Metric Value
cc 28
nc 28
nop 1
dl 0
loc 87
ccs 85
cts 85
cp 1
crap 28
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 15 and the first side effect is on line 692.

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
namespace GV\Mocks;
3
4
/**
5
 * This file contains mock code for deprecated functions.
6
 */
7
8
/**
9
 * @see \GravityView_View_Data::add_view
10
 * @internal
11
 * @since 2.0
12
 *
13
 * @return array|false The old array data, or false on error.
14
 */
15
function GravityView_View_Data_add_view( $view_id, $atts, $_this ) {
0 ignored issues
show
Coding Style introduced by
The function name GravityView_View_Data_add_view is in camel caps, but expected _gravity_view_view_data_add_view instead as per the coding standard.
Loading history...
16
	/** Handle array of IDs. */
17
	if ( is_array( $view_id ) ) {
18
		foreach ( $view_id as $id ) {
19
			call_user_func( __FUNCTION__, $id, $atts, $_this );
20
		}
21
22
		if ( ! $_this->views->count() ) {
23
			return array();
24
		}
25
26
		return array_combine(
27
			array_map( function( $view ) { return $view->ID; }, $_this->views->all() ),
28
			array_map( function( $view ) { return $view->as_data(); }, $_this->views->all() )
29
		);
30
	}
31
32
	/** View has been set already. */
33
	if ( $view = $_this->views->get( $view_id ) ) {
34
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s already exists.', $view_id ) );
35
		return $view->as_data();
36
	}
37
38
	$view = \GV\View::by_id( $view_id );
39
	if ( ! $view ) {
40
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id ) );
41
		return false;
42
	}
43
44
	/** Doesn't have a connected form. */
45
	if ( ! $view->form ) {
46
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; Post ID #%s does not have a connected form.', $view_id ) );
47
		return false;
48
	}
49
50
	/** Update the settings */
51
	if ( is_array( $atts ) ) {
52
		$view->settings->update( $atts );
53
	}
54
55
	$_this->views->add( $view );
56
57
	return $view->as_data();
0 ignored issues
show
Deprecated Code introduced by
The method GV\View::as_data() has been deprecated.

This method has been deprecated.

Loading history...
58
}
59
60
/**
61
 * @see \GravityView_frontend::get_view_entries
62
 * @internal
63
 * @since 2.0
64
 *
65
 * @return array The old associative array data as returned by
66
 *  \GravityView_frontend::get_view_entries(), the paging parameters
67
 *  and a total count of all entries.
68
 */
69
function GravityView_frontend_get_view_entries( $args, $form_id, $parameters, $count ) {
0 ignored issues
show
Coding Style introduced by
The function name GravityView_frontend_get_view_entries is in camel caps, but expected _gravity_view_frontend_get_view_entries instead as per the coding standard.
Loading history...
70 2
	$form = \GV\GF_Form::by_id( $form_id );
71
72
	/**
73
	 * Kick off all advanced filters.
74
	 *
75
	 * Parameters and criteria are pretty much the same thing here, just
76
	 *  different naming, where `$parameters` are the initial parameters
77
	 *  calculated for hte view, and `$criteria` are the filtered ones
78
	 *  retrieved via `GVCommon::calculate_get_entries_criteria`.
79
	 */
80 2
	$criteria = \GVCommon::calculate_get_entries_criteria( $parameters, $form->ID );
81
82 2
	do_action( 'gravityview_log_debug', '[gravityview_get_entries] Final Parameters', $criteria );
83
84
	/** ...and all the (now deprectated) filters that usually follow `gravityview_get_entries` */
85
86
	/**
87
	 * @deprecated
88
	 * Do not use this filter anymore.
89
	 */
90 2
	$entries = apply_filters_ref_array( 'gravityview_before_get_entries', array( null, $criteria, $parameters, &$count ) );
91
92 2
	if ( ! is_null( $entries ) ) {
93
		/**
94
		 * We've been given an entries result that we can return,
95
		 *  just set the paging and we're good to go.
96
		 */
97 2
		$paging = \GV\Utils::get( $parameters, 'paging' );
98
	} else {
99 2
		$entries = new \GV\Entry_Collection();
100 2
		if ( $view = \GV\View::by_id( \GV\Utils::get( $args, 'id' ) ) ) {
101 2
			$view->settings->update( $args );
102 2
			$entries = $view->get_entries( gravityview()->request );
103
		}
104
105 2
		$page = \GV\Utils::get( $parameters['paging'], 'current_page' ) ?
106 2
			: ( ( ( $parameters['paging']['offset'] - $view->settings->get( 'offset' ) ) / $parameters['paging']['page_size'] ) + 1 );
107
108
		/** Set paging, count and unwrap the entries. */
109
		$paging = array(
110 2
			'offset' => ( $page - 1 ) * $view->settings->get( 'page_size' ),
111 2
			'page_size' => $view->settings->get( 'page_size' ),
112
		);
113
		/**
114
		 * GF_Query does not subtract the offset, we have to subtract it ourselves.
115
		 */
116 2
		$count = $entries->total() - ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ? $view->settings->get( 'offset' ) : 0 );
117
		$entries = array_map( function( $e ) { return $e->as_entry(); }, $entries->all() );
118
	}
119
120
	/** Just one more filter, for compatibility's sake! */
121
122
	/**
123
	 * @deprecated
124
	 * Do not use this filter anymore.
125
	 */
126 2
	$entries = apply_filters_ref_array( 'gravityview_entries', array( $entries, $criteria, $parameters, &$count ) );
127
128 2
	return array( $entries, $paging, $count );
129
}
130
131
/**
132
 * The old function does a bit too much, not only does it retrieve
133
 *  the value for a field, but it also renders some output. We are
134
 *  stubbing the plain value part of it, the rendering will follow once
135
 *  our field renderers are ready.
136
 *
137
 * @see \GravityView_API::field_value
138
 * @deprecated Use \GV\Field_Template::render()
139
 * @internal
140
 * @since 2.0
141
 *
142
 * @return null|string The value of a field in an entry.
143
 */
144
function GravityView_API_field_value( $entry, $field_settings, $format ) {
0 ignored issues
show
Unused Code introduced by
The parameter $format 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...
Coding Style introduced by
The function name GravityView_API_field_value is in camel caps, but expected _gravity_view_a_p_i_field_value instead as per the coding standard.
Loading history...
145 3
	if ( empty( $entry['form_id'] ) || empty( $field_settings['id'] ) ) {
146 1
		gravityview()->log->error( 'No entry or field_settings[id] supplied', array( 'data' => array( func_get_args() ) ) );
147 1
		return null;
148
	}
149
150 3
	if ( ! empty( $entry['_multi'] ) && ! empty( $field_settings['form_id'] ) && ! empty( $entry['_multi'][ $field_settings['form_id'] ] ) ) {
151 2
		$multientry = \GV\Multi_Entry::from_entries( array_map( '\GV\GF_Entry::from_entry', $entry['_multi'] ) );
152 2
		$entry = $entry['_multi'][ $field_settings['form_id'] ];
153
	}
154
155 3
	if ( empty( $entry['id'] ) || ! $entry = \GV\GF_Entry::by_id( $entry['id'] ) ) {
156
		gravityview()->log->error( 'Invalid \GV\GF_Entry supplied', array( 'data' => $entry ) );
157
		return null;
158
	}
159
160
	/**
161
	 * Determine the source backend.
162
	 *
163
	 * Fields with a numeric ID are Gravity Forms ones.
164
	 */
165 3
	$source = is_numeric( $field_settings['id'] ) ? \GV\Source::BACKEND_GRAVITYFORMS : \GV\Source::BACKEND_INTERNAL;;
166
167
	/** Initialize the future field. */
168
	switch ( $source ):
169
		/** The Gravity Forms backend. */
170 3
		case \GV\Source::BACKEND_GRAVITYFORMS:
171 3
			if ( ! $form = \GV\GF_Form::by_id( $entry['form_id'] ) ) {
172
				gravityview()->log->error( 'No form Gravity Form found for entry', array( 'data' => $entry ) );
173
				return null;
174
			}
175
176 3
			if ( ! $field = $form::get_field( $form, $field_settings['id'] ) ) {
177
				return null;
178
			}
179
180 3
			break;
181
182
		/** Our internal backend. */
183 3
		case \GV\Source::BACKEND_INTERNAL:
184 3
			if ( ! $field = \GV\Internal_Source::get_field( $field_settings['id'] ) ) {
185
				return null;
186
			}
187
188 3
			break;
189
190
		/** An unidentified backend. */
191
		default:
192
			gravityview()->log->error( 'Could not determine source for entry', array( 'data' => array( func_get_args() ) ) );
193
			return null;
194
	endswitch;
195
196
	/** Add the field settings. */
197 3
	$field->update_configuration( $field_settings );
198
199 3
	$renderer = new \GV\Field_Renderer();
200 3
	return $renderer->render( $field, gravityview()->views->get(), $source == \GV\Source::BACKEND_GRAVITYFORMS ? $form : null, isset( $multientry ) ? $multientry : $entry, gravityview()->request );
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...
Bug introduced by
The variable $form does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
201
}
202
203
/**
204
 * Mock out the \GravityView_API::field_label method
205
 *
206
 * Uses the new \GV\Field::get_label methods
207
 *
208
 * @see \GravityView_API::field_label
209
 * @internal
210
 * @since 2.0
211
 *
212
 * @return string The label of a field in an entry.
213
 */
214
function GravityView_API_field_label( $form, $field_settings, $entry, $force_show_label = false ) {
0 ignored issues
show
Coding Style introduced by
The function name GravityView_API_field_label is in camel caps, but expected _gravity_view_a_p_i_field_label instead as per the coding standard.
Loading history...
215
216
	/** A bail condition. */
217
	$bail = function( $label, $field_settings, $entry, $force_show_label, $form ) {
218 2
		if ( ! empty( $field_settings['show_label'] ) || $force_show_label ) {
219
220 2
			$label = isset( $field_settings['label'] ) ? $field_settings['label'] : '';
221
222
			// Use Gravity Forms label by default, but if a custom label is defined in GV, use it.
223 2
			if ( ! empty( $field_settings['custom_label'] ) ) {
224 1
				$label = \GravityView_API::replace_variables( $field_settings['custom_label'], $form, $entry );
225
			}
226
227
			/**
228
			 * @filter `gravityview_render_after_label` Append content to a field label
229
			 * @param[in,out] string $appended_content Content you can add after a label. Empty by default.
230
			 * @param[in] array $field GravityView field array
231
			 */
232 2
			$label .= apply_filters( 'gravityview_render_after_label', '', $field_settings );
233
		}
234
235
		/**
236
		 * @filter `gravityview/template/field_label` Modify field label output
237
		 * @since 1.7
238
		 * @param[in,out] string $label Field label HTML
239
		 * @param[in] array $field GravityView field array
240
		 * @param[in] array $form Gravity Forms form array
241
		 * @param[in] array $entry Gravity Forms entry array
242
		 */
243 2
		$label = apply_filters( 'gravityview/template/field_label', $label, $field_settings, $form, $entry );
244
245 2
		return $label;
246 3
	};
247
248 3
	$label = '';
249
250 3
	if ( ! empty( $entry['_multi'] ) && ! empty( $field_settings['form_id'] ) && ! empty( $entry['_multi'][ $field_settings['form_id'] ] ) ) {
251 2
		$entry = $entry['_multi'][ $field_settings['form_id'] ];
252 2
		if ( $_form = \GV\GF_Form::by_id( $field_settings['form_id'] ) ) {
253 2
			$form = $_form->form;
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
254
		}
255
	}
256
257 3
	if ( empty( $entry['form_id'] ) || empty( $field_settings['id'] ) ) {
258 2
		gravityview()->log->error( 'No entry or field_settings[id] supplied', array( 'data' => array( func_get_args() ) ) );
259 2
		return $bail( $label, $field_settings, $entry, $force_show_label, $form );
260
	}
261
262 3
	if ( empty( $entry['id'] ) || ! $gv_entry = \GV\GF_Entry::by_id( $entry['id'] ) ) {
263
		gravityview()->log->error( 'Invalid \GV\GF_Entry supplied', array( 'data' => $entry ) );
264
		return $bail( $label, $field_settings, $entry, $force_show_label, $form );
265
	}
266
267 3
	$entry = $gv_entry;
268
269
	/**
270
	 * Determine the source backend.
271
	 *
272
	 * Fields with a numeric ID are Gravity Forms ones.
273
	 */
274 3
	$source = is_numeric( $field_settings['id'] ) ? \GV\Source::BACKEND_GRAVITYFORMS : \GV\Source::BACKEND_INTERNAL;
275
276
	/** Initialize the future field. */
277
	switch ( $source ):
278
		/** The Gravity Forms backend. */
279 3
		case \GV\Source::BACKEND_GRAVITYFORMS:
280 2
			if ( ! $gf_form = \GV\GF_Form::by_id( $entry['form_id'] ) ) {
281
				gravityview()->log->error( 'No form Gravity Form found for entry', array( 'data' => $entry ) );
282
				return $bail( $label, $field_settings, $entry->as_entry(), $force_show_label, $form );
283
			}
284
285 2
			if ( ! $field = $gf_form::get_field( $gf_form, $field_settings['id'] ) ) {
286
				gravityview()->log->error( 'No field found for specified form and field ID #{field_id}', array( 'field_id' => $field_settings['id'], 'data' => $form ) );
287
				return $bail( $label, $field_settings, $entry->as_entry(), $force_show_label, $gf_form->form );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
288
			}
289 2
			if ( empty( $field_settings['show_label'] ) ) {
290
				/** The label never wins... */
291 1
				$field_settings['label'] = '';
292
			}
293
294 2
			break;
295
296
		/** Our internal backend. */
297 3
		case \GV\Source::BACKEND_INTERNAL:
298 3
			if ( ! $field = \GV\Internal_Source::get_field( $field_settings['id'] ) ) {
299
				return $bail( $label, $field_settings, $entry->as_entry(), $force_show_label, $form );
300
			}
301 3
			break;
302
303
		/** An unidentified backend. */
304
		default:
305
			gravityview()->log->error( 'Could not determine source for entry. Using empty field.', array( 'data' => array( func_get_args() ) ) );
306
			$field = new \GV\Field();
307
			break;
308
	endswitch;
309
310 3
	if( $force_show_label ) {
311 1
		$field_settings['show_label'] = '1';
312
	}
313
314
	/** Add the field settings. */
315 3
	$field->update_configuration( $field_settings );
316
317 3
	if ( ! empty( $field->show_label ) ) {
318
319 3
		$label = $field->get_label( null, isset( $gf_form ) ? $gf_form : null, $entry );
320
321
		/**
322
		 * @filter `gravityview_render_after_label` Append content to a field label
323
		 * @param[in,out] string $appended_content Content you can add after a label. Empty by default.
324
		 * @param[in] array $field GravityView field array
325
		 */
326 3
		$label .= apply_filters( 'gravityview_render_after_label', '', $field->as_configuration() );
327
328
	}
329
330
	/**
331
	 * @filter `gravityview/template/field_label` Modify field label output
332
	 * @since 1.7
333
	 * @param[in,out] string $label Field label HTML
334
	 * @param[in] array $field GravityView field array
335
	 * @param[in] array $form Gravity Forms form array
336
	 * @param[in] array $entry Gravity Forms entry array
337
	 */
338 3
	return apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), isset( $gf_form ) ? $gf_form->form : $form, $entry->as_entry() );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
339
}
340
341
342
/**
343
 * A manager of legacy global states and contexts.
344
 *
345
 * Handles mocking of:
346
 * - \GravityView_View_Data
347
 * - \GravityView_View
348
 * - \GravityView_frontend
349
 *
350
 * Allows us to set a specific state globally using the old
351
 *  containers, then reset it. Useful for legacy code that keeps
352
 *  on depending on these variables.
353
 *
354
 * Some examples right now include template files, utility functions,
355
 *  some actions and filters that expect the old contexts to be set.
356
 */
357
final class Legacy_Context {
358
	private static $stack = array();
359
360
	/**
361
	 * Set the state depending on the provided configuration.
362
	 *
363
	 * Saves current global state and context.
364
	 *
365
	 * Configuration keys:
366
	 *
367
	 * - \GV\View	view:		sets \GravityView_View::atts, \GravityView_View::view_id,
368
	 *								 \GravityView_View::back_link_label
369
	 *								 \GravityView_frontend::context_view_id,
370
	 *								 \GravityView_View::form, \GravityView_View::form_id
371
	 * - \GV\Field	field:		sets \GravityView_View::_current_field, \GravityView_View::field_data,
372
	 * - \GV\Entry	entry:		sets \GravityView_View::_current_entry, \GravityView_frontend::single_entry,
373
	 *								 \GravityView_frontend::entry
374
	 * - \WP_Post	post:		sets \GravityView_View::post_id, \GravityView_frontend::post_id,
375
	 *								 \GravityView_frontend::is_gravityview_post_type,
376
	 *								 \GravityView_frontend::post_has_shortcode
377
	 * - array		paging:		sets \GravityView_View::paging
378
	 * - array		sorting:	sets \GravityView_View::sorting
379
	 * - array		template:	sets \GravityView_View::template_part_slug, \GravityView_View::template_part_name
380
	 *
381
	 * - boolean	in_the_loop sets $wp_actions['loop_start'] and $wp_query::in_the_loop
382
	 *
383
	 * also:
384
	 *
385
	 * - \GV\Request	request:	sets \GravityView_frontend::is_search, \GravityView_frontend::single_entry,
386
	 *									 \GravityView_View::context, \GravityView_frontend::entry
387
	 *
388
	 * - \GV\View_Collection	views:		sets \GravityView_View_Data::views
389
	 * - \GV\Field_Collection	fields:		sets \GravityView_View::fields
390
	 * - \GV\Entry_Collection	entries:	sets \GravityView_View::entries, \GravityView_View::total_entries
391
	 *
392
	 * and automagically:
393
	 *
394
	 * - \GravityView_View		data:		sets \GravityView_frontend::gv_output_data
395
	 *
396
	 * @param array $configuration The configuration.
397
	 *
398
	 * @return void
399
	 */
400 33
	public static function push( $configuration ) {
401 33
		array_push( self::$stack, self::freeze() );
402 33
		self::load( $configuration );
403 33
	}
404
405
	/**
406
	 * Restores last saved state and context.
407
	 *
408
	 * @return void
409
	 */
410 30
	public static function pop() {
411 30
		self::thaw( array_pop( self::$stack ) );
412 30
	}
413
414
	/**
415
	 * Serializes the current configuration as needed.
416
	 *
417
	 * @return array The configuration.
418
	 */
419 33
	public static function freeze() {
420 33
		global $wp_actions, $wp_query;
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...
421
422
		return array(
423 33
			'\GravityView_View::atts' => \GravityView_View::getInstance()->getAtts(),
424 33
			'\GravityView_View::view_id' => \GravityView_View::getInstance()->getViewId(),
425 33
			'\GravityView_View::back_link_label' => \GravityView_View::getInstance()->getBackLinkLabel( false ),
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View::getBackLinkLabel() has been deprecated with message: Use $template->get_back_label();

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
426 33
			'\GravityView_View_Data::views' => \GravityView_View_Data::getInstance()->views,
427 33
			'\GravityView_View::entries' => \GravityView_View::getInstance()->getEntries(),
428 33
			'\GravityView_View::form' => \GravityView_View::getInstance()->getForm(),
429 33
			'\GravityView_View::form_id' => \GravityView_View::getInstance()->getFormId(),
430 33
			'\GravityView_View::context' => \GravityView_View::getInstance()->getContext(),
431 33
			'\GravityView_View::total_entries' => \GravityView_View::getInstance()->getTotalEntries(),
432 33
			'\GravityView_View::post_id' => \GravityView_View::getInstance()->getPostId(),
433 33
			'\GravityView_View::hide_until_searched' => \GravityView_View::getInstance()->isHideUntilSearched(),
434 33
			'\GravityView_frontend::post_id' => \GravityView_frontend::getInstance()->getPostId(),
435 33
			'\GravityView_frontend::context_view_id' => \GravityView_frontend::getInstance()->get_context_view_id(),
436 33
			'\GravityView_frontend::is_gravityview_post_type' => \GravityView_frontend::getInstance()->isGravityviewPostType(),
437 33
			'\GravityView_frontend::post_has_shortcode' => \GravityView_frontend::getInstance()->isPostHasShortcode(),
438 33
			'\GravityView_frontend::gv_output_data' => \GravityView_frontend::getInstance()->getGvOutputData(),
439 33
			'\GravityView_View::paging' => \GravityView_View::getInstance()->getPaging(),
440 33
			'\GravityView_View::sorting' => \GravityView_View::getInstance()->getSorting(),
441 33
			'\GravityView_frontend::is_search' => \GravityView_frontend::getInstance()->isSearch(),
442 33
			'\GravityView_frontend::single_entry' => \GravityView_frontend::getInstance()->getSingleEntry(),
443 33
			'\GravityView_frontend::entry' => \GravityView_frontend::getInstance()->getEntry(),
444 33
			'\GravityView_View::_current_entry' => \GravityView_View::getInstance()->getCurrentEntry(),
445 33
			'\GravityView_View::fields' => \GravityView_View::getInstance()->getFields(),
446 33
			'wp_actions[loop_start]' => empty( $wp_actions['loop_start'] ) ? 0 : $wp_actions['loop_start'],
447 33
			'wp_query::in_the_loop' => $wp_query->in_the_loop,
448
		);
449
	}
450
451
	/**
452
	 * Unserializes a saved configuration. Modifies the global state.
453
	 *
454
	 * @param array $data Saved configuration from self::freeze()
455
	 */
456 70
	public static function thaw( $data ) {
457 70
		foreach ( (array)$data as $key => $value ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
458 70
			switch ( $key ):
459 70
				case '\GravityView_View::atts':
460 33
					\GravityView_View::getInstance()->setAtts( $value );
461 33
					break;
462 70
				case '\GravityView_View::view_id':
463 33
					\GravityView_View::getInstance()->setViewId( $value );
464 33
					break;
465 70
				case '\GravityView_View::back_link_label':
466 33
					\GravityView_View::getInstance()->setBackLinkLabel( $value );
467 33
					break;
468 70
				case '\GravityView_View_Data::views':
469 33
					\GravityView_View_Data::getInstance()->views = $value;
470 33
					break;
471 70
				case '\GravityView_View::entries':
472 33
					\GravityView_View::getInstance()->setEntries( $value );
473 33
					break;
474 70
				case '\GravityView_View::form':
475 33
					\GravityView_View::getInstance()->setForm( $value );
476 33
					break;
477 70
				case '\GravityView_View::form_id':
478 33
					\GravityView_View::getInstance()->setFormId( $value );
479 33
					break;
480 70
				case '\GravityView_View::context':
481 33
					\GravityView_View::getInstance()->setContext( $value );
482 33
					break;
483 70
				case '\GravityView_View::total_entries':
484 33
					\GravityView_View::getInstance()->setTotalEntries( $value );
485 33
					break;
486 70
				case '\GravityView_View::post_id':
487 31
					\GravityView_View::getInstance()->setPostId( $value );
488 31
					break;
489 70
				case '\GravityView_View::is_hide_until_searched':
490 33
					\GravityView_View::getInstance()->setHideUntilSearched( $value );
491 33
					break;
492 70
				case '\GravityView_frontend::post_id':
493 31
					\GravityView_frontend::getInstance()->setPostId( $value );
494 31
					break;
495 70
				case '\GravityView_frontend::context_view_id':
496 33
					$frontend = \GravityView_frontend::getInstance();
497 33
					$frontend->context_view_id = $value;
498 33
					break;
499 70
				case '\GravityView_frontend::is_gravityview_post_type':
500 31
					\GravityView_frontend::getInstance()->setIsGravityviewPostType( $value );
501 31
					break;
502 70
				case '\GravityView_frontend::post_has_shortcode':
503 31
					\GravityView_frontend::getInstance()->setPostHasShortcode( $value );
504 31
					break;
505 70
				case '\GravityView_frontend::gv_output_data':
506 33
					\GravityView_frontend::getInstance()->setGvOutputData( $value );
507 33
					break;
508 70
				case '\GravityView_View::paging':
509 30
					\GravityView_View::getInstance()->setPaging( $value );
510 30
					break;
511 70
				case '\GravityView_View::sorting':
512 30
					\GravityView_View::getInstance()->setSorting( $value );
513 30
					break;
514 70
				case '\GravityView_frontend::is_search':
515 33
					\GravityView_frontend::getInstance()->setIsSearch( $value );
516 33
					break;
517 70
				case '\GravityView_frontend::single_entry':
518 33
					\GravityView_frontend::getInstance()->setSingleEntry( $value );
519 33
					break;
520 70
				case '\GravityView_frontend::entry':
521 33
					\GravityView_frontend::getInstance()->setEntry( $value );
522 33
					break;
523 70
				case '\GravityView_View::_current_entry':
524 33
					\GravityView_View::getInstance()->setCurrentEntry( $value );
525 33
					break;
526 68
				case '\GravityView_View::fields':
527 30
					\GravityView_View::getInstance()->setFields( $value );
528 30
					break;
529 68
				case '\GravityView_View::_current_field':
530 61
					\GravityView_View::getInstance()->setCurrentField( $value );
531 61
					break;
532 30
				case 'wp_actions[loop_start]':
533 30
					global $wp_actions;
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...
534 30
					$wp_actions['loop_start'] = $value;
535 30
					break;
536 30
				case 'wp_query::in_the_loop':
537 30
					global $wp_query;
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...
538 30
					$wp_query->in_the_loop = $value;
539 70
					break;
540
			endswitch;
541
		}
542 70
	}
543
544
	/**
545
	 * Hydrates the legacy context globals as needed.
546
	 *
547
	 * @see self::push() for format.
548
	 *
549
	 * @return void
550
	 */
551 70
	public static function load( $configuration ) {
552 70
		foreach ( (array)$configuration as $key => $value ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
553 70
			switch ( $key ):
554 70
				case 'view':
555 33
					$views = new \GV\View_Collection();
556 33
					$views->add( $value );
557
558 33
					self::thaw( array(
559 33
						'\GravityView_View::atts' => $value->settings->as_atts(),
560 33
						'\GravityView_View::view_id' => $value->ID,
561 33
						'\GravityView_View::back_link_label' => $value->settings->get( 'back_link_label', null ),
562 33
						'\GravityView_View::form' => $value->form ? $value->form->form : null,
563 33
						'\GravityView_View::form_id' => $value->form ? $value->form->ID : null,
564 33
						'\GravityView_View::is_hide_until_searched' => $value->settings->get( 'hide_until_searched', null ) && ! gravityview()->request->is_search(),
565
566 33
						'\GravityView_View_Data::views' => $views,
567 33
						'\GravityView_frontend::gv_output_data' => \GravityView_View_Data::getInstance(),
568 33
						'\GravityView_frontend::context_view_id' => $value->ID,
569
					) );
570 33
					break;
571 70
				case 'post':
572 11
					$has_shortcode = false;
573 11
					foreach ( \GV\Shortcode::parse( $value->post_content ) as $shortcode ) {
574 3
						if ( $shortcode->name == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
575 3
							$has_shortcode = true;
576 3
							break;
577
						}
578
					}
579 11
					self::thaw( array(
580 11
						'\GravityView_View::post_id' => $value->ID,
581 11
						'\GravityView_frontend::post_id' => $value->ID,
582 11
						'\GravityView_frontend::is_gravityview_post_type' => $value->post_type == 'gravityview',
583 11
						'\GravityView_frontend::post_has_shortcode' => $has_shortcode,
584
					) );
585 11
					break;
586 70
				case 'views':
587
					self::thaw( array(
588
						'\GravityView_View_Data::views' => $value,
589
						'\GravityView_frontend::gv_output_data' => \GravityView_View_Data::getInstance(),
590
					) );
591
					break;
592 70
				case 'entries':
593 33
					self::thaw( array(
594
						'\GravityView_View::entries' => array_map( function( $e ) { return $e->as_entry(); }, $value->all() ),
595 33
						'\GravityView_View::total_entries' => $value->total(),
596
					) );
597 33
					break;
598 70
				case 'entry':
599 28
					self::thaw( array(
600 28
						'\GravityView_frontend::single_entry' => $value->ID,
601 28
						'\GravityView_frontend::entry' => $value->as_entry(),
602 28
						'\GravityView_View::_current_entry' => $value->as_entry(),
603
					) );
604 28
					break;
605 70
				case 'fields':
606 3
					self::thaw( array(
607 3
						'\GravityView_View::fields' => $value->as_configuration(),
608
					) );
609 3
					break;
610 69
				case 'field':
611 61
					self::thaw( array(
612
						'\GravityView_View::_current_field' => array(
613 61
							'field_id' => $value->ID,
614 61
							'field' => $value->field,
615 61
							'field_settings' => $value->as_configuration(),
616 61
							'form' => \GravityView_View::getInstance()->getForm(),
617 61
							'field_type' => $value->type, /** {@since 1.6} */
618 61
							'entry' => \GravityView_View::getInstance()->getCurrentEntry(),
619
620
							// 'field_path' => $field_path, /** {@since 1.16} */
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
621
							// 'value' => $value,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
622
							// 'display_value' => $display_value,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
623
							// 'format' => $format,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
624
						),
625
					) );
626 61
					break;
627 32
				case 'request':
628 32
					self::thaw( array(
629 32
						'\GravityView_View::context' => (
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
630 32
							$value->is_entry() ? 'single' :
631 24
							( $value->is_edit_entry() ? 'edit' :
632 24
									( $value->is_view() ? 'directory': null )
633
								)
634
						),
635 32
						'\GravityView_frontend::is_search' => $value->is_search(),
636
					) );
637
638 32
					if ( ! $value->is_entry() ) {
639 24
						self::thaw( array(
640 24
							'\GravityView_frontend::single_entry' => 0,
641
							'\GravityView_frontend::entry' => 0
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
642
						) );
643
					}
644 32
					break;
645 19
				case 'paging':
646 19
					self::thaw( array(
647 19
						'\GravityView_View::paging' => $value,
648
					) );
649 19
					break;
650 19
				case 'sorting':
651 19
					self::thaw( array(
652 19
						'\GravityView_View::sorting' => $value,
653
					) );
654 19
					break;
655 5
				case 'in_the_loop':
656 2
					self::thaw( array(
657 2
						'wp_query::in_the_loop' => $value,
658 2
						'wp_actions[loop_start]' => $value ? 1 : 0,
659
					) );
660 70
					break;
661
			endswitch;
662
		}
663
664 70
		global $gravityview_view;
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...
665 70
		$gravityview_view = \GravityView_View::getInstance();
666 70
	}
667
668
	/**
669
	 * Resets the global state completely.
670
	 *
671
	 * Use with utmost care, as filter and action callbacks
672
	 *  may be added again.
673
	 *
674
	 * Does not touch the context stack.
675
	 *
676
	 * @return void
677
	 */
678 77
	public static function reset() {
679 77
		\GravityView_View::$instance = null;
0 ignored issues
show
Bug introduced by
The property instance cannot be accessed from this context as it is declared private in class GravityView_View.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
680 77
		\GravityView_frontend::$instance = null;
0 ignored issues
show
Bug introduced by
The property instance cannot be accessed from this context as it is declared private in class GravityView_frontend.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
681 77
		\GravityView_View_Data::$instance = null;
0 ignored issues
show
Bug introduced by
The property instance cannot be accessed from this context as it is declared private in class GravityView_View_Data.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
682
683 77
		global $wp_query, $wp_actions;
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...
684
685 77
		$wp_query->in_the_loop = false;
686 77
		$wp_actions['loop_start'] = 0;
687 77
	}
688
}
689
690
691
/** Add some global fix for field capability discrepancies. */
692
add_filter( 'gravityview/configuration/fields', function( $fields ) {
693 63
	if ( empty( $fields  ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
694
		return $fields;
695
	}
696
697
	/**
698
	 * Each view field is saved in a weird capability state by default.
699
	 *
700
	 * With loggedin set to false, but a capability of 'read' it introduces
701
	 *  some logical issues and is not robust. Fix this behavior throughout
702
	 *  core by making sure capability is '' if log in is not required.
703
	 *
704
	 * Perhaps in the UI a fix would be to unite the two fields (as our new
705
	 *  \GV\Field class already does) into one dropdown:
706
	 *
707
	 * Anyone, Logged In Only, ... etc. etc.
708
	 *
709
	 * The two "settings" should be as tightly coupled as possible to avoid
710
	 *  split logic scenarios. Uniting them into one field is the way to go.
711
	 */
712
713 63
	foreach ( $fields as $position => &$_fields ) {
714
715 63
		if ( empty( $_fields ) || ! is_array( $_fields ) ) {
716
			continue;
717
		}
718
719 63
		foreach ( $_fields as $uid => &$_field ) {
720 63
			if ( ! isset( $_field['only_loggedin'] ) ) {
721 52
				continue;
722
			}
723
			/** If we do not require login, we don't require a cap. */
724 63
			$_field['only_loggedin'] != '1' && ( $_field['only_loggedin_cap'] = '' );
725
		}
726
	}
727 63
	return $fields;
728
} );
729
730
731
/** Add a future fix to make sure field configurations include the form ID. */
732
add_filter( 'gravityview/view/configuration/fields', function( $fields, $view ) {
733 116
	if ( ! $view || empty( $fields ) ) {
734
		return $fields;
735
	}
736
737 116
	if ( ! $view->form || ! $view->form->ID ) {
738
		return $fields;
739
	}
740
741
	/**
742
	 * In order to instantiate the correct \GV\Field implementation
743
	 *  we need to provide a form_id inside the configuration.
744
	 *
745
	 * @todo Make sure this actually happens in the admin side
746
	 *  when saving the views.
747
	 */
748 116
	foreach ( $fields as $position => &$_fields ) {
749
750 116
		if ( empty( $_fields ) || ! is_array( $_fields ) ) {
751
			continue;
752
		}
753
754 116
		foreach ( $_fields as $uid => &$_field ) {
755 116
			if ( ! empty( $_field['id'] ) && is_numeric( $_field['id'] ) && empty( $_field['form_id'] ) ) {
756 116
				$_field['form_id'] = $view->form->ID;
757
			}
758
		}
759
	}
760
761 116
	return $fields;
762
}, 10, 2 );
763
764
765
/** Make sure the non-configured notice is not output twice. */
766
add_action( 'gravityview/template/after', function( $gravityview = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $gravityview 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...
767 27
	if ( class_exists( '\GravityView_frontend' ) ) {
768 27
		global $wp_filter;
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...
769
770 27
		if ( empty( $wp_filter['gravityview_after'] ) ) {
771
			return;
772
		}
773
774
		/** WordPress 4.6 and lower compatibility, when WP_Hook classes were still absent. */
775 27
		if ( is_array( $wp_filter['gravityview_after'] ) ) {
776
			if ( ! empty( $wp_filter['gravityview_after'][10] ) ) {
777
				foreach ( $wp_filter['gravityview_after'][10] as $function_key => $callback ) {
778
					if ( strpos( $function_key, 'context_not_configured_warning' ) ) {
779
						unset( $wp_filter['gravityview_after'][10][ $function_key ] );
780
					}
781
				}
782
			}
783
			return;
784
		}
785
786 27
		foreach ( $wp_filter['gravityview_after']->callbacks[10] as $function_key => $callback ) {
787 27
			if ( strpos( $function_key, 'context_not_configured_warning' ) ) {
788 27
				unset( $wp_filter['gravityview_after']->callbacks[10][ $function_key ] );
789
			}
790
		}
791
	}
792
} );
793