Skip to content

Commit

Permalink
v 0.5.0
Browse files Browse the repository at this point in the history
- Gitlab action: eslint.
- Add JS filter on results.
- Add uninstall.
  • Loading branch information
Darklg committed Sep 22, 2023
1 parent ec5fae3 commit ac690bf
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 15 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
"no-prototype-builtins": 0
},
"globals": {
"wp": true,
"jQuery": true
}
}
17 changes: 17 additions & 0 deletions .github/workflows/js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: ESLint

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install modules
run: npm install -g eslint
- name: Run ESLint
run: eslint assets/ --ext .js
9 changes: 9 additions & 0 deletions assets/back.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@charset "UTF-8";

/* ----------------------------------------------------------
Table
---------------------------------------------------------- */

.wp-list-table--wpu_override_gettext tr[data-visible="0"] {
display: none;
}
42 changes: 42 additions & 0 deletions assets/back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
document.addEventListener("DOMContentLoaded", function() {
'use strict';
var $table = document.getElementById('wp-list-table--wpu_override_gettext');
if (!$table) {
return;
}
var $table_lines = $table.querySelectorAll('tr[data-filter-text]'),
$filter = document.getElementById('wpu_override_gettext__filter_results');
$filter.addEventListener('keyup', function() {
filter_table($filter.value);
}, 1);

function filter_table(filter_value) {
if (!filter_value) {
Array.prototype.forEach.call($table_lines, function(el) {
el.setAttribute('data-visible', '1');
});
return;
}
filter_value = filter_value.toLowerCase();
var value_words = filter_value.split(' ');
Array.prototype.forEach.call($table_lines, function(el) {
var _search_string = el.getAttribute('data-filter-text');

/* Visible by default */
el.setAttribute('data-visible', '1');
for (var i = 0, len = value_words.length; i < len; i++) {
/* Ignore empty parts */
if (!value_words[i]) {
continue;
}
/* Hide if a part is not present */
if (_search_string.indexOf(value_words[i]) < 0) {
console.log(value_words[i]);
el.setAttribute('data-visible', '0');
return;
}
}
});

}
});
13 changes: 13 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
die;
}

/* Delete options */
$options = array(
'wpu_override_gettext__translations'
);
foreach ($options as $opt) {
delete_option($opt);
delete_site_option($opt);
}
57 changes: 42 additions & 15 deletions wpu_override_gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
Plugin URI: https://github.com/WordPressUtilities/wpu_override_gettext
Update URI: https://github.com/WordPressUtilities/wpu_override_gettext
Description: Override gettext strings
Version: 0.4.0
Version: 0.5.0
Author: darklg
Author URI: https://darklg.me/
Text Domain: wpu_override_gettext
Domain Path: /lang/
Domain Path: /lang
Requires at least: 6.2
Requires PHP: 8.0
License: MIT License
Expand All @@ -18,7 +18,7 @@
class WPUOverrideGettext {
public $plugin_description;
public $adminpages;
private $plugin_version = '0.4.0';
private $plugin_version = '0.5.0';
private $plugin_settings = array(
'id' => 'wpu_override_gettext',
'name' => 'WPU Override gettext'
Expand All @@ -27,8 +27,9 @@ class WPUOverrideGettext {
private $text_domains = false;

public function __construct() {
add_filter('plugins_loaded', array(&$this, 'plugins_loaded'));
add_filter('gettext', array(&$this, 'translate_text'), 10, 3);
add_action('plugins_loaded', array(&$this, 'plugins_loaded'));
add_action('gettext', array(&$this, 'translate_text'), 10, 3);
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
}

public function plugins_loaded() {
Expand Down Expand Up @@ -61,13 +62,13 @@ public function plugins_loaded() {
'level' => 'delete_users',
'basename' => plugin_basename(__FILE__)
);
include dirname(__FILE__) . '/inc/WPUBaseAdminPage/WPUBaseAdminPage.php';
require_once dirname(__FILE__) . '/inc/WPUBaseAdminPage/WPUBaseAdminPage.php';
$this->adminpages = new \wpu_override_gettext\WPUBaseAdminPage();
$this->adminpages->init($pages_options, $admin_pages);

# MESSAGES
if (is_admin()) {
include dirname(__FILE__) . '/inc/WPUBaseMessages/WPUBaseMessages.php';
require_once dirname(__FILE__) . '/inc/WPUBaseMessages/WPUBaseMessages.php';
$this->messages = new \wpu_override_gettext\WPUBaseMessages($this->plugin_settings['id']);
}
}
Expand Down Expand Up @@ -124,7 +125,11 @@ public function page_content__main() {
/* Load existing translations */
$translations = $this->get_fixed_translations();

echo '<table class="wp-list-table widefat striped">';
echo '<p>';
echo '<label for="wpu_override_gettext__filter_results">' . __('Filter strings', 'wpu_override_gettext') . '</label> ';
echo '<input name="wpu_override_gettext__filter_results" id="wpu_override_gettext__filter_results" type="text" />';
echo '</p>';
echo '<table id="wp-list-table--wpu_override_gettext" class="wp-list-table--wpu_override_gettext wp-list-table widefat striped">';
echo '<thead><tr>';
echo '<th>' . __('String', 'wpu_override_gettext') . '</th>';
echo '<th>' . __('Domain', 'wpu_override_gettext') . '</th>';
Expand All @@ -134,15 +139,20 @@ public function page_content__main() {
if (!isset($str['domain']) || !in_array($str['domain'], $this->text_domains)) {
continue;
}

/* Load new translation if available */
$new_translation = $this->get_string_translation($str['string'], '');
$old_translation = __($str['string'], $str['domain']);
$files_present = array_unique($str['files']);
$filter_text = array_merge($files_present, array($str['string'], $new_translation, $old_translation));
$filter_text = strtolower(strip_tags(implode(' ', array_unique(array_filter($filter_text)))));

echo '<tr>';
echo '<td><strong>' . esc_html($str['string']) . '</strong><small style="display:block">' . implode('<br />', array_unique($str['files'])) . '</small></td>';
echo '<tr data-visible="1" data-filter-text="' . esc_attr($filter_text) . '">';
echo '<td><strong>' . esc_html($str['string']) . '</strong><small style="display:block">' . implode('<br />', $files_present) . '</small></td>';
echo '<td>' . $str['domain'] . '</td>';
echo '<td>';
echo '<input type="hidden" name="original_string[]" value="' . esc_attr($str['string']) . '" />';
echo '<textarea name="translated_string[]" style="width:100%" placeholder="' . esc_attr(__($str['string'], $str['domain'])) . '">' . esc_html($new_translation) . '</textarea>';
echo '<textarea name="translated_string[]" style="width:100%" placeholder="' . esc_attr($old_translation) . '">' . esc_html($new_translation) . '</textarea>';
echo '</td>';
echo '</tr>';
}
Expand All @@ -165,9 +175,26 @@ public function page_action__main() {
}
}

/* HELPERS */
/* ----------------------------------------------------------
Assets
---------------------------------------------------------- */

function admin_enqueue_scripts() {

/* Back script */
wp_register_script('wpu_override_gettext_back_script', plugins_url('assets/back.js', __FILE__), array(), $this->plugin_version, true);
wp_enqueue_script('wpu_override_gettext_back_script');

/* Back Style */
wp_register_style('wpu_override_gettext_back_style', plugins_url('assets/back.css', __FILE__), array(), $this->plugin_version);
wp_enqueue_style('wpu_override_gettext_back_style');
}

/* ----------------------------------------------------------
Helpers
---------------------------------------------------------- */

function get_fixed_translations(){
function get_fixed_translations() {
$translations = get_option('wpu_override_gettext__translations');
if (!is_array($translations)) {
return array();
Expand All @@ -180,9 +207,9 @@ function get_fixed_translations(){
return $translations_fixed;
}

function get_string_translation($string, $new_translation){
function get_string_translation($string, $new_translation) {
$translations = $this->get_fixed_translations();
if(!is_array($translations) || empty($translations)){
if (!is_array($translations) || empty($translations)) {
return $new_translation;
}

Expand Down

0 comments on commit ac690bf

Please sign in to comment.