Ticket #6686: hefo.php

File hefo.php, 4.6 kB (added by Mrasnika, 3 months ago)
Line 
1 <?php
2 /*
3 Plugin Name: HeFo
4 Plugin URI: http://kaloyan.info/
5 Description: This plugin is designed to inject HTML snippets into the header and the footer of WordPress pages.
6 Author: Kaloyan K. Tsvetkov
7 Version: 0.1
8 Author URI: http://kaloyan.info/
9 */
10
11 /////////////////////////////////////////////////////////////////////////////
12
13 /**
14 * @internal prevent from direct calls
15 */
16 if (!defined('ABSPATH')) {
17     return ;
18     }
19
20 /**
21 * @internal prevent from second inclusion
22 */
23 if (!class_exists('hefo')) {
24
25 /////////////////////////////////////////////////////////////////////////////
26
27 /**
28 * "HeFo" WordPress Plugin
29 */
30 Class hefo {
31
32     // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
33
34     /**
35     * Constructor
36     */
37     function hefo() {
38
39         // attach the handler
40         //
41         add_action('wp_head',
42             array($this, 'wp_head'));
43         add_action('wp_footer',
44             array($this, 'wp_footer'));
45
46
47         // attach to admin menu
48         //
49         if (is_admin()) {
50             add_action('admin_menu',
51                 array(&$this, '_menu')
52                 );
53             }
54         
55         // attach to plugin installation
56         //
57         add_action(
58             'activate_' . str_replace(
59                 DIRECTORY_SEPARATOR, '/',
60                 str_replace(
61                     realpath(ABSPATH . PLUGINDIR) . DIRECTORY_SEPARATOR,
62                         '', __FILE__
63                     )
64                 ),
65             array(&$this, 'install')
66             );
67         }
68     
69     // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70     
71     /**
72     * Inject the wp_head related snippets
73     * @return string
74     */
75     function wp_head() {
76         return $this->heforize(__FUNCTION__);
77         }
78
79     /**
80     * Inject the wp_footer related snippets
81     * @return string
82     */
83     function wp_footer() {
84         return $this->heforize(__FUNCTION__);
85         }
86
87     /**
88     * Inject the wp_footer related snippets
89     * @return string
90     */
91     function heforize($tag) {
92         $hefo_settings = (array) get_option('hefo_settings');
93         if (isset($hefo_settings['snippets'][$tag])) {
94             echo $hefo_settings['snippets'][$tag];
95             }
96         }
97
98     // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
99
100     /**
101     * Performs the routines required at plugin installation:
102     * in general introducing the settings array
103     */   
104     function install() {
105         add_option(
106             'hefo_settings',
107                 array(
108                     'snippets' => array(
109                         'wp_head' => '',
110                         'wp_footer' => '',
111                         )
112                 )
113             );
114         }
115
116     // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
117     
118     /**
119     * Attach the menu page to the `Options` tab
120     */
121     function _menu() {
122         add_submenu_page('options-general.php',
123              'HeFo',
124              'HeFo', 8,
125              __FILE__,
126              array($this, 'menu')
127             );
128         }
129         
130     /**
131     * Handles and renders the menu page
132     */
133     function menu() {
134
135         // sanitize referrer
136         //
137         $_SERVER['HTTP_REFERER'] = preg_replace(
138             '~&saved=.*$~Uis','', $_SERVER['HTTP_REFERER']
139             );
140         
141         // information updated ?
142         //
143         if ($_POST['submit']) {
144
145             $_ = $_POST['hefo_settings'];
146             $_['snippets'] = array_map('stripCSlashes', $_['snippets']);
147             
148             // save
149             //
150             update_option(
151                 'hefo_settings',
152                 $_
153                 );
154
155             die("<script>document.location.href = '{$_SERVER['HTTP_REFERER']}&saved=settings:" . time() . "';</script>");
156             }
157
158         // operation report detected
159         //
160         if (@$_GET['saved']) {
161             
162             list($saved, $ts) = explode(':', $_GET['saved']);
163             if (time() - $ts < 10) {
164                 echo '<div class="updated"><p>';
165     
166                 switch ($saved) {
167                     case 'settings' :
168                         echo 'Settings saved.';
169                         break;
170                     }
171     
172                 echo '</p></div>';
173                 }
174             }
175
176         // read the settings
177         //
178         $hefo_settings = (array) get_option('hefo_settings');
179
180 ?>
181 <div class="wrap">
182     <h2>HeFo</h2>
183     <p>
184     This plugin is designed to help you inject portions of HTML code (or as
185     we call them "HTML snippets") into your blog without having to modify
186     the theme you are using.
187     </p>
188
189     <form method="post">
190     <fieldset class="options">
191
192         <label for="wp_head_html">Header:</label><br/>
193         <textarea name="hefo_settings[snippets][wp_head]" style="width:90%; height:120px;"
194             id="wp_head_html"><?php echo $hefo_settings['snippets']['wp_head']; ?></textarea><br/><br/>
195
196         <label for="wp_footer_html">Footer:</label><br/>
197         <textarea name="hefo_settings[snippets][wp_footer]" style="width:90%; height:120px;"
198             id="wp_footer_html"><?php echo $hefo_settings['snippets']['wp_footer']; ?></textarea>
199
200         <p class="submit" style="text-align:left;"><input type="submit" name="submit" value="Update &raquo;" /></p>
201     </fieldset>
202     </form>
203 </div>
204 <?php
205         }
206     
207     // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
208     
209     //--end-of-class
210     }
211
212 }
213
214 /////////////////////////////////////////////////////////////////////////////
215
216 /**
217 * Initiating the plugin...
218 * @see hefo
219 */
220 new hefo;
221
222 ?>