@@ -2,57 +2,57 @@ |
||
2 | 2 | |
3 | 3 | class ACFTimber { |
4 | 4 | |
5 | - function __construct() { |
|
6 | - add_filter( 'timber_post_get_meta', array( $this, 'post_get_meta' ), 10, 2 ); |
|
7 | - add_filter( 'timber_post_get_meta_field', array( $this, 'post_get_meta_field' ), 10, 3 ); |
|
8 | - add_filter( 'timber_term_get_meta', array( $this, 'term_get_meta' ), 10, 3 ); |
|
9 | - add_filter( 'timber_term_get_meta_field', array( $this, 'term_get_meta_field' ), 10, 4 ); |
|
10 | - add_filter( 'timber_user_get_meta_field_pre', array( $this, 'user_get_meta_field' ), 10, 3 ); |
|
11 | - add_filter( 'timber_term_set_meta', array( $this, 'term_set_meta'), 10, 4 ); |
|
12 | - } |
|
13 | - |
|
14 | - function post_get_meta( $customs, $post_id ) { |
|
15 | - return $customs; |
|
16 | - } |
|
17 | - |
|
18 | - function post_get_meta_field( $value, $post_id, $field_name ) { |
|
19 | - return get_field( $field_name, $post_id ); |
|
20 | - } |
|
21 | - |
|
22 | - function term_get_meta_field( $value, $term_id, $field_name, $term ) { |
|
23 | - $searcher = $term->taxonomy . "_" . $term->ID; |
|
24 | - return get_field( $field_name, $searcher ); |
|
25 | - } |
|
26 | - |
|
27 | - function term_set_meta( $value, $field, $term_id, $term ) { |
|
28 | - $searcher = $term->taxonomy . "_" . $term->ID; |
|
29 | - update_field( $field, $value, $searcher ); |
|
30 | - return $value; |
|
31 | - } |
|
32 | - |
|
33 | - function term_get_meta( $fields, $term_id, $term ) { |
|
34 | - $searcher = $term->taxonomy . "_" . $term->ID; // save to a specific category |
|
35 | - $fds = get_fields( $searcher ); |
|
36 | - if ( is_array( $fds ) ) { |
|
37 | - foreach ( $fds as $key => $value ) { |
|
38 | - $key = preg_replace( '/_/', '', $key, 1 ); |
|
39 | - $key = str_replace( $searcher, '', $key ); |
|
40 | - $key = preg_replace( '/_/', '', $key, 1 ); |
|
41 | - $field = get_field( $key, $searcher ); |
|
42 | - $fields[$key] = $field; |
|
43 | - } |
|
44 | - $fields = array_merge( $fields, $fds ); |
|
45 | - } |
|
46 | - return $fields; |
|
47 | - } |
|
48 | - |
|
49 | - function user_get_meta( $fields, $user_id ) { |
|
50 | - return $fields; |
|
51 | - } |
|
52 | - |
|
53 | - function user_get_meta_field( $value, $uid, $field ) { |
|
54 | - return get_field( $field, 'user_' . $uid ); |
|
55 | - } |
|
5 | + function __construct() { |
|
6 | + add_filter( 'timber_post_get_meta', array( $this, 'post_get_meta' ), 10, 2 ); |
|
7 | + add_filter( 'timber_post_get_meta_field', array( $this, 'post_get_meta_field' ), 10, 3 ); |
|
8 | + add_filter( 'timber_term_get_meta', array( $this, 'term_get_meta' ), 10, 3 ); |
|
9 | + add_filter( 'timber_term_get_meta_field', array( $this, 'term_get_meta_field' ), 10, 4 ); |
|
10 | + add_filter( 'timber_user_get_meta_field_pre', array( $this, 'user_get_meta_field' ), 10, 3 ); |
|
11 | + add_filter( 'timber_term_set_meta', array( $this, 'term_set_meta'), 10, 4 ); |
|
12 | + } |
|
13 | + |
|
14 | + function post_get_meta( $customs, $post_id ) { |
|
15 | + return $customs; |
|
16 | + } |
|
17 | + |
|
18 | + function post_get_meta_field( $value, $post_id, $field_name ) { |
|
19 | + return get_field( $field_name, $post_id ); |
|
20 | + } |
|
21 | + |
|
22 | + function term_get_meta_field( $value, $term_id, $field_name, $term ) { |
|
23 | + $searcher = $term->taxonomy . "_" . $term->ID; |
|
24 | + return get_field( $field_name, $searcher ); |
|
25 | + } |
|
26 | + |
|
27 | + function term_set_meta( $value, $field, $term_id, $term ) { |
|
28 | + $searcher = $term->taxonomy . "_" . $term->ID; |
|
29 | + update_field( $field, $value, $searcher ); |
|
30 | + return $value; |
|
31 | + } |
|
32 | + |
|
33 | + function term_get_meta( $fields, $term_id, $term ) { |
|
34 | + $searcher = $term->taxonomy . "_" . $term->ID; // save to a specific category |
|
35 | + $fds = get_fields( $searcher ); |
|
36 | + if ( is_array( $fds ) ) { |
|
37 | + foreach ( $fds as $key => $value ) { |
|
38 | + $key = preg_replace( '/_/', '', $key, 1 ); |
|
39 | + $key = str_replace( $searcher, '', $key ); |
|
40 | + $key = preg_replace( '/_/', '', $key, 1 ); |
|
41 | + $field = get_field( $key, $searcher ); |
|
42 | + $fields[$key] = $field; |
|
43 | + } |
|
44 | + $fields = array_merge( $fields, $fds ); |
|
45 | + } |
|
46 | + return $fields; |
|
47 | + } |
|
48 | + |
|
49 | + function user_get_meta( $fields, $user_id ) { |
|
50 | + return $fields; |
|
51 | + } |
|
52 | + |
|
53 | + function user_get_meta_field( $value, $uid, $field ) { |
|
54 | + return get_field( $field, 'user_' . $uid ); |
|
55 | + } |
|
56 | 56 | } |
57 | 57 | |
58 | 58 |
@@ -6,31 +6,31 @@ |
||
6 | 6 | */ |
7 | 7 | class TimberCommand { |
8 | 8 | |
9 | - public static function clear_cache($mode = 'all'){ |
|
10 | - if (is_array($mode)){ |
|
11 | - $mode = reset($mode); |
|
12 | - } |
|
13 | - if ($mode == 'all') { |
|
14 | - $twig_cache = self::clear_cache_twig(); |
|
15 | - $timber_cache = self::clear_cache_timber(); |
|
16 | - if ($twig_cache && $timber_cache){ |
|
17 | - return true; |
|
18 | - } |
|
19 | - } else if ($mode == 'twig') { |
|
20 | - return self::clear_cache_twig(); |
|
21 | - } else if ($mode == 'timber') { |
|
22 | - return self::clear_cache_timber(); |
|
23 | - } |
|
24 | - } |
|
9 | + public static function clear_cache($mode = 'all'){ |
|
10 | + if (is_array($mode)){ |
|
11 | + $mode = reset($mode); |
|
12 | + } |
|
13 | + if ($mode == 'all') { |
|
14 | + $twig_cache = self::clear_cache_twig(); |
|
15 | + $timber_cache = self::clear_cache_timber(); |
|
16 | + if ($twig_cache && $timber_cache){ |
|
17 | + return true; |
|
18 | + } |
|
19 | + } else if ($mode == 'twig') { |
|
20 | + return self::clear_cache_twig(); |
|
21 | + } else if ($mode == 'timber') { |
|
22 | + return self::clear_cache_timber(); |
|
23 | + } |
|
24 | + } |
|
25 | 25 | |
26 | - static function clear_cache_timber(){ |
|
27 | - $loader = new TimberLoader(); |
|
28 | - return $loader->clear_cache_timber(); |
|
29 | - } |
|
26 | + static function clear_cache_timber(){ |
|
27 | + $loader = new TimberLoader(); |
|
28 | + return $loader->clear_cache_timber(); |
|
29 | + } |
|
30 | 30 | |
31 | - static function clear_cache_twig(){ |
|
32 | - $loader = new TimberLoader(); |
|
33 | - return $loader->clear_cache_twig(); |
|
34 | - } |
|
31 | + static function clear_cache_twig(){ |
|
32 | + $loader = new TimberLoader(); |
|
33 | + return $loader->clear_cache_twig(); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | } |
@@ -5,53 +5,53 @@ |
||
5 | 5 | |
6 | 6 | class Timber_WP_CLI_Command extends WP_CLI_Command { |
7 | 7 | |
8 | - /** |
|
9 | - * Clears Timber and Twig's Cache |
|
10 | - * |
|
11 | - * ## EXAMPLES |
|
12 | - * |
|
13 | - * wp timber clear_cache |
|
14 | - * |
|
15 | - */ |
|
16 | - public function clear_cache($mode = 'all') { |
|
17 | - TimberCommand::clear_cache($mode); |
|
18 | - } |
|
8 | + /** |
|
9 | + * Clears Timber and Twig's Cache |
|
10 | + * |
|
11 | + * ## EXAMPLES |
|
12 | + * |
|
13 | + * wp timber clear_cache |
|
14 | + * |
|
15 | + */ |
|
16 | + public function clear_cache($mode = 'all') { |
|
17 | + TimberCommand::clear_cache($mode); |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * Clears Twig's Cache |
|
22 | - * |
|
23 | - * ## EXAMPLES |
|
24 | - * |
|
25 | - * wp timber clear_cache_twig |
|
26 | - * |
|
27 | - */ |
|
28 | - function clear_cache_twig(){ |
|
29 | - $clear = TimberCommand::clear_cache_twig(); |
|
30 | - if ($clear){ |
|
31 | - WP_CLI::success('Cleared contents of twig cache'); |
|
32 | - } else { |
|
33 | - WP_CLI::warning('Failed to clear twig cache'); |
|
34 | - } |
|
35 | - } |
|
20 | + /** |
|
21 | + * Clears Twig's Cache |
|
22 | + * |
|
23 | + * ## EXAMPLES |
|
24 | + * |
|
25 | + * wp timber clear_cache_twig |
|
26 | + * |
|
27 | + */ |
|
28 | + function clear_cache_twig(){ |
|
29 | + $clear = TimberCommand::clear_cache_twig(); |
|
30 | + if ($clear){ |
|
31 | + WP_CLI::success('Cleared contents of twig cache'); |
|
32 | + } else { |
|
33 | + WP_CLI::warning('Failed to clear twig cache'); |
|
34 | + } |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Clears Timber's Cache |
|
39 | - * |
|
40 | - * ## EXAMPLES |
|
41 | - * |
|
42 | - * wp timber clear_cache_timber |
|
43 | - * |
|
44 | - */ |
|
45 | - function clear_cache_timber() { |
|
46 | - $clear = TimberCommand::clear_cache_timber(); |
|
47 | - $message = 'Failed to clear timber cache'; |
|
48 | - if ($clear){ |
|
49 | - $message = "Cleared contents of Timber's Cache"; |
|
50 | - WP_CLI::success($message); |
|
51 | - } else { |
|
52 | - WP_CLI::warning($message); |
|
53 | - } |
|
54 | - return $message; |
|
55 | - } |
|
37 | + /** |
|
38 | + * Clears Timber's Cache |
|
39 | + * |
|
40 | + * ## EXAMPLES |
|
41 | + * |
|
42 | + * wp timber clear_cache_timber |
|
43 | + * |
|
44 | + */ |
|
45 | + function clear_cache_timber() { |
|
46 | + $clear = TimberCommand::clear_cache_timber(); |
|
47 | + $message = 'Failed to clear timber cache'; |
|
48 | + if ($clear){ |
|
49 | + $message = "Cleared contents of Timber's Cache"; |
|
50 | + WP_CLI::success($message); |
|
51 | + } else { |
|
52 | + WP_CLI::warning($message); |
|
53 | + } |
|
54 | + return $message; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
@@ -2,9 +2,9 @@ |
||
2 | 2 | |
3 | 3 | class TimberAdmin { |
4 | 4 | |
5 | - public static function init() { |
|
6 | - return add_filter( 'plugin_row_meta', array( __CLASS__, 'meta_links' ), 10, 2 ); |
|
7 | - } |
|
5 | + public static function init() { |
|
6 | + return add_filter( 'plugin_row_meta', array( __CLASS__, 'meta_links' ), 10, 2 ); |
|
7 | + } |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * @param array $links |
@@ -2,116 +2,116 @@ |
||
2 | 2 | |
3 | 3 | abstract class TimberCore { |
4 | 4 | |
5 | - public $id; |
|
6 | - public $ID; |
|
7 | - public $object_type; |
|
5 | + public $id; |
|
6 | + public $ID; |
|
7 | + public $object_type; |
|
8 | 8 | |
9 | - /** |
|
10 | - * |
|
11 | - * |
|
12 | - * @return boolean |
|
13 | - */ |
|
14 | - function __isset( $field ) { |
|
15 | - if ( isset( $this->$field ) ) { |
|
16 | - return $this->$field; |
|
17 | - } |
|
18 | - return false; |
|
19 | - } |
|
9 | + /** |
|
10 | + * |
|
11 | + * |
|
12 | + * @return boolean |
|
13 | + */ |
|
14 | + function __isset( $field ) { |
|
15 | + if ( isset( $this->$field ) ) { |
|
16 | + return $this->$field; |
|
17 | + } |
|
18 | + return false; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * This is helpful for twig to return properties and methods see: https://github.com/fabpot/Twig/issues/2 |
|
23 | - * @return mixed |
|
24 | - */ |
|
25 | - function __call( $field, $args ) { |
|
26 | - return $this->__get( $field ); |
|
27 | - } |
|
21 | + /** |
|
22 | + * This is helpful for twig to return properties and methods see: https://github.com/fabpot/Twig/issues/2 |
|
23 | + * @return mixed |
|
24 | + */ |
|
25 | + function __call( $field, $args ) { |
|
26 | + return $this->__get( $field ); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * This is helpful for twig to return properties and methods see: https://github.com/fabpot/Twig/issues/2 |
|
31 | - * |
|
32 | - * @return mixed |
|
33 | - */ |
|
34 | - function __get( $field ) { |
|
35 | - if ( property_exists($this, $field) ) { |
|
36 | - return $this->$field; |
|
37 | - } |
|
38 | - if ( method_exists($this, 'meta') && $meta_value = $this->meta( $field ) ) { |
|
39 | - return $this->$field = $meta_value; |
|
40 | - } |
|
41 | - if ( method_exists($this, $field) ) { |
|
42 | - return $this->$field = $this->$field(); |
|
43 | - } |
|
44 | - return $this->$field = false; |
|
45 | - } |
|
29 | + /** |
|
30 | + * This is helpful for twig to return properties and methods see: https://github.com/fabpot/Twig/issues/2 |
|
31 | + * |
|
32 | + * @return mixed |
|
33 | + */ |
|
34 | + function __get( $field ) { |
|
35 | + if ( property_exists($this, $field) ) { |
|
36 | + return $this->$field; |
|
37 | + } |
|
38 | + if ( method_exists($this, 'meta') && $meta_value = $this->meta( $field ) ) { |
|
39 | + return $this->$field = $meta_value; |
|
40 | + } |
|
41 | + if ( method_exists($this, $field) ) { |
|
42 | + return $this->$field = $this->$field(); |
|
43 | + } |
|
44 | + return $this->$field = false; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Takes an array or object and adds the properties to the parent object |
|
49 | - * @example |
|
50 | - * ```php |
|
51 | - * $data = array('airplane' => '757-200', 'flight' => '5316'); |
|
52 | - * $post = new TimberPost() |
|
53 | - * $post->import(data); |
|
54 | - * echo $post->airplane; //757-200 |
|
55 | - * ``` |
|
56 | - * @param array|object $info an object or array you want to grab data from to attach to the Timber object |
|
57 | - */ |
|
58 | - function import( $info, $force = false ) { |
|
59 | - if ( is_object( $info ) ) { |
|
60 | - $info = get_object_vars( $info ); |
|
61 | - } |
|
62 | - if ( is_array( $info ) ) { |
|
63 | - foreach ( $info as $key => $value ) { |
|
64 | - if ( !empty( $key ) && $force ) { |
|
65 | - $this->$key = $value; |
|
66 | - } else if ( !empty( $key ) && !method_exists($this, $key) ){ |
|
67 | - $this->$key = $value; |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
47 | + /** |
|
48 | + * Takes an array or object and adds the properties to the parent object |
|
49 | + * @example |
|
50 | + * ```php |
|
51 | + * $data = array('airplane' => '757-200', 'flight' => '5316'); |
|
52 | + * $post = new TimberPost() |
|
53 | + * $post->import(data); |
|
54 | + * echo $post->airplane; //757-200 |
|
55 | + * ``` |
|
56 | + * @param array|object $info an object or array you want to grab data from to attach to the Timber object |
|
57 | + */ |
|
58 | + function import( $info, $force = false ) { |
|
59 | + if ( is_object( $info ) ) { |
|
60 | + $info = get_object_vars( $info ); |
|
61 | + } |
|
62 | + if ( is_array( $info ) ) { |
|
63 | + foreach ( $info as $key => $value ) { |
|
64 | + if ( !empty( $key ) && $force ) { |
|
65 | + $this->$key = $value; |
|
66 | + } else if ( !empty( $key ) && !method_exists($this, $key) ){ |
|
67 | + $this->$key = $value; |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | 73 | |
74 | - /** |
|
75 | - * @ignore |
|
76 | - * @param string $key |
|
77 | - * @param mixed $value |
|
78 | - */ |
|
79 | - function update( $key, $value ) { |
|
80 | - update_metadata( $this->object_type, $this->ID, $key, $value ); |
|
81 | - } |
|
74 | + /** |
|
75 | + * @ignore |
|
76 | + * @param string $key |
|
77 | + * @param mixed $value |
|
78 | + */ |
|
79 | + function update( $key, $value ) { |
|
80 | + update_metadata( $this->object_type, $this->ID, $key, $value ); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Can you edit this post/term/user? Well good for you. You're no better than me. |
|
85 | - * @example |
|
86 | - * ```twig |
|
87 | - * {% if post.can_edit %} |
|
88 | - * <a href="{{ post.edit_link }}">Edit</a> |
|
89 | - * {% endif %} |
|
90 | - * ``` |
|
91 | - * ```html |
|
92 | - * <a href="http://example.org/wp-admin/edit.php?p=242">Edit</a> |
|
93 | - * ``` |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - function can_edit() { |
|
97 | - if ( !function_exists( 'current_user_can' ) ) { |
|
98 | - return false; |
|
99 | - } |
|
100 | - if ( current_user_can( 'edit_post', $this->ID ) ) { |
|
101 | - return true; |
|
102 | - } |
|
103 | - return false; |
|
104 | - } |
|
83 | + /** |
|
84 | + * Can you edit this post/term/user? Well good for you. You're no better than me. |
|
85 | + * @example |
|
86 | + * ```twig |
|
87 | + * {% if post.can_edit %} |
|
88 | + * <a href="{{ post.edit_link }}">Edit</a> |
|
89 | + * {% endif %} |
|
90 | + * ``` |
|
91 | + * ```html |
|
92 | + * <a href="http://example.org/wp-admin/edit.php?p=242">Edit</a> |
|
93 | + * ``` |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + function can_edit() { |
|
97 | + if ( !function_exists( 'current_user_can' ) ) { |
|
98 | + return false; |
|
99 | + } |
|
100 | + if ( current_user_can( 'edit_post', $this->ID ) ) { |
|
101 | + return true; |
|
102 | + } |
|
103 | + return false; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - function get_method_values() { |
|
112 | - $ret = array(); |
|
113 | - $ret['can_edit'] = $this->can_edit(); |
|
114 | - return $ret; |
|
115 | - } |
|
106 | + /** |
|
107 | + * |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + function get_method_values() { |
|
112 | + $ret = array(); |
|
113 | + $ret['can_edit'] = $this->can_edit(); |
|
114 | + return $ret; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | } |
@@ -5,20 +5,20 @@ |
||
5 | 5 | * @package timber |
6 | 6 | */ |
7 | 7 | class TimberIntegrations { |
8 | - public static function init() { |
|
8 | + public static function init() { |
|
9 | 9 | |
10 | - add_action( 'init', array( __CLASS__, 'maybe_init_acftimber' ) ); |
|
10 | + add_action( 'init', array( __CLASS__, 'maybe_init_acftimber' ) ); |
|
11 | 11 | |
12 | - if ( class_exists( 'WP_CLI_Command' ) ) { |
|
13 | - WP_CLI::add_command( 'timber', 'Timber_WP_CLI_Command' ); |
|
14 | - } |
|
15 | - } |
|
12 | + if ( class_exists( 'WP_CLI_Command' ) ) { |
|
13 | + WP_CLI::add_command( 'timber', 'Timber_WP_CLI_Command' ); |
|
14 | + } |
|
15 | + } |
|
16 | 16 | |
17 | - public static function maybe_init_acftimber() { |
|
17 | + public static function maybe_init_acftimber() { |
|
18 | 18 | |
19 | - if ( class_exists( 'ACF' ) ) { |
|
20 | - new ACFTimber(); |
|
21 | - } |
|
19 | + if ( class_exists( 'ACF' ) ) { |
|
20 | + new ACFTimber(); |
|
21 | + } |
|
22 | 22 | |
23 | - } |
|
23 | + } |
|
24 | 24 | } |
@@ -2,96 +2,96 @@ |
||
2 | 2 | |
3 | 3 | // Exit if accessed directly |
4 | 4 | if ( !defined( 'ABSPATH' ) ) |
5 | - exit; |
|
5 | + exit; |
|
6 | 6 | |
7 | 7 | class TimberPostsCollection extends ArrayObject { |
8 | 8 | |
9 | - public function __construct( $posts = array(), $post_class = 'TimberPost' ) { |
|
10 | - $returned_posts = array(); |
|
11 | - if ( is_null( $posts ) ){ |
|
12 | - $posts = array(); |
|
13 | - } |
|
14 | - foreach ( $posts as $post_object ) { |
|
15 | - $post_class_use = $post_class; |
|
16 | - |
|
17 | - if ( is_array( $post_class ) ) { |
|
18 | - $post_type = get_post_type( $post_object ); |
|
19 | - $post_class_use = 'TimberPost'; |
|
20 | - |
|
21 | - if ( isset( $post_class[$post_type] ) ) { |
|
22 | - $post_class_use = $post_class[$post_type]; |
|
23 | - |
|
24 | - } else { |
|
25 | - if ( is_array( $post_class ) ) { |
|
26 | - TimberHelper::error_log( $post_type . ' of ' . $post_object->ID . ' not found in ' . print_r( $post_class, true ) ); |
|
27 | - } else { |
|
28 | - TimberHelper::error_log( $post_type . ' not found in ' . $post_class ); |
|
29 | - } |
|
30 | - } |
|
31 | - } |
|
32 | - |
|
33 | - // Don't create yet another object if $post_object is already of the right type |
|
34 | - if ( is_a( $post_object, $post_class_use ) ) { |
|
35 | - $post = $post_object; |
|
36 | - } else { |
|
37 | - $post = new $post_class_use( $post_object ); |
|
38 | - } |
|
39 | - |
|
40 | - if ( isset( $post->ID ) ) { |
|
41 | - $returned_posts[] = $post; |
|
42 | - } |
|
43 | - } |
|
44 | - |
|
45 | - $returned_posts = self::maybe_set_preview($returned_posts); |
|
46 | - |
|
47 | - parent::__construct( $returned_posts, $flags = 0, 'TimberPostsIterator' ); |
|
48 | - } |
|
49 | - |
|
50 | - public function get_posts() { |
|
51 | - return $this->getArrayCopy(); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param array $posts |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - static function maybe_set_preview( $posts ) { |
|
59 | - if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
|
60 | - && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
|
61 | - && current_user_can( 'edit_post', $_GET['preview_id'] ) ) { |
|
62 | - // No need to check the nonce, that already happened in _show_post_preview on init |
|
63 | - |
|
64 | - $preview_id = $_GET['preview_id']; |
|
65 | - foreach( $posts as &$post ) { |
|
66 | - if ( is_object( $post ) && $post->ID == $preview_id ) { |
|
67 | - // Based on _set_preview( $post ), but adds import_custom |
|
68 | - $preview = wp_get_post_autosave( $preview_id ); |
|
69 | - if ( is_object($preview) ) { |
|
70 | - |
|
71 | - $preview = sanitize_post($preview); |
|
72 | - |
|
73 | - $post->post_content = $preview->post_content; |
|
74 | - $post->post_title = $preview->post_title; |
|
75 | - $post->post_excerpt = $preview->post_excerpt; |
|
76 | - $post->import_custom( $preview_id ); |
|
77 | - |
|
78 | - add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
79 | - } |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - return $posts; |
|
86 | - } |
|
9 | + public function __construct( $posts = array(), $post_class = 'TimberPost' ) { |
|
10 | + $returned_posts = array(); |
|
11 | + if ( is_null( $posts ) ){ |
|
12 | + $posts = array(); |
|
13 | + } |
|
14 | + foreach ( $posts as $post_object ) { |
|
15 | + $post_class_use = $post_class; |
|
16 | + |
|
17 | + if ( is_array( $post_class ) ) { |
|
18 | + $post_type = get_post_type( $post_object ); |
|
19 | + $post_class_use = 'TimberPost'; |
|
20 | + |
|
21 | + if ( isset( $post_class[$post_type] ) ) { |
|
22 | + $post_class_use = $post_class[$post_type]; |
|
23 | + |
|
24 | + } else { |
|
25 | + if ( is_array( $post_class ) ) { |
|
26 | + TimberHelper::error_log( $post_type . ' of ' . $post_object->ID . ' not found in ' . print_r( $post_class, true ) ); |
|
27 | + } else { |
|
28 | + TimberHelper::error_log( $post_type . ' not found in ' . $post_class ); |
|
29 | + } |
|
30 | + } |
|
31 | + } |
|
32 | + |
|
33 | + // Don't create yet another object if $post_object is already of the right type |
|
34 | + if ( is_a( $post_object, $post_class_use ) ) { |
|
35 | + $post = $post_object; |
|
36 | + } else { |
|
37 | + $post = new $post_class_use( $post_object ); |
|
38 | + } |
|
39 | + |
|
40 | + if ( isset( $post->ID ) ) { |
|
41 | + $returned_posts[] = $post; |
|
42 | + } |
|
43 | + } |
|
44 | + |
|
45 | + $returned_posts = self::maybe_set_preview($returned_posts); |
|
46 | + |
|
47 | + parent::__construct( $returned_posts, $flags = 0, 'TimberPostsIterator' ); |
|
48 | + } |
|
49 | + |
|
50 | + public function get_posts() { |
|
51 | + return $this->getArrayCopy(); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param array $posts |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + static function maybe_set_preview( $posts ) { |
|
59 | + if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
|
60 | + && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
|
61 | + && current_user_can( 'edit_post', $_GET['preview_id'] ) ) { |
|
62 | + // No need to check the nonce, that already happened in _show_post_preview on init |
|
63 | + |
|
64 | + $preview_id = $_GET['preview_id']; |
|
65 | + foreach( $posts as &$post ) { |
|
66 | + if ( is_object( $post ) && $post->ID == $preview_id ) { |
|
67 | + // Based on _set_preview( $post ), but adds import_custom |
|
68 | + $preview = wp_get_post_autosave( $preview_id ); |
|
69 | + if ( is_object($preview) ) { |
|
70 | + |
|
71 | + $preview = sanitize_post($preview); |
|
72 | + |
|
73 | + $post->post_content = $preview->post_content; |
|
74 | + $post->post_title = $preview->post_title; |
|
75 | + $post->post_excerpt = $preview->post_excerpt; |
|
76 | + $post->import_custom( $preview_id ); |
|
77 | + |
|
78 | + add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
79 | + } |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + return $posts; |
|
86 | + } |
|
87 | 87 | |
88 | 88 | } |
89 | 89 | |
90 | 90 | class TimberPostsIterator extends ArrayIterator { |
91 | 91 | |
92 | - public function current() { |
|
93 | - global $post; |
|
94 | - $post = parent::current(); |
|
95 | - return $post; |
|
96 | - } |
|
92 | + public function current() { |
|
93 | + global $post; |
|
94 | + $post = parent::current(); |
|
95 | + return $post; |
|
96 | + } |
|
97 | 97 | } |
@@ -1,156 +1,156 @@ |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | 3 | if ( !defined( 'ABSPATH' ) ) |
4 | - exit; |
|
4 | + exit; |
|
5 | 5 | |
6 | 6 | class TimberQueryIterator implements Iterator { |
7 | 7 | |
8 | - /** |
|
9 | - * |
|
10 | - * |
|
11 | - * @var WP_Query |
|
12 | - */ |
|
13 | - private $_query = null; |
|
14 | - private $_posts_class = 'TimberPost'; |
|
15 | - |
|
16 | - public function __construct( $query = false, $posts_class = 'TimberPost' ) { |
|
17 | - add_action( 'pre_get_posts', array($this, 'fix_number_posts_wp_quirk' )); |
|
18 | - if ( $posts_class ) |
|
19 | - $this->_posts_class = $posts_class; |
|
20 | - |
|
21 | - if ( is_a( $query, 'WP_Query' ) ) { |
|
22 | - // We got a full-fledged WP Query, look no further! |
|
23 | - $the_query = $query; |
|
24 | - |
|
25 | - } elseif ( false === $query ) { |
|
26 | - // If query is explicitly set to false, use the main loop |
|
27 | - global $wp_query; |
|
28 | - $the_query =& $wp_query; |
|
29 | - //if we're on a custom posts page? |
|
30 | - $the_query = self::handle_maybe_custom_posts_page($the_query); |
|
31 | - } elseif ( TimberHelper::is_array_assoc( $query ) || ( is_string( $query ) && strstr( $query, '=' ) ) ) { |
|
32 | - // We have a regularly formed WP query string or array to use |
|
33 | - $the_query = new WP_Query( $query ); |
|
34 | - |
|
35 | - } elseif ( is_numeric( $query ) || is_string( $query ) ) { |
|
36 | - // We have what could be a post name or post ID to pull out |
|
37 | - $the_query = self::get_query_from_string( $query ); |
|
38 | - |
|
39 | - } elseif ( is_array( $query ) && count( $query ) && ( is_integer( $query[0] ) || is_string( $query[0] ) ) ) { |
|
40 | - // We have a list of pids (post IDs) to extract from |
|
41 | - $the_query = self::get_query_from_array_of_ids( $query ); |
|
42 | - } elseif ( is_array($query) && empty($query)) { |
|
43 | - // it's an empty array |
|
44 | - $the_query = array(); |
|
45 | - } else { |
|
46 | - TimberHelper::error_log( 'I have failed you! in ' . basename( __FILE__ ) . '::' . __LINE__ ); |
|
47 | - TimberHelper::error_log( $query ); |
|
48 | - |
|
49 | - // We have failed hard, at least let get something. |
|
50 | - $the_query = new WP_Query(); |
|
51 | - } |
|
52 | - |
|
53 | - $this->_query = $the_query; |
|
54 | - |
|
55 | - } |
|
56 | - |
|
57 | - public function get_posts( $return_collection = false ) { |
|
58 | - if (isset($this->_query->posts)){ |
|
59 | - $posts = new TimberPostsCollection( $this->_query->posts, $this->_posts_class ); |
|
60 | - return ( $return_collection ) ? $posts : $posts->get_posts(); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - // |
|
65 | - // GET POSTS |
|
66 | - // |
|
67 | - public static function get_query_from_array_of_ids( $query = array() ) { |
|
68 | - if ( !is_array( $query ) || !count( $query ) ) |
|
69 | - return null; |
|
70 | - |
|
71 | - return new WP_Query( array( |
|
72 | - 'post_type'=> 'any', |
|
73 | - 'ignore_sticky_posts' => true, |
|
74 | - 'post__in' => $query, |
|
75 | - 'orderby' => 'post__in', |
|
76 | - 'nopaging' => true |
|
77 | - ) ); |
|
78 | - } |
|
79 | - |
|
80 | - public static function get_query_from_string( $string = '' ) { |
|
81 | - $post_type = false; |
|
82 | - |
|
83 | - if ( is_string( $string ) && strstr( $string, '#' ) ) { |
|
84 | - //we have a post_type directive here |
|
85 | - list( $post_type, $string ) = explode( '#', $string ); |
|
86 | - } |
|
87 | - |
|
88 | - $query = array( |
|
89 | - 'post_type' => ( $post_type ) ? $post_type : 'any' |
|
90 | - ); |
|
91 | - |
|
92 | - if ( is_numeric( $string ) ) { |
|
93 | - $query['p'] = $string; |
|
94 | - |
|
95 | - } else { |
|
96 | - $query['name'] = $string; |
|
97 | - } |
|
98 | - |
|
99 | - return new WP_Query( $query ); |
|
100 | - } |
|
101 | - |
|
102 | - // |
|
103 | - // Iterator Interface |
|
104 | - // |
|
105 | - |
|
106 | - public function valid() { |
|
107 | - return $this->_query->have_posts(); |
|
108 | - } |
|
109 | - |
|
110 | - public function current() { |
|
111 | - global $post; |
|
112 | - |
|
113 | - $this->_query->the_post(); |
|
114 | - |
|
115 | - // Sets up the global post, but also return the post, for use in Twig template |
|
116 | - $posts_class = $this->_posts_class; |
|
117 | - return new $posts_class( $post ); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Don't implement next, because current already advances the loop |
|
122 | - */ |
|
123 | - final public function next() {} |
|
124 | - |
|
125 | - public function rewind() { |
|
126 | - $this->_query->rewind_posts(); |
|
127 | - } |
|
128 | - |
|
129 | - public function key() { |
|
130 | - $this->_query->current_post; |
|
131 | - } |
|
132 | - |
|
133 | - //get_posts users numberposts |
|
134 | - static function fix_number_posts_wp_quirk( $query ) { |
|
135 | - if (isset($query->query) && isset($query->query['numberposts']) |
|
136 | - && !isset($query->query['posts_per_page'])) { |
|
137 | - $query->set( 'posts_per_page', $query->query['numberposts'] ); |
|
138 | - } |
|
139 | - return $query; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * this will test for whether a custom page to display posts is active, and if so, set the query to the default |
|
144 | - * @param WP_Query $query the original query recived from WordPress |
|
145 | - * @return WP_Query |
|
146 | - */ |
|
147 | - static function handle_maybe_custom_posts_page( $query ) { |
|
148 | - if ($custom_posts_page = get_option('page_for_posts')) { |
|
149 | - if ( isset($query->query['p']) && $query->query['p'] == $custom_posts_page ) { |
|
150 | - return new WP_Query(array('post_type' => 'post')); |
|
151 | - } |
|
152 | - } |
|
153 | - return $query; |
|
154 | - } |
|
8 | + /** |
|
9 | + * |
|
10 | + * |
|
11 | + * @var WP_Query |
|
12 | + */ |
|
13 | + private $_query = null; |
|
14 | + private $_posts_class = 'TimberPost'; |
|
15 | + |
|
16 | + public function __construct( $query = false, $posts_class = 'TimberPost' ) { |
|
17 | + add_action( 'pre_get_posts', array($this, 'fix_number_posts_wp_quirk' )); |
|
18 | + if ( $posts_class ) |
|
19 | + $this->_posts_class = $posts_class; |
|
20 | + |
|
21 | + if ( is_a( $query, 'WP_Query' ) ) { |
|
22 | + // We got a full-fledged WP Query, look no further! |
|
23 | + $the_query = $query; |
|
24 | + |
|
25 | + } elseif ( false === $query ) { |
|
26 | + // If query is explicitly set to false, use the main loop |
|
27 | + global $wp_query; |
|
28 | + $the_query =& $wp_query; |
|
29 | + //if we're on a custom posts page? |
|
30 | + $the_query = self::handle_maybe_custom_posts_page($the_query); |
|
31 | + } elseif ( TimberHelper::is_array_assoc( $query ) || ( is_string( $query ) && strstr( $query, '=' ) ) ) { |
|
32 | + // We have a regularly formed WP query string or array to use |
|
33 | + $the_query = new WP_Query( $query ); |
|
34 | + |
|
35 | + } elseif ( is_numeric( $query ) || is_string( $query ) ) { |
|
36 | + // We have what could be a post name or post ID to pull out |
|
37 | + $the_query = self::get_query_from_string( $query ); |
|
38 | + |
|
39 | + } elseif ( is_array( $query ) && count( $query ) && ( is_integer( $query[0] ) || is_string( $query[0] ) ) ) { |
|
40 | + // We have a list of pids (post IDs) to extract from |
|
41 | + $the_query = self::get_query_from_array_of_ids( $query ); |
|
42 | + } elseif ( is_array($query) && empty($query)) { |
|
43 | + // it's an empty array |
|
44 | + $the_query = array(); |
|
45 | + } else { |
|
46 | + TimberHelper::error_log( 'I have failed you! in ' . basename( __FILE__ ) . '::' . __LINE__ ); |
|
47 | + TimberHelper::error_log( $query ); |
|
48 | + |
|
49 | + // We have failed hard, at least let get something. |
|
50 | + $the_query = new WP_Query(); |
|
51 | + } |
|
52 | + |
|
53 | + $this->_query = $the_query; |
|
54 | + |
|
55 | + } |
|
56 | + |
|
57 | + public function get_posts( $return_collection = false ) { |
|
58 | + if (isset($this->_query->posts)){ |
|
59 | + $posts = new TimberPostsCollection( $this->_query->posts, $this->_posts_class ); |
|
60 | + return ( $return_collection ) ? $posts : $posts->get_posts(); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + // |
|
65 | + // GET POSTS |
|
66 | + // |
|
67 | + public static function get_query_from_array_of_ids( $query = array() ) { |
|
68 | + if ( !is_array( $query ) || !count( $query ) ) |
|
69 | + return null; |
|
70 | + |
|
71 | + return new WP_Query( array( |
|
72 | + 'post_type'=> 'any', |
|
73 | + 'ignore_sticky_posts' => true, |
|
74 | + 'post__in' => $query, |
|
75 | + 'orderby' => 'post__in', |
|
76 | + 'nopaging' => true |
|
77 | + ) ); |
|
78 | + } |
|
79 | + |
|
80 | + public static function get_query_from_string( $string = '' ) { |
|
81 | + $post_type = false; |
|
82 | + |
|
83 | + if ( is_string( $string ) && strstr( $string, '#' ) ) { |
|
84 | + //we have a post_type directive here |
|
85 | + list( $post_type, $string ) = explode( '#', $string ); |
|
86 | + } |
|
87 | + |
|
88 | + $query = array( |
|
89 | + 'post_type' => ( $post_type ) ? $post_type : 'any' |
|
90 | + ); |
|
91 | + |
|
92 | + if ( is_numeric( $string ) ) { |
|
93 | + $query['p'] = $string; |
|
94 | + |
|
95 | + } else { |
|
96 | + $query['name'] = $string; |
|
97 | + } |
|
98 | + |
|
99 | + return new WP_Query( $query ); |
|
100 | + } |
|
101 | + |
|
102 | + // |
|
103 | + // Iterator Interface |
|
104 | + // |
|
105 | + |
|
106 | + public function valid() { |
|
107 | + return $this->_query->have_posts(); |
|
108 | + } |
|
109 | + |
|
110 | + public function current() { |
|
111 | + global $post; |
|
112 | + |
|
113 | + $this->_query->the_post(); |
|
114 | + |
|
115 | + // Sets up the global post, but also return the post, for use in Twig template |
|
116 | + $posts_class = $this->_posts_class; |
|
117 | + return new $posts_class( $post ); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Don't implement next, because current already advances the loop |
|
122 | + */ |
|
123 | + final public function next() {} |
|
124 | + |
|
125 | + public function rewind() { |
|
126 | + $this->_query->rewind_posts(); |
|
127 | + } |
|
128 | + |
|
129 | + public function key() { |
|
130 | + $this->_query->current_post; |
|
131 | + } |
|
132 | + |
|
133 | + //get_posts users numberposts |
|
134 | + static function fix_number_posts_wp_quirk( $query ) { |
|
135 | + if (isset($query->query) && isset($query->query['numberposts']) |
|
136 | + && !isset($query->query['posts_per_page'])) { |
|
137 | + $query->set( 'posts_per_page', $query->query['numberposts'] ); |
|
138 | + } |
|
139 | + return $query; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * this will test for whether a custom page to display posts is active, and if so, set the query to the default |
|
144 | + * @param WP_Query $query the original query recived from WordPress |
|
145 | + * @return WP_Query |
|
146 | + */ |
|
147 | + static function handle_maybe_custom_posts_page( $query ) { |
|
148 | + if ($custom_posts_page = get_option('page_for_posts')) { |
|
149 | + if ( isset($query->query['p']) && $query->query['p'] == $custom_posts_page ) { |
|
150 | + return new WP_Query(array('post_type' => 'post')); |
|
151 | + } |
|
152 | + } |
|
153 | + return $query; |
|
154 | + } |
|
155 | 155 | |
156 | 156 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | |
65 | 65 | /** @api |
66 | 66 | * @var string for people who like trackback spam |
67 | - */ |
|
67 | + */ |
|
68 | 68 | public $pingback_url; |
69 | 69 | public $siteurl; |
70 | 70 | /** |