| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: RB Internal Links |
|---|
| 4 | Version: 0.21 |
|---|
| 5 | Plugin URI: http://www.blograndom.com/blog/extras/ |
|---|
| 6 | Author: Cohen |
|---|
| 7 | Author URI: http://www.blograndom.com/ |
|---|
| 8 | Description: Use wiki style tags to link to internal posts and pages within your blog without hardcoding them. |
|---|
| 9 | |
|---|
| 10 | Installation: See readme. |
|---|
| 11 | |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | $rbinternal_version = '0.21'; |
|---|
| 15 | |
|---|
| 16 | // set up important actions and filters |
|---|
| 17 | add_filter('the_content', 'rbinternal_parse_content', 1); |
|---|
| 18 | add_filter('the_content_rss', 'rbinternal_parse_content', 1); |
|---|
| 19 | add_filter('the_excerpt', 'rbinternal_parse_content', 1); |
|---|
| 20 | add_action('init', 'rbinternal_init'); |
|---|
| 21 | add_action('admin_head', 'rbinternal_admin_header', 10); |
|---|
| 22 | add_action('admin_menu', 'rbinternal_add_pages'); |
|---|
| 23 | add_filter('tiny_mce_version', 'rbinternal_refresh_mce'); // update tinymce cache |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | $rbinternal_url = get_settings('siteurl').'/wp-content/plugins/rb-internal-links/'; |
|---|
| 27 | |
|---|
| 28 | function rbinternal_init(){ |
|---|
| 29 | |
|---|
| 30 | global $rbinternal_version; |
|---|
| 31 | $current_version = get_option('rbinternal_version'); |
|---|
| 32 | |
|---|
| 33 | if($rbinternal_version != $current_version){ |
|---|
| 34 | rbinternal_update($rbinternal_version, $current_version); |
|---|
| 35 | update_option('rbinternal_version', $rbinternal_version); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | rbinternal_addbuttons(); |
|---|
| 40 | |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function rbinternal_update($version, $current_version){ |
|---|
| 44 | |
|---|
| 45 | // set some default options |
|---|
| 46 | add_option('rbinternal_tinymce', 1); |
|---|
| 47 | add_option('rbinternal_post_orderby', 'post_date'); |
|---|
| 48 | add_option('rbinternal_post_sort', 'DESC'); |
|---|
| 49 | add_option('rbinternal_page_orderby', 'post_title'); |
|---|
| 50 | add_option('rbinternal_page_sort', 'ASC'); |
|---|
| 51 | add_option('rbinternal_return_par am', 'ID'); |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | function rbinternal_refresh_mce($ver) { |
|---|
| 56 | $ver += 36; |
|---|
| 57 | return $ver; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | // this function gets the url based on post id or slug |
|---|
| 61 | function rbinternal_get_url($id, &$text, $type) { |
|---|
| 62 | global $wpdb; |
|---|
| 63 | |
|---|
| 64 | if(empty($id)) return false; |
|---|
| 65 | |
|---|
| 66 | switch($type){ |
|---|
| 67 | case 'post': |
|---|
| 68 | $field = (is_numeric($id))? 'ID' : 'post_name'; |
|---|
| 69 | $post = $wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE $field = '$id'"); |
|---|
| 70 | if(empty($post->post_title)) return false; |
|---|
| 71 | elseif(empty($text)) $text = $post->post_title; |
|---|
| 72 | |
|---|
| 73 | return get_permalink($post->ID); |
|---|
| 74 | case 'category': |
|---|
| 75 | return get_category_link($id); |
|---|
| 76 | default: |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // posts and content get sent to this function which will look for our bbcode |
|---|
| 83 | function rbinternal_parse_content($content) { |
|---|
| 84 | |
|---|
| 85 | if(strpos($content, "{{post") !== FALSE OR strpos($content, "<!--intlink") !== FALSE) |
|---|
| 86 | $content = preg_replace("/({{|<!--)(post|intlink)([^}}].*?|[^-->].*?)(}}|-->)/ei", "rbinternal_parse_params('\\2', '\\3')", $content); |
|---|
| 87 | // {{ * }} for backwards compatibility |
|---|
| 88 | |
|---|
| 89 | return $content; |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | function rbinternal_parse_params($verb, $paramStr){ |
|---|
| 94 | |
|---|
| 95 | $paramStr = stripslashes($paramStr); |
|---|
| 96 | $paramStr = str_replace('"', '"', $paramStr); |
|---|
| 97 | $paramStr = str_replace("”", '"', $paramStr); |
|---|
| 98 | |
|---|
| 99 | preg_match_all("/(\w+)\=\"([^\"].*?)\"/i", $paramStr, $matches); |
|---|
| 100 | |
|---|
| 101 | if(is_array($matches[1]) AND is_array($matches[2])) |
|---|
| 102 | foreach($matches[1] AS $i=>$key) |
|---|
| 103 | $params[$key] = isset($matches[2][$i])? $matches[2][$i] : false; |
|---|
| 104 | |
|---|
| 105 | return rbinternal_render_content($verb, $params); |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | function rbinternal_render_content($verb, $params){ |
|---|
| 110 | |
|---|
| 111 | if(!isset($params['type'])) $params['type'] = 'post'; |
|---|
| 112 | |
|---|
| 113 | switch($verb){ |
|---|
| 114 | case 'intlink': |
|---|
| 115 | case 'post': //backwards compatibility |
|---|
| 116 | if(!isset($params['id'])) return false; |
|---|
| 117 | if(!isset($params['text'])) $params['text'] = 0; |
|---|
| 118 | $html = '<a href="'. rbinternal_get_url($params['id'], $params['text'], $params['type']) . ((isset($params['anchor']))? '#' . $params['anchor'] : '') . '" '; |
|---|
| 119 | if(isset($params['class'])) $html .= 'class="'. $params['class'] . '" '; |
|---|
| 120 | if(isset($params['target'])) $html .= 'target="'. $params['target'] . '" '; |
|---|
| 121 | $html .= '>'. $params['text'] .'</a>'; |
|---|
| 122 | |
|---|
| 123 | return $html; |
|---|
| 124 | default: |
|---|
| 125 | return '[rbinternal code not found]'; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | // tinyMCE functions |
|---|
| 131 | function rbinternal_addbuttons() { |
|---|
| 132 | global $wp_db_version; |
|---|
| 133 | // Check for WordPress 2.5+ and that its turned on |
|---|
| 134 | if($wp_db_version >= 7098 AND get_option('rbinternal_tinymce') == 1){ |
|---|
| 135 | // Don't bother doing this stuff if the current user lacks permissions |
|---|
| 136 | if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) |
|---|
| 137 | return; |
|---|
| 138 | |
|---|
| 139 | // Add only in Rich Editor mode |
|---|
| 140 | if ( get_user_option('rich_editing') == 'true') { |
|---|
| 141 | add_filter("mce_external_plugins", "rbinternal_external_plugins_25"); |
|---|
| 142 | add_filter('mce_buttons', 'rbinternal_mce_buttons'); |
|---|
| 143 | add_filter("mce_css", "rbinternal_mce_css"); |
|---|
| 144 | } |
|---|
| 145 | // Check for WordPress 2.1+ and that its turned on |
|---|
| 146 | }elseif(3664 <= $wp_db_version AND get_option('rbinternal_tinymce') == 1){ |
|---|
| 147 | if ('true' == get_user_option('rich_editing')) { |
|---|
| 148 | add_filter("mce_plugins", "rbinternal_mce_plugins", 10); |
|---|
| 149 | add_filter("mce_buttons", "rbinternal_mce_buttons", 10); |
|---|
| 150 | add_action('tinymce_before_init','rbinternal_external_plugins'); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | // pre v2.5 tinymce plugin load |
|---|
| 155 | function rbinternal_mce_plugins($plugins) { |
|---|
| 156 | array_push($plugins, "-rbinternallinks"); |
|---|
| 157 | return $plugins; |
|---|
| 158 | } |
|---|
| 159 | // pre 2.5 plugin load |
|---|
| 160 | function rbinternal_external_plugins() { |
|---|
| 161 | global $rbinternal_url; |
|---|
| 162 | echo 'tinyMCE.loadPlugin("rbinternallinks", "'.$rbinternal_url.'tmce/rb-internal-links/");' . "\n"; |
|---|
| 163 | return; |
|---|
| 164 | } |
|---|
| 165 | // Load the TinyMCE plugin : editor_plugin.js (wp2.5) |
|---|
| 166 | function rbinternal_external_plugins_25($plugin_array) { |
|---|
| 167 | global $rbinternal_url; |
|---|
| 168 | $plugin_array['rbinternallinks'] = $rbinternal_url.'tmce/rb-internal-links/editor_plugin_25.js'; |
|---|
| 169 | return $plugin_array; |
|---|
| 170 | } |
|---|
| 171 | // 2.5 + pre 2.5 button load |
|---|
| 172 | function rbinternal_mce_buttons($buttons) { |
|---|
| 173 | array_push($buttons, "separator", "rbinternallinks"); |
|---|
| 174 | return $buttons; |
|---|
| 175 | } |
|---|
| 176 | // 2.5 + mce css load |
|---|
| 177 | function rbinternal_mce_css($css){ |
|---|
| 178 | global $rbinternal_url; |
|---|
| 179 | return $css . ',' . $rbinternal_url.'styles.css'; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | function rbinternal_admin_header(){ |
|---|
| 185 | global $rbinternal_url; |
|---|
| 186 | echo '<script language="JavaScript" type="text/javascript">' . "\n" . '// <![CDATA[ ' . "\n"; |
|---|
| 187 | echo 'var rbinternal_url="'. $rbinternal_url .'";' . "\n"; |
|---|
| 188 | echo '// ]]>' . "\n" . '</script>'; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | function rbinternal_add_pages(){ |
|---|
| 192 | add_options_page('RB Internal Links', 'RB Internal Links', 8, __FILE__, 'rbinternal_admin_page'); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | function rbinternal_admin_page(){ |
|---|
| 196 | |
|---|
| 197 | if( $_POST['rbinternal_submit'] == 'Y' ) { |
|---|
| 198 | // Read their posted value |
|---|
| 199 | $rbinternal_tinymce = isset($_REQUEST['rbinternal_tinymce'])? $_REQUEST['rbinternal_tinymce'] : 0; |
|---|
| 200 | $rbinternal_post_orderby = $_REQUEST['rbinternal_post_orderby']; |
|---|
| 201 | $rbinternal_post_sort = $_REQUEST['rbinternal_post_sort']; |
|---|
| 202 | $rbinternal_page_orderby = $_REQUEST['rbinternal_page_orderby']; |
|---|
| 203 | $rbinternal_page_sort = $_REQUEST['rbinternal_page_sort']; |
|---|
| 204 | $rbinternal_return_param = $_REQUEST['rbinternal_return_param']; |
|---|
| 205 | |
|---|
| 206 | // Save the posted value in the database |
|---|
| 207 | update_option('rbinternal_tinymce', $rbinternal_tinymce); |
|---|
| 208 | update_option('rbinternal_post_orderby', $rbinternal_post_orderby); |
|---|
| 209 | update_option('rbinternal_post_sort', $rbinternal_post_sort); |
|---|
| 210 | update_option('rbinternal_page_orderby', $rbinternal_page_orderby); |
|---|
| 211 | update_option('rbinternal_page_sort', $rbinternal_page_sort); |
|---|
| 212 | update_option('rbinternal_return_param', $rbinternal_return_param); |
|---|
| 213 | |
|---|
| 214 | // Put an options updated message on the screen |
|---|
| 215 | ?> |
|---|
| 216 | <div class="updated"><p><strong>Settings updated.</strong></p></div> |
|---|
| 217 | <?php |
|---|
| 218 | }else{ |
|---|
| 219 | $rbinternal_tinymce = get_option('rbinternal_tinymce'); |
|---|
| 220 | $rbinternal_post_orderby = get_option('rbinternal_post_orderby'); |
|---|
| 221 | $rbinternal_post_sort = get_option('rbinternal_post_sort'); |
|---|
| 222 | $rbinternal_page_orderby = get_option('rbinternal_page_orderby'); |
|---|
| 223 | $rbinternal_page_sort = get_option('rbinternal_page_sort'); |
|---|
| 224 | $rbinternal_return_param = get_option('rbinternal_return_param'); |
|---|
| 225 | } |
|---|
| 226 | ?> |
|---|
| 227 | |
|---|
| 228 | <div class="wrap"> |
|---|
| 229 | <h2>RB Internal Link Settings</h2> |
|---|
| 230 | <form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>"> |
|---|
| 231 | <input type="hidden" name="rbinternal_submit" value="Y"> |
|---|
| 232 | |
|---|
| 233 | <h3>General</h3> |
|---|
| 234 | <ul> |
|---|
| 235 | <li><input type="checkbox" name="rbinternal_tinymce" value="1" <?php if($rbinternal_tinymce == 1) echo 'checked="checked"'; ?>> Enable wysiwyg editor plugin.</li> |
|---|
| 236 | <li> |
|---|
| 237 | Return the |
|---|
| 238 | <select name="rbinternal_return_param"> |
|---|
| 239 | <option value="ID" <?php if($rbinternal_return_param == 'ID') echo 'selected="selected"'; ?>>article ID</option> |
|---|
| 240 | <option value="slug" <?php if($rbinternal_return_param == 'slug') echo 'selected="selected"'; ?>>article slug</option> |
|---|
| 241 | </select> |
|---|
| 242 | to the editor |
|---|
| 243 | </li> |
|---|
| 244 | <li> |
|---|
| 245 | Order the list of posts by |
|---|
| 246 | <select name="rbinternal_post_orderby"> |
|---|
| 247 | <option value="post_date" <?php if($rbinternal_post_orderby == 'post_date') echo 'selected="selected"'; ?>>Post Date</option> |
|---|
| 248 | <option value="post_title" <?php if($rbinternal_post_orderby == 'post_title') echo 'selected="selected"'; ?>>Post Title</option> |
|---|
| 249 | <option value="post_modified" <?php if($rbinternal_post_orderby == 'post_modified') echo 'selected="selected"'; ?>>Modified Date</option> |
|---|
| 250 | <option value="ID" <?php if($rbinternal_post_orderby == 'ID') echo 'selected="selected"'; ?>>Post ID</option> |
|---|
| 251 | <option value="post_author" <?php if($rbinternal_post_orderby == 'post_author') echo 'selected="selected"'; ?>>Post Author</option> |
|---|
| 252 | <option value="post_name" <?php if($rbinternal_post_orderby == 'post_name') echo 'selected="selected"'; ?>>Post Slug</option> |
|---|
| 253 | </select> |
|---|
| 254 | and sort |
|---|
| 255 | <select name="rbinternal_post_sort"> |
|---|
| 256 | <option value="ASC" <?php if($rbinternal_post_sort == 'ASC') echo 'selected="selected"'; ?>>Ascending</option> |
|---|
| 257 | <option value="DESC" <?php if($rbinternal_post_sort == 'DESC') echo 'selected="selected"'; ?>>Descending</option> |
|---|
| 258 | </select> |
|---|
| 259 | </li> |
|---|
| 260 | <li> |
|---|
| 261 | Order the list of pages by |
|---|
| 262 | <select name="rbinternal_page_orderby"> |
|---|
| 263 | <option value="post_date" <?php if($rbinternal_page_orderby == 'post_date') echo 'selected="selected"'; ?>>Post Date</option> |
|---|
| 264 | <option value="post_title" <?php if($rbinternal_page_orderby == 'post_title') echo 'selected="selected"'; ?>>Post Title</option> |
|---|
| 265 | <option value="post_modified" <?php if($rbinternal_page_orderby == 'post_modified') echo 'selected="selected"'; ?>>Modified Date</option> |
|---|
| 266 | <option value="ID" <?php if($rbinternal_page_orderby == 'ID') echo 'selected="selected"'; ?>>Post ID</option> |
|---|
| 267 | <option value="post_author" <?php if($rbinternal_page_orderby == 'post_author') echo 'selected="selected"'; ?>>Post Author</option> |
|---|
| 268 | <option value="post_name" <?php if($rbinternal_page_orderby == 'post_name') echo 'selected="selected"'; ?>>Post Slug</option> |
|---|
| 269 | </select> |
|---|
| 270 | and sort |
|---|
| 271 | <select name="rbinternal_page_sort"> |
|---|
| 272 | <option value="ASC" <?php if($rbinternal_page_sort == 'ASC') echo 'selected="selected"'; ?>>Ascending</option> |
|---|
| 273 | <option value="DESC" <?php if($rbinternal_page_sort == 'DESC') echo 'selected="selected"'; ?>>Descending</option> |
|---|
| 274 | </select> |
|---|
| 275 | </li> |
|---|
| 276 | </ul> |
|---|
| 277 | |
|---|
| 278 | <p class="submit"> |
|---|
| 279 | <input type="submit" name="Submit" value="Update Options" /> |
|---|
| 280 | </p> |
|---|
| 281 | |
|---|
| 282 | <h2>Debug:</h2> |
|---|
| 283 | <p>DB Version: <?php global $wp_db_version; echo $wp_db_version; ?></p> |
|---|
| 284 | <p>Rich Editing Enabled: <?php if ('true' == get_user_option('rich_editing')) echo 'Yes'; else echo 'No'; ?></p> |
|---|
| 285 | <p>Plugin wysiwyg enabled: <?php if (get_option('rbinternal_tinymce') == 1) echo 'Yes'; else echo 'No'; ?></p> |
|---|
| 286 | |
|---|
| 287 | </form> |
|---|
| 288 | </div> |
|---|
| 289 | <?php |
|---|
| 290 | } |
|---|
| 291 | ?> |
|---|