root/trunk/wp_iminr.php

Revision 68, 16.2 kB (checked in by yann, 5 years ago)

valid xhtml 1.0 strict

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.2
26 Author URI: http://www.sans-savoir.net
27 */
28
29 require_once(dirname(__FILE__) . '/lib/nusoap.php');
30
31 // print current tracker without other tags
32 function iMinR () {
33     wp_iminr_insert_tracker();
34 }
35
36 // print current tracker with meta tags
37 function wp_iminr_tracker_in_meta() {
38     wp_iminr_insert_tracker('meta');
39 }
40
41 // print current tracker with footer tags
42 function wp_iminr_tracker_in_footer() {
43     wp_iminr_insert_tracker('footer');
44 }
45
46 // insert tracker code or "desable" comment
47 function wp_iminr_insert_tracker($location='') {
48     global $wp_iminr_tracker_inserted;
49     
50     if (!$wp_iminr_tracker_inserted) {
51         $account_id = wp_iminr_get_option('account_id');
52         if ($account_id != '' && wp_iminr_enable_for_user()) {
53             echo "<!-- iMinR for WordPress is enable -->\n";
54             $wp_iminr_tracker_inserted = true;
55             
56             if (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
57                 $tracker = "<script type=\"text/javascript\" language=\"javascript\" src=\"http://tag.iminr.com/audit/tag.aspx?id=$account_id\"></script>";
58             } else {
59                 $tracker = "<script type=\"text/javascript\" language=\"javascript\" src=\"https://tags.iminr.com/audit/tag.aspx?id=$account_id\"></script>";
60             }
61             
62             switch ($location) {
63             case 'meta':
64                 echo wp_iminr_get_option('tracker_in_meta_before') . $tracker . wp_iminr_get_option('tracker_in_meta_after');
65                 break;
66             case 'footer':
67                 echo wp_iminr_get_option('tracker_in_footer_before') . $tracker . wp_iminr_get_option('tracker_in_footer_after');
68                 break;
69             default:
70                 echo $tracker;
71             }
72         } elseif ($account_id != '' && !wp_iminr_enable_for_user()) {
73             echo "<!-- iMinR for WordPress is desable for current user -->\n";
74         } else {
75             echo "<!-- iMinR for WordPress is desable because account id isn\'t set -->\n";
76         }
77     }
78 }
79
80 // insert variables in header
81 function wp_iminr_variables_in_header() {
82     global $wp_iminr_variables;
83     
84     $wp_iminr_variables['wp_comments'] = 1;
85     $wp_iminr_variables['wp_comment_posts'] = 'Mon article pour iMinR';
86     $wp_iminr_variables['wp_comment_authors'] = 'Yann';
87     
88     if (count($wp_iminr_variables)) {
89         echo "<!-- iMinR for WordPress -->\n";
90         echo '<script language="javascript">';
91         echo 'var iminr_cv = "";';
92         foreach ($wp_iminr_variables as $name => $value) {
93             echo "iminr_cv += \"$name=$value; \";";
94         }
95         echo '</script>';
96     }
97 }
98
99 // track new comment
100 function wp_iminr_comment($id) {
101     global $wp_iminr_variables;
102     
103     $wp_iminr_variables['wp_comments'] = 1;
104     $wp_iminr_variables['wp_comment_posts'] = 'Mon article pour iMinR';
105     $wp_iminr_variables['wp_comment_authors'] = 'Yann';
106 }
107
108 // track new trackback
109 function wp_iminr_trackback($id) {
110     global $wp_iminr_variables;
111 }
112
113 // track new pingback
114 function wp_iminr_pingback($id) {
115     wp_iminr_trackback($id);
116 }
117
118 // get option from the WordPress options database table or return the default value
119 function wp_iminr_get_option($option_name) {
120     // get options from the database
121     $wp_iminr_options = get_option('wp_iminr_options');
122     
123     // if no options in database or not this specific option
124     // create default options array
125     if (!$wp_iminr_options || !array_key_exists($option_name, $wp_iminr_options) ) {
126         $wp_iminr_default_options = array();
127         
128         // defaults values
129         $wp_iminr_default_options['wsap_uri'] = 'http://www.iminr.com/mining/wsMining.asmx?WSDL';
130         
131         $wp_iminr_default_options['account_id'] = '';
132         
133         $wp_iminr_default_options['enable_tracker'] = false;
134         $wp_iminr_default_options['tracker_in_meta'] = true;
135         $wp_iminr_default_options['tracker_in_meta_before']  = '<li>';
136         $wp_iminr_default_options['tracker_in_meta_after'] = '</li>';
137         $wp_iminr_default_options['tracker_in_footer'] = false;
138         $wp_iminr_default_options['tracker_in_footer_before'] = '<br />';
139         $wp_iminr_default_options['tracker_in_footer_after'] = '';
140         $wp_iminr_default_options['desable_for_roles'] = array('administrator');
141         
142         $wp_iminr_default_options['enable_wp_comments'] = false;
143         $wp_iminr_default_options['enable_wp_comment_posts'] = false;
144         $wp_iminr_default_options['enable_wp_comment_authors'] = false;
145         $wp_iminr_default_options['enable_wp_trackbacks'] = false;
146         $wp_iminr_default_options['enable_wp_trackback_posts'] = false;
147         $wp_iminr_default_options['enable_wp_trackback_origins'] = false;
148         
149         $wp_iminr_default_options['error_variables'] = array();
150         
151         // add default options to the database unless options already exist
152         add_option('wp_iminr_options', $wp_iminr_default_options, 'Settings for iMinR for WordPress plugin');
153         
154         // return default option
155         return $wp_iminr_default_options[$option_name];
156     } else {
157         // return option found in database
158         return $wp_iminr_options[$option_name];
159     }
160 }
161
162 // add options page to administration menu
163 function wp_iminr_admin() {
164     if (function_exists('add_options_page')) {
165         add_options_page(__('iMinR for WorldPress'), 'iMinR', 8, basename(__FILE__), 'wp_iminr_options');
166     }
167 }
168
169 // options page for administration
170 function wp_iminr_options() {
171     if (isset($_POST['info_update'])) {
172         ?><div class="updated"><p><strong><?php
173         // process submitted form
174         $wp_iminr_options = get_option('wp_iminr_options');
175         
176         $wp_iminr_options['account_id'] =  $_POST['account_id'];
177         
178         $wp_iminr_options['enable_tracker'] = ($_POST['enable_tracker'] == 'true' ? true : false);
179         $wp_iminr_options['tracker_in_meta'] = ($_POST['tracker_in'] == 'meta' ? true : false);
180         $wp_iminr_options['tracker_in_meta_before'] =  $_POST['tracker_in_meta_before'];
181         $wp_iminr_options['tracker_in_meta_after'] =  $_POST['tracker_in_meta_after'];
182         $wp_iminr_options['tracker_in_footer'] = ($_POST['tracker_in'] == 'footer' ? true : false);
183         $wp_iminr_options['tracker_in_footer_before'] =  $_POST['tracker_in_footer_before'];
184         $wp_iminr_options['tracker_in_footer_after'] =  $_POST['tracker_in_footer_after'];
185         $wp_iminr_options['desable_for_roles'] = ($_POST['desable_for_roles'] == NULL ? array() : $_POST['desable_for_roles']);
186         
187         $wp_iminr_options['enable_wp_comments'] = ($_POST['enable_wp_comments'] == 'true' ? true : false);
188         $wp_iminr_options['enable_wp_comment_posts'] = ($_POST['enable_wp_comment_posts'] == 'true' ? true : false);
189         $wp_iminr_options['enable_wp_comment_authors'] = ($_POST['enable_wp_comment_authors'] == 'true' ? true : false);
190         $wp_iminr_options['enable_wp_trackbacks'] = ($_POST['enable_wp_trackbacks'] == 'true' ? true : false);
191         $wp_iminr_options['enable_wp_trackback_posts'] = ($_POST['enable_wp_trackback_posts'] == 'true' ? true : false);
192         $wp_iminr_options['enable_wp_trackback_origins'] = ($_POST['enable_wp_trackback_origins'] == 'true' ? true : false);
193         
194         update_option('wp_iminr_options', $wp_iminr_options);
195         
196         _e('Options saved', 'wp_iminr')
197         ?></strong></p></div><?php
198     }
199     ?>
200 <div class="wrap">
201     <form action="" method="post">
202         <h2><?php _e('iMinR for WordPress'); ?></h2>
203         <fieldset class="options" id="general">
204         <legend><?php _e('General settings', 'wp_iminr') ?></legend>
205        
206         <table width="100%" cellspacing="2" cellpadding="5" class="editform">
207             <tr>
208                 <th valign="top" style="white-space: nowrap"><label for="account_id"><?php _e('Account ID', 'wp_iminr') ?></label></th>
209                 <td valign="top">
210                     <input type="text" name="account_id" id="account_id" value="<?php echo wp_iminr_get_option('account_id'); ?>" size="50" />
211                     <br /><small><?php _e('Enter your iMinR account ID.', 'wp_iminr') ?></small>
212                 </td>
213             </tr>
214         </table>
215        
216         </fieldset>
217         <fieldset class="options" id="integration">
218             <legend><?php _e('Tracker integration', 'wp_iminr') ?></legend>
219             <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>
220
221             <table width="100%" cellspacing="2" cellpadding="5" class="editform">
222                 <tr>
223                     <th valign="top" style="white-space: nowrap"><label for="enable_tracker"><?php _e('Enable tracker', 'wp_iminr') ?></label></th>
224                     <td valign="top" width="100%">
225                         <input type="checkbox" name="enable_tracker" id="enable_tracker" value="true" <?php if (wp_iminr_get_option('enable_tracker')) echo 'checked="checked"'; ?> />
226                         <br /><small><?php _e('By unchecking this checkbox tracker will be not included on the page.', 'wp_iminr') ?></small>
227                     </td>
228                 </tr>
229                 <tr>
230                     <th valign="top" style="white-space: nowrap"><label for="tracker_in_meta"><?php _e('Add in sidebar', 'wp_iminr') ?></label></th>
231                     <td valign="top" width="100%">
232                         <input type="radio" name="tracker_in" id="tracker_in_meta" value="meta" <?php if (wp_iminr_get_option('tracker_in_meta')) echo 'checked="checked"'; ?> />
233                         <small><?php _e('By checking this radio button tracker will be included in meta (if template implemente this option).', 'wp_iminr') ?></small>
234                         <br /><input type="text" name="tracker_in_meta_before" id="tracker_in_meta_before" value="<?php echo htmlspecialchars(wp_iminr_get_option('tracker_in_meta_before')); ?>" size="25" style="text-align: right;" />
235                         <img src="" alt="iMinR" />
236                         <input type="text" name="tracker_in_meta_after" id="tracker_in_meta_after" value="<?php echo htmlspecialchars(wp_iminr_get_option('tracker_in_meta_after')); ?>" size="25" />
237                         <br /><small><?php _e('This tags inclose iMinR javascript, useful with logo.', 'wp_iminr') ?></small>
238                     </td>
239                 </tr>
240                 <tr>
241                     <th valign="top" style="white-space: nowrap"><label for="tracker_in_footer"><?php _e('Add in footer', 'wp_iminr') ?></label></th>
242                     <td valign="top" width="100%">
243                         <input type="radio" name="tracker_in" id="tracker_in_footer" value="footer" <?php if (wp_iminr_get_option('tracker_in_footer')) echo 'checked="checked"'; ?> />
244                         <small><?php _e('By checking this radio button tracker will be included in footer.', 'wp_iminr') ?></small>
245                         <br />
246                         <input type="text" name="tracker_in_footer_before" id="tracker_in_footer_before" value="<?php echo htmlspecialchars(wp_iminr_get_option('tracker_in_footer_before')); ?>" size="25" style="text-align: right;" />
247                         <img src="" alt="iMinR" />
248                         <input type="text" name="tracker_in_footer_after" id="tracker_in_footer_after" value="<?php echo htmlspecialchars(wp_iminr_get_option('tracker_in_footer_after')); ?>" size="25" />
249                         <br /><small><?php _e('This tags inclose iMinR javascript, useful with logo.', 'wp_iminr') ?></small>
250                     </td>
251                 </tr>
252                 <tr>
253                     <th valign="top" style="white-space: nowrap"><?php _e('Desable for roles', 'wp_iminr') ?></th>
254                     <td valign="top" width="100%">
255                         <select name="desable_for_roles[]" id="desable_for_roles" multiple="multiple">
256                         <?php wp_iminr_roles_options_for_select(wp_iminr_get_option('desable_for_roles')); ?>
257                         </select>
258                         <br /><small><?php _e('Desable tracker for selected roles. Use CTRL+Click to select multiple entries', 'wp_iminr') ?></small>
259                     </td>
260             </table>
261        
262         </fieldset>
263         <fieldset class="options" id="variables">
264             <legend><?php _e('Enable statistics for comment', 'wp_iminr') ?></legend>
265             <p><?php _e('Create variables with iMinR interface before checking the checkbox.', 'wp_iminr') ?></p>
266            
267             <table width="100%" cellspacing="2" cellpadding="5" class="editform">
268                 <tr>
269                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_comments"><?php _e('wp_comments (integer)', 'wp_iminr') ?></label></th>
270                     <td valign="top" width="100%">
271                         <input type="checkbox" name="enable_wp_comments" id="enable_wp_comments" value="true" <?php if (wp_iminr_get_option('enable_wp_comments')) echo 'checked="checked"'; ?> />
272                         <br /><small><?php _e('Comment count', 'wp_iminr') ?></small>
273                     </td>
274                 </tr>
275                 <tr>
276                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_comment_posts"><?php _e('wp_comment_posts (text)', 'wp_iminr') ?></label></th>
277                     <td valign="top" width="100%">
278                         <input type="checkbox" name="enable_wp_comment_posts" id="enable_wp_comment_posts" value="true" <?php if (wp_iminr_get_option('enable_wp_comment_posts')) echo 'checked="checked"'; ?> />
279                         <br /><small><?php _e('Comment per post', 'wp_iminr') ?></small>
280                     </td>
281                 </tr>
282                 <tr>
283                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_comment_authors"><?php _e('wp_comment_authors (text)', 'wp_iminr') ?></label></th>
284                     <td valign="top" width="100%">
285                         <input type="checkbox" name="enable_wp_comment_authors" id="enable_wp_comment_authors" value="true" <?php if (wp_iminr_get_option('enable_wp_comment_authors')) echo 'checked="checked"'; ?> />
286                         <br /><small><?php _e('Comment pet author', 'wp_iminr') ?></small>
287                     </td>
288                 </tr>
289                 <tr>
290                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_trackbacks"><?php _e('wp_trackbacks (integer)', 'wp_iminr') ?></label></th>
291                     <td valign="top" width="100%">
292                         <input type="checkbox" name="enable_wp_trackbacks" id="enable_wp_trackbacks" value="true" <?php if (wp_iminr_get_option('enable_wp_trackbacks')) echo 'checked="checked"'; ?> />
293                         <br /><small><?php _e('Trackback/Pingback count', 'wp_iminr') ?></small>
294                     </td>
295                 </tr>
296                 <tr>
297                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_trackback_posts"><?php _e('wp_trackback_posts (text)', 'wp_iminr') ?></label></th>
298                     <td valign="top" width="100%">
299                         <input type="checkbox" name="enable_wp_trackback_posts" id="enable_wp_trackback_posts" value="true" <?php if (wp_iminr_get_option('enable_wp_trackback_posts')) echo 'checked="checked"'; ?> />
300                         <br /><small><?php _e('Trackback/Pingback per post', 'wp_iminr') ?></small>
301                     </td>
302                 </tr>
303                 <tr>
304                     <th valign="top" style="white-space: nowrap"><label for="enable_wp_trackback_origins"><?php _e('wp_trackback_origins (text)', 'wp_iminr') ?></label></th>
305                     <td valign="top" width="100%">
306                         <input type="checkbox" name="enable_wp_trackback_origins" id="enable_wp_trackback_origins" value="true" <?php if (wp_iminr_get_option('enable_wp_trackback_origins')) echo 'checked="checked"'; ?> />
307                         <br /><small><?php _e('Trackback/Pingback per domain', 'wp_iminr') ?></small>
308                     </td>
309                 </tr>
310             </table>
311        
312         </fieldset>
313        
314         <div class="submit">
315             <input type="submit" name="info_update" value="<?php _e('Update options', 'wp_iminr') ?>" />
316         </div></form>
317 </div><?php
318 }
319
320 // print roles in options for select
321 function wp_iminr_roles_options_for_select( $default = array() ) {
322     global $wp_roles;
323     if (!is_array($default)) {
324         $default = array($default);
325     }
326     
327     foreach( $wp_roles->role_names as $role => $name ) {
328         if (in_array($role, $default)) {
329             $selected = 'selected="selected"';
330         } else {
331             $selected = '';
332         }
333       echo "\n\t<option $selected value='$role'>$name</option>";
334     }
335 }
336
337 // Return true if tracker isn't desable for current user role
338 function wp_iminr_enable_for_user() {
339     $user = wp_get_current_user();
340     foreach( $user->roles as $role ) {
341         if (in_array($role, wp_iminr_get_option('desable_for_roles'))) {
342             return false;
343         }
344     }
345     return true;
346 }
347
348 // **************
349 // initialization
350 $wp_iminr_variables = array();
351
352 // load texts for localization
353 load_plugin_textdomain('wp_iminr','wp-content/plugins/wp_iminr/lang');
354
355 // admin menu
356 add_action('admin_menu', 'wp_iminr_admin');
357
358 // tracker inclusion
359 if ( wp_iminr_get_option('enable_tracker') && wp_iminr_get_option('tracker_in_meta') ) {
360     add_action('wp_meta', 'wp_iminr_tracker_in_meta');
361 } elseif ( wp_iminr_get_option('enable_tracker') && wp_iminr_get_option('tracker_in_footer') ) {
362     add_action('wp_footer', 'wp_iminr_tracker_in_footer');
363 }
364 add_action('wp_head', 'wp_iminr_variables_in_header');
365
366 // WP tracking
367 add_action('comment_post', 'wp_iminr_comment');
368 add_action('trackback_post', 'wp_iminr_trackback');
369 add_action('pingback_post', 'wp_iminr_pingback');
370
371 ?>
Note: See TracBrowser for help on using the browser.