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