PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Gashler/sg
PHP | 374 lines | 280 code | 90 blank | 4 comment | 52 complexity | 98ecaef26e9287917deee6cb634950b2 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('su_get_setting-internal-link-aliases-alias_dir', array(&$this, 'filter_alias_dir'));
  11. if (suwp::permalink_mode() == SUWP_PRETTY_PERMALINKS) {
  12. add_filter('the_content', array(&$this, 'apply_aliases'), 9); //Run before wp_texturize etc.
  13. add_action('template_redirect', array(&$this, 'redirect_aliases'), 0);
  14. add_action('do_robotstxt', array(&$this, 'block_aliases_dir'));
  15. add_action('su_do_robotstxt', array(&$this, 'block_aliases_dir'));
  16. }
  17. }
  18. function admin_page_init() {
  19. $this->jlsuggest_init();
  20. }
  21. static function get_module_title() { return __('Link Mask Generator', 'seo-ultimate'); }
  22. static function get_menu_title() { return __('Link Mask Generator', 'seo-ultimate'); }
  23. function get_settings_key() { return 'internal-link-aliases'; }
  24. function admin_page_contents() {
  25. if (suwp::permalink_mode() != SUWP_PRETTY_PERMALINKS)
  26. $this->print_message('error', sprintf(__('Link Mask Generator won&#8217;t work with default or &#8220;pathinfo&#8221; permalinks. Please change your <a href="%s">permalink structure</a> to enable this module&#8217;s functionality.', 'seo-ultimate'), 'options-permalink.php'));
  27. $this->children_admin_page_tabs_form();
  28. }
  29. function get_admin_page_tabs() {
  30. return array(
  31. array('id' => 'aliases', 'title' => __('Aliases', 'seo-ultimate'), 'callback' => 'editor_tab')
  32. , array('id' => 'settings', 'title' => __('Settings', 'seo-ultimate'), 'callback' => 'settings_tab')
  33. );
  34. }
  35. function remove_empty_aliases($alias) {
  36. return !empty($alias['to']);
  37. }
  38. function editor_tab() {
  39. $aliases = $this->get_setting('aliases', array());
  40. $aliases = array_map('unserialize', array_unique(array_map('serialize', $aliases)));
  41. $aliases = array_filter($aliases, array(&$this, 'remove_empty_aliases'));
  42. $num_aliases = count($aliases);
  43. if ($this->is_action('update')) {
  44. $aliases = array();
  45. for ($i=0; $i <= $num_aliases; $i++) {
  46. $id = stripslashes($_POST["alias_{$i}_id"]);
  47. $from = stripslashes($_POST["alias_{$i}_from"]);
  48. $to = stripslashes($_POST["alias_{$i}_to"]);
  49. $jls_post = stripslashes($_POST["alias_{$i}_posts"]);
  50. if ($jls_post) {
  51. $jls_post = $this->jlsuggest_value_explode($jls_post);
  52. $posts = array($jls_post[2]);
  53. } else {
  54. $posts = array();
  55. }
  56. $delete = isset($_POST["alias_{$i}_delete"]) ? (intval($_POST["alias_{$i}_delete"]) == 1) : false;
  57. if (!$delete && $from && $to)
  58. $aliases[$id] = compact('from', 'to', 'posts');
  59. }
  60. $this->update_setting('aliases', $aliases);
  61. $num_aliases = count($aliases);
  62. }
  63. if ($num_aliases > 0) {
  64. $this->admin_subheader(__('Edit Existing Aliases', 'seo-ultimate'));
  65. $this->aliases_form(0, $aliases);
  66. }
  67. $this->admin_subheader(__('Add a New Alias', 'seo-ultimate'));
  68. $this->aliases_form($num_aliases, array(array()), false);
  69. }
  70. function aliases_form($start_id = 0, $aliases, $existing_item = true) {
  71. //Set headers
  72. $headers = array(
  73. 'alias-from' => __('Actual URL', 'seo-ultimate')
  74. , 'alias-to' => __('Alias URL', 'seo-ultimate')
  75. , 'alias-posts' => __('Only on This Post&hellip; <em>(optional)</em>', 'seo-ultimate')
  76. );
  77. if ($existing_item) $headers['alias-delete'] = __('Delete', 'seo-ultimate');
  78. //Begin table; output headers
  79. $this->admin_wftable_start($headers);
  80. //Cycle through links
  81. $i = $start_id;
  82. foreach ($aliases as $id => $alias) {
  83. if (!is_string($id)) $id = uniqid($i, true);
  84. if (!isset($alias['from'])) $alias['from'] = '';
  85. if (!isset($alias['to'])) $alias['to'] = '';
  86. $u_alias_to = urlencode($alias['to']);
  87. if (isset($alias['posts'][0]))
  88. $jlsuggest_value = 'obj_posttype_' . get_post_type($alias['posts'][0]) . '/' . $alias['posts'][0];
  89. else
  90. $jlsuggest_value = '';
  91. $alias_dir = $this->get_setting('alias_dir', 'go', null, true);
  92. $alias_url = get_bloginfo('url') . "/$alias_dir/$u_alias_to/";
  93. $test_link = $existing_item ? "<td class='su-alias-to-test'>[<a href='$alias_url' target='_blank'>" . __('Test', 'seo-ultimate') . "</a>]</td>" : '';
  94. $cells = array(
  95. 'alias-from' =>
  96. $this->get_input_element('hidden', "alias_{$i}_id", $id)
  97. . $this->get_input_element('textbox', "alias_{$i}_from", $alias['from'])
  98. , 'alias-to' => "
  99. <table><tr>
  100. <td class='su-alias-to-dir'>/$alias_dir/</td>
  101. <td class='su-alias-to-slug'>" . $this->get_input_element('textbox', "alias_{$i}_to", $alias['to']) . "</td>
  102. $test_link
  103. </tr></table>"
  104. , 'alias-posts' => $this->get_jlsuggest_box("alias_{$i}_posts", $jlsuggest_value, 'types=posttype')
  105. );
  106. if ($existing_item)
  107. $cells['alias-delete'] = $this->get_input_element('checkbox', "alias_{$i}_delete");
  108. $this->table_row($cells, $i, 'alias');
  109. $i++;
  110. }
  111. $this->admin_wftable_end();
  112. }
  113. function settings_tab() {
  114. $this->admin_form_table_start();
  115. $this->textbox('alias_dir', __('Alias Directory', 'seo-ultimate'), $this->get_default_setting('alias_dir'));
  116. if ($this->plugin->module_exists('link-nofollow'))
  117. $this->checkbox('nofollow_aliased_links', __('Nofollow masked links', 'seo-ultimate'), __('Link Attributes', 'seo-ultimate'));
  118. $this->admin_form_table_end();
  119. }
  120. function filter_alias_dir($alias_dir) {
  121. return trim(sustr::preg_filter('a-zA-Z0-9_/', $alias_dir), '/');
  122. }
  123. function postmeta_fields($fields, $screen) {
  124. if (!current_user_can('manage_options'))
  125. return $fields;
  126. $post_id = suwp::get_post_id();
  127. $post = get_post($post_id);
  128. if (!$post) return $fields;
  129. $content = $post->post_content;
  130. $alias_dir = $this->get_setting('alias_dir', 'go');
  131. if ($content && preg_match_all('@ href=["\']([^#][^"\']+)["\']@', $content, $matches)) {
  132. $urls = array_unique($matches[1]);
  133. $html = "<tr valign='top'>\n<th scope='row' class='su'>".__('Link Masks:', 'seo-ultimate')."</th>\n<td>\n";
  134. $html .= "<table class='widefat'><thead>\n";
  135. $headers = array(__('URL', 'seo-ultimate'), '', __('Mask URL', 'seo-ultimate'));
  136. foreach ($headers as $header) $html .= "<th>$header</th>\n";
  137. $html .= "</thead>\n<tbody>";
  138. $aliases = $this->get_setting('aliases', array());
  139. $post_aliases = array();
  140. foreach ($aliases as $id => $alias) {
  141. if (empty($alias['posts']) || in_array($post->ID, $alias['posts']))
  142. $post_aliases[$alias['from']] = array('id' => $id, 'to' => $alias['to']);
  143. }
  144. foreach ($urls as $url) {
  145. $a_url = su_esc_attr($url);
  146. $un_h_url = htmlspecialchars_decode($url);
  147. $ht_url = esc_html(sustr::truncate($url, 30));
  148. if (isset($post_aliases[$url]))
  149. $url_key = $url;
  150. elseif (isset($post_aliases[$un_h_url]))
  151. $url_key = $un_h_url;
  152. else
  153. $url_key = false;
  154. $alias_to = '';
  155. $alias_id = uniqid('', true);
  156. if ($url_key) {
  157. if (isset($post_aliases[$url_key]['to'])) $alias_to = $post_aliases[$url_key]['to'];
  158. if (isset($post_aliases[$url_key]['id'])) $alias_id = $post_aliases[$url_key]['id'];
  159. }
  160. $a_alias_to = esc_attr($alias_to);
  161. $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";
  162. }
  163. $html .= "</tbody>\n</table>\n";
  164. $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";
  165. $html .= "</td>\n</tr>\n";
  166. $fields['links'][100]['aliases'] = $html;
  167. }
  168. return $fields;
  169. }
  170. function save_post_aliases($false, $saved_aliases, $metakey, $post) {
  171. if ($post->post_type == 'revision' || !is_array($saved_aliases)) return true;
  172. $aliases = $this->get_setting('aliases', array());
  173. foreach ($saved_aliases as $saved_id => $saved_data) {
  174. if (isset($aliases[$saved_id])) {
  175. if ($saved_data['to'])
  176. $aliases[$saved_id]['to'] = $saved_data['to'];
  177. else
  178. unset($aliases[$saved_id]);
  179. } elseif ($saved_data['to']) {
  180. $aliases[$saved_id]['from'] = $saved_data['from'];
  181. $aliases[$saved_id]['to'] = $saved_data['to'];
  182. $aliases[$saved_id]['posts'] = array($post->ID);
  183. }
  184. }
  185. $this->update_setting('aliases', $aliases);
  186. return true;
  187. }
  188. function apply_aliases($content) {
  189. return preg_replace_callback('@<a ([^>]*)href=(["\'])([^"\']+)(["\'])([^>]*)>@', array(&$this, 'apply_aliases_callback'), $content);
  190. }
  191. function apply_aliases_callback($matches) {
  192. $id = suwp::get_post_id();
  193. static $aliases = false;
  194. //Just in case we have duplicate aliases, make sure the most recent ones are applied first
  195. if ($aliases === false) $aliases = array_reverse($this->get_setting('aliases', array()), true);
  196. static $alias_dir = false;
  197. if ($alias_dir === false) $alias_dir = $this->get_setting('alias_dir', 'go');
  198. $new_url = $old_url = $matches[3];
  199. foreach ($aliases as $alias) {
  200. $to = urlencode($alias['to']);
  201. if ((empty($alias['posts']) || in_array($id, $alias['posts'])) && $to) {
  202. $from = $alias['from'];
  203. $h_from = esc_html($from);
  204. $to = get_bloginfo('url') . "/$alias_dir/$to/";
  205. if ($from == $old_url || $h_from == $old_url) {
  206. $new_url = $to;
  207. break;
  208. }
  209. }
  210. }
  211. $attrs = "{$matches[1]}href={$matches[2]}{$new_url}{$matches[4]}{$matches[5]}";
  212. if ($old_url != $new_url && $this->get_setting('nofollow_aliased_links', false) && $this->plugin->module_exists('link-nofollow'))
  213. $this->plugin->call_module_func('link-nofollow', 'nofollow_attributes_string', $attrs, $attrs);
  214. return "<a $attrs>";
  215. }
  216. function redirect_aliases() {
  217. $aliases = $this->get_setting('aliases', array());
  218. $alias_dir = $this->get_setting('alias_dir', 'go');
  219. foreach ($aliases as $alias)
  220. if ($to = $alias['to'])
  221. if (suurl::equal(suurl::current(), get_bloginfo('url') . "/$alias_dir/$to/"))
  222. wp_redirect($alias['from']);
  223. }
  224. function block_aliases_dir() {
  225. echo '# ';
  226. _e("Added by SEO Ultimate's Link Mask Generator module", 'seo-ultimate');
  227. echo "\n";
  228. $urlinfo = parse_url(get_bloginfo('url'));
  229. $path = $urlinfo['path'];
  230. echo "User-agent: *\n";
  231. $alias_dir = $this->get_setting('alias_dir', 'go');
  232. echo "Disallow: $path/$alias_dir/\n";
  233. echo '# ';
  234. _e('End Link Mask Generator output', 'seo-ultimate');
  235. echo "\n\n";
  236. }
  237. function add_help_tabs($screen) {
  238. $screen->add_help_tab(array(
  239. 'id' => 'su-internal-link-aliases-overview'
  240. , 'title' => __('Overview', 'seo-ultimate')
  241. , 'content' => __("
  242. <ul>
  243. <li><strong>What it does:</strong> Link Mask Generator lets you replace ugly affiliate links with clean-looking link aliases that redirect to the real URLs. Link Mask Generator will scan your posts for the links to the actual URLs and replace them with links to the alias URLs. When a visitor clicks on the link to the alias URL, Link Mask Generator will redirect the visitor to the actual URL.</li>
  244. <li><strong>Why it helps:</strong> This type of functionality is a staple in an affiliate marketer&#8217;s toolkit. Link Mask Generator helps you by doing it in an SEO-friendly way: by funneling your affiliate links through a directory (e.g. <code>/go/</code>) which is blocked with <code>robots.txt</code> rules, effectively sealing off link juice flow to your affiliate links.</li>
  245. <li><strong>How to use it:</strong> Type in the real URL, type in an alias URL, and click &#8220;Save Changes&#8221; &mdash; that&#8217;s it!</li>
  246. </ul>
  247. ", 'seo-ultimate')));
  248. $screen->add_help_tab(array(
  249. 'id' => 'su-internal-link-aliases-aliases'
  250. , 'title' => __('Aliases Tab', 'seo-ultimate')
  251. , 'content' => __("
  252. <p>To add a link alias, fill in the fields and then click &#8220;Save Changes.&#8221; Once you do so, you can edit your newly masked link or add another one.</p>
  253. <ul>
  254. <li><strong>Actual URL</strong> &mdash; This box is where you put your affiliate URL (or other URL that you want to mask).</li>
  255. <li><strong>Alias URL</strong> &mdash; This box is where you specify the new URL that will replace the actual one.</li>
  256. <li><strong>Only on This Post</strong> &mdash; If you want to mask the actual URL across your entire site, leave this box blank. If you only want to mask the actual URL within an individual post, then type its name into this box and select it from the dropdown.</li>
  257. <li><strong>Delete</strong> &mdash; To delete a link mask, tick its &#8220;Delete&#8221; checkbox and then click &#8220;Save Changes.&#8221;</li>
  258. </ul>
  259. ", 'seo-ultimate')));
  260. $screen->add_help_tab(array(
  261. 'id' => 'su-internal-link-aliases-settings'
  262. , 'title' => __('Settings Tab', 'seo-ultimate')
  263. , 'content' => __("
  264. <p>The following options are available on the Settings tab:</p>
  265. <ul>
  266. <li><strong>Alias Directory</strong> &mdash; If you&#8217;d like, you can change the name of the directory that contains all your alias URLs. (Don&#8217;t worry, you won&#8217;t break any links by changing this.)</li>
  267. <li><strong>Nofollow masked links</strong> &mdash; Checking this will add the <code>rel=&quot;nofollow&quot;</code> attribute to any masked links on your site. This makes it super-easy to nofollow all your affiliate links automatically.</li>
  268. </ul>
  269. ", 'seo-ultimate')));
  270. $screen->add_help_tab(array(
  271. 'id' => 'su-autolinks-faq'
  272. , 'title' => __('FAQ', 'seo-ultimate')
  273. , 'content' => __("
  274. <ul>
  275. <li><strong>Can I automatically link a phrase on my site to one of my alias URLs?</strong><br />Yes. Once you&#8217;ve created your link mask, go to Deeplink Juggernaut&#8217;s &#8220;Content Links&#8221; section, type the contents of your link mask&#8217;s &#8220;Alias URL&#8221; field into Deeplink Juggernaut&#8217;s &#8220;Destination&#8221; field, and select your link mask from the dropdown that appears.</li>
  276. <li><strong>Will Link Mask Generator still add the <code>robots.txt</code> rules if I&#8217;m using the File Editor module to create a custom <code>robots.txt</code>?</strong><br />Yes.</li>
  277. </ul>
  278. ", 'seo-ultimate')));
  279. }
  280. }
  281. ?>