Admin
7 years ago
Ajax
7 years ago
Autoloader
7 years ago
Check
7 years ago
Column
7 years ago
Deprecated
7 years ago
Exception
7 years ago
Form
7 years ago
Helper
7 years ago
Integration
7 years ago
ListScreen
7 years ago
Message
7 years ago
Meta
7 years ago
Plugin
7 years ago
Preferences
7 years ago
Relation
7 years ago
Request
7 years ago
Response
7 years ago
Screen
7 years ago
Settings
7 years ago
Storage
7 years ago
Table
7 years ago
ThirdParty
7 years ago
Transient
7 years ago
API.php
7 years ago
Addon.php
7 years ago
Admin.php
7 years ago
AdminColumns.php
7 years ago
ArrayIterator.php
7 years ago
Autoloader.php
7 years ago
Builder.php
7 years ago
Capabilities.php
7 years ago
Collection.php
7 years ago
Column.php
7 years ago
Config.php
7 years ago
Dependencies.php
7 years ago
Expirable.php
7 years ago
Groups.php
7 years ago
Helper.php
7 years ago
Integration.php
7 years ago
IntegrationFactory.php
7 years ago
Integrations.php
7 years ago
ListScreen.php
7 years ago
ListScreenFactory.php
7 years ago
ListScreenGroups.php
7 years ago
ListScreenPost.php
7 years ago
ListScreenWP.php
7 years ago
Message.php
7 years ago
MetaType.php
7 years ago
Middleware.php
7 years ago
Plugin.php
7 years ago
PluginInformation.php
7 years ago
Preferences.php
7 years ago
Registrable.php
7 years ago
Relation.php
7 years ago
Request.php
7 years ago
Screen.php
7 years ago
ScreenController.php
7 years ago
Settings.php
7 years ago
Transient.php
7 years ago
TypedArrayIterator.php
7 years ago
View.php
7 years ago
API.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC; |
| 4 | |
| 5 | class API { |
| 6 | |
| 7 | /** |
| 8 | * @var array [ |
| 9 | * $listscreen_key => [ |
| 10 | * [ 'columns' ][ array $column_settings ] |
| 11 | * [ 'layout' ][ array $layout_settings ] |
| 12 | * ] |
| 13 | * ] |
| 14 | */ |
| 15 | private $columndata; |
| 16 | |
| 17 | /** |
| 18 | * @param ListScreen $list_screen |
| 19 | */ |
| 20 | public function set_column_settings( ListScreen $list_screen ) { |
| 21 | $settings = $this->get_column_settings( $list_screen ); |
| 22 | |
| 23 | if ( ! $settings ) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | $list_screen->set_settings( $settings )->set_read_only( true ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param ListScreen $list_screen |
| 32 | * |
| 33 | * @return array|false |
| 34 | */ |
| 35 | public function get_column_settings( ListScreen $list_screen ) { |
| 36 | $columndata = $this->get_columndata( $list_screen->get_key() ); |
| 37 | |
| 38 | if ( ! $columndata ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | foreach ( $columndata as $data ) { |
| 43 | if ( $list_screen->get_storage_key() === $list_screen->get_key() . $data['layout']['id'] ) { |
| 44 | return $data['columns']; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param ListScreen $list_screen |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function get_layouts_settings( $list_screen ) { |
| 57 | $columndata = $this->get_columndata( $list_screen->get_key() ); |
| 58 | |
| 59 | if ( ! $columndata ) { |
| 60 | return array(); |
| 61 | } |
| 62 | |
| 63 | $layouts = array(); |
| 64 | foreach ( $columndata as $data ) { |
| 65 | $layouts[] = $data['layout']; |
| 66 | } |
| 67 | |
| 68 | return $layouts; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param $list_screen_keys |
| 73 | * @param $columndata |
| 74 | */ |
| 75 | public function load_columndata( $list_screen_keys, $columndata ) { |
| 76 | foreach ( (array) $list_screen_keys as $list_screen_key ) { |
| 77 | $this->add_columndata( $list_screen_key, $columndata ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param string $list_screen_key List screen key |
| 83 | * @param $columndata |
| 84 | */ |
| 85 | private function add_columndata( $list_screen_key, $columndata ) { |
| 86 | $columndata = $this->convert_old_format_to_current( $columndata ); |
| 87 | $columndata = $this->set_as_read_only( $columndata ); |
| 88 | |
| 89 | $this->columndata[ $list_screen_key ] = array_merge( $this->get_columndata( $list_screen_key ), $columndata ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param string $list_screen_key |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | private function get_columndata( $list_screen_key ) { |
| 98 | if ( ! isset( $this->columndata[ $list_screen_key ] ) ) { |
| 99 | return array(); |
| 100 | } |
| 101 | |
| 102 | return $this->columndata[ $list_screen_key ]; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param array $columndata |
| 107 | * |
| 108 | * @return array |
| 109 | */ |
| 110 | private function set_as_read_only( $columndata ) { |
| 111 | foreach ( $columndata as $k => $column ) { |
| 112 | $columndata[ $k ]['layout']['read_only'] = true; |
| 113 | } |
| 114 | |
| 115 | return $columndata; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Convert the old v3 format to the latest which includes layouts |
| 120 | * |
| 121 | * @param array $columndata |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | private function convert_old_format_to_current( $columndata ) { |
| 126 | |
| 127 | // Convert old export formats to new layout format |
| 128 | $old_format_columns = array(); |
| 129 | foreach ( $columndata as $k => $data ) { |
| 130 | if ( ! isset( $data['columns'] ) ) { |
| 131 | $old_format_columns[ $k ] = $data; |
| 132 | unset( $columndata[ $k ] ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if ( $old_format_columns ) { |
| 137 | array_unshift( $columndata, array( 'columns' => $old_format_columns ) ); |
| 138 | } |
| 139 | |
| 140 | // Add layout if missing |
| 141 | foreach ( $columndata as $k => $data ) { |
| 142 | if ( ! isset( $data['layout'] ) ) { |
| 143 | |
| 144 | $columndata[ $k ] = array( |
| 145 | 'columns' => isset( $data['columns'] ) ? $data['columns'] : $data, |
| 146 | 'layout' => array( |
| 147 | // unique id based on settings |
| 148 | 'id' => sanitize_key( substr( md5( serialize( $data ) ), 0, 16 ) ), |
| 149 | 'name' => __( 'Imported' ) . ( $k ? ' #' . $k : '' ), |
| 150 | ), |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $columndata; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param string $json JSON encoded settings |
| 160 | */ |
| 161 | public function load_from_json( $json ) { |
| 162 | $array = json_decode( $json, true ); |
| 163 | |
| 164 | $this->load_from_array( $array ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param array $array |
| 169 | */ |
| 170 | public function load_from_array( $array ) { |
| 171 | foreach ( $array as $list_screen => $columndata ) { |
| 172 | $this->load_columndata( $list_screen, $columndata ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |