Real-Time Find and Replace - Version 1.0

Version Description

Download this release

Release Info

Developer Marios Alexandrou
Plugin Icon 128x128 Real-Time Find and Replace
Version 1.0
Comparing to
See all releases

Version 1.0

js/jquery.dynamicfields.js ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ function addFormField() {
2
+ var id = document.getElementById("id").value;
3
+ jQuery("#divTxt").append("<p id='row" + id + "'><label for='farfind" + id + "'>Find:&nbsp;</label><textarea rows='2' cols='40' name='farfind[" + id + "]' id='farfind" + id + "' /></textarea>&nbsp;&nbsp;<label for='farregex" + id + "'>RegEx?:&nbsp;</label><input type='checkbox' name='farregex[" + id + "]' id='farregex" + id + "' />&nbsp;&nbsp;<label for='farreplace" + id + "'>Replace:&nbsp;</label><textarea rows='2' cols='40' name='farreplace[" + id + "]' id='farreplace" + id + "' /></textarea>&nbsp;&nbsp&nbsp;<a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Remove</a></p>");
4
+ jQuery('#row' + id).highlightFade({
5
+ speed:1000
6
+ });
7
+ id = (id - 1) + 2;
8
+ document.getElementById("id").value = id;
9
+ }
10
+ function removeFormField(id) {
11
+ jQuery(id).highlightFade({color:'rgb(255,0,0)',complete:function() { jQuery(id).remove() },iterator:'sinusoidal'});}
js/jquery.highlightFade.js ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * jQuery Plugin highlightFade (jquery.offput.ca/highlightFade)
3
+ * (c) 2006 Blair Mitchelmore (offput.ca) blair@offput.ca
4
+ */
5
+ /**
6
+ * This is version 0.7 of my highlightFade plugin. It follows the yellow fade technique of Web 2.0 fame
7
+ * but expands it to allow any starting colour and allows you to specify the end colour as well.
8
+ *
9
+ * For the moment, I'm done with this plug-in. Unless I come upon a really cool feature it should have
10
+ * this plug-in will only receive updates to ensure future compatibility with jQuery.
11
+ *
12
+ * As of now (Aug. 16, 2006) the plugin has been written with the 1.0.1 release of jQuery (rev 249) which
13
+ * is available from http://jquery.com/src/jquery-1.0.1.js
14
+ *
15
+ * A note regarding rgb() syntax: I noticed that most browsers implement rgb syntax as either an integer
16
+ * (0-255) or percentage (0-100%) value for each field, that is, rgb(i/p,i/p,i/p); however, the W3C
17
+ * standard clearly defines it as "either three integer values or three percentage values" [http://www.w3.org/TR/CSS21/syndata.html]
18
+ * which I choose to follow despite the error redundancy of the typical behaviour browsers employ.
19
+ *
20
+ * Changelog:
21
+ *
22
+ * 0.7:
23
+ * - Added the awesome custom attribute support written by George Adamson (slightly modified)
24
+ * - Removed bgColor plugin dependency seeing as attr is customizable now...
25
+ * 0.6:
26
+ * - Abstracted getBGColor into its own plugin with optional test and data retrieval functions
27
+ * - Converted all $ references to jQuery references as John's code seems to be shifting away
28
+ * from that and I don't want to have to update this for a long time.
29
+ * 0.5:
30
+ * - Added simple argument syntax for only specifying start colour of event
31
+ * - Removed old style argument syntax
32
+ * - Added 'interval', 'final, and 'end' properties
33
+ * - Renamed 'color' property to 'start'
34
+ * - Added second argument to $.highlightFade.getBGColor to bypass the e.highlighting check
35
+ * 0.4:
36
+ * - Added rgb(%,%,%) color syntax
37
+ * 0.3:
38
+ * - Fixed bug when event was called while parent was also running event corrupting the
39
+ * the background colour of the child
40
+ * 0.2:
41
+ * - Fixed bug where an unspecified onComplete function made the page throw continuous errors
42
+ * - Fixed bug where multiple events on the same element would speed each subsequent event
43
+ * 0.1:
44
+ * - Initial Release
45
+ *
46
+ * @author Blair Mitchelmore (blair@offput.ca)
47
+ * @version 0.5
48
+ */
49
+ jQuery.fn.highlightFade = function(settings) {
50
+ var o = (settings && settings.constructor == String) ? {start: settings} : settings || {};
51
+ var d = jQuery.highlightFade.defaults;
52
+ var i = o['interval'] || d['interval'];
53
+ var a = o['attr'] || d['attr'];
54
+ var ts = {
55
+ 'linear': function(s,e,t,c) { return parseInt(s+(c/t)*(e-s)); },
56
+ 'sinusoidal': function(s,e,t,c) { return parseInt(s+Math.sin(((c/t)*90)*(Math.PI/180))*(e-s)); },
57
+ 'exponential': function(s,e,t,c) { return parseInt(s+(Math.pow(c/t,2))*(e-s)); }
58
+ };
59
+ var t = (o['iterator'] && o['iterator'].constructor == Function) ? o['iterator'] : ts[o['iterator']] || ts[d['iterator']] || ts['linear'];
60
+ if (d['iterator'] && d['iterator'].constructor == Function) t = d['iterator'];
61
+ return this.each(function() {
62
+ if (!this.highlighting) this.highlighting = {};
63
+ var e = (this.highlighting[a]) ? this.highlighting[a].end : jQuery.highlightFade.getBaseValue(this,a) || [255,255,255];
64
+ var c = jQuery.highlightFade.getRGB(o['start'] || o['colour'] || o['color'] || d['start'] || [255,255,128]);
65
+ var s = jQuery.speed(o['speed'] || d['speed']);
66
+ var r = o['final'] || (this.highlighting[a] && this.highlighting[a].orig) ? this.highlighting[a].orig : jQuery.curCSS(this,a);
67
+ if (o['end'] || d['end']) r = jQuery.highlightFade.asRGBString(e = jQuery.highlightFade.getRGB(o['end'] || d['end']));
68
+ if (typeof o['final'] != 'undefined') r = o['final'];
69
+ if (this.highlighting[a] && this.highlighting[a].timer) window.clearInterval(this.highlighting[a].timer);
70
+ this.highlighting[a] = { steps: ((s.duration) / i), interval: i, currentStep: 0, start: c, end: e, orig: r, attr: a };
71
+ jQuery.highlightFade(this,a,o['complete'],t);
72
+ });
73
+ };
74
+
75
+ jQuery.highlightFade = function(e,a,o,t) {
76
+ e.highlighting[a].timer = window.setInterval(function() {
77
+ var newR = t(e.highlighting[a].start[0],e.highlighting[a].end[0],e.highlighting[a].steps,e.highlighting[a].currentStep);
78
+ var newG = t(e.highlighting[a].start[1],e.highlighting[a].end[1],e.highlighting[a].steps,e.highlighting[a].currentStep);
79
+ var newB = t(e.highlighting[a].start[2],e.highlighting[a].end[2],e.highlighting[a].steps,e.highlighting[a].currentStep);
80
+ jQuery(e).css(a,jQuery.highlightFade.asRGBString([newR,newG,newB]));
81
+ if (e.highlighting[a].currentStep++ >= e.highlighting[a].steps) {
82
+ jQuery(e).css(a,e.highlighting[a].orig || '');
83
+ window.clearInterval(e.highlighting[a].timer);
84
+ e.highlighting[a] = null;
85
+ if (o && o.constructor == Function) o.call(e);
86
+ }
87
+ },e.highlighting[a].interval);
88
+ };
89
+
90
+ jQuery.highlightFade.defaults = {
91
+ start: [255,255,128],
92
+ interval: 50,
93
+ speed: 400,
94
+ attr: 'backgroundColor'
95
+ };
96
+
97
+ jQuery.highlightFade.getRGB = function(c,d) {
98
+ var result;
99
+ if (c && c.constructor == Array && c.length == 3) return c;
100
+ if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(c))
101
+ return [parseInt(result[1]),parseInt(result[2]),parseInt(result[3])];
102
+ else if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(c))
103
+ return [parseFloat(result[1])*2.55,parseFloat(result[2])*2.55,parseFloat(result[3])*2.55];
104
+ else if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(c))
105
+ return [parseInt("0x" + result[1]),parseInt("0x" + result[2]),parseInt("0x" + result[3])];
106
+ else if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(c))
107
+ return [parseInt("0x"+ result[1] + result[1]),parseInt("0x" + result[2] + result[2]),parseInt("0x" + result[3] + result[3])];
108
+ else
109
+ return jQuery.highlightFade.checkColorName(c) || d || null;
110
+ };
111
+
112
+ jQuery.highlightFade.asRGBString = function(a) {
113
+ return "rgb(" + a.join(",") + ")";
114
+ };
115
+
116
+ jQuery.highlightFade.getBaseValue = function(e,a,b) {
117
+ var s, t;
118
+ b = b || false;
119
+ t = a = a || jQuery.highlightFade.defaults['attr'];
120
+ do {
121
+ s = jQuery(e).css(t || 'backgroundColor');
122
+ if ((s != '' && s != 'transparent') || (e.tagName.toLowerCase() == "body") || (!b && e.highlighting && e.highlighting[a] && e.highlighting[a].end)) break;
123
+ t = false;
124
+ } while (e = e.parentNode);
125
+ if (!b && e.highlighting && e.highlighting[a] && e.highlighting[a].end) s = e.highlighting[a].end;
126
+ if (s == undefined || s == '' || s == 'transparent') s = [255,255,255];
127
+ return jQuery.highlightFade.getRGB(s);
128
+ };
129
+
130
+ jQuery.highlightFade.checkColorName = function(c) {
131
+ if (!c) return null;
132
+ switch(c.replace(/^\s*|\s*$/g,'').toLowerCase()) {
133
+ case 'aqua': return [0,255,255];
134
+ case 'black': return [0,0,0];
135
+ case 'blue': return [0,0,255];
136
+ case 'fuchsia': return [255,0,255];
137
+ case 'gray': return [128,128,128];
138
+ case 'green': return [0,128,0];
139
+ case 'lime': return [0,255,0];
140
+ case 'maroon': return [128,0,0];
141
+ case 'navy': return [0,0,128];
142
+ case 'olive': return [128,128,0];
143
+ case 'purple': return [128,0,128];
144
+ case 'red': return [255,0,0];
145
+ case 'silver': return [192,192,192];
146
+ case 'teal': return [0,128,128];
147
+ case 'white': return [255,255,255];
148
+ case 'yellow': return [255,255,0];
149
+ }
150
+ };
readme.txt ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Real-Time Find and Replace ===
2
+ Contributors: marios-alexandrou
3
+ Tags: find, replace
4
+ Requires at least: 2.7
5
+ Tested up to: 2.8
6
+ Stable tag: 1.0.0
7
+
8
+ Set up find and replace rules that are executed AFTER a page is generated by WordPress, but BEFORE it is sent to a user's browser.
9
+
10
+ == Description ==
11
+
12
+ This plugin allows you to dynamically (i.e. at the time when a page is generated) replace code and text from themes and other plugins with code and text of your choosing before a page is delivered to a user's browser.
13
+
14
+ Because the find and replace happens in real-time no changes are needed to plugins or themes which means upgrades remain easy!
15
+
16
+ Here are some real-world examples:
17
+
18
+ 1. Don't like the "Category:" text that the Dagon Design Sitemap plugin puts in front of every category? Remove it!
19
+ 2. Annoyed by the link that Global Translator adds to every page? Remove it!
20
+ 3. Have you noticed that the Sociable plugin doesn't correctly display the Twitter image? No problem, insert it!
21
+
22
+ And remember, all of the above can be done WITHOUT modifying themes or plugin files so you'll always be able to upgrade them without having to worry about losing custom edits.
23
+
24
+ == Installation ==
25
+
26
+ 1. Upload the real-time-find-and-replace folder to the '/wp-content/plugins/' directory
27
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
28
+ 3. The find and replace rules are in the Tools sidebar menu.
29
+ 4. Click on the Add link on the Find and Replace admin page to add as many rules as you want.
30
+
31
+ == Frequently Asked Questions ==
32
+
33
+ = Where is data stored? =
34
+
35
+ In an array in the wp_options table. Just one record regardless of the number of find and replace rules.
36
+
37
+ = Will the find and replace slow my site? =
38
+
39
+ Unless you're using 50+ rules, you shouldn't notice any performance impact.
40
+
41
+ = What does the regex checkbox do? =
42
+
43
+ You can do a straight up find and replace where the plugin will look for an exact match of what you specified. You can also used advanced pattern matching that is available through regular expressions by checking the regex checkbox.
real-time-find-and-replace.php ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Real-Time Find and Replace
4
+ Version: 1.0.0
5
+ Plugin URI: http://www.mariosalexandrou.com/wordpress-real-time-find-and-replace.asp
6
+ Description: Set up find and replace rules that are executed AFTER a page is generated by WordPress, but BEFORE it is sent to a user's browser.
7
+ Author: Marios Alexandrou
8
+ Author URI: http://www.mariosalexandrou.com/
9
+ */
10
+
11
+ /*
12
+ * Admin Page
13
+ */
14
+
15
+ add_action('admin_menu', 'far_add_pages');
16
+
17
+ function far_add_pages() { // Add a submenu under Tools
18
+ $page = add_submenu_page( 'tools.php', 'Real-Time Search and Replace', 'Real-Time Search and Replace', 'activate_plugins', 'real-time-find-and-replace', 'far_options_page');
19
+ add_action( "admin_print_scripts-$page", 'far_admin_scripts' );
20
+ }
21
+ function far_options_page(){
22
+ if (isset($_POST['setup-update'])) {
23
+ $_POST = stripslashes_deep($_POST);
24
+ if (is_array($_POST['farfind'])){ // If atleast one find has been submitted
25
+ foreach ($_POST['farfind'] as $key => $find){
26
+ if (empty($find)){ // if empty ones have been submitted we get rid of the extra data submitted if any.
27
+ unset($_POST['farfind'][$key]);
28
+ unset($_POST['farregex'][$key]);
29
+ unset($_POST['farreplace'][$key]);
30
+ }
31
+ if (!isset($_POST['farregex'][$key])) // convert line feeds on non-regex only
32
+ $_POST['farfind'][$key] = str_replace("\r\n", "\n", $find);
33
+ }
34
+ }
35
+ unset($_POST['setup-update']);
36
+
37
+ if(empty($_POST['farfind']))
38
+ delete_option('far_plugin_settings'); // delete the option if there are no settings. Keeps the database clean if they aren't using it and uninstall
39
+ else
40
+ update_option('far_plugin_settings', $_POST);
41
+
42
+ ?><div id="message" class="updated fade">
43
+ <p><strong>Options Updated</strong></p>
44
+ </div><?php
45
+ } ?>
46
+ <div class="wrap" style="padding-bottom:5em">
47
+ <h2>Real-Time Search and Replace</h2>
48
+ <p>Enter your find and replace cases below.</p>
49
+ <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
50
+ <?php
51
+ $farsettings = get_option('far_plugin_settings');
52
+ if (is_array($farsettings['farfind'])){ //if there are any finds already set
53
+ $i=1;
54
+ foreach ($farsettings['farfind'] as $key => $find){
55
+ if(isset($farsettings['farregex'][$key]))
56
+ $regex = 'CHECKED';
57
+ $replace = $farsettings['farreplace'][$key];
58
+ echo "<p id='row$i'><label for='farfind$i'>Find:&nbsp;</label><textarea rows='3' cols='30' name='farfind[$i]' id='farfind$i'>$find</textarea>&nbsp;&nbsp;<label for='farregex$i'>RegEx?:&nbsp;</label><input type='checkbox' name='farregex[$i]' id='farregex$i' $regex />&nbsp;&nbsp;<label for='farreplace$i'>Replace:&nbsp;</label><textarea rows='3' cols='30' name='farreplace[$i]' id='farreplace$i'>$replace</textarea>&nbsp;&nbsp&nbsp;<a href='#' onClick='removeFormField(\"#row$i\"); return false;'>Remove</a></p>\n"; // this is identical to what the js returns when adding new items
59
+ unset($regex);
60
+ $i++;
61
+ }
62
+ } else {
63
+ echo 'Click "Add" below to begin.';
64
+ }
65
+ ?>
66
+ <div id="divTxt"></div>
67
+ <p><a href="#" onClick="addFormField(); return false;">Add</a></p>
68
+ <input type="hidden" id="id" value="<?php echo $i; /*used so javascript returns unique ids*/ ?>" />
69
+ <input type="hidden" name="setup-update" />
70
+ <p><input type="submit" class="button" value="Update Settings" /></p>
71
+ </form>
72
+ </div>
73
+ <?php
74
+ }
75
+ function far_admin_scripts(){ // these scripts print on the admin page
76
+ wp_enqueue_script('far_dynamicfields', plugins_url() . '/real-time-find-and-replace/js/jquery.dynamicfields.js', array('jquery'));
77
+ wp_enqueue_script('highlightFade', plugins_url() . '/real-time-find-and-replace/js/jquery.highlightFade.js', array('jquery'));
78
+ }
79
+
80
+ /*
81
+ * Core Functionality
82
+ */
83
+ function far_ob_call($buffer){ // $buffer contains entire page
84
+ $farsettings = get_option('far_plugin_settings');
85
+ if (is_array($farsettings['farfind'])){
86
+ foreach ($farsettings['farfind'] as $key => $find){
87
+ if(isset($farsettings['farregex'][$key]))
88
+ $buffer = preg_replace($find, $farsettings['farreplace'][$key], $buffer);
89
+ else
90
+ $buffer = str_replace($find, $farsettings['farreplace'][$key], $buffer);
91
+ }
92
+ }
93
+ return $buffer;
94
+ }
95
+
96
+ function far_template_redirect(){
97
+ ob_start();
98
+ ob_start('far_ob_call');
99
+ }
100
+ add_action('template_redirect', 'far_template_redirect');
101
+
102
+ ?>