Say what? - Version 1.3

Version Description

Download this release

Release Info

Developer leewillis77
Plugin Icon 128x128 Say what?
Version 1.3
Comparing to
See all releases

Code changes from version 1.1 to 1.3

css/admin.css CHANGED
@@ -1,12 +1,13 @@
1
  .wp-list-table .column-orig_string,
2
- .wp-list-table .column-context,
3
  .wp-list-table .column-replacement_string {
4
  width: 26%;
5
  }
6
- .wp-list-table .column-domain {
7
- width: 12%;
 
 
8
  }
9
  .wp-list-table .edit_links,
10
  .wp-list-table .delete_links {
11
- width: 5%;
12
  }
1
  .wp-list-table .column-orig_string,
 
2
  .wp-list-table .column-replacement_string {
3
  width: 26%;
4
  }
5
+ .wp-list-table .column-domain,
6
+ .wp-list-table .column-context
7
+ {
8
+ width: 18%;
9
  }
10
  .wp-list-table .edit_links,
11
  .wp-list-table .delete_links {
12
+ width: 6%;
13
  }
html/say-what-admin-addedit.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <div class="wrap">
3
+ <div id="icon-tools" class="icon32"></div>
4
+ <h2><?php _e( 'Text changes', 'say_what' ); ?></h2>
5
+
6
+ <p><?php _e( "Fill in the details of the original translatable string, the string's text domain, and the string you would like to use instead. For more information check out the <a href='http://plugins.leewillis.co.uk/doc_post/adding-string-replacement/'>getting started guide</a>." ); ?></p>
7
+ <form action="tools.php?page=say_what_admin&amp;say_what_action=addedit" method="post">
8
+ <input type="hidden" name="say_what_save" value="1">
9
+ <?php wp_nonce_field( 'swaddedit', 'nonce' ); ?>
10
+ <?php if ( ! empty ( $replacement->string_id ) ) : ?>
11
+ <input type="hidden" name="say_what_string_id" value="<?php echo esc_attr( $replacement->string_id ); ?>">
12
+ <?php endif; ?>
13
+ <p>
14
+ <label for="say_what_orig_string"><?php _e( 'Original string', 'say_what' ); ?></label><br/>
15
+ <input type="text" name="say_what_orig_string" size="120" value="<?php echo esc_attr( $replacement->orig_string ) ?>"><br/>
16
+ </p>
17
+ <p>
18
+ <label for="say_what_domain"><?php _e( 'Text domain', 'say_what' ); ?></label> <a href="http://plugins.leewillis.co.uk/doc_post/adding-string-replacement/"><i class="dashicons dashicons-info">&nbsp;</i></a><br/>
19
+ <input type="text" name="say_what_domain" size="30" value="<?php echo esc_attr( $replacement->domain ); ?>"><br/>
20
+ </p>
21
+ <p>
22
+ <label for="say_what_context"><?php _e( 'Text context', 'say_what' ); ?></label> <a href="http://plugins.leewillis.co.uk/doc_post/replacing-wordpress-strings-context/"><i class="dashicons dashicons-info">&nbsp;</i></a><br/>
23
+ <input type="text" name="say_what_context" size="30" value="<?php echo esc_attr( $replacement->context ); ?>"><br/>
24
+ </p>
25
+ <p>
26
+ <label for="say_what_replacement_string"><?php _e( 'Replacement string', 'say_what' ); ?></label><br/>
27
+ <input type="text" name="say_what_replacement_string" size="120" value="<?php echo esc_attr( $replacement->replacement_string ); ?>"><br/>
28
+ </p>
29
+ <p>
30
+ <input type="submit" class="button-primary" value="<?php ! empty( $replacement->string_id ) ? _e( 'Update', 'say_what' ) : _e( 'Add', 'say_what' ); ?>">
31
+ </p>
32
+ </form>
33
+
34
+ </div>
html/say-what-admin-delete.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <div id="icon-tools" class="icon32"></div>
3
+ <h2><?php _e( 'Text changes', 'say_what' ); ?></h2>
4
+
5
+ <p><?php printf( __( 'Are you sure you want to delete the replacement string for &quot;%s&quot;?', 'say_what' ), esc_html( $replacement->orig_string ) ); ?></p>
6
+ <p>
7
+ <a href="tools.php?page=say_what_admin&amp;say_what_action=delete-confirmed&amp;id=<?php echo urlencode( $_GET['id'] ); ?>&amp;nonce=<?php echo urlencode( $_GET['nonce'] ); ?>" class="button"><?php _e( 'Yes', 'say_what' ); ?></a> <a href="tools.php?page=say_what_admin" class="button button-primary"><?php _e( 'No', 'say_what' ); ?></a>
8
+ </div>
html/say-what-admin-list.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <div id="icon-tools" class="icon32"></div>
3
+ <h2><?php _e( 'Text changes', 'say_what' ); ?><a href="tools.php?page=say_what_admin&amp;say_what_action=addedit" class="add-new-h2"><?php _e( 'Add New', 'say_what' ); ?></a></h2>
4
+ <?php
5
+ $list_table_instance = new SayWhatListTable( $this->settings );
6
+ $list_table_instance->prepare_items();
7
+ $list_table_instance->display();
8
+ ?>
9
+ </div>
phpci.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ build_settings:
2
+ ignore:
3
+ test:
4
+ php_mess_detector:
5
+ allowed_warnings: 0
6
+ php_code_sniffer:
7
+ standard: "WordPress-Custom"
8
+ allowed_warnings: 0
9
+ allowed_errors: 0
10
+ php_loc:
11
+ php_docblock_checker:
12
+ failure:
13
+ email:
14
+ committer: true
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://www.leewillis.co.uk/wordpress-plugins/?utm_source=wordpress&
4
  Tags: string, change, translation
