Completed
Pull Request — master (#869)
by Zack
06:18
created

View_Collection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.59%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 145
ccs 35
cts 37
cp 0.9459
rs 10
c 2
b 0
f 1
wmc 17
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 3
A get() 0 8 3
A contains() 0 3 1
B from_post() 0 38 4
B from_content() 0 22 6
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
	 * @throws \InvalidArgumentException if $view is not of type \GV\View.
19
	 *
20
	 * @api
21
	 * @since future
22
	 * @return void
23
	 */
24 3
	public function add( $view ) {
25
26
		// TODO: @soulseekah
27 3
		if ( is_null( $view ) ) {
28
			return null;
29
		}
30
31 3
		if ( ! $view instanceof View ) {
32 1
			throw new \InvalidArgumentException( 'View_Collections can only contain objects of type \GV\View.' );
33
		}
34
35 3
		parent::add( $view );
36 3
	}
37
38
	/**
39
	 * Get a \GV\View from this list.
40
	 *
41
	 * @param int $view_id The ID of the view to get.
42
	 *
43
	 * @api
44
	 * @since future
45
	 *
46
	 * @return \GV\View|null The \GV\View with the $view_id as the ID, or null if not found.
47
	 */
48 3
	public function get( $view_id ) {
49 3
		foreach ( $this->all() as $view ) {
50 3
			if ( $view->ID == $view_id ) {
51 3
				return $view;
52
			}
53
		}
54 3
		return null;
55
	}
56
57
	/**
58
	 * Check whether \GV\View with an ID is already here.
59
	 *
60
	 * @param int $view_id The ID of the view to check.
61
	 *
62
	 * @api
63
	 * @since future
64
	 *
65
	 * @return boolean Whether it exists or not.
66
	 */
67 3
	public function contains( $view_id ) {
68 3
		return ! is_null( $this->get( $view_id ) );
69
	}
70
71
	/**
72
	 * Get a list of \GV\View objects inside the supplied \WP_Post.
73
	 *
74
	 * The post can be a gravityview post, which is the simplest case.
75
	 * The post can contain gravityview shortcodes as well.
76
	 * The post meta can contain gravityview shortcodes.
77
	 *
78
	 * @param \WP_Post $post The \WP_Post object to look into.
79
	 *
80
	 * @api
81
	 * @since future
82
	 * @return \GV\View_Collection A \GV\View_Collection instance contanining the views inside the supplied \WP_Post.
83
	 */
84 3
	public static function from_post( \WP_Post $post ) {
85 3
		$views = new self();
86
87 3
		if ( get_post_type( $post ) == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
88
			/** A straight up gravityview post. */
89 3
			$views->add( View::from_post( $post ) );
90
		} else {
91 1
			$views->merge( self::from_content( $post->post_content ) );
92
93
			/**
94
			 * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
95
			 *
96
			 * This is useful when using themes that store content that may contain shortcodes in custom post meta.
97
			 *
98
			 * @since future
99
			 *
100
			 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
101
			 * @param[in] \WP_Post $post The post that is being checked
102
			 */
103 1
			$meta_keys = apply_filters( 'gravityview/view_collection/from_post/meta_keys', array(), $post );
104
105
			/**
106
			 * @filter `gravityview/data/parse/meta_keys`
107
			 * @deprecated
108
			 * @see The `gravityview/view_collection/from_post/meta_keys` filter.
109
			 */
110 1
			$meta_keys = (array)apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
111
112
			/** What about inside post meta values? */
113 1
			foreach ( $meta_keys as $meta_key ) {
114 1
				if ( is_string( $meta_key ) ) {
115 1
					$views->merge( self::from_content( $post->$meta_key ) );
116
				}
117
			}
118
		}
119
120 3
		return $views;
121
	}
122
123
	/**
124
	 * Get a list of detected \GV\View objects inside the supplied content.
125
	 *
126
	 * The content can have a shortcode, this is the simplest case.
127
	 *
128
	 * @param string $content The content to look into.
129
	 *
130
	 * @api
131
	 * @since future
132
	 * @return \GV\View_Collection A \GV\View_Collection instance contanining the views inside the supplied \WP_Post.
133
	 */
134 1
	public static function from_content( $content ) {
135 1
		$views = new self();
136
137
		/** Let's find us some [gravityview] shortcodes perhaps. */
138 1
		foreach ( Shortcode::parse( $content ) as $shortcode ) {
139 1
			if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
140
				continue;
141
			}
142
143 1
			if ( is_numeric( $shortcode->atts['id'] ) ) {
144 1
				$view = View::by_id( $shortcode->atts['id'] );
145 1
				if ( ! $view ) {
146 1
					continue;
147
				}
148
				
149 1
				$view->settings->update( $shortcode->atts );
150 1
				$views->add( $view );
151
			}
152
		}
153
154 1
		return $views;
155
	}
156
}
157