| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Settings; |
| 4 |
|
| 5 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist; |
| 6 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; |
| 7 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 8 |
use WPDataAccess\WPDA; |
| 9 |
class WPDA_Settings_FrontEnd extends WPDA_Settings { |
| 10 |
/** |
| 11 |
* Available UI themes |
| 12 |
*/ |
| 13 |
const UI_THEMES = array( |
| 14 |
'ui-darkness', |
| 15 |
'ui-lightness', |
| 16 |
'swanky-purse', |
| 17 |
'sunny', |
| 18 |
'start', |
| 19 |
'smoothness', |
| 20 |
'black-tie', |
| 21 |
'blitzer', |
| 22 |
'cupertino', |
| 23 |
'dark-hive', |
| 24 |
'dot-luv', |
| 25 |
'eggplant', |
| 26 |
'excite-bike', |
| 27 |
'flick', |
| 28 |
'hot-sneaks', |
| 29 |
'humanity', |
| 30 |
'le-frog', |
| 31 |
'mint-choc', |
| 32 |
'overcast', |
| 33 |
'pepper-grinder', |
| 34 |
'redmond', |
| 35 |
'south-street', |
| 36 |
'trontastic', |
| 37 |
'vader' |
| 38 |
); |
| 39 |
|
| 40 |
/** |
| 41 |
* Add front-end tab content |
| 42 |
* |
| 43 |
* See class documentation for flow explanation. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
protected function add_content() { |
| 48 |
global $wpdb; |
| 49 |
if ( isset( $_REQUEST['database'] ) ) { |
| 50 |
$database = sanitize_text_field( wp_unslash( $_REQUEST['database'] ) ); |
| 51 |
// input var okay. |
| 52 |
} else { |
| 53 |
$database = $wpdb->dbname; |
| 54 |
} |
| 55 |
$is_wp_database = $database === $wpdb->dbname; |
| 56 |
if ( isset( $_REQUEST['action'] ) ) { |
| 57 |
$action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); |
| 58 |
// input var okay. |
| 59 |
// Security check. |
| 60 |
$wp_nonce = ( isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '' ); |
| 61 |
// input var okay. |
| 62 |
if ( !wp_verify_nonce( $wp_nonce, 'wpda-front-end-settings-' . WPDA::get_current_user_login() ) ) { |
| 63 |
wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); |
| 64 |
} |
| 65 |
if ( 'save' === $action ) { |
| 66 |
if ( $is_wp_database ) { |
| 67 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS, ( isset( $_REQUEST['table_access'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['table_access'] ) ) : null ) ); |
| 68 |
} else { |
| 69 |
update_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database, ( isset( $_REQUEST['table_access'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['table_access'] ) ) : null ) ); |
| 70 |
} |
| 71 |
$table_access_selected_new_value = ( isset( $_REQUEST['table_access_selected'] ) ? WPDA::sanitize_text_field_array( $_REQUEST['table_access_selected'] ) : null ); |
| 72 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 73 |
if ( is_array( $table_access_selected_new_value ) ) { |
| 74 |
// Check the requested table names for sql injection. This is simply done by checking if the table |
| 75 |
// name exists in our WordPress database. |
| 76 |
$table_access_selected_new_value_checked = array(); |
| 77 |
foreach ( $table_access_selected_new_value as $key => $value ) { |
| 78 |
$wpda_dictionary_checks = new WPDA_Dictionary_Exist($database, $value); |
| 79 |
if ( $wpda_dictionary_checks->table_exists( false, false ) ) { |
| 80 |
// Add existing table to list. |
| 81 |
$table_access_selected_new_value_checked[$key] = $value; |
| 82 |
} else { |
| 83 |
// An invalid table name was provided. Might be an sql injection attack or an invalid state. |
| 84 |
wp_die( __( 'ERROR: Table not found', 'wp-data-access' ) ); |
| 85 |
} |
| 86 |
} |
| 87 |
} else { |
| 88 |
$table_access_selected_new_value_checked = ''; |
| 89 |
} |
| 90 |
if ( $is_wp_database ) { |
| 91 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED, $table_access_selected_new_value_checked ); |
| 92 |
} else { |
| 93 |
update_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database, $table_access_selected_new_value_checked ); |
| 94 |
} |
| 95 |
WPDA::set_option( WPDA::OPTION_FE_PAGINATION, ( isset( $_REQUEST['pagination'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['pagination'] ) ) : null ) ); |
| 96 |
if ( isset( $_REQUEST['ui_theme'] ) ) { |
| 97 |
WPDA::set_option( WPDA::WPDA_DT_UI_THEME_DEFAULT, sanitize_text_field( wp_unslash( $_REQUEST['ui_theme'] ) ) ); |
| 98 |
} |
| 99 |
WPDA::set_option( WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR, ( isset( $_REQUEST['add_projects_to_toolbar'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['add_projects_to_toolbar'] ) ) : 'off' ) ); |
| 100 |
} elseif ( 'setdefaults' === $action ) { |
| 101 |
// Set all front-end settings back to default |
| 102 |
if ( $is_wp_database ) { |
| 103 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS ); |
| 104 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED ); |
| 105 |
} else { |
| 106 |
update_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database, 'select' ); |
| 107 |
update_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database, '' ); |
| 108 |
} |
| 109 |
WPDA::set_option( WPDA::OPTION_FE_PAGINATION ); |
| 110 |
WPDA::set_option( WPDA::WPDA_DT_UI_THEME_DEFAULT ); |
| 111 |
WPDA::set_option( WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR ); |
| 112 |
} |
| 113 |
$msg = new WPDA_Message_Box(array( |
| 114 |
'message_text' => __( 'Settings saved', 'wp-data-access' ), |
| 115 |
)); |
| 116 |
$msg->box(); |
| 117 |
} |
| 118 |
// Get options |
| 119 |
if ( $is_wp_database ) { |
| 120 |
$table_access = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS ); |
| 121 |
$table_access_selected = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED ); |
| 122 |
} else { |
| 123 |
$table_access = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database ); |
| 124 |
if ( false === $table_access ) { |
| 125 |
$table_access = 'select'; |
| 126 |
} |
| 127 |
$table_access_selected = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database ); |
| 128 |
if ( false === $table_access_selected ) { |
| 129 |
$table_access_selected = ''; |
| 130 |
} |
| 131 |
} |
| 132 |
if ( is_array( $table_access_selected ) ) { |
| 133 |
// Convert table for simple access. |
| 134 |
$table_access_selected_by_name = array(); |
| 135 |
foreach ( $table_access_selected as $key => $value ) { |
| 136 |
$table_access_selected_by_name[$value] = true; |
| 137 |
} |
| 138 |
} |
| 139 |
$pagination = WPDA::get_option( WPDA::OPTION_FE_PAGINATION ); |
| 140 |
$ui_theme_default = WPDA::get_option( WPDA::WPDA_DT_UI_THEME_DEFAULT ); |
| 141 |
$add_projects_to_toolbar = WPDA::get_option( WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR ); |
| 142 |
?> |
| 143 |
<form id="wpda_settings_frontend" method="post" |
| 144 |
action="?page=<?php |
| 145 |
echo esc_attr( $this->page ); |
| 146 |
?>&tab=frontend"> |
| 147 |
<table class="wpda-table-settings"> |
| 148 |
<?php |
| 149 |
?> |
| 150 |
<tr> |
| 151 |
<th><?php |
| 152 |
echo __( 'Default pagination value', 'wp-data-access' ); |
| 153 |
?></th> |
| 154 |
<td> |
| 155 |
<input |
| 156 |
type="number" step="1" min="1" max="999" name="pagination" maxlength="3" |
| 157 |
value="<?php |
| 158 |
echo esc_attr( $pagination ); |
| 159 |
?>"> |
| 160 |
<div style="padding-top:10px"> |
| 161 |
Only for shortcode <strong>wpdadiehard</strong> |
| 162 |
</div> |
| 163 |
</td> |
| 164 |
</tr> |
| 165 |
<tr> |
| 166 |
<th><?php |
| 167 |
echo __( 'Table access', 'wp-data-access' ); |
| 168 |
?></th> |
| 169 |
<td> |
| 170 |
<select name="database" id="schema_name"> |
| 171 |
<?php |
| 172 |
$schema_names = WPDA_Dictionary_Lists::get_db_schemas(); |
| 173 |
foreach ( $schema_names as $schema_name ) { |
| 174 |
$selected = ( $database === $schema_name['schema_name'] ? ' selected' : '' ); |
| 175 |
echo "<option value='{$schema_name['schema_name']}'{$selected}>{$schema_name['schema_name']}</option>"; |
| 176 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 177 |
} |
| 178 |
?> |
| 179 |
</select> |
| 180 |
<br/><br/> |
| 181 |
<label> |
| 182 |
<input |
| 183 |
type="radio" |
| 184 |
name="table_access" |
| 185 |
value="show" |
| 186 |
<?php |
| 187 |
echo ( 'show' === $table_access ? 'checked' : '' ); |
| 188 |
?> |
| 189 |
><?php |
| 190 |
echo ( $is_wp_database ? __( 'Show WordPress tables', 'wp-data-access' ) : __( 'Show all tables', 'wp-data-access' ) ); |
| 191 |
?> |
| 192 |
</label> |
| 193 |
<br/> |
| 194 |
<?php |
| 195 |
if ( $is_wp_database ) { |
| 196 |
?> |
| 197 |
<label> |
| 198 |
<input |
| 199 |
type="radio" |
| 200 |
name="table_access" |
| 201 |
value="hide" |
| 202 |
<?php |
| 203 |
echo ( 'hide' === $table_access ? 'checked' : '' ); |
| 204 |
?> |
| 205 |
><?php |
| 206 |
echo __( 'Hide WordPress tables', 'wp-data-access' ); |
| 207 |
?> |
| 208 |
</label> |
| 209 |
<br/> |
| 210 |
<?php |
| 211 |
} |
| 212 |
?> |
| 213 |
<label> |
| 214 |
<input |
| 215 |
type="radio" |
| 216 |
name="table_access" |
| 217 |
value="select" |
| 218 |
<?php |
| 219 |
echo ( 'select' === $table_access ? 'checked' : '' ); |
| 220 |
?> |
| 221 |
><?php |
| 222 |
echo __( 'Show only selected tables', 'wp-data-access' ); |
| 223 |
?> |
| 224 |
</label> |
| 225 |
<div id="tables_selected" <?php |
| 226 |
echo ( 'select' === $table_access ? '' : 'style="display:none"' ); |
| 227 |
?>> |
| 228 |
<br/> |
| 229 |
<select name="table_access_selected[]" multiple size="10"> |
| 230 |
<?php |
| 231 |
$tables = WPDA_Dictionary_Lists::get_tables( true, $database ); |
| 232 |
foreach ( $tables as $table ) { |
| 233 |
$table_name = $table['table_name']; |
| 234 |
?> |
| 235 |
<option value="<?php |
| 236 |
echo esc_attr( $table_name ); |
| 237 |
?>" <?php |
| 238 |
echo ( isset( $table_access_selected_by_name[$table_name] ) ? 'selected' : '' ); |
| 239 |
?>><?php |
| 240 |
echo esc_attr( $table_name ); |
| 241 |
?></option> |
| 242 |
<?php |
| 243 |
} |
| 244 |
?> |
| 245 |
</select> |
| 246 |
</div> |
| 247 |
<script type='text/javascript'> |
| 248 |
jQuery(function () { |
| 249 |
jQuery("input[name='table_access']").on("click", function () { |
| 250 |
if (this.value == 'select') { |
| 251 |
jQuery("#tables_selected").show(); |
| 252 |
} else { |
| 253 |
jQuery("#tables_selected").hide(); |
| 254 |
} |
| 255 |
}); |
| 256 |
jQuery('#schema_name').on('change', function() { |
| 257 |
window.location = '?page=<?php |
| 258 |
echo esc_attr( $this->page ); |
| 259 |
?>&tab=frontend&database=' + jQuery(this).val(); |
| 260 |
}); |
| 261 |
}); |
| 262 |
</script> |
| 263 |
</td> |
| 264 |
</tr> |
| 265 |
<tr> |
| 266 |
<th><?php |
| 267 |
echo __( 'Admin toolbar', 'wp-data-access' ); |
| 268 |
?></th> |
| 269 |
<td> |
| 270 |
<label> |
| 271 |
<input type="checkbox" name="add_projects_to_toolbar" |
| 272 |
<?php |
| 273 |
echo ( 'on' === $add_projects_to_toolbar ? 'checked' : '' ); |
| 274 |
?> |
| 275 |
/> |
| 276 |
<?php |
| 277 |
echo __( 'Add projects to toolbar', 'wp-data-access' ); |
| 278 |
?> |
| 279 |
</label> |
| 280 |
</td> |
| 281 |
</tr> |
| 282 |
</table> |
| 283 |
<div class="wpda-table-settings-button"> |
| 284 |
<input type="hidden" name="action" value="save"/> |
| 285 |
<button type="submit" class="button button-primary"> |
| 286 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 287 |
<?php |
| 288 |
echo __( 'Save Front-end Settings', 'wp-data-access' ); |
| 289 |
?> |
| 290 |
</button> |
| 291 |
<a href="javascript:void(0)" |
| 292 |
onclick="if (confirm('<?php |
| 293 |
echo __( 'Reset to defaults?', 'wp-data-access' ); |
| 294 |
?>')) { |
| 295 |
jQuery('input[name="action"]').val('setdefaults'); |
| 296 |
jQuery('#wpda_settings_frontend').trigger('submit') |
| 297 |
}" |
| 298 |
class="button"> |
| 299 |
<i class="fas fa-times-circle wpda_icon_on_button"></i> |
| 300 |
<?php |
| 301 |
echo __( 'Reset Front-end Settings To Defaults', 'wp-data-access' ); |
| 302 |
?> |
| 303 |
</a> |
| 304 |
</div> |
| 305 |
<?php |
| 306 |
wp_nonce_field( 'wpda-front-end-settings-' . WPDA::get_current_user_login(), '_wpnonce', false ); |
| 307 |
?> |
| 308 |
</form> |
| 309 |
|
| 310 |
<?php |
| 311 |
} |
| 312 |
|
| 313 |
} |
| 314 |
|