Completed
Push — develop ( 4d830b...72e688 )
by Zack
06:12
created

View_Collection::all()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
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
	/**
15
	 * @inheritDoc
16
	 * @return View[]
17
	 */
18 24
	public function all() {
19 24
		return parent::all();
20
	}
21
22
	/**
23
	 * Add a \GV\View to this collection.
24
	 *
25
	 * @param \GV\View $view The view to add to the internal array.
26
	 *
27
	 * @api
28
	 * @since 2.0
29
	 * @return void
30
	 */
31 66
	public function add( $view ) {
32
33 66
		if ( ! $view instanceof View ) {
34 1
			gravityview()->log->error( 'View_Collections can only contain objects of type \GV\View.' );
35 1
			return;
36
		}
37
38 66
		parent::add( $view );
39 66
	}
40
41
	/**
42
	 * Get a \GV\View from this list.
43
	 *
44
	 * @param int $view_id The ID of the view to get.
45
	 *
46
	 * @api
47
	 * @since 2.0
48
	 *
49
	 * @return \GV\View|null The \GV\View with the $view_id as the ID, or null if not found.
50
	 */
51 25
	public function get( $view_id ) {
52 25
		foreach ( $this->all() as $view ) {
53 25
			if ( $view->ID == $view_id ) {
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54 25
				return $view;
55
			}
56
		}
57 25
		return null;
58
	}
59
60
	/**
61
	 * Check whether \GV\View with an ID is already here.
62
	 *
63
	 * @param int $view_id The ID of the view to check.
64
	 *
65
	 * @api
66
	 * @since 2.0
67
	 *
68
	 * @return boolean Whether it exists or not.
69
	 */
70 25
	public function contains( $view_id ) {
71 25
		return ! is_null( $this->get( $view_id ) );
72
	}
73
74
	/**
75
	 * Get a list of \GV\View objects inside the supplied \WP_Post.
76
	 *
77
	 * The post can be a gravityview post, which is the simplest case.
78
	 * The post can contain gravityview shortcodes as well.
79
	 * The post meta can contain gravityview shortcodes.
80
	 *
81
	 * @param \WP_Post $post The \WP_Post object to look into.
82
	 *
83
	 * @api
84
	 * @since 2.0
85
	 * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
86
	 */
87 29
	public static function from_post( \WP_Post $post ) {
88 29
		$views = new self();
89
90 29
		$post_type = get_post_type( $post );
91
92 29
		if ( 'gravityview' === $post_type ) {
93
			/** A straight up gravityview post. */
94 27
			$views->add( View::from_post( $post ) );
95
		} else {
96 3
			$views->merge( self::from_content( $post->post_content ) );
97
98
			/**
99
			 * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
100
			 *
101
			 * This is useful when using themes that store content that may contain shortcodes in custom post meta.
102
			 *
103
			 * @since 2.0
104
			 * @since 2.0.7 Added $views parameter, passed by reference
105
			 *
106
			 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
107
			 * @param[in] \WP_Post $post The post that is being checked
108
			 * @param \GV\View_Collection $views The current View Collection object, passed as reference
109
			 */
110 3
			$meta_keys = apply_filters_ref_array( 'gravityview/view_collection/from_post/meta_keys', array( array(), $post, &$views ) );
111
112 3
			if ( function_exists( 'apply_filters_deprecated' ) ) {
113 3
				$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' );
114
			} else {
115
				/**
116
				 * @filter `gravityview/data/parse/meta_keys`
117
				 * @deprecated
118
				 * @todo Require WP 4.6.0 so that `apply_filters_deprecated` is always available
119
				 * @see The `gravityview/view_collection/from_post/meta_keys` filter.
120
				 */
121
				$meta_keys = (array) apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID );
122
			}
123
124
			/** What about inside post meta values? */
125 3
			foreach ( $meta_keys as $meta_key ) {
126 1
				$views = self::merge_deep( $views, $post->{$meta_key} );
127
			}
128
		}
129
130 29
		return $views;
131
	}
132
133
	/**
134
	 * Process meta values when stored singular (string) or multiple (array). Supports nested arrays and JSON strings.
135
	 *
136
	 * @since 2.1
137
	 *
138
	 * @param \GV\View_Collection $views Existing View Collection to merge with
139
	 * @param string|array $meta_value Value to parse. Normally the value of $post->{$meta_key}.
140
	 *
141
	 * @return \GV\View_Collection $views View Collection containing any additional Views found
142
	 */
143
	private static function merge_deep( $views, $meta_value ) {
144
145
		$meta_value = gv_maybe_json_decode( $meta_value, true );
146
147
		if ( is_array( $meta_value ) ) {
148
			foreach ( $meta_value as $index => $item ) {
149
				$meta_value[ $index ] = self::merge_deep( $views, $item );
150
			}
151
		}
152
153
		if ( is_string( $meta_value ) ) {
154
			$views->merge( self::from_content( $meta_value ) );
155
		}
156
157
		return $views;
158
	}
159
160
	/**
161
	 * Get a list of detected \GV\View objects inside the supplied content.
162
	 *
163
	 * The content can have a shortcode, this is the simplest case.
164
	 *
165
	 * @param string $content The content to look into.
166
	 *
167
	 * @api
168
	 * @since 2.0
169
	 * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
170
	 */
171 3
	public static function from_content( $content ) {
172 3
		$views = new self();
173
174
		/** Let's find us some [gravityview] shortcodes perhaps. */
175 3
		foreach ( Shortcode::parse( $content ) as $shortcode ) {
176 3
			if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
177
				continue;
178
			}
179
180 3
			if ( is_numeric( $shortcode->atts['id'] ) ) {
181 3
				$view = View::by_id( $shortcode->atts['id'] );
182 3
				if ( ! $view ) {
183 1
					continue;
184
				}
185
186 3
				$view->settings->update( $shortcode->atts );
187 3
				$views->add( $view );
188
			}
189
		}
190
191 3
		return $views;
192
	}
193
}
194