5
  Requires at least: 3.5
6
  Tested up to: 4.0
7
- Stable tag: 1.1
8
 
9
  == Description ==
10
  An easy-to-use plugin that allows you to alter strings on your site without editing WordPress core, or plugin code. Simply enter the current string, and what you want to replace it with and the plugin will automatically do the rest!
@@ -29,6 +29,16 @@ You can either have a guess, or checkout the plugin in question's source code, t
29
 
30
  `$foo = __('This is a translatable string', 'plugin-domain');`
31
 
 
 
 
 
 
 
 
 
 
 
32
  == Screenshots ==
33
 
34
  1. Finding a string to replace
@@ -38,6 +48,9 @@ You can either have a guess, or checkout the plugin in question's source code, t
38
 
39
  == Changelog ==
40
 
 
 
 
41
  = 1.1 =
42
  Fix incorrect escaping on the admin screens.
43
 
4
  Tags: string, change, translation
5
  Requires at least: 3.5
6
  Tested up to: 4.0
7
+ Stable tag: 1.3
8
 
9
  == Description ==
10
  An easy-to-use plugin that allows you to alter strings on your site without editing WordPress core, or plugin code. Simply enter the current string, and what you want to replace it with and the plugin will automatically do the rest!
29
 
30
  `$foo = __('This is a translatable string', 'plugin-domain');`
31
 
