| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataAccess\User_Menu |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataAccess\User_Menu { |
| 9 |
|
| 10 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; |
| 11 |
use WPDataAccess\Simple_Form\WPDA_Simple_Form; |
| 12 |
use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item; |
| 13 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 14 |
use WPDataAccess\WPDA; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class WPDA_User_Menu_Form |
| 18 |
* |
| 19 |
* An extention of class WPDA_Simple_Form that implements a data entry form for plugin table wp_wpda_menu_items. |
| 20 |
* |
| 21 |
* @package WPDataAccess\User_Menu |
| 22 |
* @author Peter Schulz |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class WPDA_User_Menu_Form extends WPDA_Simple_Form { |
| 26 |
|
| 27 |
/** |
| 28 |
* WPDA_User_Menu_Form constructor |
| 29 |
* |
| 30 |
* Overwrites messages (success and failure). |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* |
| 34 |
* @param string $schema_name Database schema name. |
| 35 |
* @param string $table_name Database table name. |
| 36 |
* @param \WPDataAccess\Data_Dictionary\WPDA_List_Columns $wpda_list_columns Database column list. |
| 37 |
*/ |
| 38 |
public function __construct( |
| 39 |
$schema_name, |
| 40 |
$table_name, |
| 41 |
&$wpda_list_columns |
| 42 |
) { |
| 43 |
$args = [ |
| 44 |
'wpda_success_msg' => __( 'Succesfully saved changes to database (you might need to refresh your menu)', 'wp-data-access' ), |
| 45 |
'wpda_failure_msg' => __( 'Saving changes to database failed', 'wp-data-access' ), |
| 46 |
]; |
| 47 |
|
| 48 |
$this->check_table_type = false; |
| 49 |
$this->title = 'Back'; |
| 50 |
|
| 51 |
parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Overwrite method. |
| 56 |
* |
| 57 |
* @param bool $allow_save |
| 58 |
* @param string $add_param |
| 59 |
*/ |
| 60 |
public function show( $allow_save = true, $add_param = '' ) { |
| 61 |
parent::show( $allow_save, '&tab=menus' ); |
| 62 |
?> |
| 63 |
<script language="JavaScript"> |
| 64 |
jQuery(document).ready(function () { |
| 65 |
jQuery("#<?php echo esc_attr( $this->current_form_id ); ?> input.wpda_primary_key").prop("readonly", true); |
| 66 |
jQuery("#<?php echo esc_attr( $this->current_form_id ); ?> select.wpda_primary_key").prop("disabled", true); |
| 67 |
}); |
| 68 |
</script> |
| 69 |
<?php |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Set item attributes |
| 74 |
* |
| 75 |
* Overrides WPDA_Simple_Form::prepare_items(). |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
* |
| 79 |
* @param boolean $set_back_form_values Set back values entered by user. |
| 80 |
*/ |
| 81 |
protected function prepare_items( $set_back_form_values = false ) { |
| 82 |
|
| 83 |
// Allow keys to be changed for this form. |
| 84 |
$this->set_update_keys( true ); |
| 85 |
|
| 86 |
// Build a list with tables. |
| 87 |
$all_tables_init = WPDA_Dictionary_Lists::get_tables(); // Get all tables first. |
| 88 |
$all_tables = []; |
| 89 |
foreach ( $all_tables_init as $key => $value ) { |
| 90 |
$all_tables[ $value['table_name'] ] = true; |
| 91 |
} |
| 92 |
|
| 93 |
// Check table access. |
| 94 |
$table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS ); |
| 95 |
if ( 'hide' === $table_access ) { |
| 96 |
|
| 97 |
// No access to WordPress tables: filter WordPress table. |
| 98 |
global $wpdb; |
| 99 |
foreach ( $wpdb->tables( 'all', true ) as $wp_table ) { |
| 100 |
unset( $all_tables[ $wp_table ] ); |
| 101 |
} |
| 102 |
} elseif ( 'select' === $table_access ) { |
| 103 |
|
| 104 |
$option = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED ); |
| 105 |
if ( '' !== $option ) { |
| 106 |
// Allow only access to selected tables. |
| 107 |
unset( $all_tables ); |
| 108 |
foreach ( $option as $table_name ) { |
| 109 |
$all_tables[ $table_name ] = true; |
| 110 |
} |
| 111 |
} else { |
| 112 |
// No tables selected: no access. |
| 113 |
unset( $all_tables ); |
| 114 |
die( '<p>Adding menus is currently not available!<p/><p>Select at least one table in the Plugin/Back-end Settings to continue.</p>' ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
global $wp_roles; |
| 119 |
$all_roles = $wp_roles->get_names(); // Get all available roles. |
| 120 |
$wp_roles_array = ( (array) $wp_roles ); // Write roles to array. |
| 121 |
$all_capabilities = $wp_roles_array['roles']; // Get all available capabilities per role. |
| 122 |
|
| 123 |
// Array holding roles and their capabilities. |
| 124 |
// Used to build listbox of all available capabilities. |
| 125 |
$roles_capabilities_array = []; |
| 126 |
// Array holding all capalities (not role related). |
| 127 |
// Used to show which roles are granted access when a capability is selected (JS). |
| 128 |
$capabilities_array = []; |
| 129 |
|
| 130 |
foreach ( $all_roles as $role ) { |
| 131 |
foreach ( $all_capabilities[ strtolower( $role ) ] as $role_capabilities ) { |
| 132 |
$roles_capabilities_array[ $role ] = []; |
| 133 |
if ( is_array( $role_capabilities ) ) { |
| 134 |
foreach ( $role_capabilities as $capability => $value ) { |
| 135 |
$roles_capabilities_array[ $role ][] = $capability; |
| 136 |
$capabilities_array[ $capability ] = true; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
ksort( $capabilities_array ); // Sort array to show values in ascending order in listbox. |
| 142 |
|
| 143 |
// JS array used to check roles and capabilities. |
| 144 |
$js_roles_capabilities_array = json_encode( $roles_capabilities_array ); |
| 145 |
|
| 146 |
// Add dummy item to column list. |
| 147 |
$this->add_dummy_column( 'dummy_roles_granted' ); |
| 148 |
|
| 149 |
// Reorder columns (menu_slug should be the first column te be displayed). |
| 150 |
$this->order_and_filter_columns( |
| 151 |
[ |
| 152 |
'menu_id', |
| 153 |
'menu_slug', |
| 154 |
'menu_name', |
| 155 |
'menu_table_name', |
| 156 |
'menu_capability', |
| 157 |
'dummy_roles_granted', |
| 158 |
] |
| 159 |
); |
| 160 |
|
| 161 |
$form_item_labels = [ |
| 162 |
'menu_id' => [ |
| 163 |
'label' => __( 'Menu ID', 'wp-data-access' ), |
| 164 |
], |
| 165 |
'menu_slug' => [ |
| 166 |
'label' => __( 'Menu Slug', 'wp-data-access' ), |
| 167 |
], |
| 168 |
'menu_name' => [ |
| 169 |
'label' => __( 'Menu Name', 'wp-data-access' ), |
| 170 |
], |
| 171 |
'menu_table_name' => [ |
| 172 |
'label' => __( 'Table Name', 'wp-data-access' ), |
| 173 |
], |
| 174 |
'menu_capability' => [ |
| 175 |
'label' => __( 'Capability', 'wp-data-access' ), |
| 176 |
], |
| 177 |
'dummy_roles_granted' => [ |
| 178 |
'label' => __( 'Roles Authorized', 'wp-data-access' ), |
| 179 |
], |
| 180 |
]; |
| 181 |
|
| 182 |
$count_cols = count( $this->table_columns ); |
| 183 |
for ( $i = 0; $i < $count_cols; $i++ ) { |
| 184 |
// Process column. |
| 185 |
$column_name = $this->table_columns[ $i ]['column_name']; |
| 186 |
$item_event = ''; |
| 187 |
$item_js = ''; |
| 188 |
|
| 189 |
// Build a listbox for tables. |
| 190 |
if ( 'menu_table_name' === $column_name ) { |
| 191 |
// Check if table is still available. |
| 192 |
if ( isset( $this->row ) ) { |
| 193 |
if ( ! isset( $all_tables[ $this->row[0][ $column_name ] ] ) ) { |
| 194 |
// Table access is turned of in back-end settings. |
| 195 |
// We'll add the table_name to the list and show a message to inform the user. |
| 196 |
$all_tables[ $this->row[0][ $column_name ] ] = true; |
| 197 |
$msg = new WPDA_Message_Box( |
| 198 |
[ |
| 199 |
'message_text' => __( 'Access to table is disabled (menu page will not be accessible)', 'wp-data-access' ), |
| 200 |
'message_type' => 'error', |
| 201 |
'message_is_dismissible' => false, |
| 202 |
] |
| 203 |
); |
| 204 |
$msg->box(); |
| 205 |
$msg = new WPDA_Message_Box( |
| 206 |
[ |
| 207 |
'message_text' => __( 'Add table in back-end settings, delete menu item or select another table', 'wp-data-access' ), |
| 208 |
'message_type' => 'action', |
| 209 |
] |
| 210 |
); |
| 211 |
$msg->box(); |
| 212 |
} |
| 213 |
} |
| 214 |
$this->table_columns[ $i ]['data_type'] = 'enum'; // Set type to enum to show listbox. |
| 215 |
$this->table_columns[ $i ]['column_type'] = |
| 216 |
"enum('" . implode( "','", array_keys( $all_tables ) ) . "')"; |
| 217 |
} |
| 218 |
|
| 219 |
// Build a listbox for column capability. |
| 220 |
if ( 'menu_capability' === $column_name ) { |
| 221 |
$this->table_columns[ $i ]['data_type'] = 'enum'; // Set type to enum to show listbox. |
| 222 |
$this->table_columns[ $i ]['column_type'] = |
| 223 |
"enum('" . implode( "','", array_keys( $capabilities_array ) ) . "')"; |
| 224 |
// Build JS code in 3 steps (loop through array in step 2). |
| 225 |
$item_js = ' |
| 226 |
var capabilities_array = ' . $js_roles_capabilities_array . '; |
| 227 |
var roles_matched = []; |
| 228 |
function check() { |
| 229 |
capability = jQuery("select[name=\'menu_capability\']").val(); |
| 230 |
roles_matched = []; |
| 231 |
'; |
| 232 |
foreach ( $all_roles as $role ) { |
| 233 |
$item_js .= ' |
| 234 |
capabilities_array["' . $role . '"].forEach(function (role_capability) { |
| 235 |
if (role_capability == capability) { |
| 236 |
roles_matched.push("' . $role . '"); |
| 237 |
} |
| 238 |
}); |
| 239 |
'; |
| 240 |
} |
| 241 |
$item_js .= ' |
| 242 |
jQuery("input[name=\'dummy_roles_granted\']").val(roles_matched.join()); |
| 243 |
} |
| 244 |
jQuery(document).ready(function() { |
| 245 |
check(); |
| 246 |
jQuery("select[name=\'menu_capability\']").on("change", function (e) { |
| 247 |
check(); |
| 248 |
}); |
| 249 |
jQuery("input[name=\'reset_button\']").on("click", function (e) { |
| 250 |
check(); |
| 251 |
}); |
| 252 |
}); |
| 253 |
'; |
| 254 |
} |
| 255 |
|
| 256 |
if ( 'dummy_roles_granted' === $column_name ) { |
| 257 |
// Set values for dummy item: dummy_roles_granted. |
| 258 |
$item_data_type = 'dummy'; |
| 259 |
$item_value = ''; |
| 260 |
$item_extra = ''; |
| 261 |
$item_enum = ''; |
| 262 |
$item_column_type = ''; |
| 263 |
$item_hide_icon = true; |
| 264 |
$item_js = ' |
| 265 |
jQuery("input[name=\'dummy_roles_granted\']").prop("readonly", true); |
| 266 |
jQuery("input[name=\'dummy_roles_granted\']").prop("style", "border: none; background: transparent"); |
| 267 |
'; |
| 268 |
$item_class = 'wpda_user_menu_input'; |
| 269 |
} else { |
| 270 |
// Set values for database columns. |
| 271 |
$item_data_type = $this->table_columns[ $i ]['data_type']; |
| 272 |
if ( $set_back_form_values ) { |
| 273 |
// Set value back to what user entered. |
| 274 |
if ( $this->is_key_column( $column_name ) ) { |
| 275 |
// This situation appears after a data entry error. If the column is a key column, values must |
| 276 |
// be reversed. Otherwise the key value would point to a non existing record. In this special |
| 277 |
// case we'll get the value from the database to maintain consistency. |
| 278 |
$item_value = isset( $this->row ) ? $this->row[0][ $column_name ] : ''; |
| 279 |
} else { |
| 280 |
// Get post data. |
| 281 |
$item_value = $this->get_new_value( $column_name ); |
| 282 |
} |
| 283 |
} else { |
| 284 |
// Get value from database. |
| 285 |
$item_value = isset( $this->row ) ? $this->row[0][ $column_name ] : ''; |
| 286 |
} |
| 287 |
if ( 'menu_capability' === $column_name && '' === $item_value ) { |
| 288 |
$item_value = 'manage_options'; // Default value. |
| 289 |
} |
| 290 |
$item_extra = $this->table_columns[ $i ]['extra']; |
| 291 |
$item_enum = ''; |
| 292 |
if ( 'enum' === $item_data_type ) { |
| 293 |
$item_enum = $this->table_columns[ $i ]['column_type']; |
| 294 |
} |
| 295 |
$item_column_type = $this->table_columns[ $i ]['column_type']; |
| 296 |
$item_hide_icon = false; |
| 297 |
if ('menu_table_name' === $column_name || |
| 298 |
'menu_capability' === $column_name |
| 299 |
) { |
| 300 |
$item_class = ''; |
| 301 |
} else { |
| 302 |
$item_class = 'wpda_user_menu_input'; |
| 303 |
} |
| 304 |
} |
| 305 |
$item_label = $form_item_labels[ $column_name ]['label']; |
| 306 |
|
| 307 |
// Set all item specific properties. |
| 308 |
$item = new WPDA_Simple_Form_Item( |
| 309 |
[ |
| 310 |
'item_name' => $column_name, |
| 311 |
'data_type' => $item_data_type, |
| 312 |
'item_label' => $item_label, |
| 313 |
'item_value' => $item_value, |
| 314 |
'item_extra' => $item_extra, |
| 315 |
'item_enum' => $item_enum, |
| 316 |
'column_type' => $item_column_type, |
| 317 |
'item_event' => $item_event, |
| 318 |
'item_js' => $item_js, |
| 319 |
'item_hide_icon' => $item_hide_icon, |
| 320 |
'item_class' => $item_class, |
| 321 |
] |
| 322 |
); |
| 323 |
|
| 324 |
$this->add_form_item( $i, $item ); |
| 325 |
} |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Validate form data |
| 331 |
* |
| 332 |
* @since 1.0.0 |
| 333 |
* |
| 334 |
* @return bool TRUE = data is valid |
| 335 |
*/ |
| 336 |
protected function validate() { |
| 337 |
|
| 338 |
if ( $this->get_new_value( 'menu_name' ) === '' ) { |
| 339 |
$this->set_message( '1', __( 'Menu name must be entered', 'wp-data-access' ) ); |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
if ( $this->get_new_value( 'menu_table_name' ) === '' ) { |
| 344 |
$this->set_message( '1', __( 'Table name must be entered', 'wp-data-access' ) ); |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
if ( $this->get_new_value( 'menu_capability' ) === '' ) { |
| 349 |
$this->set_message( '1', __( 'Capability must be entered', 'wp-data-access' ) ); |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
return parent::validate(); |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
|