-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
Core.php
198 lines (184 loc) · 5.43 KB
/
Core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Timber;
/**
* Class Core
*/
abstract class Core {
public $id;
public $ID;
public $object_type;
/**
* This method is needed to complement the magic __get() method, because Twig uses `isset()`
* internally.
*
* @internal
* @link https://github.com/twigphp/Twig/issues/601
* @link https://twig.symfony.com/doc/2.x/recipes.html#using-dynamic-object-properties
* @return boolean
*/
public function __isset( $field ) {
if ( isset($this->$field) ) {
return $this->$field;
}
return false;
}
/**
* Magic method dispatcher for meta fields, for convience in Twig views.
*
* Called when explicitly invoking non-existent methods on a Core object. This method is not
* meant to be called directly.
*
* @example
* ```php
* $post = Timber\Post::get( get_the_ID() );
*
* update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );
*
* Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );
* ```
* ```twig
* {# Since this method does not exist explicitly on the Post class,
* it will dynamically dispatch the magic __call() method with an argument
* of "favorite_zep4_track" #}
* <span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>
* ```
* @link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call
* @link https://github.com/twigphp/Twig/issues/2
* @api
*
* @param string $field The name of the method being called.
* @param array $arguments Enumerated array containing the parameters passed to the function.
* Not used.
*
* @return mixed The value of the meta field named `$field` if truthy, `false` otherwise.
*/
public function __call( $field, $arguments ) {
if ( method_exists( $this, 'meta' ) && $meta_value = $this->meta( $field ) ) {
return $meta_value;
}
return false;
}
/**
* Magic getter for dynamic meta fields, for convenience in Twig views.
*
* This method is not meant to be called directly.
*
* @example
* ```php
* $post = Timber\Post::get( get_the_ID() );
*
* update_post_meta( $post->id, 'favorite_darkside_track', 'Any Colour You Like' );
*
* Timber::render('rock-n-roll.twig', array( 'post' => $post ));
* ```
* ```twig
* {# Since this property does not exist explicitly on the Post class,
* it will dynamically dispatch the magic __get() method with an argument
* of "favorite_darkside_track" #}
* <span>Favorite <i>Dark Side of the Moon</i> Track: {{ post.favorite_darkside_track }}</span>
* ```
* @link https://secure.php.net/manual/en/language.oop5.overloading.php#object.get
* @link https://twig.symfony.com/doc/2.x/recipes.html#using-dynamic-object-properties
*
* @param string $field The name of the property being accessed.
*
* @return mixed The value of the meta field, or the result of invoking `$field()` as a method
* with no arguments, or `false` if neither returns a truthy value.
*/
public function __get( $field ) {
if ( method_exists($this, 'meta') && $meta_value = $this->meta($field) ) {
return $this->$field = $meta_value;
}
if ( method_exists($this, $field) ) {
return $this->$field = $this->$field();
}
if ( 'custom' === $field ) {
Helper::deprecated(
"Accessing a meta value through {{ {$this->object_type}.custom }}",
"{{ {$this->object_type}.meta() }} or {{ {$this->object_type}.raw_meta() }}",
'2.0.0'
);
}
return $this->$field = false;
}
/**
* Takes an array or object and adds the properties to the parent object.
*
* @example
* ```php
* $data = array( 'airplane' => '757-200', 'flight' => '5316' );
* $post = Timber::get_post();
* $post->import(data);
*
* echo $post->airplane; // 757-200
* ```
* @param array|object $info an object or array you want to grab data from to attach to the Timber object
*/
public function import( $info, $force = false, $only_declared_properties = false ) {
if ( is_object($info) ) {
$info = get_object_vars($info);
}
if ( is_array($info) ) {
foreach ( $info as $key => $value ) {
if ( $key === '' || ord($key[0]) === 0 ) {
continue;
}
if ( !empty($key) && $force ) {
$this->$key = $value;
} else if ( !empty($key) && !method_exists($this, $key) ) {
if ( $only_declared_properties ) {
if ( property_exists($this, $key) ) {
$this->$key = $value;
}
} else {
$this->$key = $value;
}
}
}
}
}
/**
* Updates metadata for the object.
*
* @deprecated 2.0.0 Use `update_metadata()` instead.
*
* @param string $key The key of the meta field to update.
* @param mixed $value The new value.
*/
public function update( $key, $value ) {
Helper::deprecated( 'Timber\Core::update()', 'update_metadata()', '2.0.0' );
update_metadata($this->object_type, $this->ID, $key, $value);
}
/**
* Can you edit this post/term/user? Well good for you. You're no better than me.
* @example
* ```twig
* {% if post.can_edit %}
* <a href="{{ post.edit_link }}">Edit</a>
* {% endif %}
* ```
* ```html
* <a href="http://example.org/wp-admin/edit.php?p=242">Edit</a>
* ```
* @return bool
*/
public function can_edit() {
if ( !function_exists('current_user_can') ) {
return false;
}
if ( current_user_can('edit_post', $this->ID) ) {
return true;
}
return false;
}
/**
*
*
* @return array
*/
public function get_method_values() {
$ret = array();
$ret['can_edit'] = $this->can_edit();
return $ret;
}
}