Skip to content

Commit

Permalink
feat: Format code for WordPress standards (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalnagar authored Jul 21, 2024
1 parent 372510b commit 3efe421
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 468 deletions.
524 changes: 261 additions & 263 deletions admin/AdminClass.php

Large diffs are not rendered by default.

258 changes: 129 additions & 129 deletions admin/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,181 +50,181 @@ public function admin_notice( $type, $message ) {

public function initialize_table_options() {
global $wpdb;
if(current_user_can('administrator')) {
$count = count( $this->options_defaults );
$sql = 'INSERT INTO ' . $wpdb->prefix . $this->table_options . ' (name, value) VALUES ';
foreach ( $this->options_defaults as $key => $option ) {
if ( $key !== ( $count - 1 ) ) {
$sql .= "('" . $option->name . "', '" . $option->value . "'),";
} else {
$sql .= "('" . $option->name . "', '" . $option->value . "')";
}
}
$wpdb->query( $sql );
}
if ( current_user_can( 'administrator' ) ) {
$count = count( $this->options_defaults );
$sql = 'INSERT INTO ' . $wpdb->prefix . $this->table_options . ' (name, value) VALUES ';
foreach ( $this->options_defaults as $key => $option ) {
if ( $key !== ( $count - 1 ) ) {
$sql .= "('" . $option->name . "', '" . $option->value . "'),";
} else {
$sql .= "('" . $option->name . "', '" . $option->value . "')";
}
}
$wpdb->query( $sql );
}
}

public function is_option( $option_name ) {
global $wpdb;
if(current_user_can('administrator')) {
$query = 'SELECT * FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
$result = $wpdb->get_results( $query );
if ( empty( $result ) ) {
return false;
} else {
return $result[0];
}
}
if ( current_user_can( 'administrator' ) ) {
$query = 'SELECT * FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
$result = $wpdb->get_results( $query );
if ( empty( $result ) ) {
return false;
} else {
return $result[0];
}
}
}

public function get_option( $option_name ) {
global $wpdb;
if(current_user_can('administrator')) {
$query = 'SELECT value FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
$result = $wpdb->get_var( $query );
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$query = 'SELECT value FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
$result = $wpdb->get_var( $query );
return $result;
}
}

public function insert_option( $option_name, $option_value ) {
global $wpdb;
if(current_user_can('administrator')) {
$result = $wpdb->insert(
$wpdb->prefix . $this->table_options,
array(
'name' => $option_name,
'value' => $option_value,
)
);
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$result = $wpdb->insert(
$wpdb->prefix . $this->table_options,
array(
'name' => $option_name,
'value' => $option_value,
)
);
return $result;
}
}

public function update_option( $option_name, $option_value ) {
global $wpdb;
if(current_user_can('administrator')) {
$result = $wpdb->update(
$wpdb->prefix . $this->table_options,
array( 'value' => $option_value ),
array( 'name' => $option_name )
);
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$result = $wpdb->update(
$wpdb->prefix . $this->table_options,
array( 'value' => $option_value ),
array( 'name' => $option_name )
);
return $result;
}
}

public function upsert_option( $option_name, $option_value ) {
global $wpdb;
if(current_user_can('administrator')) {
if ( self::is_option( $option_name ) ) {
$result = self::update_option( $option_name, $option_value );
} else {
$result = self::insert_option( $option_name, $option_value );
}
return $result;
}
if ( current_user_can( 'administrator' ) ) {
if ( self::is_option( $option_name ) ) {
$result = self::update_option( $option_name, $option_value );
} else {
$result = self::insert_option( $option_name, $option_value );
}
return $result;
}
}

public function get_logs_columns() {
global $wpdb;
if(current_user_can('administrator')) {
$query = 'SHOW COLUMNS FROM ' . $wpdb->prefix . $this->table_logs;
$result = $wpdb->get_results( $query );
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$query = 'SHOW COLUMNS FROM ' . $wpdb->prefix . $this->table_logs;
$result = $wpdb->get_results( $query );
return $result;
}
}

public function get_old_logs_count() {
global $wpdb;
if(current_user_can('administrator')) {
$query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . "posts WHERE post_type='c4p_log'";
$result = $wpdb->get_var( $query );
return (int) $result;
}
if ( current_user_can( 'administrator' ) ) {
$query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . "posts WHERE post_type='c4p_log'";
$result = $wpdb->get_var( $query );
return (int) $result;
}
}

public function delete_old_logs( $logIDs ) {
global $wpdb;
if(current_user_can('administrator')) {
foreach ( $logIDs as $id ) {
wp_delete_post( $id, true );
}
}
if ( current_user_can( 'administrator' ) ) {
foreach ( $logIDs as $id ) {
wp_delete_post( $id, true );
}
}
}

public function create_logs( $logsData, $isDeletingOld ) {
global $wpdb;
if(current_user_can('administrator')) {
$count = count( $logsData );
$logIDs = [];
$query = 'INSERT INTO ' . $wpdb->prefix . $this->table_logs . ' (ip, path, referer, user_agent) VALUES';
foreach ( $logsData as $key => $log ) {
if ( ! empty( $log->id ) ) {
array_push( $logIDs, $log->id );
}
$query .= " ('$log->ip', '$log->path', '$log->referer', '$log->user_agent')";
if ( $key < $count - 1 ) {
$query .= ',';
}
}
$result = $wpdb->query( $query );
if ( ! is_wp_error( $result ) ) {
if ( ! empty( $isDeletingOld ) && $isDeletingOld ) {
self::delete_old_logs( $logIDs );
}
}
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$count = count( $logsData );
$logIDs = array();
$query = 'INSERT INTO ' . $wpdb->prefix . $this->table_logs . ' (ip, path, referer, user_agent) VALUES';
foreach ( $logsData as $key => $log ) {
if ( ! empty( $log->id ) ) {
array_push( $logIDs, $log->id );
}
$query .= " ('$log->ip', '$log->path', '$log->referer', '$log->user_agent')";
if ( $key < $count - 1 ) {
$query .= ',';
}
}
$result = $wpdb->query( $query );
if ( ! is_wp_error( $result ) ) {
if ( ! empty( $isDeletingOld ) && $isDeletingOld ) {
self::delete_old_logs( $logIDs );
}
}
return $result;
}
}

public function get_logs() {
global $wpdb;
if(current_user_can('administrator')) {
$query = 'SELECT * from ' . $wpdb->prefix . $this->table_logs;
$result = $wpdb->get_results( $query, ARRAY_A );
return $result;
}
if ( current_user_can( 'administrator' ) ) {
$query = 'SELECT * from ' . $wpdb->prefix . $this->table_logs;
$result = $wpdb->get_results( $query, ARRAY_A );
return $result;
}
}

public function delete_logs( $path ) {
global $wpdb;
if(current_user_can('administrator')) {
if ( $path === 'all' ) {
$query = 'TRUNCATE TABLE ' . $wpdb->prefix . $this->table_logs;
} elseif ( is_array( $path ) ) {
$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id in (' . implode( ',', $path ) . ')';
} else {
$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id=' . $path . '';
}
$result = $wpdb->query( $query );
return $result;
}
if ( current_user_can( 'administrator' ) ) {
if ( $path === 'all' ) {
$query = 'TRUNCATE TABLE ' . $wpdb->prefix . $this->table_logs;
} elseif ( is_array( $path ) ) {
$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id in (' . implode( ',', $path ) . ')';
} else {
$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id=' . $path . '';
}
$result = $wpdb->query( $query );
return $result;
}
}

public function export_logs_csv() {
if(current_user_can('administrator')) {
$filename = 'logs_' . time() . '.csv';
$csv_output = '';
$columns = self::get_logs_columns();
if ( count( $columns ) > 0 ) {
foreach ( $columns as $column ) {
$csv_output .= $column->Field . ', ';
}
}
$csv_output .= "\n";
$results = self::get_logs();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
foreach ( $result as $q ) {
$csv_output .= "\"$q\"" . ', ';
}
$csv_output .= "\n";
}
}
header( 'Content-Type: application/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Pragma: no-cache' );
print $csv_output;
exit;
}
if ( current_user_can( 'administrator' ) ) {
$filename = 'logs_' . time() . '.csv';
$csv_output = '';
$columns = self::get_logs_columns();
if ( count( $columns ) > 0 ) {
foreach ( $columns as $column ) {
$csv_output .= $column->Field . ', ';
}
}
$csv_output .= "\n";
$results = self::get_logs();
if ( count( $results ) > 0 ) {
foreach ( $results as $result ) {
foreach ( $result as $q ) {
$csv_output .= "\"$q\"" . ', ';
}
$csv_output .= "\n";
}
}
header( 'Content-Type: application/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Pragma: no-cache' );
print $csv_output;
exit;
}
}
}
20 changes: 10 additions & 10 deletions admin/LogsClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function prepare_items() {
$sql = 'SELECT * FROM ' . $wpdb->prefix . $helpers->table_logs;

if ( array_key_exists( 'orderby', $_GET ) ) {
$order_by = esc_html($_GET['orderby']);
$order = strtoupper( esc_html($_GET['order']) );
$order_by = esc_html( $_GET['orderby'] );
$order = strtoupper( esc_html( $_GET['order'] ) );
if ( ! empty( $order_by ) && ! empty( $order ) ) {
$sql = self::manage_sorting( $order_by, $order, $sql );
}
}

if ( array_key_exists( 's', $_GET ) ) {
$search = esc_html($_GET['s']);
$search = esc_html( $_GET['s'] );
if ( ! empty( $search ) ) {
$sql = self::manage_search( $search, $sql );
}
Expand All @@ -59,11 +59,11 @@ public function prepare_items() {
for ( $i = 0; $i < count( $sql_data ); $i++ ) {
$temp = array();
$temp['id'] = $sql_data[ $i ]->id;
$temp['ip'] = sanitize_text_field($sql_data[ $i ]->ip);
$temp['path'] = sanitize_text_field($sql_data[ $i ]->path);
$temp['referer'] = sanitize_text_field($sql_data[ $i ]->referer);
$temp['user_agent'] = sanitize_text_field($sql_data[ $i ]->user_agent);
$temp['created'] = sanitize_text_field($sql_data[ $i ]->created);
$temp['ip'] = sanitize_text_field( $sql_data[ $i ]->ip );
$temp['path'] = sanitize_text_field( $sql_data[ $i ]->path );
$temp['referer'] = sanitize_text_field( $sql_data[ $i ]->referer );
$temp['user_agent'] = sanitize_text_field( $sql_data[ $i ]->user_agent );
$temp['created'] = sanitize_text_field( $sql_data[ $i ]->created );
array_push( $data, $temp );
}
$per_page = 50;
Expand Down Expand Up @@ -97,7 +97,7 @@ public function manage_sorting( $order_by, $order, $sql ) {

public function manage_search( $search, $sql ) {
$escaped_search = esc_sql( $search );
$sql .= " WHERE (ip LIKE '%" . esc_sql( $escaped_search ) . "%' OR path LIKE '%" . $escaped_search . "%' OR referer LIKE '%" . $escaped_search . "%' OR user_agent LIKE '%" . $escaped_search . "%' OR created LIKE '%" . $escaped_search . "%')";
$sql .= " WHERE (ip LIKE '%" . esc_sql( $escaped_search ) . "%' OR path LIKE '%" . $escaped_search . "%' OR referer LIKE '%" . $escaped_search . "%' OR user_agent LIKE '%" . $escaped_search . "%' OR created LIKE '%" . $escaped_search . "%')";
return $sql;
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public function get_sortable_columns() {

public function column_ip( $item ) {
$actions = array(
'c4p-logs--delete' => sprintf( '<a href="?page=%s&action=%s&path=%s">Delete</a>', esc_html($_REQUEST['page']), 'c4p-logs--delete', $item['id'] ),
'c4p-logs--delete' => sprintf( '<a href="?page=%s&action=%s&path=%s">Delete</a>', esc_html( $_REQUEST['page'] ), 'c4p-logs--delete', $item['id'] ),
);
return sprintf(
'%1$s %2$s',
Expand Down
6 changes: 3 additions & 3 deletions admin/views/about.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
$plugin_main_file = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/custom-404-pro/custom-404-pro.php';
$plugin_main_file = dirname( dirname( dirname( __DIR__ ) ) ) . '/custom-404-pro/custom-404-pro.php';
$plugin_data = get_plugin_data( $plugin_main_file );
?>

Expand Down Expand Up @@ -38,8 +38,8 @@
<input type="hidden" name="last_name" value="Nagar">
<input type="hidden" name="email" value="[email protected]">
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif"
alt="PayPal - The safer, easier way to pay online">
src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif"
alt="PayPal - The safer, easier way to pay online">
</form>
</div>
</div>
Expand Down
Loading

0 comments on commit 3efe421

Please sign in to comment.