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

View_Collection::from_post()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 38
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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