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