32
+ = Is there any support for importing replacements? =
33
+ "Say What?" has preliminary support for exporting, and importing replacements via [http://wp-cli.org/](WP-CLI). The following commands are currently
34
+ supported:
35
+ * export - Export all current string replacements.
36
+ * import - Import string replacements from a CSV file.
37
+ * list - Export all current string replacements. Synonym for 'export'.
38
+ * update - update string replacements from a CSV file.
39
+
40
+ See the [GitHub homepage](https://github.com/leewillis77/say-what) for examples.
41
+
42
  == Screenshots ==
43
 
44
  1. Finding a string to replace
48
 
49
  == Changelog ==
50
 
51
+ = 1.2 =
52
+ Swap database to UTF-8 to fix problems entering non-ASCII strings.
53
+
54
  = 1.1 =
55
  Fix incorrect escaping on the admin screens.
56
 
say-what-admin.php CHANGED
@@ -1,12 +1,13 @@
1
  <?php
2
 
3
- if ( ! defined( 'ABSPATH' ) )
4
- exit; // Exit if accessed directly
 
5
 
6
  /**
7
  * Say What admin class - controller for all of the admin pages
8
  */
9
- class say_what_admin {
10
 
11
  private $settings;
12
 
@@ -19,14 +20,14 @@ class say_what_admin {
19
  add_action( 'admin_init', array( $this, 'admin_init' ) );
20
  }
21
 
22
- /**
23
- * Admin init actions. Takes care of saving stuff before redirects
24
- */
25
  public function admin_init() {
26
  if ( isset ( $_POST['say_what_save'] ) ) {
27
  $this->save();
28
  }
29
- if ( isset ( $_GET['say_what_action'] ) && ( $_GET['say_what_action'] == 'delete-confirmed' ) ) {
30
  $this->admin_delete_confirmed();
31
  }
32
  }
@@ -35,18 +36,18 @@ class say_what_admin {
35
  * Register the menu item for the admin pages
36
  */
37
  public function admin_menu() {
38
- if ( current_user_can( 'manage_options' ) ) {
39
- $page = add_management_page(
40
- __( 'Text changes', 'say_what' ),
41
- __( 'Text changes', 'say_what' ),
42
- 'manage_options',
43
- 'say_what_admin',
44
- array( $this, 'admin' )
45
- );
46
- if ( isset( $_GET['page'] ) && $_GET['page'] == 'say_what_admin' ) {
47
- add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_scripts' ) );
48
- }
49
- }
50
  }
51
 
52
  /**
@@ -80,7 +81,8 @@ class say_what_admin {
80
  * Render the list of currently configured replacement strings
81
  */
82
  public function admin_list() {
83
- require_once('html/say_what_admin_list.php');
 
84
  }
85
 
86
  /**
@@ -98,7 +100,7 @@ class say_what_admin {
98
  if ( ! $replacement ) {
99
  wp_die( __( 'Did you really mean to do that? Please go back and try again.', 'say_what' ) );
100
  }
101
- require_once('html/say_what_admin_delete.php');
102
  }
103
 
104
  /**
@@ -107,7 +109,7 @@ class say_what_admin {
107
  public function admin_delete_confirmed() {
108
  global $wpdb, $table_prefix;
109
  if ( ! wp_verify_nonce( $_GET['nonce'], 'swdelete' ) ||
110
- empty( $_GET['id'] ) ) {
111
  wp_die( __( 'Did you really mean to do that? Please go back and try again.', 'say_what' ) );
112
  }
113
  $sql = "DELETE FROM {$table_prefix}say_what_strings WHERE string_id = %d";
@@ -134,7 +136,7 @@ class say_what_admin {
134
  $replacement->domain = '';
135
  $replacement->context = '';
136
  }
137
- require_once('html/say_what_admin_addedit.php');
138
  }
139
 
140
  /**
@@ -149,15 +151,15 @@ class say_what_admin {
149
  $_POST = stripslashes_deep( $_POST );
150
  if ( isset ( $_POST['say_what_string_id'] ) ) {
151
  $sql = "UPDATE {$table_prefix}say_what_strings
152
- SET orig_string = %s,
153
- replacement_string = %s,
154
- domain = %s,
155
- context = %s
156
- WHERE string_id = %d";
157
  $wpdb->query(
158
  $wpdb->prepare(
159
  $sql,
160
- $_POST['say_what_orig_string'],
161
  $_POST['say_what_replacement_string'],
162
  $_POST['say_what_domain'],
163
  $_POST['say_what_context'],
@@ -166,16 +168,16 @@ class say_what_admin {
166
  );
167
  } else {
168
  $sql = "INSERT INTO {$table_prefix}say_what_strings
169
- VALUES ( NULL,
170
- %s,
171
- %s,
172
- %s,
173
- %s )";
174
 
175
  $wpdb->query(
176
  $wpdb->prepare(
177
  $sql,
178
- $_POST['say_what_orig_string'],
179
  $_POST['say_what_domain'],
180
  $_POST['say_what_replacement_string'],
181
  $_POST['say_what_context']
@@ -186,14 +188,4 @@ class say_what_admin {
186
  die();
187
  }
188
 
189
- /**
190
- * Show the current list of configured replacements.
191
- */
192
- private function show_current() {
193
- require_once ( 'say-what-list-table.class.php' );
194
- $list_table_instance = new say_what_list_table( $this->settings );
195
- $list_table_instance->prepare_items();
196
- $list_table_instance->display();
197
- }
198
-
199
  }
1
  <?php
2
 
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit; // Exit if accessed directly
5
+ }
6
 
7
  /**
8
  * Say What admin class - controller for all of the admin pages
9
  */
10
+ class SayWhatAdmin {
11
 
12
  private $settings;
13
 
20
  add_action( 'admin_init', array( $this, 'admin_init' ) );
21
  }
22
 
23
+ /**
24
+ * Admin init actions. Takes care of saving stuff before redirects
25
+ */
26
  public function admin_init() {
27
  if ( isset ( $_POST['say_what_save'] ) ) {
28
  $this->save();
29
  }
30
+ if ( isset ( $_GET['say_what_action'] ) && ( 'delete-confirmed' == $_GET['say_what_action'] ) ) {
31
  $this->admin_delete_confirmed();
32
  }
33
  }
36
  * Register the menu item for the admin pages
37
  */
38
  public function admin_menu() {
39
+ if ( current_user_can( 'manage_options' ) ) {
40
+ $page = add_management_page(
41
+ __( 'Text changes', 'say_what' ),
42
+ __( 'Text changes', 'say_what' ),
43
+ 'manage_options',
44
+ 'say_what_admin',
45
+ array( $this, 'admin' )
46
+ );
47
+ if ( isset( $_GET['page'] ) && 'say_what_admin' == $_GET['page'] ) {
48
+ add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_scripts' ) );
49
+ }
50
+ }
51
  }
52
 
53
  /**
81
  * Render the list of currently configured replacement strings
82
  */
83
  public function admin_list() {
84
+ require_once ( 'say-what-list-table.class.php' );
85
+ require_once('html/say-what-admin-list.php');
86
  }
87
 
88
  /**
100
  if ( ! $replacement ) {
101
  wp_die( __( 'Did you really mean to do that? Please go back and try again.', 'say_what' ) );
102
  }
103
+ require_once('html/say-what-admin-delete.php');
104
  }
105
 
106
  /**
109
  public function admin_delete_confirmed() {
110
  global $wpdb, $table_prefix;
111
  if ( ! wp_verify_nonce( $_GET['nonce'], 'swdelete' ) ||
112
+ empty( $_GET['id'] ) ) {
113
  wp_die( __( 'Did you really mean to do that? Please go back and try again.', 'say_what' ) );
114
  }
115
  $sql = "DELETE FROM {$table_prefix}say_what_strings WHERE string_id = %d";
136
  $replacement->domain = '';
137
  $replacement->context = '';
138
  }
139
+ require_once('html/say-what-admin-addedit.php');
140
  }
141
 
142
  /**
151
  $_POST = stripslashes_deep( $_POST );
152
  if ( isset ( $_POST['say_what_string_id'] ) ) {
153
  $sql = "UPDATE {$table_prefix}say_what_strings
154
+ SET orig_string = %s,
155
+ replacement_string = %s,
156
+ domain = %s,
157
+ context = %s
158
+ WHERE string_id = %d";
159
  $wpdb->query(
160
  $wpdb->prepare(
161
  $sql,
162
+ $_POST['say_what_orig_string'],
163
  $_POST['say_what_replacement_string'],
164
  $_POST['say_what_domain'],
165
  $_POST['say_what_context'],
168
  );
169
  } else {
170
  $sql = "INSERT INTO {$table_prefix}say_what_strings
171
+ VALUES ( NULL,
172
+ %s,
173
+ %s,
174
+ %s,
175
+ %s )";
176
 
177
  $wpdb->query(
178
  $wpdb->prepare(
179
  $sql,
180
+ $_POST['say_what_orig_string'],
181
  $_POST['say_what_domain'],
182
  $_POST['say_what_replacement_string'],
183
  $_POST['say_what_context']
188
  die();
189
  }
190
 
 
 
 
 
 
 
 
 
 
 
191
  }
say-what-cli.class.php ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Provides WP-CLI features for interacting with the "Say what?" plugin.
5
+ */
6
+ class SayWhatCli extends \WP_CLI\CommandWithDBObject {
7
+
8
+ protected $obj_type = 'stdClass';
9
+ protected $obj_fields = array(
10
+ 'string_id',
11
+ 'orig_string',
12
+ 'domain',
13
+ 'replacement_string',
14
+ 'context',
15
+ );
16
+
17
+ /**
18
+ * Export all current string replacements.
19
+ *
20
+ * ## OPTIONS
21
+ *
22
+ * [--format=<format>]
23
+ * : Accepted values: table, csv, json, count. Default: table
24
+ *
25
+ * ## EXAMPLES
26
+ *
27
+ * wp say-what export
28
+ *
29
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
30
+ */
31
+ public function export( $args, $assoc_args ) {
32
+ $formatter = $this->get_formatter( $assoc_args );
33
+ $replacements = $this->get_replacements();
34
+ $formatter->display_items( $replacements );
35
+ }
36
+
37
+ /**
38
+ * Export all current string replacements. Synonym for 'export'.
39
+ *
40
+ * ## OPTIONS
41
+ *
42
+ * [--format=<format>]
43
+ * : Accepted values: table, csv, json, count. Default: table
44
+ *
45
+ * ## EXAMPLES
46
+ *
47
+ * wp say-what list
48
+ *
49
+ * @subcommand list
50
+ */
51
+ public function _list( $args, $assoc_args ) {
52
+ return $this->export( $args, $assoc_args );
53
+ }
54
+
55
+ /**
56
+ * Import string replacements from a CSV file.
57
+ *
58
+ * All items in the import sheet will be added as new items.
59
+ *
60
+ * ## OPTIONS
61
+ *
62
+ * None.
63
+ *
64
+ * ## EXAMPLES
65
+ *
66
+ * wp say-what import {file}
67
+ *
68
+ * @subcommand import
69
+ *
70
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
71
+ */
72
+ public function _import( $args, $assoc_args ) {
73
+ $filename = $args[0];
74
+ $inserted = 0;
75
+ foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $item ) {
76
+ $this->insert_replacement( $item );
77
+ $inserted++;
78
+ }
79
+ WP_CLI::success( sprintf( '%d new items created.', $inserted ) );
80
+ }
81
+
82
+ /**
83
+ * update string replacements from a CSV file.
84
+ *
85
+ * Items with a string ID will have their information updated. Items without a string ID
86
+ * will be inserted as a new item.
87
+ *
88
+ * ## OPTIONS
89
+ *
90
+ * None.
91
+ *
92
+ * ## EXAMPLES
93
+ *
94
+ * wp say-what update {file}
95
+ *
96
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
97
+ */
98
+ public function update( $args, $assoc_args ) {
99
+ $filename = $args[0];
100
+ $updated = $inserted = 0;
101
+ foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $item ) {
102
+ if ( ! empty( $item['string_id'] ) ) {
103
+ $this->update_replacement( $item );
104
+ $updated++;
105
+ } else {
106
+ $this->insert_replacement( $item );
107
+ $inserted++;
108
+ }
109
+ }
110
+ WP_CLI::success( sprintf( '%d records updated, %d new items created.', $updated, $inserted ) );
111
+ }
112
+
113
+ /**
114
+ * Gets a list of the currentl set replacements.
115
+ *
116
+ * @return array An array of replacement objects.
117
+ */
118
+ protected function get_replacements() {
119
+ global $wpdb, $table_prefix;
120
+ $table = $table_prefix . 'say_what_strings';
121
+ return $wpdb->get_results( "SELECT * FROM $table" );
122
+ }
123
+
124
+ /**
125
+ * updates an existing replacement into the database.
126
+ *
127
+ * @param array $item The item to be updated..
128
+ */
129
+ protected function update_replacement( $item ) {
130
+ global $wpdb, $table_prefix;
131
+
132
+ $sql = "UPDATE {$table_prefix}say_what_strings
133
+ SET orig_string = %s,
134
+ domain = %s,
135
+ replacement_string = %s,
136
+ context = %s
137
+ WHERE string_id = %d";
138
+ $wpdb->query(
139
+ $wpdb->prepare(
140
+ $sql,
141
+ $item['orig_string'],
142
+ $item['domain'],
143
+ $item['replacement_string'],
144
+ $item['context'],
145
+ $item['string_id']
146
+ )
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Inserts a replacement into the database.
152
+ *
153
+ * @param array $item The item to be inserted.
154
+ */
155
+ protected function insert_replacement( $item ) {
156
+ global $wpdb, $table_prefix;
157
+ $sql = "INSERT INTO {$table_prefix}say_what_strings
158
+ VALUES ( NULL,
159
+ %s,
160
+ %s,
161
+ %s,
162
+ %s
163
+ )";
164
+
165
+ $wpdb->query(
166
+ $wpdb->prepare(
167
+ $sql,
168
+ $item['orig_string'],
169
+ $item['domain'],
170
+ $item['replacement_string'],
171
+ $item['context']
172
+ )
173
+ );
174
+ }
175
+ }
say-what-frontend.php CHANGED
@@ -1,33 +1,36 @@
1
  <?php
2
 
3
- if ( ! defined( 'ABSPATH' ) )
4
- exit; // Exit if accessed directly
 
5
 
6
  /**
7
  * The frontend class, responsible for performing the actual replacements
8
  */
9
- class say_what_frontend {
10
 
11
  private $replacements;
12
 
13
- /**
14
- * Read the settings in and put them into a format optimised for the final filter
15
- */
16
  function __construct( $settings ) {
17
- foreach ( $settings->replacements as $key => $value ) {
18
- if ( empty ( $value['domain'] ) )
19
  $value['domain'] = 'default';
20
- if ( empty ( $value['context'] ) )
 
21
  $value['context'] = 'sw-default-context';
22
- $this->replacements[$value['domain']][$value['orig_string']][$value['context']] = $value['replacement_string'];
 
23
  }
24
  add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 );
25
  add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 );
26
  }
27
 
28
- /**
29
- * Perform a string replacement without context.
30
- */
31
  public function gettext( $translated, $original, $domain ) {
32
  return $this->gettext_with_context( $translated, $original, 'sw-default-context', $domain );
33
  }
@@ -36,8 +39,8 @@ class say_what_frontend {
36
  * Perform a string replacement with context.
37
  */
38
  public function gettext_with_context( $translated, $original, $context, $domain ) {
39
- if ( isset ( $this->replacements[$domain][$original][$context] ) ) {
40
- return $this->replacements[$domain][$original][$context];
41
  } else {
42
  return $translated;
43
  }
1
  <?php
2
 
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit; // Exit if accessed directly
5
+ }
6
 
7
  /**
8
  * The frontend class, responsible for performing the actual replacements
9
  */
10
+ class SayWhatFrontend {
11
 
12
  private $replacements;
13
 
14
+ /**
15
+ * Read the settings in and put them into a format optimised for the final filter
16
+ */
17
  function __construct( $settings ) {
18
+ foreach ( $settings->replacements as $value ) {
19
+ if ( empty ( $value['domain'] ) ) {
20
  $value['domain'] = 'default';
21
+ }
22
+ if ( empty ( $value['context'] ) ) {
23
  $value['context'] = 'sw-default-context';
24
+ }
25
+ $this->replacements[ $value['domain'] ][ $value['orig_string'] ][ $value['context'] ] = $value['replacement_string'];
26
  }
27
  add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 );
28
  add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 );
29
  }
30
 
31
+ /**
32
+ * Perform a string replacement without context.
33
+ */
34
  public function gettext( $translated, $original, $domain ) {
35
  return $this->gettext_with_context( $translated, $original, 'sw-default-context', $domain );
36
  }
39
  * Perform a string replacement with context.
40
  */
41
  public function gettext_with_context( $translated, $original, $context, $domain ) {
42
+ if ( isset ( $this->replacements[ $domain ][ $original ][ $context ] ) ) {
43
+ return $this->replacements[ $domain ][ $original ][ $context ];
44
  } else {
45
  return $translated;
46
  }
say-what-list-table.class.php CHANGED
@@ -7,13 +7,13 @@ if ( ! class_exists( 'WP_List_Table' ) ) {
7
  /**
8
  * List table class for the admin pages
9
  */
10
- class say_what_list_table extends WP_List_Table {
11
 
12
  private $settings;
13
 
14
- /**
15
- * Constructor
16
- */
17
  function __construct( $settings ) {
18
  $this->settings = $settings;
19
  parent::__construct();
@@ -23,7 +23,7 @@ class say_what_list_table extends WP_List_Table {
23
  * Description shown when no replacements configured
24
  */
25
  function no_items() {
26
- _e( 'No string replacements configured yet.', 'say_what' );
27
  }
28
 
29
  /**
@@ -32,7 +32,7 @@ class say_what_list_table extends WP_List_Table {
32
  */
33
  function get_columns(){
34
  $columns = array(
35
- /*'cb' => 'Checkboxes',*/
36
  'string_id' => 'String replacement ID (Internal)',
37
  'orig_string' => __( 'Original string', 'say_what' ),
38
  'domain' => __( 'Text domain', 'say_what' ),
@@ -52,9 +52,9 @@ class say_what_list_table extends WP_List_Table {
52
  global $wpdb, $table_prefix;
53
 
54
  $columns = $this->get_columns();
55
- $hidden = array('string_id');
56
  $sortable = $this->get_sortable_columns();
57
- $this->_column_headers = array($columns, $hidden, $sortable);
58
  //$this->search_box(__('Search', 'say_what'), 'search_id'); // FIXME - implement searching
59
 
60
  // We don't use the replacements from the settings object, we query them separately to make
@@ -71,10 +71,10 @@ class say_what_list_table extends WP_List_Table {
71
  $this->items = $wpdb->get_results( $sql, ARRAY_A );
72
  }
73
 
74
- /**
75
- * Indicate which columns are sortable
76
- * @return array A list of the columns that are sortable.
77
- */
78
  function get_sortable_columns() {
79
  return array(
80
  'orig_string' => array( 'orig_string', true ),
@@ -83,32 +83,32 @@ class say_what_list_table extends WP_List_Table {
83
  'replacement_string' => array( 'replacement_string', false ) );
84
  }
85
 
86
- /**
87
- * Specify the bulk actions available. Not used currently
88
- */
89
  function get_bulk_actions() {
90
  // @TODO - implement bulk actions
91
  //$actions = array ( 'delete' => __( 'Delete', 'say_what' ) );
92
  return array();
93
  }
94
 
95
- /**
96
- * Checkboxes for the rows. Not used while we don't have bulk actions
97
- */
98
  function column_cb( $item ) {
99
  return sprintf( '<input type="checkbox" name="string_id[]" value="%d" />', $item['string_id'] );
100
  }
101
 
102
- /**
103
- * Output column data
104
- */
105
  function column_default( $item, $column_name ) {
106
- return esc_html( $item[$column_name] );
107
  }
108
 
109
- /**
110
- * Output an edit link for the row
111
- */
112
  function column_edit_links( $item ) {
113
  return '<a href="tools.php?page=say_what_admin&amp;say_what_action=addedit&amp;id=' .
114
  urlencode( $item['string_id'] ) .
@@ -119,9 +119,9 @@ class say_what_list_table extends WP_List_Table {
119
  '</a>';
120
  }
121
 
122
- /**
123
- * Output a delete link for the row
124
- */
125
  function column_delete_links( $item ) {
126
  return '<a href="tools.php?page=say_what_admin&say_what_action=delete&id=' .
127
  urlencode( $item['string_id'] ) .
7
  /**
8
  * List table class for the admin pages
9
  */
10
+ class SayWhatListTable extends WP_List_Table {
11
 
12
  private $settings;
13
 
14
+ /**
15
+ * Constructor
16
+ */
17
  function __construct( $settings ) {
18
  $this->settings = $settings;
19
  parent::__construct();
23
  * Description shown when no replacements configured
24
  */
25
  function no_items() {
26
+ _e( 'No string replacements configured yet.', 'say_what' );
27
  }
28
 
29
  /**
32
  */
33
  function get_columns(){
34
  $columns = array(
35
+ /*'cb' => 'Checkboxes',*/
36
  'string_id' => 'String replacement ID (Internal)',
37
  'orig_string' => __( 'Original string', 'say_what' ),
38
  'domain' => __( 'Text domain', 'say_what' ),
52
  global $wpdb, $table_prefix;
53
 
54
  $columns = $this->get_columns();
55
+ $hidden = array( 'string_id' );
56
  $sortable = $this->get_sortable_columns();
57
+ $this->_column_headers = array( $columns, $hidden, $sortable );
58
  //$this->search_box(__('Search', 'say_what'), 'search_id'); // FIXME - implement searching
59
 
60
  // We don't use the replacements from the settings object, we query them separately to make
71
  $this->items = $wpdb->get_results( $sql, ARRAY_A );
72
  }
73
 
74
+ /**
75
+ * Indicate which columns are sortable
76
+ * @return array A list of the columns that are sortable.
77
+ */
78
  function get_sortable_columns() {
79
  return array(
80
  'orig_string' => array( 'orig_string', true ),
83
  'replacement_string' => array( 'replacement_string', false ) );
84
  }
85
 
86
+ /**
87
+ * Specify the bulk actions available. Not used currently
88
+ */
89
  function get_bulk_actions() {
90
  // @TODO - implement bulk actions
91
  //$actions = array ( 'delete' => __( 'Delete', 'say_what' ) );
92
  return array();
93
  }
94
 
95
+ /**
96
+ * Checkboxes for the rows. Not used while we don't have bulk actions
97
+ */
98
  function column_cb( $item ) {
99
  return sprintf( '<input type="checkbox" name="string_id[]" value="%d" />', $item['string_id'] );
100
  }
101
 
102
+ /**
103
+ * Output column data
104
+ */
105
  function column_default( $item, $column_name ) {
106
+ return esc_html( $item[ $column_name ] );
107
  }
108
 
109
+ /**
110
+ * Output an edit link for the row
111
+ */
112
  function column_edit_links( $item ) {
113
  return '<a href="tools.php?page=say_what_admin&amp;say_what_action=addedit&amp;id=' .
114
  urlencode( $item['string_id'] ) .
119
  '</a>';
120
  }
121
 
122
+ /**
123
+ * Output a delete link for the row
124
+ */
125
  function column_delete_links( $item ) {
126
  return '<a href="tools.php?page=say_what_admin&say_what_action=delete&id=' .
127
  urlencode( $item['string_id'] ) .
say-what-settings.php CHANGED
@@ -1,14 +1,22 @@
1
  <?php
2
 
3
- if ( ! defined( 'ABSPATH' ) )
4
- exit; // Exit if accessed directly
 
5
 
6
  /**
7
  * Settings class. Possibly overkill at the moment
8
  */
9
- class say_what_settings {
 
10
  public $replacements;
11
- function __construct() {
 
 
 
 
 
 
12
  global $wpdb, $table_prefix;
13
  // @TODO - Read other settings in when we have them
14
  $sql = "SELECT * FROM {$table_prefix}say_what_strings";
1
  <?php
2
 
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit; // Exit if accessed directly
5
+ }
6
 
7
  /**
8
  * Settings class. Possibly overkill at the moment
9
  */
10
+ class SayWhatSettings {
11
+
12
  public $replacements;
13
+
14
+ /**
15
+ * Constructor.
16
+ *
17
+ * Loads the settings from the database.
18
+ */
19
+ public function __construct() {
20
  global $wpdb, $table_prefix;
21
  // @TODO - Read other settings in when we have them
22
  $sql = "SELECT * FROM {$table_prefix}say_what_strings";
say-what.php CHANGED
@@ -4,7 +4,7 @@
4
  Plugin Name: Say What?
5
  Plugin URI: https://github.com/leewillis77/say-what
6
  Description: An easy-to-use plugin that allows you to alter strings on your site without editing WordPress core, or plugin code
7
- Version: 1.1
8
  Author: Lee Willis
9
  Author URI: http://www.leewillis.co.uk/
10
  */
@@ -31,32 +31,39 @@ Author URI: http://www.leewillis.co.uk/
31
  * **********************************************************************
32
  */
33
 
34
- if ( ! defined( 'ABSPATH' ) )
35
- exit; // Exit if accessed directly
 
 
 
36
 
37
  /**
38
  * Main plugin class, responsible for triggering everything
39
  */
40
- class say_what {
41
 
42
  private $settings_instance;
43
  private $frontend_instance;
44
  private $admin_instance;
45
- private $db_version = 2;
46
 
47
  /**
48
  * Constructor
49
  */
50
  public function __construct(){
51
  require_once ( 'say-what-settings.php' );
52
- $this->settings_instance = new say_what_settings();
53
  if ( is_admin() ) {
54
  require_once ( 'say-what-admin.php' );
55
- $this->admin_instance = new say_what_admin( $this->settings_instance );
56
  }
57
  require_once ( 'say-what-frontend.php' );
58
- $this->frontend_instance = new say_what_frontend( $this->settings_instance );
59
 
 
 
 
 
60
  add_action( 'init', array( $this, 'init' ) );
61
  add_action( 'admin_init', array( $this, 'admin_init' ) );
62
  }
@@ -103,38 +110,56 @@ class say_what {
103
  * Database v2 upgrade.
104
  *
105
  * Add context to database schema.
 
 
106
  */
107
  private function upgrade_db_to_2() {
108
  global $wpdb;
109
  $table_name = $wpdb->prefix . 'say_what_strings';
110
  $sql = "CREATE TABLE $table_name (
111
- `string_id` int(11) NOT NULL AUTO_INCREMENT,
112
- `orig_string` text NOT NULL,
113
- `domain` varchar(255),
114
- `replacement_string` text,
115
- `context` text
116
- )";
117
  require_once ABSPATH . 'wp-admin/includes/upgrade.php';
118
  dbDelta( $sql );
119
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  }
121
 
122
  /**
123
  * Install function. Create the table to store the replacements
124
  */
125
  function say_what_install() {
126
- global $wpdb;
127
- $table_name = $wpdb->prefix . 'say_what_strings';
128
- $sql = "CREATE TABLE $table_name (
129
- `string_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
130
- `orig_string` text NOT NULL,
131
- `domain` varchar(255),
132
- `replacement_string` text,
133
- `context` text
134
- )";
135
- require_once ABSPATH . 'wp-admin/includes/upgrade.php';
136
- dbDelta( $sql );
 
137
  }
138
  register_activation_hook( __FILE__, 'say_what_install' );
139
 
140
- $say_what = new say_what();
4
  Plugin Name: Say What?
5
  Plugin URI: https://github.com/leewillis77/say-what
6
  Description: An easy-to-use plugin that allows you to alter strings on your site without editing WordPress core, or plugin code
7
+ Version: 1.3
8
  Author: Lee Willis
9
  Author URI: http://www.leewillis.co.uk/
10
  */
31
  * **********************************************************************
32
  */
33
 
34
+ if ( ! defined( 'ABSPATH' ) ) {
35
+ exit; // Exit if accessed directly
36
+ }
37
+
38
+ define( 'SAY_WHAT_DB_VERSION', 3 );
39
 
40
  /**
41
  * Main plugin class, responsible for triggering everything
42
  */
43
+ class SayWhat {
44
 
45
  private $settings_instance;
46
  private $frontend_instance;
47
  private $admin_instance;
48
+ private $db_version = SAY_WHAT_DB_VERSION;
49
 
50
  /**
51
  * Constructor
52
  */
53
  public function __construct(){
54
  require_once ( 'say-what-settings.php' );
55
+ $this->settings_instance = new SayWhatSettings();
56
  if ( is_admin() ) {
57
  require_once ( 'say-what-admin.php' );
58
+ $this->admin_instance = new SayWhatAdmin( $this->settings_instance );
59
  }
60
  require_once ( 'say-what-frontend.php' );
61
+ $this->frontend_instance = new SayWhatFrontend( $this->settings_instance );
62
 
63
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
64
+ require_once( 'say-what-cli.class.php');
65
+ WP_CLI::add_command( 'say-what', 'SayWhatCli' );
66
+ }
67
  add_action( 'init', array( $this, 'init' ) );
68
  add_action( 'admin_init', array( $this, 'admin_init' ) );
69
  }
110
  * Database v2 upgrade.
111
  *
112
  * Add context to database schema.
113
+ *
114
+ * @SuppressWarnings(PMD.UnusedPrivateMethod)
115
  */
116
  private function upgrade_db_to_2() {
117
  global $wpdb;
118
  $table_name = $wpdb->prefix . 'say_what_strings';
119
  $sql = "CREATE TABLE $table_name (
120
+ string_id int(11) NOT NULL AUTO_INCREMENT,
121
+ orig_string text NOT NULL,
122
+ domain varchar(255),
123
+ replacement_string text,
124
+ context text
125
+ )";
126
  require_once ABSPATH . 'wp-admin/includes/upgrade.php';
127
  dbDelta( $sql );
128
  }
129
+
130
+ /**
131
+ * Database v3 upgrade.
132
+ *
133
+ * Convert character set to utf8.
134
+ *
135
+ * @SuppressWarnings(PMD.UnusedPrivateMethod)
136
+ */
137
+ private function upgrade_db_to_3() {
138
+ global $wpdb;
139
+ $table_name = $wpdb->prefix . 'say_what_strings';
140
+ $sql = "ALTER TABLE $table_name
141
+ CONVERT TO CHARACTER SET utf8";
142
+ $wpdb->query( $sql );
143
+ }
144
  }
145
 
146
  /**
147
  * Install function. Create the table to store the replacements
148
  */
149
  function say_what_install() {
150
+ global $wpdb;
151
+ $table_name = $wpdb->prefix . 'say_what_strings';
152
+ $sql = "CREATE TABLE $table_name (
153
+ string_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
154
+ orig_string text NOT NULL,
155
+ domain varchar(255),
156
+ replacement_string text,
157
+ context text
158
+ ) DEFAULT CHARACTER SET utf8";
159
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
160
+ dbDelta( $sql );
161
+ update_option( 'say_what_db_version', SAY_WHAT_DB_VERSION );
162
  }
163
  register_activation_hook( __FILE__, 'say_what_install' );
164
 
165
+ $say_what = new SayWhat();