| 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 |
|
| 10 |
class WPDA_Settings_FrontEnd extends WPDA_Settings { |
| 11 |
|
| 12 |
/** |
| 13 |
* Add front-end tab content |
| 14 |
* |
| 15 |
* See class documentation for flow explanation. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
protected function add_content() { |
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
if ( isset( $_REQUEST['database'] ) ) { |
| 23 |
$database = sanitize_text_field( wp_unslash( $_REQUEST['database'] ) ); // input var okay. |
| 24 |
} else { |
| 25 |
$database = $wpdb->dbname; |
| 26 |
} |
| 27 |
$is_wp_database = $database === $wpdb->dbname; |
| 28 |
|
| 29 |
if ( isset( $_REQUEST['action'] ) ) { |
| 30 |
$action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // input var okay. |
| 31 |
|
| 32 |
// Security check. |
| 33 |
$wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay. |
| 34 |
if ( ! wp_verify_nonce( $wp_nonce, 'wpda-front-end-settings-' . WPDA::get_current_user_login() ) ) { |
| 35 |
wp_die( esc_attr__( 'ERROR: Not authorized', 'wp-data-access' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
if ( 'save' === $action ) { |
| 39 |
if ( $is_wp_database ) { |
| 40 |
WPDA::set_option( |
| 41 |
WPDA::OPTION_FE_TABLE_ACCESS, |
| 42 |
isset( $_REQUEST['table_access'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['table_access'] ) ) : null // input var okay. |
| 43 |
); |
| 44 |
} else { |
| 45 |
update_option( |
| 46 |
WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database, |
| 47 |
isset( $_REQUEST['table_access'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['table_access'] ) ) : null // input var okay. |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
$table_access_selected_new_value = isset( $_REQUEST['table_access_selected'] ) ? |
| 52 |
WPDA::sanitize_text_field_array( $_REQUEST['table_access_selected'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 53 |
if ( is_array( $table_access_selected_new_value ) ) { |
| 54 |
// Check the requested table names for sql injection. This is simply done by checking if the table |
| 55 |
// name exists in our WordPress database. |
| 56 |
$table_access_selected_new_value_checked = array(); |
| 57 |
foreach ( $table_access_selected_new_value as $key => $value ) { |
| 58 |
$wpda_dictionary_checks = new WPDA_Dictionary_Exist( $database, $value ); |
| 59 |
if ( $wpda_dictionary_checks->table_exists( false, false ) ) { |
| 60 |
// Add existing table to list. |
| 61 |
$table_access_selected_new_value_checked[ $key ] = $value; |
| 62 |
} else { |
| 63 |
// An invalid table name was provided. Might be an sql injection attack or an invalid state. |
| 64 |
wp_die( esc_attr__( 'ERROR: Table not found', 'wp-data-access' ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
} else { |
| 68 |
$table_access_selected_new_value_checked = ''; |
| 69 |
} |
| 70 |
|
| 71 |
if ( $is_wp_database ) { |
| 72 |
WPDA::set_option( |
| 73 |
WPDA::OPTION_FE_TABLE_ACCESS_SELECTED, |
| 74 |
$table_access_selected_new_value_checked |
| 75 |
); |
| 76 |
} else { |
| 77 |
update_option( |
| 78 |
WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database, |
| 79 |
$table_access_selected_new_value_checked |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
WPDA::set_option( |
| 84 |
WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR, |
| 85 |
isset( $_REQUEST['add_projects_to_toolbar'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['add_projects_to_toolbar'] ) ) : 'off' // input var okay. |
| 86 |
); |
| 87 |
} elseif ( 'setdefaults' === $action ) { |
| 88 |
// Set all front-end settings back to default |
| 89 |
if ( $is_wp_database ) { |
| 90 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS ); |
| 91 |
WPDA::set_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED ); |
| 92 |
} else { |
| 93 |
update_option( |
| 94 |
WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database, |
| 95 |
'select' |
| 96 |
); |
| 97 |
update_option( |
| 98 |
WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database, |
| 99 |
'' |
| 100 |
); |
| 101 |
} |
| 102 |
WPDA::set_option( WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR ); |
| 103 |
} |
| 104 |
|
| 105 |
$msg = new WPDA_Message_Box( |
| 106 |
array( |
| 107 |
'message_text' => __( 'Settings saved', 'wp-data-access' ), |
| 108 |
) |
| 109 |
); |
| 110 |
$msg->box(); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
// Get options |
| 115 |
if ( $is_wp_database ) { |
| 116 |
$table_access = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS ); |
| 117 |
$table_access_selected = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED ); |
| 118 |
} else { |
| 119 |
$table_access = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $database ); |
| 120 |
if ( false === $table_access ) { |
| 121 |
$table_access = 'select'; |
| 122 |
} |
| 123 |
$table_access_selected = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $database ); |
| 124 |
if ( false === $table_access_selected ) { |
| 125 |
$table_access_selected = ''; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( is_array( $table_access_selected ) ) { |
| 130 |
// Convert table for simple access. |
| 131 |
$table_access_selected_by_name = array(); |
| 132 |
foreach ( $table_access_selected as $key => $value ) { |
| 133 |
$table_access_selected_by_name[ $value ] = true; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$add_projects_to_toolbar = WPDA::get_option( WPDA::OPTION_FE_ADD_PROJECTS_TO_TOOLBAR ); |
| 138 |
?> |
| 139 |
<form id="wpda_settings_frontend" method="post" |
| 140 |
action="?page=<?php echo esc_attr( $this->page ); ?>&tab=frontend"> |
| 141 |
<table class="wpda-table-settings"> |
| 142 |
<tr> |
| 143 |
<th><?php esc_html_e( 'Table access', 'wp-data-access' ); ?></th> |
| 144 |
<td> |
| 145 |
<select name="database" id="schema_name"> |
| 146 |
<?php |
| 147 |
$schema_names = WPDA_Dictionary_Lists::get_db_schemas(); |
| 148 |
foreach ( $schema_names as $schema_name ) { |
| 149 |
$selected = $database === $schema_name['schema_name'] ? ' selected' : ''; |
| 150 |
echo "<option value='{$schema_name['schema_name']}'$selected>{$schema_name['schema_name']}</option>"; // phpcs:ignore WordPress.Security.EscapeOutput |
| 151 |
} |
| 152 |
?> |
| 153 |
</select> |
| 154 |
<br/><br/> |
| 155 |
<label> |
| 156 |
<input |
| 157 |
type="radio" |
| 158 |
name="table_access" |
| 159 |
value="show" |
| 160 |
<?php echo 'show' === $table_access ? 'checked' : ''; ?> |
| 161 |
><?php echo $is_wp_database ? esc_attr__( 'Show WordPress tables', 'wp-data-access' ) : esc_attr__( 'Show all tables', 'wp-data-access' ); ?> |
| 162 |
</label> |
| 163 |
<br/> |
| 164 |
<?php |
| 165 |
if ( $is_wp_database ) { |
| 166 |
?> |
| 167 |
<label> |
| 168 |
<input |
| 169 |
type="radio" |
| 170 |
name="table_access" |
| 171 |
value="hide" |
| 172 |
<?php echo 'hide' === $table_access ? 'checked' : ''; ?> |
| 173 |
><?php esc_html_e( 'Hide WordPress tables', 'wp-data-access' ); ?> |
| 174 |
</label> |
| 175 |
<br/> |
| 176 |
<?php |
| 177 |
} |
| 178 |
?> |
| 179 |
<label> |
| 180 |
<input |
| 181 |
type="radio" |
| 182 |
name="table_access" |
| 183 |
value="select" |
| 184 |
<?php echo 'select' === $table_access ? 'checked' : ''; ?> |
| 185 |
><?php esc_html_e( 'Show only selected tables', 'wp-data-access' ); ?> |
| 186 |
</label> |
| 187 |
<div id="tables_selected" <?php echo 'select' === $table_access ? '' : 'style="display:none"'; ?>> |
| 188 |
<br/> |
| 189 |
<select name="table_access_selected[]" multiple size="10"> |
| 190 |
<?php |
| 191 |
$tables = WPDA_Dictionary_Lists::get_tables( true, $database ); |
| 192 |
foreach ( $tables as $table ) { |
| 193 |
$table_name = $table['table_name']; |
| 194 |
?> |
| 195 |
<option value="<?php echo esc_attr( $table_name ); ?>" <?php echo isset( $table_access_selected_by_name[ $table_name ] ) ? 'selected' : ''; ?>><?php echo esc_attr( $table_name ); ?></option> |
| 196 |
<?php |
| 197 |
} |
| 198 |
?> |
| 199 |
</select> |
| 200 |
</div> |
| 201 |
<script type='text/javascript'> |
| 202 |
jQuery(function () { |
| 203 |
jQuery("input[name='table_access']").on("click", function () { |
| 204 |
if (this.value == 'select') { |
| 205 |
jQuery("#tables_selected").show(); |
| 206 |
} else { |
| 207 |
jQuery("#tables_selected").hide(); |
| 208 |
} |
| 209 |
}); |
| 210 |
jQuery('#schema_name').on('change', function() { |
| 211 |
window.location = '?page=<?php echo esc_attr( $this->page ); ?>&tab=frontend&database=' + jQuery(this).val(); |
| 212 |
}); |
| 213 |
}); |
| 214 |
</script> |
| 215 |
</td> |
| 216 |
</tr> |
| 217 |
<tr> |
| 218 |
<th><?php esc_html_e( 'Admin toolbar', 'wp-data-access' ); ?></th> |
| 219 |
<td> |
| 220 |
<label> |
| 221 |
<input type="checkbox" name="add_projects_to_toolbar" |
| 222 |
<?php echo 'on' === $add_projects_to_toolbar ? 'checked' : ''; ?> |
| 223 |
/> |
| 224 |
<?php esc_html_e( 'Add projects to toolbar', 'wp-data-access' ); ?> |
| 225 |
</label> |
| 226 |
</td> |
| 227 |
</tr> |
| 228 |
</table> |
| 229 |
|
| 230 |
<div class="wpda-table-settings-button"> |
| 231 |
<input type="hidden" name="action" value="save"/> |
| 232 |
<button type="submit" class="button button-primary"> |
| 233 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 234 |
<?php esc_html_e( 'Save Front-end Settings', 'wp-data-access' ); ?> |
| 235 |
</button> |
| 236 |
<a href="javascript:void(0)" |
| 237 |
onclick="if (confirm('<?php esc_html_e( 'Reset to defaults?', 'wp-data-access' ); ?>')) { |
| 238 |
jQuery('input[name="action"]').val('setdefaults'); |
| 239 |
jQuery('#wpda_settings_frontend').trigger('submit') |
| 240 |
}" |
| 241 |
class="button"> |
| 242 |
<i class="fas fa-times-circle wpda_icon_on_button"></i> |
| 243 |
<?php esc_html_e( 'Reset Front-end Settings To Defaults', 'wp-data-access' ); ?> |
| 244 |
</a> |
| 245 |
</div> |
| 246 |
<?php wp_nonce_field( 'wpda-front-end-settings-' . WPDA::get_current_user_login(), '_wpnonce', false ); ?> |
| 247 |
</form> |
| 248 |
|
| 249 |
<?php |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|