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
|
|
|
* 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
|
28 |
|
public function add( $view ) { |
23
|
|
|
|
24
|
28 |
|
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
|
28 |
|
parent::add( $view ); |
30
|
28 |
|
} |
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
|
4 |
|
public function get( $view_id ) { |
43
|
4 |
|
foreach ( $this->all() as $view ) { |
44
|
4 |
|
if ( $view->ID == $view_id ) { |
45
|
4 |
|
return $view; |
46
|
|
|
} |
47
|
|
|
} |
48
|
4 |
|
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
|
4 |
|
public function contains( $view_id ) { |
62
|
4 |
|
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 contanining the views inside the supplied \WP_Post. |
77
|
|
|
*/ |
78
|
4 |
|
public static function from_post( \WP_Post $post ) { |
79
|
4 |
|
$views = new self(); |
80
|
|
|
|
81
|
4 |
|
if ( get_post_type( $post ) == 'gravityview' ) { |
|
|
|
|
82
|
|
|
/** A straight up gravityview post. */ |
83
|
4 |
|
$views->add( View::from_post( $post ) ); |
84
|
|
|
} else { |
85
|
1 |
|
$views->merge( self::from_content( $post->post_content ) ); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content. |
89
|
|
|
* |
90
|
|
|
* This is useful when using themes that store content that may contain shortcodes in custom post meta. |
91
|
|
|
* |
92
|
|
|
* @since 2.0 |
93
|
|
|
* @since 2.0.7 Added $views parameter, passed by reference |
94
|
|
|
* |
95
|
|
|
* @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array |
96
|
|
|
* @param[in] \WP_Post $post The post that is being checked |
97
|
|
|
* @param \GV\View_Collection $views The current View Collection object, passed as reference |
98
|
|
|
*/ |
99
|
1 |
|
$meta_keys = apply_filters_ref_array( 'gravityview/view_collection/from_post/meta_keys', array( array(), $post, &$views ) ); |
100
|
|
|
|
101
|
1 |
|
if ( function_exists( 'apply_filters_deprecated' ) ) { |
102
|
1 |
|
$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' ); |
103
|
|
|
} else { |
104
|
|
|
/** |
105
|
|
|
* @filter `gravityview/data/parse/meta_keys` |
106
|
|
|
* @deprecated |
107
|
|
|
* @todo Require WP 4.6.0 so that `apply_filters_deprecated` is always available |
108
|
|
|
* @see The `gravityview/view_collection/from_post/meta_keys` filter. |
109
|
|
|
*/ |
110
|
|
|
$meta_keys = (array) apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** What about inside post meta values? */ |
114
|
1 |
|
foreach ( $meta_keys as $meta_key ) { |
115
|
1 |
|
if ( is_string( $post->$meta_key ) ) { |
116
|
1 |
|
$views->merge( self::from_content( $post->$meta_key ) ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
return $views; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a list of detected \GV\View objects inside the supplied content. |
126
|
|
|
* |
127
|
|
|
* The content can have a shortcode, this is the simplest case. |
128
|
|
|
* |
129
|
|
|
* @param string $content The content to look into. |
130
|
|
|
* |
131
|
|
|
* @api |
132
|
|
|
* @since 2.0 |
133
|
|
|
* @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post. |
134
|
|
|
*/ |
135
|
1 |
|
public static function from_content( $content ) { |
136
|
1 |
|
$views = new self(); |
137
|
|
|
|
138
|
|
|
/** Let's find us some [gravityview] shortcodes perhaps. */ |
139
|
1 |
|
foreach ( Shortcode::parse( $content ) as $shortcode ) { |
140
|
1 |
|
if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) { |
|
|
|
|
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
if ( is_numeric( $shortcode->atts['id'] ) ) { |
145
|
1 |
|
$view = View::by_id( $shortcode->atts['id'] ); |
146
|
1 |
|
if ( ! $view ) { |
147
|
1 |
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
$view->settings->update( $shortcode->atts ); |
151
|
1 |
|
$views->add( $view ); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $views; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.