Completed
Push — develop ( 0c9afc...d4082e )
by Zack
21:17 queued 17:22
created

View_Collection::merge_deep()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 20
rs 9.8666
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 12 and the first side effect is on line 6.

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;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * A collection of \GV\View objects.
11
 */
12
class View_Collection extends Collection {
13
	/**
14
	 * Add a \GV\View to this collection.
15
	 *
16
	 * @param \GV\View $view The view to add to the internal array.
17
	 *
18
	 * @api
19
	 * @since 2.0
20
	 * @return void
21
	 */
22 37
	public function add( $view ) {
23
24 37
		if ( ! $view instanceof View ) {
25 1
			gravityview()->log->error( 'View_Collections can only contain objects of type \GV\View.' );
26 1
			return;
27
		}
28
29 37
		parent::add( $view );
30 37
	}
31
32
	/**
33
	 * Get a \GV\View from this list.
34
	 *
35
	 * @param int $view_id The ID of the view to get.
36
	 *
37
	 * @api
38
	 * @since 2.0
39
	 *
40
	 * @return \GV\View|null The \GV\View with the $view_id as the ID, or null if not found.
41
	 */
42 5
	public function get( $view_id ) {
43 5
		foreach ( $this->all() as $view ) {
44 5
			if ( $view->ID == $view_id ) {
45 5
				return $view;
46
			}
47
		}
48 5
		return null;
49
	}
50
51
	/**
52
	 * Check whether \GV\View with an ID is already here.
53
	 *
54
	 * @param int $view_id The ID of the view to check.
55
	 *
56
	 * @api
57
	 * @since 2.0
58
	 *
59
	 * @return boolean Whether it exists or not.
60
	 */
61 5
	public function contains( $view_id ) {
62 5
		return ! is_null( $this->get( $view_id ) );
63
	}
64
65
	/**
66
	 * Get a list of \GV\View objects inside the supplied \WP_Post.
67
	 *
68
	 * The post can be a gravityview post, which is the simplest case.
69
	 * The post can contain gravityview shortcodes as well.
70
	 * The post meta can contain gravityview shortcodes.
71
	 *
72
	 * @param \WP_Post $post The \WP_Post object to look into.
73
	 *
74
	 * @api
75
	 * @since 2.0
76
	 * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
77
	 */
78 5
	public static function from_post( \WP_Post $post ) {
79 5
		$views = new self();
80
81 5
		if ( get_post_type( $post ) == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
82
			/** A straight up gravityview post. */
83 4
			$views->add( View::from_post( $post ) );
84
		} else {
85 2
			$views->merge( self::from_content( $post->post_content ) );
86
87
			/**
88
			 * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
89
			 *
90
			 * This is useful when using themes that store content that may contain shortcodes in custom post meta.
91
			 *
92
			 * @since 2.0
93
			 * @since 2.0.7 Added $views parameter, passed by reference
94
			 *
95
			 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
96
			 * @param[in] \WP_Post $post The post that is being checked
97
			 * @param \GV\View_Collection $views The current View Collection object, passed as reference
98
			 */
99 2
			$meta_keys = apply_filters_ref_array( 'gravityview/view_collection/from_post/meta_keys', array( array(), $post, &$views ) );
100
101 2
			if ( function_exists( 'apply_filters_deprecated' ) ) {
102 2
				$meta_keys = (array) apply_filters_deprecated( 'gravityview/data/parse/meta_keys', array( $meta_keys, $post->ID ), '2.0.7', 'gravityview/view_collection/from_post/meta_keys' );
103
			} else {
104
				/**
105
				 * @filter `gravityview/data/parse/meta_keys`
106
				 * @deprecated
107
				 * @todo Require WP 4.6.0 so that `apply_filters_deprecated` is always available
108
				 * @see The `gravityview/view_collection/from_post/meta_keys` filter.
109
				 */
110
				$meta_keys = (array) apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID );
111
			}
112
113
			/** What about inside post meta values? */
114 2
			foreach ( $meta_keys as $meta_key ) {
115 1
				$views = self::merge_deep( $views, $post, $post->{$meta_key} );
116
			}
117
		}
118
119 5
		return $views;
120
	}
121
122
	/**
123
	 * Process meta values when stored singular (string) or multiple (array). Supports nested arrays.
124
	 *
125
	 * @since 2.1
126
	 *
127
	 * @param \GV\View_Collection $views
128
	 * @param \WP_Post $post The \WP_Post object to look into.
129
	 * @param string|array $meta_value Meta value of $post at $meta_key, can be string or associative array
130
	 *
131
	 * @return \GV\View_Collection $views
132
	 */
133
	private static function merge_deep( $views, $post, $meta_value ) {
134
135
		if ( is_array( $meta_value ) ) {
136
			foreach ( $meta_value as $value ) {
137
				$views = self::merge_deep( $views, $post, $value );
138
			}
139
		} elseif ( is_string( $meta_value ) ) {
140
			$views->merge( self::from_content( $meta_value ) );
141
		}
142
143
		return $views;
144
	}
145
146
	/**
147
	 * Get a list of detected \GV\View objects inside the supplied content.
148
	 *
149
	 * The content can have a shortcode, this is the simplest case.
150
	 *
151
	 * @param string $content The content to look into.
152
	 *
153
	 * @api
154
	 * @since 2.0
155
	 * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
156
	 */
157 2
	public static function from_content( $content ) {
158 2
		$views = new self();
159
160
		/** Let's find us some [gravityview] shortcodes perhaps. */
161 2
		foreach ( Shortcode::parse( $content ) as $shortcode ) {
162 2
			if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
163
				continue;
164
			}
165
166 2
			if ( is_numeric( $shortcode->atts['id'] ) ) {
167 2
				$view = View::by_id( $shortcode->atts['id'] );
168 2
				if ( ! $view ) {
169 1
					continue;
170
				}
171
				
172 2
				$view->settings->update( $shortcode->atts );
173 2
				$views->add( $view );
174
			}
175
		}
176
177 2
		return $views;
178
	}
179
}
180