Skip to content

Commit

Permalink
Merge pull request #5 from threadi/feature/optimizeSql2
Browse files Browse the repository at this point in the history
optimize SQL
  • Loading branch information
threadi authored Sep 4, 2023
2 parents bbcc0b8 + d47d275 commit 737c45f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
28 changes: 10 additions & 18 deletions classes/eml/Model/class-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ class Log {
*/
private wpdb $wpdb;

/**
* DB-Table-name.
*
* @var string
*/
private string $table_name;

/**
* Constructor, not used as this a Singleton object.
*/
Expand All @@ -49,7 +42,7 @@ private function __construct() {
$this->wpdb = $wpdb;

// set the table-name.
$this->table_name = $this->wpdb->prefix . 'eml_logs';
$this->wpdb->eml_table = $this->wpdb->prefix . 'eml_logs';
}

/**
Expand All @@ -60,8 +53,7 @@ private function __construct() {
*/
public function get_logs(): array {
global $wpdb;
$table = $this->table_name;
return $wpdb->get_results( $wpdb->prepare( 'SELECT `state`, `time` AS `date`, `log`, `url` FROM ' . $table . ' WHERE 1 = %s ORDER BY `time` DESC', array( 1 ) ), ARRAY_A ); // phpcs:ignore
return $wpdb->get_results( $wpdb->prepare( 'SELECT `state`, `time` AS `date`, `log`, `url` FROM ' . $wpdb->eml_table . ' WHERE 1 = %s ORDER BY `time` DESC', array( 1 ) ), ARRAY_A ); // phpcs:ignore
}

/**
Expand Down Expand Up @@ -90,7 +82,7 @@ public function install(): void {
$charset_collate = $this->wpdb->get_charset_collate();

// table for import-log.
$sql = 'CREATE TABLE ' . $this->table_name . " (
$sql = 'CREATE TABLE ' . $this->wpdb->eml_table . " (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`time` datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
`log` text DEFAULT '' NOT NULL,
Expand All @@ -109,8 +101,8 @@ public function install(): void {
* @return void
*/
public function uninstall(): void {
$sql = "DROP TABLE IF EXISTS {$this->table_name}";
$this->wpdb->query( $sql ); // phpcs:ignore
global $wpdb;
$this->wpdb->query( sprintf( 'DROP TABLE IF EXISTS %s', $wpdb->eml_table ) );
}

/**
Expand All @@ -131,7 +123,7 @@ public function create( string $message, string $url, string $state, int $level

// add log entry.
$this->wpdb->insert(
$this->table_name,
$this->wpdb->eml_table,
array(
'time' => gmdate( 'Y-m-d H:i:s' ),
'log' => $message,
Expand All @@ -149,8 +141,8 @@ public function create( string $message, string $url, string $state, int $level
* @noinspection SqlResolve
*/
private function clean_log(): void {
$sql = sprintf( 'DELETE FROM `%s` WHERE `time` < DATE_SUB(NOW(), INTERVAL %d DAY)', $this->table_name, 50 );
$this->wpdb->query( $sql ); // phpcs:ignore
global $wpdb;
$this->wpdb->query( sprintf( 'DELETE FROM `%s` WHERE `time` < DATE_SUB(NOW(), INTERVAL %d DAY)', $wpdb->eml_table, 50 ) );
}

/**
Expand All @@ -159,8 +151,8 @@ private function clean_log(): void {
* @return void
*/
public function truncate_log(): void {
$sql = 'TRUNCATE TABLE `' . $this->table_name . '`';
$this->wpdb->query( $sql ); // phpcs:ignore
global $wpdb;
$this->wpdb->query( sprintf( 'TRUNCATE TABLE %s', $wpdb->eml_table ) );
}

/**
Expand Down
42 changes: 21 additions & 21 deletions inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function eml_admin_menu_init(): void {

// get possible mime types.
$mime_types = array();
foreach( External_Files::get_instance()->get_possible_mime_types() as $mime_type => $settings ) {
$mime_types[$mime_type] = $settings['label'];
foreach ( External_Files::get_instance()->get_possible_mime_types() as $mime_type => $settings ) {
$mime_types[ $mime_type ] = $settings['label'];
}

// select allowed mime-types.
Expand Down Expand Up @@ -230,7 +230,7 @@ function eml_admin_menu_init(): void {
'label_for' => 'eml_proxy',
'fieldId' => 'eml_proxy',
'description' => __( 'This option is only available if images are hosted external. If this option is disabled, external images will be embedded with their external URL. To prevent privacy protection issue you could enable this option to load the images locally.', 'external-files-in-media-library' ),
'readonly' => get_option('eml_images_mode', '') != 'external'
'readonly' => 'external' !== get_option( 'eml_images_mode', '' ),
)
);
register_setting( 'eml_settings_group', 'eml_proxy', array( 'sanitize_callback' => 'eml_admin_validate_checkbox' ) );
Expand All @@ -246,7 +246,7 @@ function eml_admin_menu_init(): void {
'label_for' => 'eml_proxy_max_age',
'fieldId' => 'eml_proxy_max_age',
'description' => __( 'Defines how long images, which are loaded via our own proxy, are saved locally. After this time their cache will be renewed.', 'external-files-in-media-library' ),
'readonly' => get_option('eml_images_mode', '') != 'external'
'readonly' => 'external' !== get_option( 'eml_images_mode', '' ),
)
);
register_setting( 'eml_settings_group', 'eml_proxy_max_age', array( 'sanitize_callback' => 'eml_admin_validate_number' ) );
Expand Down Expand Up @@ -600,17 +600,17 @@ function eml_admin_media_box(): void {
$url = $external_file_obj->get_url( true );

// get shorter URL to show (only protocol and host) to save space.
$parsed_url = wp_parse_url($url);
$parsed_url = wp_parse_url( $url );
$url_to_show = $url;
if( !empty($parsed_url['scheme']) ) {
$url_to_show = $parsed_url['scheme'].'://'.$parsed_url['host'].'..';
if ( ! empty( $parsed_url['scheme'] ) ) {
$url_to_show = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '..';
}

// output.
?>
<div class="misc-pub-external-file">
<p>
<?php echo esc_html__( 'File-URL:', 'external-files-in-media-library'); ?><br><a href="<?php echo esc_url($url); ?>" title="<?php echo esc_attr($url); ?>"><?php echo esc_html($url_to_show); ?></a>
<?php echo esc_html__( 'File-URL:', 'external-files-in-media-library' ); ?><br><a href="<?php echo esc_url( $url ); ?>" title="<?php echo esc_attr( $url ); ?>"><?php echo esc_html( $url_to_show ); ?></a>
</p>
<?php
if ( $external_file_obj->get_availability() ) {
Expand Down Expand Up @@ -641,7 +641,7 @@ function eml_admin_media_box(): void {
</p>
<p>
<?php
// TODO nur anzeigen wenn proxy aktiviert ist
// TODO nur anzeigen wenn proxy aktiviert ist.
if ( false !== $external_file_obj->is_cached() ) {
echo esc_html__( 'This file is delivered through proxied cache.', 'external-files-in-media-library' );
} else {
Expand All @@ -651,17 +651,17 @@ function eml_admin_media_box(): void {
</p>
</div>
<?php
} else {
?>
} else {
?>
<div class="notice notice-error notice-alt inline">
<p>
<?php
echo esc_html__( 'This file is not an external file.', 'external-files-in-media-library' );
?>
<?php
echo esc_html__( 'This file is not an external file.', 'external-files-in-media-library' );
?>
</p>
</div>
<?php
}
}
}

/**
Expand Down Expand Up @@ -878,14 +878,14 @@ function eml_admin_number_field( array $attr ): void {
?>
<input type="number" id="<?php echo esc_attr( $attr['fieldId'] ); ?>"
name="<?php echo esc_attr( $attr['fieldId'] ); ?>"
value="<?php echo esc_attr($value); ?>"
value="<?php echo esc_attr( $value ); ?>"
step="1"
min="0"
max="10000"
class="eml-field-width"
title="<?php echo esc_attr( $title ); ?>"
<?php
echo esc_attr($readonly);
echo esc_attr( $readonly );
?>
>
<?php
Expand Down Expand Up @@ -1154,9 +1154,9 @@ function eml_admin_dismiss(): void {
/**
* Validate the value from number-field.
*
* @param $value
* @param string $value Variable to validate.
* @return int
*/
function eml_admin_validate_number( $value ): int {
return absint($value);
}
function eml_admin_validate_number( string $value ): int {
return absint( $value );
}
2 changes: 1 addition & 1 deletion inc/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
exit;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions inc/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
exit;
}

/**
Expand All @@ -31,6 +31,6 @@
const EML_CAP_NAME = 'eml_manage_files';

/**
* options-list of transients.
* Options-list of transients.
*/
const EML_TRANSIENT_LIST = 'eml_transients';

0 comments on commit 737c45f

Please sign in to comment.