Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

_mocks.php ➔ GravityView_View_Data_add_view()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 23
nc 9
nop 2
dl 0
loc 44
ccs 20
cts 20
cp 1
crap 8
rs 5.3846
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 15 and the first side effect is on line 61.

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 future
12
 *
13
 * @return array|false The old array data, or false on error.
14
 */
15
function GravityView_View_Data_add_view( $view_id, $atts ) {
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 1
	if ( is_array( $view_id ) ) {
18 1
		foreach ( $view_id as $id ) {
19 1
			call_user_func( __FUNCTION__, $id, $atts );
20
		}
21
22 1
		if ( ! gravityview()->request->views->count() ) {
23 1
			return array();
24
		}
25
26 1
		return array_combine(
27
			array_map( function( $view ) { return $view->ID; }, gravityview()->request->views->all() ),
28
			array_map( function( $view ) { return $view->as_data(); }, gravityview()->request->views->all() )
29
		);
30
	}
31
32
	/** View has been set already. */
33 1
	if ( $view = gravityview()->request->views->get( $view_id ) ) {
34 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s already exists.', $view_id ) );
35 1
		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...
36
	}
37
38 1
	$view = \GV\View::by_id( $view_id );
39 1
	if ( ! $view ) {
40 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id ) );
41 1
		return false;
42
	}
43
44
	/** Doesn't have a connected form. */
45 1
	if ( ! $view->form ) {
46 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; Post ID #%s does not have a connected form.', $view_id ) );
47 1
		return false;
48
	}
49
50
	/** Update the settings */
51 1
	if ( is_array( $atts ) ) {
52 1
		$view->settings->update( $atts );
53
	}
54
55 1
	gravityview()->request->views->add( $view );
56
57 1
	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
/** Add some global fix for field capability discrepancies. */
61
add_filter( 'gravityview/configuration/fields', function( $fields ) {
62 2
	if ( empty( $fields  ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
63
		return $fields;
64
	}
65
66
	/**
67
	 * Each view field is saved in a weird capability state by default.
68
	 *
69
	 * With loggedin set to false, but a capability of 'read' it introduces
70
	 *  some logical issues and is not robust. Fix this behavior throughout
71
	 *  core by making sure capability is '' if log in is not required.
72
	 *
73
	 * Perhaps in the UI a fix would be to unite the two fields (as our new
74
	 *  \GV\Field class already does) into one dropdown:
75
	 *
76
	 * Anyone, Logged In Only, ... etc. etc.
77
	 *
78
	 * The two "settings" should be as tightly coupled as possible to avoid
79
	 *  split logic scenarios. Uniting them into one field is the way to go.
80
	 */
81
82 2
	foreach ( $fields as $position => &$_fields ) {
83 2
		foreach ( $_fields as $uid => &$_field ) {
84 2
			if ( ! isset( $_field['only_loggedin'] ) ) {
85
				continue;
86
			}
87
			/** If we do not require login, we don't require a cap. */
88 2
			$_field['only_loggedin'] != '1' && ( $_field['only_loggedin_cap'] = '' );
89
		}
90
	}
91 2
	return $fields;
92
} );
93