From 49a305a7e842409dc7eb9c1d4fd1f388e16e91fb Mon Sep 17 00:00:00 2001 From: Javad Ahshamian Date: Fri, 16 Dec 2022 15:04:20 +0330 Subject: [PATCH] Optimization and cleaning Optimization and cleaning based on the recommendations of the WordPress Plugin Review Team --- inc/admin/add-word.php | 15 ++-- inc/admin/dashboard-template.php | 6 +- inc/admin/data-template.php | 10 +-- inc/admin/import-data.php | 5 +- inc/admin/settings-template.php | 6 +- inc/admin/settings.php | 4 +- inc/admin/words-list.php | 2 +- inc/admin/words-table.php | 60 ++++++++----- inc/ajax.php | 12 ++- inc/pagination/Pagination.class.php | 39 --------- inc/pagination/render.inc.php | 16 ++-- inc/search-tool.php | 35 ++++---- inc/templates/description.php | 6 +- inc/templates/searchbox.php | 21 ++--- lang/mdict-fa_IR.mo | Bin 6371 -> 6369 bytes lang/mdict-fa_IR.po | 130 ++++++++++++++-------------- moein-dictionary-free.php | 4 +- readme.txt | 7 +- 18 files changed, 181 insertions(+), 197 deletions(-) diff --git a/inc/admin/add-word.php b/inc/admin/add-word.php index 8816086..05d3eec 100644 --- a/inc/admin/add-word.php +++ b/inc/admin/add-word.php @@ -50,7 +50,11 @@ function mdict_add() {
-

+

@@ -59,18 +63,17 @@ function mdict_add() {
- +
- + @@ -145,7 +148,7 @@ function save_func() { $data_id = $wpdb->insert_id; do_action('mdict_word_add', $data_id, $data_array); - $url = admin_url('admin.php?page=mdict-add&item_id=' . $data_id); + $url = esc_url(admin_url('admin.php?page=mdict-add&item_id=' . $data_id)); wp_redirect($url); exit(); } diff --git a/inc/admin/dashboard-template.php b/inc/admin/dashboard-template.php index a9e54c7..4dc529a 100644 --- a/inc/admin/dashboard-template.php +++ b/inc/admin/dashboard-template.php @@ -20,14 +20,14 @@
  • '.__('Install the data', 'mdict') .''; + ?>🔔
  • -
  • +
diff --git a/inc/admin/data-template.php b/inc/admin/data-template.php index b9d06d1..32984d6 100644 --- a/inc/admin/data-template.php +++ b/inc/admin/data-template.php @@ -1,8 +1,7 @@
-
-

+

@@ -12,7 +11,6 @@
- - + - + diff --git a/inc/admin/settings.php b/inc/admin/settings.php index 3535a18..4aeecde 100644 --- a/inc/admin/settings.php +++ b/inc/admin/settings.php @@ -52,7 +52,7 @@ public static function custom_css() { .mdict a.mdict-word-link, .mdict h1.card-header { - font-size: px; + font-size: px; }

- +
query("DELETE FROM $table WHERE `id` IN($ids)"); + + $wpdb->query($wpdb->prepare("DELETE FROM `$table` WHERE `id` IN(%s)", $ids)); $count = count($posted_data['id']); add_action('admin_notices', function () use ($count) { @@ -85,14 +86,11 @@ function prepare_items() { $per_page = $this->get_items_per_page('mdict_wl_per_page', 20); $current_page = $this->get_pagenum(); + $offset = ($current_page - 1) * $per_page; + $table_name = $wpdb->prefix . "pn_mdict"; - $query = "SELECT * FROM $table_name"; - $s = $_REQUEST["s"] ?? ''; - if (!empty($s)) - { - $query = "SELECT * FROM `$table_name` Where `Word` LIKE '$s'"; - } + $s = isset($_REQUEST["s"]) ? sanitize_text_field($_REQUEST["s"]) : ''; $orderby = filter_input(INPUT_GET, 'orderby'); $orderby = !empty($orderby) ? esc_sql($orderby) : 'id'; @@ -100,33 +98,51 @@ function prepare_items() { $order = filter_input(INPUT_GET, 'order'); $order = !empty($order) ? esc_sql($order) : 'ASC'; - if (!empty($orderby) & !empty($order)) + if (!empty($s)) + { + $s = esc_sql($s); + + $totalitems = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `$table_name` Where `Word` LIKE '%s'", $s)); + $totalpages = ceil($totalitems / $per_page); + + if (!empty($orderby) & !empty($order)) + { + $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM `$table_name` Where `Word` LIKE '%s' ORDER BY $orderby $order LIMIT $offset, $per_page", $s)); + } + else + { + $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM `$table_name` Where `Word` LIKE '%s' LIMIT $offset, $per_page", $s)); + } + } + else { - $query .= " ORDER BY $orderby $order"; + $totalitems = $wpdb->get_var("SELECT COUNT(*) FROM `$table_name`"); + $totalpages = ceil($totalitems / $per_page); + + if (!empty($orderby) & !empty($order)) + { + $this->items = $wpdb->get_results("SELECT * FROM `$table_name` ORDER BY $orderby $order LIMIT $offset, $per_page"); + } + else + { + $this->items = $wpdb->get_results("SELECT * FROM `$table_name` LIMIT $offset, $per_page"); + } } - $totalitems = $wpdb->query($query); - $offset = ($current_page - 1) * $per_page; - $totalpages = ceil($totalitems / $per_page); - $query .= " LIMIT $offset, $per_page"; $this->set_pagination_args(array( "total_items" => $totalitems, "total_pages" => $totalpages, "per_page" => $per_page, )); - $this->items = $wpdb->get_results($query); } function column_Word($item) { - $actions = array( - 'edit' => '' . __('Edit', 'mdict') . '', + 'edit' => '' . __('Edit', 'mdict') . '', ); - - $link = '' . $item->Word . ''; - + $link = '' . esc_html($item->Word) . ''; return sprintf('%1$s %2$s', '' . $link . '', $this->row_actions($actions)); } @@ -135,8 +151,6 @@ protected function get_primary_column_name() { } function column_Description($item) { - - return mdict_get_excerot($item->Description, 10); } diff --git a/inc/ajax.php b/inc/ajax.php index 2ffb89b..a000446 100644 --- a/inc/ajax.php +++ b/inc/ajax.php @@ -31,27 +31,25 @@ public static function check_register() } public static function import_data() { - $data_file = $_POST['data_file'] ?? null; + + $data_file = filter_input(INPUT_POST, 'data_file'); if (!$data_file) { return false; } - $res = MDict_Import_Data::import($data_file); - echo wp_send_json($res); - wp_die(); + wp_send_json($res); } public static function search_word() { - $word = $_POST['word'] ?? null; + $word = filter_input(INPUT_POST, 'word'); if (!$word) { return false; } $result = MDict_SearchTools::search_ajax($word); - echo wp_send_json(array('res' => 1, 'data' => $result)); - wp_die(); + wp_send_json(array('res' => 1, 'data' => $result)); } } diff --git a/inc/pagination/Pagination.class.php b/inc/pagination/Pagination.class.php index f54bb10..dc04761 100644 --- a/inc/pagination/Pagination.class.php +++ b/inc/pagination/Pagination.class.php @@ -2,45 +2,6 @@ /** * Pagination - * - * Supplies an API for setting pagination details, and renders the resulting - * pagination markup (html) through the included render.inc.php file. - * - * @note The SEO methods (canonical/rel) were written following Google's - * suggested patterns. Namely, the canoical url excludes any - * peripheral parameters that don't relate to the pagination - * series. Whereas the prev/next rel link tags include any params - * found in the request. - * @author Oliver Nassar - * @todo add setter parameter type and range checks w/ exceptions - * @example - * - * // source inclusion - * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php'; - * - * // determine page (based on <_GET>) - * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1; - * - * // instantiate with page and records as constructor parameters - * $pagination = (new Pagination($page, 200)); - * $markup = $pagination->parse(); - * - * @example - * - * // source inclusion - * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php'; - * - * // determine page (based on <_GET>) - * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1; - * - * // instantiate; set current page; set number of records - * $pagination = (new Pagination()); - * $pagination->setCurrent($page); - * $pagination->setTotal(200); - * - * // grab rendered/parsed pagination markup - * $markup = $pagination->parse(); - * */ class Pagination { diff --git a/inc/pagination/render.inc.php b/inc/pagination/render.inc.php index ba485a6..9d740a7 100644 --- a/inc/pagination/render.inc.php +++ b/inc/pagination/render.inc.php @@ -16,7 +16,7 @@ if ($pages > 1 || $alwaysShowPagination === true) { ?> -
    +
      -
    • +
    • -
    • +
    • -
    • +
    • -
    • +
    • -
    • +
    • -
    • +
    • -
    • +
    prepare("SELECT COUNT(*) FROM `$table` $where", "%$word%"); + $query_res = $wpdb->prepare("SELECT * FROM `$table` $where ORDER BY $order_by LIMIT $offset , $per_page", "%$word%", $word); } else { - if (!empty($where)) - { - $where .= " AND"; - } - else - { - $where .= "Where"; - } - $where .= " `Description` LIKE '%$word%'"; + + $where .= "Where `Description` LIKE '%s'"; $order_by = "`Word` ASC"; + + $query_total = $wpdb->prepare("SELECT COUNT(*) FROM `$table` $where", "%$word%"); + $query_res = $wpdb->prepare("SELECT * FROM `$table` $where ORDER BY $order_by LIMIT $offset , $per_page", "%$word%"); } } + else + { + $query_total = "SELECT COUNT(*) FROM `$table`"; + $query_res = "SELECT * FROM `$table` ORDER BY $order_by LIMIT $offset , $per_page"; + } - $query_total = "SELECT COUNT(*) FROM `$table` $where"; - $query_res = "SELECT * FROM `$table` $where ORDER BY $order_by LIMIT $offset , $per_page"; - $total_items = $wpdb->get_var($query_total); $data = $wpdb->get_results($query_res); @@ -82,7 +85,7 @@ public static function search_ajax($word) { global $wpdb; $word = esc_sql($word); $table = $wpdb->prefix . "pn_mdict"; - $query_res = "SELECT `id`, `Word` FROM `$table` WHERE `Word` LIKE '%$word%' ORDER BY LOCATE('$word', Word), `Word` ASC LIMIT 20"; + $query_res = $wpdb->prepare("SELECT `id`, `Word` FROM `$table` WHERE `Word` LIKE '%s' ORDER BY LOCATE('%s', Word), `Word` ASC LIMIT 20", "%$word%", $word); $data = $wpdb->get_results($query_res, ARRAY_A); return $data; } diff --git a/inc/templates/description.php b/inc/templates/description.php index 9fcc1f2..1861b8e 100644 --- a/inc/templates/description.php +++ b/inc/templates/description.php @@ -2,7 +2,7 @@
    -

    Word ?>

    +

    Word) ?>

    @@ -11,8 +11,8 @@
    Description); - echo wpautop($des); + $des = $word_item->Description; + echo wp_kses( wpautop($des), 'post'); ?>
    diff --git a/inc/templates/searchbox.php b/inc/templates/searchbox.php index 250ce89..86db4d5 100644 --- a/inc/templates/searchbox.php +++ b/inc/templates/searchbox.php @@ -1,12 +1,9 @@
    -
    -
    -
    @@ -31,7 +28,7 @@

    - ' . $word_w . '') : __('List of words', 'mdict'); ?> ( ) + ' . $word_w . '') : _e('List of words', 'mdict'); ?> ( )

    $word_item->id), $current_page_url); ?>
    -

    Word ?>

    +

    Word) ?>

    Description); - echo wpautop($des); + echo wp_kses( wpautop($des), 'post'); ?>
    '; - echo mdict_pagination($total, MDict_SearchTools::get_pagenum(), MDict_SearchTools::get_perpage()); + ?> +
    + ' . __('Nothing found!', 'mdict') . '

    '; + ?> +

    +
    diff --git a/lang/mdict-fa_IR.mo b/lang/mdict-fa_IR.mo index 865cdc9411be916a9510f9e73a63da529224a032..1564920733e31764eaf91df92e74e1e830f008dc 100644 GIT binary patch delta 772 zcmXxiPe>GD9LDkAY&*N|BxSCvsZpz9D_O2n{wa$kDhNf9f)Jxi&@GCR)swRR!ISN2 zpj&jUD6od2Q->hBZ$w)+>yUvRqKg_q-^blv_LlETBA4lk-Hs>(KA z#pb3;74oS0=bFYV6F#V3c#0~|OJq(fp~|_ATEHgW!hM{a!vZp7Z&7dfCD@8)uAHE! z)GD7{~GFU3PZcZPjr6S1UFewxXFkn`WALus{^UgDgVStw2E`NQI!<0v8YA zA?6}oIz=HQ=^Vt1r@H9PgjNv2WDnILy7}+W_hGkQ_P3vT-(}u;=6QGDJ#argHJOx% zOh-iO@dIAK4NPDjEi8$Ogwe(z#xaBrHedqla1hII9M9qtyogymgYU2wmr(cHjEa9^ zmQ7}q$$WXCh&(nj58EQW*ou5)f-mlq7Z}G^*n;oz9)876Ojd}vIEJ_I4QjD_$oohh zb)Vlwk1Hf2S6HwrMVc{%TJ?QoPkDwl_!>)b5%mFUSc1RsG9KXwI#nX)a0+KIi`t!2 zyo>SbLN{hmpOdK`E^Pd^_`?!vn^uuMWgE4vKT!)g#!0l9^x;cnNj{^#>8^JaOr-T- z2T3al5G_#;Q-}8THt8ZgL=WTN=r3N}L@lp_#K