Plugin Directory

source: codepress-admin-columns/trunk/classes/Settings/Column/Meta.php @ 2293580

Last change on this file since 2293580 was 2293580, checked in by tschutter, 5 years ago

trunk

File size: 3.9 KB
Line 
1<?php
2
3namespace AC\Settings\Column;
4
5use AC\Form\Element\Select;
6use AC\MetaType;
7use AC\Settings\Column;
8use AC\View;
9
10abstract class Meta extends Column {
11
12        /**
13         * @var string
14         */
15        private $field;
16
17        abstract protected function get_meta_keys();
18
19        protected function define_options() {
20                return [ 'field' ];
21        }
22
23        /**
24         * @return Select
25         */
26        protected function get_setting_field() {
27                $setting = $this
28                        ->create_element( 'select', 'field' )
29                        ->set_options( $this->group_keys( $this->get_cached_keys() ) )
30                        ->set_no_result( __( 'No fields available.', 'codepress-admin-columns' ) );
31
32                return $setting;
33        }
34
35        /**
36         * @return array|false
37         */
38        protected function get_cached_keys() {
39                $keys = $this->get_cache();
40
41                if ( ! $keys ) {
42                        $keys = $this->get_meta_keys();
43
44                        $this->set_cache( $keys );
45                }
46
47                return $keys;
48        }
49
50        /**
51         * @return string
52         */
53        protected function get_cache_key() {
54                return $this->column->get_list_screen()->get_storage_key();
55        }
56
57        /**
58         * @return string
59         */
60        protected function get_meta_type() {
61                return $this->column->get_list_screen()->get_meta_type();
62        }
63
64        protected function get_cache_group() {
65                return 'ac_settings_meta';
66        }
67
68        /**
69         * @return View
70         */
71        public function create_view() {
72                $view = new View( [
73                        'label'   => __( 'Field', 'codepress-admin-columns' ),
74                        'setting' => $this->get_setting_field(),
75                ] );
76
77                return $view;
78        }
79
80        /**
81         * @return string
82         */
83        public function get_field() {
84                return $this->field;
85        }
86
87        /**
88         * @param string $field
89         *
90         * @return bool
91         */
92        public function set_field( $field ) {
93                $this->field = $field;
94
95                return true;
96        }
97
98        private function get_cache() {
99                return wp_cache_get( $this->get_cache_key(), $this->get_cache_group() );
100        }
101
102        /**
103         * @param array $data
104         * @param int   $expire Seconds
105         */
106        private function set_cache( $data, $expire = 15 ) {
107                wp_cache_add( $this->get_cache_key(), $data, $this->get_cache_group(), $expire );
108        }
109
110        /**
111         * @return array
112         */
113        protected function get_meta_groups() {
114                global $wpdb;
115
116                $groups = [
117                        ''  => __( 'Public', 'codepress-admin-columns' ),
118                        '_' => __( 'Hidden', 'codepress-admin-columns' ),
119                ];
120
121                // User only
122                if ( MetaType::USER === $this->get_meta_type() ) {
123
124                        if ( is_multisite() ) {
125                                foreach ( get_sites() as $site ) {
126                                        $label = __( 'Network Site:', 'codepress-admin-columns' ) . ' ' . ac_helper()->network->get_site_option( $site->blog_id, 'blogname' );
127
128                                        if ( get_current_blog_id() == $site->blog_id ) {
129                                                $label .= ' (' . __( 'current', 'codepress-admin-columns' ) . ')';
130                                        }
131
132                                        $groups[ $wpdb->get_blog_prefix( $site->blog_id ) ] = $label;
133                                }
134                        } else {
135                                $groups[ $wpdb->get_blog_prefix() ] = __( 'Site Options', 'codepress-admin-columns' );
136                        }
137                }
138
139                return $groups;
140        }
141
142        /**
143         * @param array $keys
144         *
145         * @return array
146         */
147        private function group_keys( $keys ) {
148                if ( ! $keys ) {
149                        return [];
150                }
151
152                $grouped = [];
153
154                $groups = $this->get_meta_groups();
155
156                // groups are ordered desc because the prefixes without a blog id ( e.g. wp_ ) should be matched last.
157                krsort( $groups );
158
159                foreach ( $groups as $prefix => $title ) {
160
161                        $options = [];
162
163                        foreach ( $keys as $k => $key ) {
164
165                                // Match prefix with meta key
166                                if ( $prefix && 0 === strpos( $key, $prefix ) ) {
167                                        $options[ $key ] = $key;
168
169                                        unset( $keys[ $k ] );
170                                }
171                        }
172
173                        if ( $options ) {
174                                $grouped[ $prefix ] = [
175                                        'title'   => $title,
176                                        'options' => $options,
177                                ];
178                        }
179                }
180
181                ksort( $grouped );
182
183                // Default group
184                if ( $keys ) {
185                        $default = [
186                                'title'   => $groups[''],
187                                'options' => array_combine( $keys, $keys ),
188                        ];
189
190                        array_unshift( $grouped, $default );
191                }
192
193                // Place the hidden group at the end
194                if ( isset( $grouped['_'] ) ) {
195                        $grouped[] = $grouped['_'];
196
197                        unset( $grouped['_'] );
198                }
199
200                // Remove groups when there is only one group
201                if ( 1 === count( $grouped ) ) {
202                        $grouped = array_pop( $grouped );
203
204                        $grouped = $grouped['options'];
205                }
206
207                return $grouped;
208        }
209
210}
Note: See TracBrowser for help on using the repository browser.