PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/seo-ultimate/modules/internal-link-aliases/internal-link-aliases.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 310 lines | 229 code | 77 blank | 4 comment | 47 complexity | 437eef29f72bd1630e18537116923720 MD5 | raw file
  1. <?php
  2. class SU_InternalLinkAliases extends SU_Module {
  3. function get_default_settings() {
  4. return array(
  5. 'alias_dir' => 'go'
  6. );
  7. }
  8. function init() {
  9. add_filter('su_custom_update_postmeta-aliases', array(&$this, 'save_post_aliases'), 10, 4);
  10. add_filter('the_content', array(&$this, 'apply_aliases'), 9); //Run before wp_texturize etc.
  11. add_action('template_redirect', array(&$this, 'redirect_aliases'), 0);
  12. add_action('do_robotstxt', array(&$this, 'block_aliases_dir'));
  13. add_action('su_do_robotstxt', array(&$this, 'block_aliases_dir'));
  14. add_filter('su_get_setting-internal-link-aliases-alias_dir', array(&$this, 'filter_alias_dir'));
  15. }
  16. function admin_page_init() {
  17. $this->jlsuggest_init();
  18. }
  19. function get_module_title() { return __('Link Mask Generator', 'seo-ultimate'); }
  20. function get_settings_key() { return 'internal-link-aliases'; }
  21. function get_admin_page_tabs() {
  22. return array(
  23. array('id' => 'aliases', 'title' => __('Aliases', 'seo-ultimate'), 'callback' => 'editor_tab')
  24. , array('id' => 'settings', 'title' => __('Settings', 'seo-ultimate'), 'callback' => 'settings_tab')
  25. );
  26. }
  27. function remove_empty_aliases($alias) {
  28. return !empty($alias['to']);
  29. }
  30. function editor_tab() {
  31. $aliases = $this->get_setting('aliases', array());
  32. $aliases = array_map('unserialize', array_unique(array_map('serialize', $aliases)));
  33. $aliases = array_filter($aliases, array(&$this, 'remove_empty_aliases'));
  34. $num_aliases = count($aliases);
  35. if ($this->is_action('update')) {
  36. $aliases = array();
  37. for ($i=0; $i <= $num_aliases; $i++) {
  38. $id = stripslashes($_POST["alias_{$i}_id"]);
  39. $from = stripslashes($_POST["alias_{$i}_from"]);
  40. $to = stripslashes($_POST["alias_{$i}_to"]);
  41. $jls_post = stripslashes($_POST["alias_{$i}_posts"]);
  42. if ($jls_post) {
  43. $jls_post = $this->jlsuggest_value_explode($jls_post);
  44. $posts = array($jls_post[2]);
  45. } else {
  46. $posts = array();
  47. }
  48. $delete = isset($_POST["alias_{$i}_delete"]) ? (intval($_POST["alias_{$i}_delete"]) == 1) : false;
  49. if (!$delete && $from && $to)
  50. $aliases[$id] = compact('from', 'to', 'posts');
  51. }
  52. $this->update_setting('aliases', $aliases);
  53. $num_aliases = count($aliases);
  54. }
  55. if ($num_aliases > 0) {
  56. $this->admin_subheader(__('Edit Existing Aliases', 'seo-ultimate'));
  57. $this->aliases_form(0, $aliases);
  58. }
  59. $this->admin_subheader(__('Add a New Alias', 'seo-ultimate'));
  60. $this->aliases_form($num_aliases, array(array()), false);
  61. }
  62. function aliases_form($start_id = 0, $aliases, $existing_item = true) {
  63. //Set headers
  64. $headers = array(
  65. 'alias-from' => __('Actual URL', 'seo-ultimate')
  66. , 'alias-to' => __('Alias URL', 'seo-ultimate')
  67. , 'alias-posts' => __('Only on This Post&hellip; (optional)', 'seo-ultimate')
  68. );
  69. if ($existing_item) $headers['alias-delete'] = __('Delete', 'seo-ultimate');
  70. //Begin table; output headers
  71. $this->admin_wftable_start($headers);
  72. //Cycle through links
  73. $i = $start_id;
  74. foreach ($aliases as $id => $alias) {
  75. if (!is_string($id)) $id = uniqid($i, true);
  76. if (!isset($alias['from'])) $alias['from'] = '';
  77. if (!isset($alias['to'])) $alias['to'] = '';
  78. $u_alias_to = urlencode($alias['to']);
  79. if (isset($alias['posts'][0]))
  80. $jlsuggest_value = 'obj_posttype_' . get_post_type($alias['posts'][0]) . '/' . $alias['posts'][0];
  81. else
  82. $jlsuggest_value = '';
  83. $alias_dir = $this->get_setting('alias_dir', 'go', null, true);
  84. $alias_url = get_bloginfo('url') . "/$alias_dir/$u_alias_to/";
  85. if ($existing_item)
  86. $test_link = "<td class='su-alias-to-test'>[<a href='$alias_url' target='_blank'>" . __('Test', 'seo-ultimate') . "</a>]</td>";
  87. $cells = array(
  88. 'alias-from' =>
  89. $this->get_input_element('hidden', "alias_{$i}_id", $id)
  90. . $this->get_input_element('textbox', "alias_{$i}_from", $alias['from'])
  91. , 'alias-to' => "
  92. <table><tr>
  93. <td class='su-alias-to-dir'>/$alias_dir/</td>
  94. <td class='su-alias-to-slug'>" . $this->get_input_element('textbox', "alias_{$i}_to", $alias['to']) . "</td>
  95. $test_link
  96. </tr></table>"
  97. , 'alias-posts' => $this->get_jlsuggest_box("alias_{$i}_posts", $jlsuggest_value, 'types=posttype')
  98. );
  99. if ($existing_item)
  100. $cells['alias-delete'] = $this->get_input_element('checkbox', "alias_{$i}_delete");
  101. $this->table_row($cells, $i, 'alias');
  102. $i++;
  103. }
  104. $this->admin_wftable_end();
  105. }
  106. function settings_tab() {
  107. $this->admin_form_table_start();
  108. $this->textbox('alias_dir', __('Alias Directory', 'seo-ultimate'), $this->get_default_setting('alias_dir'));
  109. if ($this->plugin->module_exists('link-nofollow'))
  110. $this->checkbox('nofollow_aliased_links', __('Nofollow aliased links', 'seo-ultimate'), __('Link Attributes', 'seo-ultimate'));
  111. $this->admin_form_table_end();
  112. }
  113. function filter_alias_dir($alias_dir) {
  114. return trim(sustr::preg_filter('a-zA-Z0-9_/', $alias_dir), '/');
  115. }
  116. function postmeta_fields($fields) {
  117. if (!current_user_can('manage_options'))
  118. return $fields;
  119. $post_id = suwp::get_post_id();
  120. $post = get_post($post_id);
  121. if (!$post) return $fields;
  122. $content = $post->post_content;
  123. $alias_dir = $this->get_setting('alias_dir', 'go');
  124. if ($content && preg_match_all('@ href=["\']([^#][^"\']+)["\']@', $content, $matches)) {
  125. $urls = array_unique($matches[1]);
  126. $html = "<tr valign='top'>\n<th scope='row' class='su'>".__('Link Masks:', 'seo-ultimate')."</th>\n<td>\n";
  127. $html .= "<table class='widefat'><thead>\n";
  128. $headers = array(__('URL', 'seo-ultimate'), '', __('Mask URL', 'seo-ultimate'));
  129. foreach ($headers as $header) $html .= "<th>$header</th>\n";
  130. $html .= "</thead>\n<tbody>";
  131. $aliases = $this->get_setting('aliases', array());
  132. $post_aliases = array();
  133. foreach ($aliases as $id => $alias) {
  134. if (empty($alias['posts']) || in_array($post->ID, $alias['posts']))
  135. $post_aliases[$alias['from']] = array('id' => $id, 'to' => $alias['to']);
  136. }
  137. foreach ($urls as $url) {
  138. $a_url = su_esc_attr($url);
  139. $un_h_url = htmlspecialchars_decode($url);
  140. $ht_url = esc_html(sustr::truncate($url, 30));
  141. if (isset($post_aliases[$url]))
  142. $url_key = $url;
  143. elseif (isset($post_aliases[$un_h_url]))
  144. $url_key = $un_h_url;
  145. else
  146. $url_key = false;
  147. $alias_to = '';
  148. $alias_id = uniqid('', true);
  149. if ($url_key) {
  150. if (isset($post_aliases[$url_key]['to'])) $alias_to = $post_aliases[$url_key]['to'];
  151. if (isset($post_aliases[$url_key]['id'])) $alias_id = $post_aliases[$url_key]['id'];
  152. }
  153. $a_alias_to = esc_attr($alias_to);
  154. $html .= "<tr><td><a href='$a_url' title='$a_url' target='_blank'>$ht_url</a><input type='hidden' name='_su_aliases[$alias_id][from]' value='$a_url' /></td>\n<td>&rArr;</td><td>/$alias_dir/<input type='text' name='_su_aliases[$alias_id][to]' value='$a_alias_to' /></td></tr>\n";
  155. }
  156. $html .= "</tbody>\n</table>\n";
  157. $html .= '<p><small>' . __('You can stop search engines from following a link by typing in a mask for its URL.', 'seo-ultimate') . "</small></p>\n";
  158. $html .= "</td>\n</tr>\n";
  159. $fields['links'][100]['aliases'] = $html;
  160. }
  161. return $fields;
  162. }
  163. function save_post_aliases($false, $saved_aliases, $metakey, $post) {
  164. if ($post->post_type == 'revision' || !is_array($saved_aliases)) return true;
  165. $aliases = $this->get_setting('aliases', array());
  166. foreach ($saved_aliases as $saved_id => $saved_data) {
  167. if (isset($aliases[$saved_id])) {
  168. if ($saved_data['to'])
  169. $aliases[$saved_id]['to'] = $saved_data['to'];
  170. else
  171. unset($aliases[$saved_id]);
  172. } elseif ($saved_data['to']) {
  173. $aliases[$saved_id]['from'] = $saved_data['from'];
  174. $aliases[$saved_id]['to'] = $saved_data['to'];
  175. $aliases[$saved_id]['posts'] = array($post->ID);
  176. }
  177. }
  178. $this->update_setting('aliases', $aliases);
  179. return true;
  180. }
  181. function apply_aliases($content) {
  182. return preg_replace_callback('@<a ([^>]*)href=(["\'])([^"\']+)(["\'])([^>]*)>@', array(&$this, 'apply_aliases_callback'), $content);
  183. }
  184. function apply_aliases_callback($matches) {
  185. $id = suwp::get_post_id();
  186. static $aliases = false;
  187. //Just in case we have duplicate aliases, make sure the most recent ones are applied first
  188. if ($aliases === false) $aliases = array_reverse($this->get_setting('aliases', array()), true);
  189. static $alias_dir = false;
  190. if ($alias_dir === false) $alias_dir = $this->get_setting('alias_dir', 'go');
  191. $new_url = $old_url = $matches[3];
  192. foreach ($aliases as $alias) {
  193. $to = urlencode($alias['to']);
  194. if ((empty($alias['posts']) || in_array($id, $alias['posts'])) && $to) {
  195. $from = $alias['from'];
  196. $h_from = esc_html($from);
  197. $to = get_bloginfo('url') . "/$alias_dir/$to/";
  198. if ($from == $old_url || $h_from == $old_url) {
  199. $new_url = $to;
  200. break;
  201. }
  202. }
  203. }
  204. $attrs = "{$matches[1]}href={$matches[2]}{$new_url}{$matches[4]}{$matches[5]}";
  205. if ($old_url != $new_url && $this->get_setting('nofollow_aliased_links', false) && $this->plugin->module_exists('link-nofollow'))
  206. $this->plugin->call_module_func('link-nofollow', 'nofollow_attributes_string', $attrs, $attrs);
  207. return "<a $attrs>";
  208. }
  209. function redirect_aliases() {
  210. $aliases = $this->get_setting('aliases', array());
  211. $alias_dir = $this->get_setting('alias_dir', 'go');
  212. foreach ($aliases as $alias)
  213. if ($to = $alias['to'])
  214. if (suurl::equal(suurl::current(), get_bloginfo('url') . "/$alias_dir/$to/"))
  215. wp_redirect($alias['from']);
  216. }
  217. function block_aliases_dir() {
  218. echo '# ';
  219. _e("Added by SEO Ultimate's Link Mask Generator module", 'seo-ultimate');
  220. echo "\n";
  221. $urlinfo = parse_url(get_bloginfo('url'));
  222. $path = $urlinfo['path'];
  223. echo "User-agent: *\n";
  224. $alias_dir = $this->get_setting('alias_dir', 'go');
  225. echo "Disallow: $path/$alias_dir/\n";
  226. echo '# ';
  227. _e('End Link Mask Generator output', 'seo-ultimate');
  228. echo "\n\n";
  229. }
  230. }