Completed
Push — master ( d07b76...196a0a )
by Zack
18:54 queued 11:24
created

View_Collection::append()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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 11 and the first side effect is on line 5.

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' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
6
	die();
7
8
/**
9
 * A collection of \GV\View objects.
10
 */
11
class View_Collection extends Collection {
12
	/**
13
	 * Add a \GV\View to this collection.
14
	 *
15
	 * @param \GV\View $view The view to append to the internal array.
16
	 *
17
	 * @throws \InvalidArgumentException if $view is not of type \GV\View.
18
	 *
19
	 * @api
20
	 * @since future
21
	 * @return void
22
	 */
23 2
	public function append( $view ) {
24 2
		if ( ! $view instanceof View ) {
25 1
			throw new \InvalidArgumentException( 'View_Collections can only contain objects of type \GV\View.' );
26
		}
27 2
		parent::append( $view );
28 2
	}
29
30
	/**
31
	 * Get a \GV\View from this list.
32
	 *
33
	 * @param int $view_id The ID of the view to get.
34
	 *
35
	 * @api
36
	 * @since future
37
	 *
38
	 * @return \GV\View|null The \GV\View with the $view_id as the ID, or null if not found.
39
	 */
40 1
	public function get( $view_id ) {
41 1
		foreach ( $this->all() as $view ) {
42 1
			if ( $view->ID == $view_id )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
43 1
				return $view;
44
		}
45 1
		return null;
46
	}
47
48
	/**
49
	 * Get a list of \GV\View objects inside the supplied \WP_Post.
50
	 *
51
	 * The post can be a gravityview post, which is the simplest case.
52
	 * The post can contain gravityview shortcodes as well.
53
	 * The post meta can contain gravityview shortcodes.
54
	 *
55
	 * @param \WP_Post $post The \WP_Post object to look into.
56
	 *
57
	 * @api
58
	 * @since future
59
	 * @return \GV\View_Collection A \GV\View_Collection instance contanining the views inside the supplied \WP_Post.
60
	 */
61 2
	public static function from_post( \WP_Post $post ) {
62 2
		$views = new self();
63
64 2
		if ( get_post_type( $post ) == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
65
			/** A straight up gravityview post. */
66 2
			$views->append( View::from_post( $post ) );
67
		} else {
68
			/** Let's find us some [gravityview] shortcodes perhaps. */
69 1
			foreach ( Shortcode::parse( $post->post_content ) as $shortcode ) {
70 1
				if ( $shortcode->name != 'gravityview' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
71
					continue;
72
				}
73
74 1
				if ( ! isset( $shortcode->atts['id'] ) ) {
75
					do_action( 'gravityview_log_error', __METHOD__ . ': [gravityview] shortcode has no `id` attribute' );
76
					continue;
77
				}
78
79 1
				if ( is_numeric( $shortcode->atts['id'] ) ) {
80 1
					$views->append( View::by_id( $shortcode->atts['id'] ) );
81
				}
82
			}
83
84
			/**
85
			 * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
86
			 *
87
			 * This is useful when using themes that store content that may contain shortcodes in custom post meta.
88
			 *
89
			 * @since future
90
			 *
91
			 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
92
			 * @param[in] \WP_Post $post The post that is being checked
93
			 */
94 1
			$meta_keys = apply_filters( 'gravityview/view_collection/from_post/meta_keys', array(), $post );
95
96
			/**
97
			 * @filter `gravityview/data/parse/meta_keys`
98
			 * @deprecated
99
			 * @see The `gravityview/view_collection/from_post/meta_keys` filter.
100
			 */
101 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...
102
103
			/** What about inside post meta values? */
104 1
			foreach ( $meta_keys as $meta_key ) {
105 1
				foreach ( Shortcode::parse( $post->$meta_key ) as $shortcode ) {
106 1
					if ( ! $shortcode instanceof Shortcodes\gravityview ) {
107
						continue;
108
					}
109
110 1
					if ( is_numeric( $shortcode->atts['id'] ) ) {
111 1
						$views->append( View::by_id( $shortcode->atts['id'] ) );
112
					}
113
				}
114
			}
115
		}
116
117 2
		return $views;
118
	}
119
}
120