Completed
Push — develop ( d5d4b9...69e663 )
by Zack
16:25
created

View_Collection::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 38
	public function add( $view ) {
23
24 38
		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 38
		parent::add( $view );
30 38
	}
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 6
	public function get( $view_id ) {
43 6
		foreach ( $this->all() as $view ) {
44 6
			if ( $view->ID == $view_id ) {
45 6
				return $view;
46
			}
47
		}
48 6
		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 6
	public function contains( $view_id ) {
62 6
		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 6
	public static function from_post( \WP_Post $post ) {
79 6
		$views = new self();
80
81 6
		$post_type = get_post_type( $post );
82
83 6
		if ( 'gravityview' === $post_type ) {
84
			/** A straight up gravityview post. */
85 5
			$views->add( View::from_post( $post ) );
86
		} else {
87 2
			$views->merge( self::from_content( $post->post_content ) );
88
89
			/**
90
			 * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
91
			 *
92
			 * This is useful when using themes that store content that may contain shortcodes in custom post meta.
93
			 *
94
			 * @since 2.0
95
			 * @since 2.0.7 Added $views parameter, passed by reference
96
			 *
97
			 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
98
			 * @param[in] \WP_Post $post The post that is being checked
99
			 * @param \GV\View_Collection $views The current View Collection object, passed as reference
100
			 */
101 2
			$meta_keys = apply_filters_ref_array( 'gravityview/view_collection/from_post/meta_keys', array( array(), $post, &$views ) );
102
103 2
			if ( function_exists( 'apply_filters_deprecated' ) ) {
104 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' );
105
			} else {
106
				/**
107
				 * @filter `gravityview/data/parse/meta_keys`
108
				 * @deprecated
109
				 * @todo Require WP 4.6.0 so that `apply_filters_deprecated` is always available
110
				 * @see The `gravityview/view_collection/from_post/meta_keys` filter.
111
				 */
112
				$meta_keys = (array) apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID );
113
			}
114
115
			/** What about inside post meta values? */
116 2
			foreach ( $meta_keys as $meta_key ) {
117 1
				$views = self::merge_deep( $views, $post->{$meta_key} );
118
			}
119
		}
120
121 6
		return $views;
122
	}
123
124
	/**
125
	 * Process meta values when stored singular (string) or multiple (array). Supports nested arrays and JSON strings.
126
	 *
127
	 * @since 2.1
128
	 *
129
	 * @param \GV\View_Collection $views Existing View Collection to merge with
130
	 * @param string|array $meta_value Value to parse. Normally the value of $post->{$meta_key}.
131
	 *
132
	 * @return \GV\View_Collection $views View Collection containing any additional Views found
133
	 */
134
	private static function merge_deep( $views, $meta_value ) {
135
136
		$meta_value = gv_maybe_json_decode( $meta_value, true );
137
138
		if ( is_array( $meta_value ) ) {
139
			foreach ( $meta_value as $index => $item ) {
140
				$meta_value[ $index ] = self::merge_deep( $views, $item );
141
			}
142
		}
143
144
		if ( is_string( $meta_value ) ) {
145
			$views->merge( self::from_content( $meta_value ) );
146
		}
147
148
		return $views;
149
	}
150
151
	/**
152
	 * Get a list of detected \GV\View objects inside the supplied content.
153
	 *
154
	 * The content can have a shortcode, this is the simplest case.
155
	 *
156
	 * @param string $content The content to look into.
157
	 *
158
	 * @api
159
	 * @since 2.0
160
	 * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
161
	 */
162 2
	public static function from_content( $content ) {
163 2
		$views = new self();
164
165
		/** Let's find us some [gravityview] shortcodes perhaps. */
166 2
		foreach ( Shortcode::parse( $content ) as $shortcode ) {
167 2
			if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
168
				continue;
169
			}
170
171 2
			if ( is_numeric( $shortcode->atts['id'] ) ) {
172 2
				$view = View::by_id( $shortcode->atts['id'] );
173 2
				if ( ! $view ) {
174 1
					continue;
175
				}
176
				
177 2
				$view->settings->update( $shortcode->atts );
178 2
				$views->add( $view );
179
			}
180
		}
181
182 2
		return $views;
183
	}
184
}
185