root/tags/release_0.1/wp_iminr.php

Revision 51, 10.4 kB (checked in by yann, 5 years ago)

Tracker can be inserted only once

Line 
1 <?php
2 /*
3 Copyright © 2007 Yann Lugrin (email: yann.lugrin@sans-savoir.net)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; only version 2 of the
8 License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 /*
21 Plugin Name: iMinR for Wordpress
22 Plugin URI: http://dev.sans-savoir.net/trac/wp_iminr
23 Description: Enable <a href="http://www.iminr.com">iMinR</a> in your Blog.
24 Author: Yann Lugrin
25 Version: 0.1
26 Author URI: http://www.sans-savoir.net
27 */
28
29 // print current tracker without other tags
30 function iMinR () {
31     wp_iminr_insert_tracker();
32 }
33
34 // print current tracker with meta tags
35 function wp_iminr_meta() {
36     wp_iminr_insert_tracker('meta');
37 }
38
39 // print current tracker with footer tags
40 function wp_iminr_footer() {
41     wp_iminr_insert_tracker('footer');
42 }
43
44 // insert tracker code or "desable" comment
45 function wp_iminr_insert_tracker($location='') {
46     global $wp_iminr_tracker_inserted;
47     
48     if (!$wp_iminr_tracker_inserted) {
49         $account_id = wp_iminr_get_option('account_id');
50         if ($account_id != '' && wp_iminr_enable_for_user()) {
51             echo "<!-- iMinR for WordPress is enable -->\n";
52             $wp_iminr_tracker_inserted = true;
53             
54             if (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
55                 $tracker = "<script language=\"javascript\" src=\"http://tag.iminr.com/audit/tag.aspx?id=$account_id\"></script>";
56             } else {
57                 $tracker = "<script language=\"javascript\" src=\"https://tags.iminr.com/audit/tag.aspx?id=$account_id\"></script>";
58             }
59             
60             switch ($location) {
61             case 'meta':
62                 echo wp_iminr_get_option('tracker_in_meta_before') . $tracker . wp_iminr_get_option('tracker_in_meta_after');
63                 break;
64             case 'footer':
65                 echo wp_iminr_get_option('tracker_in_footer_before') . $tracker . wp_iminr_get_option('tracker_in_footer_after');
66                 break;
67             default:
68                 echo $tracker;
69             }
70         } elseif ($account_id != '' && !wp_iminr_enable_for_user()) {
71             echo "<!-- iMinR for WordPress is desable for current user -->\n";
72         } else {
73             echo "<!-- iMinR for WordPress is desable because account id isn\'t set -->\n";
74         }
75     }
76 }
77
78 // get option from the WordPress options database table or return the default value
79 function wp_iminr_get_option($option_name) {
80     // get options from the database
81     $wp_iminr_options = get_option('wp_iminr_options');
82     
83     // if no options in database or not this specific option
84     // create default options array
85     if (!$wp_iminr_options || !array_key_exists($option_name, $wp_iminr_options) ) {
86         $wp_iminr_default_options = array();
87         
88         // defaults values
89         $wp_iminr_default_options['account_id']          = '';
90         
91         $wp_iminr_default_options['enable_tracker']           = false;
92         $wp_iminr_default_options['tracker_in_meta']          = true;
93         $wp_iminr_default_options['tracker_in_meta_before']   = '<li>';
94         $wp_iminr_default_options['tracker_in_meta_after']    = '</li>';
95         $wp_iminr_default_options['tracker_in_footer']        = false;
96         $wp_iminr_default_options['tracker_in_footer_before'] = '<br />';
97         $wp_iminr_default_options['tracker_in_footer_after']  = '';
98         $wp_iminr_default_options['desable_for_roles']   = array('administrator');
99         
100         // add default options to the database unless options already exist
101         add_option('wp_iminr_options', $wp_iminr_default_options, 'Settings for iMinR for WordPress plugin');
102         
103         // return default option
104         return $wp_iminr_default_options[$option_name];
105     } else {
106         // return option found in database
107         return $wp_iminr_options[$option_name];
108     }
109 }
110
111 // add options page to administration menu
112 function wp_iminr_admin() {
113     if (function_exists('add_options_page')) {
114         add_options_page(__('iMinR for WorldPress'), 'iMinR', 8, basename(__FILE__), 'wp_iminr_options');
115     }
116 }
117
118 // options page for administration
119 function wp_iminr_options() {
120     if (isset($_POST['info_update'])) {
121         ?><div class="updated"><p><strong><?php
122         // process submitted form
123         $wp_iminr_options = get_option('wp_iminr_options');
124         
125         $wp_iminr_options['account_id']          =  $_POST['account_id'];
126         
127         $wp_iminr_options['enable_tracker']           = ($_POST['enable_tracker']            == 'true'   ? true : false);
128         $wp_iminr_options['tracker_in_meta']          = ($_POST['tracker_in']                == 'meta'   ? true : false);
129         $wp_iminr_options['tracker_in_meta_before']   =  $_POST['tracker_in_meta_before'];
130         $wp_iminr_options['tracker_in_meta_after']    =  $_POST['tracker_in_meta_after'];
131         $wp_iminr_options['tracker_in_footer']        = ($_POST['tracker_in']                == 'footer' ? true : false);
132         $wp_iminr_options['tracker_in_footer_before'] =  $_POST['tracker_in_footer_before'];
133         $wp_iminr_options['tracker_in_footer_after']  =  $_POST['tracker_in_footer_after'];
134         $wp_iminr_options['desable_for_roles']   =  $_POST['desable_for_roles'];
135                 
136         update_option('wp_iminr_options', $wp_iminr_options);
137         
138         _e('Options saved', 'wp_iminr')
139         ?></strong></p></div><?php
140     }
141     ?>
142 <div class=wrap>
143     <form method="post">
144         <h2><?php _e('iMinR for WordPress'); ?></h2>
145         <fieldset class="options" name="general">
146         <legend><?php _e('General settings', 'wp_iminr') ?></legend>
147        
148         <table width="100%" cellspacing="2" cellpadding="5" class="editform">
149             <tr>
150                 <th nowrap valign="top"><label for="account_id"><?php _e('Account ID', 'wp_iminr') ?></label></th>
151                 <td valign="top">
152                     <input type="text" name="account_id" id="account_id" value="<?php echo wp_iminr_get_option('account_id'); ?>" size="50" />
153                     <br /><small><?php _e('Enter your iMinR account ID.', 'wp_iminr') ?></small>
154                 </td>
155             </tr>
156         </table>
157        
158         </fieldset>
159         <fieldset class="options" name="integration">
160             <legend><?php _e('Tracker integration', 'wp_iminr') ?></legend>
161             <p><?php _e('Enable tracker inclusion or copy this code in your template to activate iMinR for this site: <code>&lt;?php iMinR(); ?></code> ', 'wp_iminr') ?></p>
162            
163             <table width="100%" cellspacing="2" cellpadding="5" class="editform">
164                 <tr>
165                     <th nowrap valign="top"><label for="enable_tracker"><?php _e('Enable tracker', 'wp_iminr') ?></label></th>
166                     <td valign="top">
167                         <input type="checkbox" name="enable_tracker" id="enable_tracker" value="true" <?php if (wp_iminr_get_option('enable_tracker')) echo 'checked="checked"'; ?> />
168                         <br /><small><?php _e('By unchecking this checkbox tracker will be not included on the page.', 'wp_iminr') ?></small>
169                     </td>
170                 </tr>
171                 <tr>
172                     <th nowrap valign="top"><label for="tracker_in_meta"><?php _e('Add in sidebar', 'wp_iminr') ?></label></th>
173                     <td valign="top">
174                         <input type="radio" name="tracker_in" id="tracker_in_meta" value="meta" <?php if (wp_iminr_get_option('tracker_in_meta')) echo 'checked="checked"'; ?> />
175                         <small><?php _e('By checking this radio button tracker will be included in meta (if template implemente this option).', 'wp_iminr') ?></small>
176                         <br /><input type="text" name="tracker_in_meta_before" id="tracker_in_meta_before" value="<?php echo wp_iminr_get_option('tracker_in_meta_before'); ?>" size="25" style="text-align: right;" />
177                         <img src="" alt="iMinR" />
178                         <input type="text" name="tracker_in_meta_after" id="tracker_in_meta_after" value="<?php echo wp_iminr_get_option('tracker_in_meta_after'); ?>" size="25" />
179                         <br /><small><?php _e('This tags inclose iMinR javascript, useful with logo.', 'wp_iminr') ?></small>
180                     </td>
181                 </tr>
182                 <tr>
183                     <th nowrap valign="top"><label for="tracker_in_footer"><?php _e('Add in footer', 'wp_iminr') ?></label></th>
184                     <td valign="top">
185                         <input type="radio" name="tracker_in" id="tracker_in_footer" value="footer" <?php if (wp_iminr_get_option('tracker_in_footer')) echo 'checked="checked"'; ?> />
186                         <small><?php _e('By checking this radio button tracker will be included in footer.', 'wp_iminr') ?></small>
187                         <br />
188                         <input type="text" name="tracker_in_footer_before" id="tracker_in_footer_before" value="<?php echo wp_iminr_get_option('tracker_in_footer_before'); ?>" size="25" style="text-align: right;" />
189                         <img src="" alt="iMinR" />
190                         <input type="text" name="tracker_in_footer_after" id="tracker_in_footer_after" value="<?php echo wp_iminr_get_option('tracker_in_footer_after'); ?>" size="25" />
191                         <br /><small><?php _e('This tags inclose iMinR javascript, useful with logo.', 'wp_iminr') ?></small>
192                     </td>
193                 </tr>
194                 <tr>
195                     <th nowrap valign="top"><?php _e('Desable for roles', 'wp_iminr') ?></th>
196                     <td valign="top">
197                         <select name="desable_for_roles[]" id="desable_for_roles" multiple="multiple">
198                         <?php wp_iminr_roles_options_for_select(wp_iminr_get_option('desable_for_roles')); ?>
199                         </select>
200                         <br /><small><?php _e('Desable tracker for selected roles. Use CTRL+Click to select multiple entries', 'wp_iminr') ?></small>
201                         <?php
202                         ?>
203                     </td>
204                 </tr>
205             </table>
206        
207         </fieldset>
208        
209       <div class="submit">
210             <input type="submit" name="info_update" value="<?php _e('Update options', 'wp_iminr') ?>" />
211         </div></form>
212 </div><?php
213 }
214
215 // print roles in options for select
216 function wp_iminr_roles_options_for_select( $default = array() ) {
217     global $wp_roles;
218     if (!is_array($default)) {
219         $default = array($default);
220     }
221     
222     foreach( $wp_roles->role_names as $role => $name ) {
223         if (in_array($role, $default)) {
224             $selected = 'selected="selected"';
225         } else {
226             $selected = '';
227         }
228       echo "\n\t<option $selected value='$role'>$name</option>";
229     }
230 }
231
232 // Return true if tracker isn't desable for current user role
233 function wp_iminr_enable_for_user() {
234     foreach( wp_get_current_user()->roles as $role ) {
235         if (in_array($role, wp_iminr_get_option('desable_for_roles'))) {
236             return false;
237         }
238     }
239     return true;
240 }
241
242 // **************
243 // initialization
244
245 // load texts for localization
246 load_plugin_textdomain('wp_iminr','wp-content/plugins/wp_iminr/lang');
247
248 // admin menu
249 add_action('admin_menu', 'wp_iminr_admin');
250
251 // tracker inclusion
252 if ( wp_iminr_get_option('enable_tracker') && wp_iminr_get_option('tracker_in_meta') ) {
253     add_action('wp_meta', 'wp_iminr_meta');
254 } elseif ( wp_iminr_get_option('enable_tracker') && wp_iminr_get_option('tracker_in_footer') ) {
255     add_action('wp_footer', 'wp_iminr_footer');
256 }
257
258 ?>
Note: See TracBrowser for help on using the browser.