Advertisement
bondcemil

Untitled

Apr 27th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.42 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package Frontend
  4.  *
  5.  * Main frontend code.
  6.  */
  7.  
  8. if ( !defined( 'WPSEO_VERSION' ) ) {
  9.     header( 'HTTP/1.0 403 Forbidden' );
  10.     die;
  11. }
  12.  
  13. /**
  14.  * Main frontend class for WordPress SEO, responsible for the SEO output as well as removing default WordPress output.
  15.  *
  16.  * @package WPSEO_Frontend
  17.  */
  18. class WPSEO_Frontend {
  19.  
  20.     /**
  21.      * @var array Holds the plugins options.
  22.      */
  23.     var $options = array();
  24.  
  25.     /**
  26.      * Class constructor
  27.      *
  28.      * Adds and removes a lot of filters.
  29.      */
  30.     function __construct() {
  31.  
  32.         $this->options = get_wpseo_options();
  33.  
  34.         add_action( 'wp_head', array( $this, 'head' ), 1 );
  35.  
  36.         // The head function here calls action wpseo_head, to which we hook all our functionality
  37.         add_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );
  38.         add_action( 'wpseo_head', array( $this, 'robots' ), 6 );
  39.         add_action( 'wpseo_head', array( $this, 'metadesc' ), 10 );
  40.         add_action( 'wpseo_head', array( $this, 'metakeywords' ), 11 );
  41.         add_action( 'wpseo_head', array( $this, 'canonical' ), 20 );
  42.         add_action( 'wpseo_head', array( $this, 'adjacent_rel_links' ), 21 );
  43.         add_action( 'wpseo_head', array( $this, 'author' ), 22 );
  44.         add_action( 'wpseo_head', array( $this, 'publisher' ), 23 );
  45.         add_action( 'wpseo_head', array( $this, 'webmaster_tools_authentication' ), 90 );
  46.  
  47.         // Remove actions that we will handle through our wpseo_head call, and probably change the output of
  48.         remove_action( 'wp_head', 'rel_canonical' );
  49.         remove_action( 'wp_head', 'index_rel_link' );
  50.         remove_action( 'wp_head', 'start_post_rel_link' );
  51.         remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
  52.  
  53.         add_filter( 'wp_title', array( $this, 'title' ), 15, 3 );
  54.         add_filter( 'thematic_doctitle', array( $this, 'title' ), 15 );
  55.  
  56.         add_action( 'wp', array( $this, 'page_redirect' ), 99, 1 );
  57.  
  58.         add_action( 'template_redirect', array( $this, 'noindex_feed' ) );
  59.  
  60.         add_filter( 'loginout', array( $this, 'nofollow_link' ) );
  61.         add_filter( 'register', array( $this, 'nofollow_link' ) );
  62.  
  63.         if ( isset( $this->options['hide-rsdlink'] ) && $this->options['hide-rsdlink'] )
  64.             remove_action( 'wp_head', 'rsd_link' );
  65.  
  66.         if ( isset( $this->options['hide-wlwmanifest'] ) && $this->options['hide-wlwmanifest'] )
  67.             remove_action( 'wp_head', 'wlwmanifest_link' );
  68.  
  69.         if ( isset( $this->options['hide-shortlink'] ) && $this->options['hide-shortlink'] ) {
  70.             remove_action( 'wp_head', 'wp_shortlink_wp_head' );
  71.             remove_action( 'template_redirect', 'wp_shortlink_header' );
  72.         }
  73.         if ( isset( $this->options['hide-feedlinks'] ) && $this->options['hide-feedlinks'] ) {
  74.             // @todo: add option to display just normal feed and hide comment feed.
  75.             remove_action( 'wp_head', 'feed_links', 2 );
  76.             remove_action( 'wp_head', 'feed_links_extra', 3 );
  77.         }
  78.  
  79.         if ( ( isset( $this->options['disable-date'] ) && $this->options['disable-date'] ) ||
  80.             ( isset( $this->options['disable-author'] ) && $this->options['disable-author'] ) ||
  81.             ( isset( $this->options['disable-post_formats'] ) && $this->options['disable-post_formats'] )
  82.         )
  83.             add_action( 'wp', array( $this, 'archive_redirect' ) );
  84.  
  85.         if ( isset( $this->options['redirectattachment'] ) && $this->options['redirectattachment'] )
  86.             add_action( 'template_redirect', array( $this, 'attachment_redirect' ), 1 );
  87.  
  88.         if ( isset( $this->options['trailingslash'] ) && $this->options['trailingslash'] )
  89.             add_filter( 'user_trailingslashit', array( $this, 'add_trailingslash' ), 10, 2 );
  90.  
  91.         if ( isset( $this->options['cleanpermalinks'] ) && $this->options['cleanpermalinks'] )
  92.             add_action( 'template_redirect', array( $this, 'clean_permalink' ), 1 );
  93.  
  94.         if ( isset( $this->options['cleanreplytocom'] ) && $this->options['cleanreplytocom'] )
  95.             add_filter( 'comment_reply_link', array( $this, 'remove_reply_to_com' ) );
  96.  
  97.         add_filter( 'the_content_feed', array( $this, 'embed_rssfooter' ) );
  98.         add_filter( 'the_excerpt_rss', array( $this, 'embed_rssfooter_excerpt' ) );
  99.  
  100.         if ( isset( $this->options['forcerewritetitle'] ) && $this->options['forcerewritetitle'] ) {
  101.             add_action( 'get_header', array( $this, 'force_rewrite_output_buffer' ) );
  102.             add_action( 'wp_footer', array( $this, 'flush_cache' ) );
  103.         }
  104.  
  105.         if ( isset( $this->options['title_test'] ) && $this->options['title_test'] )
  106.             add_filter( 'wpseo_title', array( $this, 'title_test_helper' ) );
  107.  
  108.         if ( isset( $_GET['replytocom'] ) )
  109.             remove_action( 'wp_head', 'wp_no_robots' );
  110.  
  111.     }
  112.  
  113.     /**
  114.      * Determine whether the current page is the homepage and shows posts.
  115.      *
  116.      * @return bool
  117.      */
  118.     function is_home_posts_page() {
  119.         return ( is_home() && 'page' != get_option( 'show_on_front' ) );
  120.     }
  121.  
  122.     /**
  123.      * Determine whether the current page is a static homepage.
  124.      *
  125.      * @return bool
  126.      */
  127.     function is_home_static_page() {
  128.         return ( is_front_page() && 'page' == get_option( 'show_on_front' ) && is_page( get_option( 'page_on_front' ) ) );
  129.     }
  130.  
  131.     /**
  132.      * Determine whether this is the posts page, regardless of whether it's the frontpage or not.
  133.      *
  134.      * @return bool
  135.      */
  136.     function is_posts_page() {
  137.         return ( is_home() && 'page' == get_option( 'show_on_front' ) );
  138.     }
  139.  
  140.     /**
  141.      * Used for static home and posts pages as well as singular titles.
  142.      *
  143.      * @param object|null $object if filled, object to get the title for
  144.      * @return string
  145.      */
  146.     function get_content_title( $object = null ) {
  147.         if ( is_null( $object ) ) {
  148.             global $wp_query;
  149.             $object = $wp_query->get_queried_object();
  150.         }
  151.  
  152.         $title = wpseo_get_value( 'title', $object->ID );
  153.  
  154.         if ( !empty( $title ) )
  155.             return wpseo_replace_vars( $title, (array) $object );
  156.  
  157.         return $this->get_title_from_options( 'title-' . $object->post_type, $object );
  158.     }
  159.  
  160.     /**
  161.      * Used for category, tag, and tax titles.
  162.      *
  163.      * @return string
  164.      */
  165.     function get_taxonomy_title() {
  166.         global $wp_query;
  167.         $object = $wp_query->get_queried_object();
  168.  
  169.         $title = trim( wpseo_get_term_meta( $object, $object->taxonomy, 'title' ) );
  170.  
  171.         if ( !empty( $title ) )
  172.             return wpseo_replace_vars( $title, (array) $object );
  173.  
  174.         return $this->get_title_from_options( 'title-' . $object->taxonomy, $object );
  175.     }
  176.  
  177.     /**
  178.      * Used for author titles.
  179.      *
  180.      * @return string
  181.      */
  182.     function get_author_title() {
  183.         $author_id = get_query_var( 'author' );
  184.         $title     = get_the_author_meta( 'wpseo_title', $author_id );
  185.  
  186.         if ( !empty( $title ) )
  187.             return wpseo_replace_vars( $title, array() );
  188.  
  189.         return $this->get_title_from_options( 'title-author' );
  190.     }
  191.  
  192.     /**
  193.      * Simple function to use to pull data from $options.
  194.      *
  195.      * All titles pulled from options will be run through the wpseo_replace_vars function.
  196.      *
  197.      * @param string        $index         name of the page to get the title from the settings for.
  198.      * @param object|array  $var_source    possible object to pul variables from.
  199.      * @return string
  200.      */
  201.     function get_title_from_options( $index, $var_source = array() ) {
  202.         if ( !isset( $this->options[$index] ) || empty( $this->options[$index] ) ) {
  203.             if ( is_singular() )
  204.                 return wpseo_replace_vars( '%%title%% %%sep%% %%sitename%%', (array) $var_source );
  205.             else
  206.                 return '';
  207.         }
  208.  
  209.         return wpseo_replace_vars( $this->options[$index], (array) $var_source );
  210.     }
  211.  
  212.     /**
  213.      * Get the default title for the current page.
  214.      *
  215.      * This is the fallback title generator used when a title hasn't been set for the specific content, taxonomy, author
  216.      * details, or in the options. It scrubs off any present prefix before or after the title (based on $seplocation) in
  217.      * order to prevent duplicate seperations from appearing in the title (this happens when a prefix is supplied to the
  218.      * wp_title call on singular pages).
  219.      *
  220.      * @param string $sep         the separator used between variables
  221.      * @param string $seplocation Whether the separator should be left or right.
  222.      * @param string $title       possible title that's already set
  223.      * @return string
  224.      */
  225.     function get_default_title( $sep, $seplocation, $title = '' ) {
  226.         if ( 'right' == $seplocation )
  227.             $regex = '/\s*' . preg_quote( trim( $sep ), '/' ) . '\s*/';
  228.         else
  229.             $regex = '/^\s*' . preg_quote( trim( $sep ), '/' ) . '\s*/';
  230.         $title = preg_replace( $regex, '', $title );
  231.  
  232.         if ( empty( $title ) ) {
  233.             $title = get_bloginfo( 'name' );
  234.             $title = $this->add_paging_to_title( $sep, $seplocation, $title );
  235.             $title = $this->add_to_title( $sep, $seplocation, $title, get_bloginfo( 'description' ) );
  236.             return $title;
  237.         }
  238.  
  239.         $title = $this->add_paging_to_title( $sep, $seplocation, $title );
  240.         $title = $this->add_to_title( $sep, $seplocation, $title, get_bloginfo( 'name' ) );
  241.         return $title;
  242.     }
  243.  
  244.     /**
  245.      * This function adds paging details to the title.
  246.      *
  247.      * @param string $sep         separator used in the title
  248.      * @param string $seplocation Whether the separator should be left or right.
  249.      * @param string $title       the title to append the paging info to
  250.      * @return string
  251.      */
  252.     function add_paging_to_title( $sep, $seplocation, $title ) {
  253.         global $wp_query;
  254.  
  255.         if ( !empty( $wp_query->query_vars['paged'] ) && $wp_query->query_vars['paged'] > 1 )
  256.             return $this->add_to_title( $sep, $seplocation, $title, $wp_query->query_vars['paged'] . '/' . $wp_query->max_num_pages );
  257.  
  258.         return $title;
  259.     }
  260.  
  261.     /**
  262.      * Add part to title, while ensuring that the $seplocation variable is respected.
  263.      *
  264.      * @param string $sep         separator used in the title
  265.      * @param string $seplocation Whether the separator should be left or right.
  266.      * @param string $title       the title to append the title_part to
  267.      * @param string $title_part  the part to append to the title
  268.      * @return string
  269.      */
  270.     function add_to_title( $sep, $seplocation, $title, $title_part ) {
  271.         if ( 'right' == $seplocation )
  272.             return $title . $sep . $title_part;
  273.         return $title_part . $sep . $title;
  274.     }
  275.  
  276.     /**
  277.      * Main title function.
  278.      *
  279.      * @param string $title       Title that might have already been set.
  280.      * @param string $sepinput    Separator determined in theme.
  281.      * @param string $seplocation Whether the separator should be left or right.
  282.      * @return string
  283.      */
  284.     function title( $title, $sepinput = '-', $seplocation = '' ) {
  285.         global $sep;
  286.  
  287.         $sep = $sepinput;
  288.  
  289.         if ( is_feed() )
  290.             return $title;
  291.  
  292.         // This needs to be kept track of in order to generate
  293.         // default titles for singular pages.
  294.         $original_title = $title;
  295.  
  296.         // This conditional ensures that sites that use of wp_title(''); as the plugin
  297.         // used to suggest will still work properly with these changes.
  298.         if ( '' == trim( $sep ) && '' == $seplocation ) {
  299.             $sep         = '-';
  300.             $seplocation = 'right';
  301.         } // In the event that $seplocation is left empty, the direction will be
  302.         // determined by whether the site is in rtl mode or not. This is based
  303.         // upon my findings that rtl sites tend to reverse the flow of the site titles.
  304.         else if ( '' == $seplocation )
  305.             $seplocation = ( is_rtl() ) ? 'left' : 'right';
  306.  
  307.         $sep = ' ' . trim( $sep ) . ' ';
  308.  
  309.         // This flag is used to determine if any additional
  310.         // processing should be done to the title after the
  311.         // main section of title generation completes.
  312.         $modified_title = true;
  313.  
  314.         // This variable holds the page-specific title part
  315.         // that is used to generate default titles.
  316.         $title_part = '';
  317.  
  318.         if ( $this->is_home_static_page() ) {
  319.             $title = $this->get_content_title();
  320.         } else if ( $this->is_home_posts_page() ) {
  321.             $title = $this->get_title_from_options( 'title-home' );
  322.         } else if ( $this->is_posts_page() ) {
  323.             $title = $this->get_content_title( get_post( get_option( 'page_for_posts' ) ) );
  324.         } else if ( is_singular() ) {
  325.             $title = $this->get_content_title();
  326.  
  327.             if ( empty( $title ) )
  328.                 $title_part = $original_title;
  329.         } else if ( is_search() ) {
  330.             $title = $this->get_title_from_options( 'title-search' );
  331.  
  332.             if ( empty( $title ) )
  333.                 $title_part = sprintf( __( 'Search for "%s"', 'wordpress-seo' ), esc_html( get_search_query() ) );
  334.         } else if ( is_category() || is_tag() || is_tax() ) {
  335.             $title = $this->get_taxonomy_title();
  336.  
  337.             if ( empty( $title ) ) {
  338.                 if ( is_category() )
  339.                     $title_part = single_cat_title( '', false );
  340.                 else if ( is_tag() )
  341.                     $title_part = single_tag_title( '', false );
  342.                 else if ( function_exists( 'single_term_title' ) ) {
  343.                     $title_part = single_term_title( '', false );
  344.                 } else {
  345.                     global $wp_query;
  346.                     $term       = $wp_query->get_queried_object();
  347.                     $title_part = $term->name;
  348.                 }
  349.             }
  350.         } else if ( is_author() ) {
  351.             $title = $this->get_author_title();
  352.  
  353.             if ( empty( $title ) )
  354.                 $title_part = get_the_author_meta( 'display_name', get_query_var( 'author' ) );
  355.         } else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
  356.             $post_type = get_query_var( 'post_type' );
  357.             $title     = $this->get_title_from_options( 'title-ptarchive-' . $post_type );
  358.  
  359.             if ( empty( $title ) ) {
  360.                 $post_type_obj = get_post_type_object( $post_type );
  361.                 if ( isset( $post_type_obj->labels->menu_name ) )
  362.                     $title_part = $post_type_obj->labels->menu_name;
  363.                 else
  364.                     $title_part = $post_type_obj->name;
  365.             }
  366.         } else if ( is_archive() ) {
  367.             $title = $this->get_title_from_options( 'title-archive' );
  368.  
  369.             if ( empty( $title ) ) {
  370.                 if ( is_month() )
  371.                     $title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), single_month_title( ' ', false ) );
  372.                 else if ( is_year() )
  373.                     $title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_query_var( 'year' ) );
  374.                 else if ( is_day() )
  375.                     $title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_the_date() );
  376.                 else
  377.                     $title_part = __( 'Archives', 'wordpress-seo' );
  378.             }
  379.         } else if ( is_404() ) {
  380.             $title = $this->get_title_from_options( 'title-404' );
  381.  
  382.             if ( empty( $title ) )
  383.                 $title_part = __( 'Page not found', 'wordpress-seo' );
  384.         } else {
  385.             // In case the page type is unknown, leave the title alone.
  386.             $modified_title = false;
  387.  
  388.             // If you would like to generate a default title instead,
  389.             // the following code could be used instead of the line above:
  390.             // $title_part = $title;
  391.         }
  392.  
  393.         if ( ( $modified_title && empty( $title ) ) || !empty( $title_part ) )
  394.             $title = $this->get_default_title( $sep, $seplocation, $title_part );
  395.  
  396.         return esc_html( strip_tags( stripslashes( apply_filters( 'wpseo_title', $title ) ) ) );
  397.     }
  398.  
  399.     /**
  400.      * Function used when title needs to be force overridden.
  401.      *
  402.      * @return string
  403.      */
  404.     function force_wp_title() {
  405.         global $wp_query;
  406.         $old_wp_query = null;
  407.  
  408.         if ( !$wp_query->is_main_query() ) {
  409.             $old_wp_query = $wp_query;
  410.             wp_reset_query();
  411.         }
  412.  
  413.         $title = $this->title( '' );
  414.  
  415.         if ( !empty( $old_wp_query ) ) {
  416.             $GLOBALS['wp_query'] = $old_wp_query;
  417.             unset( $old_wp_query );
  418.         }
  419.  
  420.         return $title;
  421.     }
  422.  
  423.     /**
  424.      * Outputs or returns the debug marker, which is also used for title replacement when force rewrite is active.
  425.      *
  426.      * @param bool $echo Whether or not to echo the debug marker.
  427.      * @return string
  428.      */
  429.     public function debug_marker( $echo = true ) {
  430.         $marker = "<!-- This site is optimized with the Yoast WordPress SEO plugin v" . WPSEO_VERSION . " - http://yoast.com/wordpress/seo/ -->";
  431.         if ( $echo === false )
  432.             return $marker;
  433.         else
  434.             echo "\n${marker}\n";
  435.     }
  436.  
  437.     /**
  438.      * Output Webmaster Tools authentication strings
  439.      */
  440.     public function webmaster_tools_authentication() {
  441.         if ( is_front_page() ) {
  442.             if ( !empty( $this->options['googleverify'] ) ) {
  443.                 $google_meta = $this->options['googleverify'];
  444.                 if ( strpos( $google_meta, 'content' ) ) {
  445.                     preg_match( '/content="([^"]+)"/', $google_meta, $match );
  446.                     $google_meta = $match[1];
  447.                 }
  448.                 echo "<meta name=\"google-site-verification\" content=\"$google_meta\" />\n";
  449.             }
  450.  
  451.             if ( !empty( $this->options['msverify'] ) ) {
  452.                 $bing_meta = $this->options['msverify'];
  453.                 if ( strpos( $bing_meta, 'content' ) ) {
  454.                     preg_match( '/content="([^"]+)"/', $bing_meta, $match );
  455.                     $bing_meta = $match[1];
  456.                 }
  457.                 echo "<meta name=\"msvalidate.01\" content=\"$bing_meta\" />\n";
  458.             }
  459.  
  460.             if ( !empty( $this->options['alexaverify'] ) ) {
  461.                 echo "<meta name=\"alexaVerifyID\" content=\"" . esc_attr( $this->options['alexaverify'] ) . "\" />\n";
  462.             }
  463.         }
  464.     }
  465.  
  466.     /**
  467.      * Main wrapper function attached to wp_head. This combines all the output on the frontend of the WP SEO plugin.
  468.      */
  469.     public function head() {
  470.         global $wp_query;
  471.  
  472.         $old_wp_query = null;
  473.  
  474.         if ( !$wp_query->is_main_query() ) {
  475.             $old_wp_query = $wp_query;
  476.             wp_reset_query();
  477.         }
  478.  
  479.         do_action( 'wpseo_head' );
  480.  
  481.         echo "<!-- / Yoast WordPress SEO plugin. -->\n\n";
  482.  
  483.         if ( !empty( $old_wp_query ) ) {
  484.             $GLOBALS['wp_query'] = $old_wp_query;
  485.             unset( $old_wp_query );
  486.         }
  487.  
  488.         return;
  489.     }
  490.  
  491.     /**
  492.      * Output the meta robots value.
  493.      */
  494.     public function robots() {
  495.         global $wp_query;
  496.  
  497.         $robots           = array();
  498.         $robots['index']  = 'index';
  499.         $robots['follow'] = 'follow';
  500.         $robots['other']  = array();
  501.  
  502.         if ( is_singular() ) {
  503.             global $post;
  504.             if ( isset( $this->options['noindex-' . $post->post_type] ) && $this->options['noindex-' . $post->post_type] )
  505.                 $robots['index'] = 'noindex';
  506.             if ( wpseo_get_value( 'meta-robots-noindex' ) == 1 )
  507.                 $robots['index'] = 'noindex';
  508.             if ( wpseo_get_value( 'meta-robots-noindex' ) == 2 )
  509.                 $robots['index'] = 'index';
  510.             if ( wpseo_get_value( 'meta-robots-nofollow' ) )
  511.                 $robots['follow'] = 'nofollow';
  512.             if ( wpseo_get_value( 'meta-robots-adv' ) && wpseo_get_value( 'meta-robots-adv' ) != 'none' ) {
  513.                 foreach ( explode( ',', wpseo_get_value( 'meta-robots-adv' ) ) as $r ) {
  514.                     $robots['other'][] = $r;
  515.                 }
  516.             }
  517.         } else {
  518.             if ( is_search() ) {
  519.                 $robots['index'] = 'noindex';
  520.             } else if ( is_tax() || is_tag() || is_category() ) {
  521.                 $term = $wp_query->get_queried_object();
  522.                 if ( isset( $this->options['noindex-' . $term->taxonomy] ) && $this->options['noindex-' . $term->taxonomy] )
  523.                     $robots['index'] = 'noindex';
  524.  
  525.                 // Three possible values, index, noindex and default, do nothing for default
  526.                 $term_meta = wpseo_get_term_meta( $term, $term->taxonomy, 'noindex' );
  527.                 if ( 'noindex' == $term_meta || 'on' == $term_meta ) // on is for backwards compatibility
  528.                     $robots['index'] = 'noindex';
  529.  
  530.                 if ( 'index' == $term_meta )
  531.                     $robots['index'] = 'index';
  532.             } else if (
  533.                 ( is_author() && isset( $this->options['noindex-author'] ) && $this->options['noindex-author'] ) ||
  534.                 ( is_date() && isset( $this->options['noindex-archive'] ) && $this->options['noindex-archive'] ) ||
  535.                 ( is_home() && get_query_var( 'paged' ) > 1 )
  536.             ) {
  537.                 $robots['index'] = 'noindex';
  538.             } else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
  539.                 $post_type = get_query_var( 'post_type' );
  540.                 if ( isset( $this->options['noindex-ptarchive-' . $post_type] ) && $this->options['noindex-ptarchive-' . $post_type] )
  541.                     $robots['index'] = 'noindex';
  542.             }
  543.  
  544.             if ( $wp_query->query_vars['paged'] && $wp_query->query_vars['paged'] > 1 && isset( $this->options['noindex-subpages'] ) && $this->options['noindex-subpages'] ) {
  545.                 $robots['index']  = 'noindex';
  546.                 $robots['follow'] = 'follow';
  547.             }
  548.         }
  549.  
  550.         foreach ( array( 'noodp', 'noydir' ) as $robot ) {
  551.             if ( isset( $this->options[$robot] ) && $this->options[$robot] ) {
  552.                 $robots['other'][] = $robot;
  553.             }
  554.         }
  555.  
  556.         $robotsstr = $robots['index'] . ',' . $robots['follow'];
  557.  
  558.         $robots['other'] = array_unique( $robots['other'] );
  559.         foreach ( $robots['other'] as $robot ) {
  560.             $robotsstr .= ',' . $robot;
  561.         }
  562.  
  563.         $robotsstr = preg_replace( '/^index,follow,?/', '', $robotsstr );
  564.  
  565.         $robotsstr = apply_filters( 'wpseo_robots', $robotsstr );
  566.  
  567.         if ( $robotsstr != '' )
  568.             echo '<meta name="robots" content="' . esc_attr( $robotsstr ) . '"/>' . "\n";
  569.     }
  570.  
  571.     /**
  572.      * This function normally outputs the canonical but is also used in other places to retrieve the canonical URL
  573.      * for the current page.
  574.      *
  575.      * @param bool $echo    Whether or not to output the canonical element.
  576.      * @param bool $unpaged Whether or not to return the canonical with or without pagination added to the URL.
  577.      * @return string $canonical
  578.      */
  579.     public function canonical( $echo = true, $unpaged = false ) {
  580.         $canonical = false;
  581.  
  582.         // Set decent canonicals for homepage, singulars and taxonomy pages
  583.         if ( is_singular() ) {
  584.             if ( wpseo_get_value( 'canonical' ) && wpseo_get_value( 'canonical' ) != '' ) {
  585.                 $canonical = wpseo_get_value( 'canonical' );
  586.             } else {
  587.                 $obj       = get_queried_object();
  588.                 $canonical = get_permalink( $obj->ID );
  589.  
  590.                 // Fix paginated pages canonical, but only if the page is truly paginated.
  591.                 if ( get_query_var( 'page' ) > 1 ) {
  592.                     global $wp_rewrite;
  593.                     $numpages = substr_count( $obj->post_content, '<!--nextpage-->' ) + 1;
  594.                     if ( $numpages && get_query_var( 'page' ) < $numpages ) {
  595.                         if ( !$wp_rewrite->using_permalinks() ) {
  596.                             $canonical = add_query_arg( 'page', get_query_var( 'page' ), $canonical );
  597.                         } else {
  598.                             $canonical = user_trailingslashit( trailingslashit( $canonical ) . get_query_var( 'page' ) );
  599.                         }
  600.                     }
  601.                 }
  602.             }
  603.         } else {
  604.             if ( is_search() ) {
  605.                 $canonical = get_search_link();
  606.             } else if ( is_front_page() ) {
  607.                 $canonical = home_url( '/' );
  608.             } else if ( $this->is_posts_page() ) {
  609.                 $canonical = get_permalink( get_option( 'page_for_posts' ) );
  610.             } else if ( is_tax() || is_tag() || is_category() ) {
  611.                 $term      = get_queried_object();
  612.                 $canonical = wpseo_get_term_meta( $term, $term->taxonomy, 'canonical' );
  613.                 if ( !$canonical )
  614.                     $canonical = get_term_link( $term, $term->taxonomy );
  615.             } else if ( function_exists( 'get_post_type_archive_link' ) && is_post_type_archive() ) {
  616.                 $canonical = get_post_type_archive_link( get_query_var( 'post_type' ) );
  617.             } else if ( is_author() ) {
  618.                 $canonical = get_author_posts_url( get_query_var( 'author' ), get_query_var( 'author_name' ) );
  619.             } else if ( is_archive() ) {
  620.                 if ( is_date() ) {
  621.                     if ( is_day() ) {
  622.                         $canonical = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
  623.                     } else if ( is_month() ) {
  624.                         $canonical = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
  625.                     } else if ( is_year() ) {
  626.                         $canonical = get_year_link( get_query_var( 'year' ) );
  627.                     }
  628.                 }
  629.             }
  630.  
  631.             if ( $canonical && $unpaged )
  632.                 return $canonical;
  633.  
  634.             if ( $canonical && get_query_var( 'paged' ) > 1 ) {
  635.                 global $wp_rewrite;
  636.                 if ( !$wp_rewrite->using_permalinks() ) {
  637.                     $canonical = add_query_arg( 'paged', get_query_var( 'paged' ), $canonical );
  638.                 } else {
  639.                     $canonical = user_trailingslashit( trailingslashit( $canonical ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var( 'paged' ) );
  640.                 }
  641.             }
  642.         }
  643.  
  644.         if ( $canonical && isset( $this->options['force_transport'] ) && 'default' != $this->options['force_transport'] )
  645.             $canonical = preg_replace( '/https?/', $this->options['force_transport'], $canonical );
  646.  
  647.         $canonical = apply_filters( 'wpseo_canonical', $canonical );
  648.  
  649.         if ( $canonical && !is_wp_error( $canonical ) ) {
  650.             if ( $echo !== false )
  651.                 echo '<link rel="canonical" href="' . esc_url( $canonical, null, 'other' ) . '" />' . "\n";
  652.             else
  653.                 return $canonical;
  654.         }
  655.     }
  656.  
  657.     /**
  658.      * Adds 'prev' and 'next' links to archives.
  659.      *
  660.      * @link  http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
  661.      * @since 1.0.3
  662.      */
  663.     public function adjacent_rel_links() {
  664.         // Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
  665.         if ( is_home() || function_exists( 'genesis' ) )
  666.             return;
  667.  
  668.         global $wp_query;
  669.  
  670.         if ( !is_singular() ) {
  671.             $url = $this->canonical( false, true );
  672.  
  673.             if ( $url ) {
  674.                 $paged = get_query_var( 'paged' );
  675.  
  676.                 if ( 0 == $paged )
  677.                     $paged = 1;
  678.  
  679.                 if ( $paged > 1 )
  680.                     $this->adjacent_rel_link( "prev", $url, $paged - 1, true );
  681.  
  682.                 if ( $paged < $wp_query->max_num_pages )
  683.                     $this->adjacent_rel_link( "next", $url, $paged + 1, true );
  684.             }
  685.         } else {
  686.             $numpages = 0;
  687.             if ( isset( $wp_query->post->post_content ) ) {
  688.                 $numpages = substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1;
  689.             }
  690.             if ( $numpages > 1 ) {
  691.                 $page = get_query_var( 'page' );
  692.                 if ( !$page )
  693.                     $page = 1;
  694.  
  695.                 $url = get_permalink( $wp_query->post->ID );
  696.  
  697.                 // If the current page is the frontpage, pagination should use /base/
  698.                 if ( $this->is_home_static_page() )
  699.                     $usebase = true;
  700.                 else
  701.                     $usebase = false;
  702.  
  703.                 if ( $page > 1 )
  704.                     $this->adjacent_rel_link( "prev", $url, $page - 1, $usebase, 'single_paged' );
  705.                 if ( $page < $numpages )
  706.                     $this->adjacent_rel_link( "next", $url, $page + 1, $usebase, 'single_paged' );
  707.             }
  708.         }
  709.     }
  710.  
  711.     /**
  712.      * Get adjacent pages link for archives
  713.      *
  714.      * @param string  $rel                  Link relationship, prev or next.
  715.      * @param string  $url                  the unpaginated URL of the current archive.
  716.      * @param string  $page                 the page number to add on to $url for the $link tag.
  717.      * @param boolean $incl_pagination_base whether or not to include /page/ or not.
  718.      * @return string $link link element
  719.      *
  720.      * @since 1.0.2
  721.      */
  722.     private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {
  723.         global $wp_rewrite;
  724.         if ( !$wp_rewrite->using_permalinks() ) {
  725.             if ( $page > 1 )
  726.                 $url = add_query_arg( 'paged', $page, $url );
  727.         } else {
  728.             if ( $page > 1 ) {
  729.                 $base = '';
  730.                 if ( $incl_pagination_base )
  731.                     $base = trailingslashit( $wp_rewrite->pagination_base );
  732.                 $url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
  733.             }
  734.         }
  735.         $link = apply_filters( "wpseo_" . $rel . "_rel_link", "<link rel=\"$rel\" href=\"$url\" />\n" );
  736.  
  737.         if ( $link )
  738.             echo $link;
  739.     }
  740.  
  741.     /**
  742.      * Output the rel=publisher code on the frontpage of the site.
  743.      */
  744.     public function publisher() {
  745.         if ( is_front_page() ) {
  746.             if ( isset( $this->options['plus-publisher'] ) && !empty( $this->options['plus-publisher'] ) )
  747.                 echo '<link rel="publisher" href="' . esc_attr( $this->options['plus-publisher'] ) . '"/>' . "\n";
  748.         }
  749.     }
  750.  
  751.     /**
  752.      * Outputs the rel=author
  753.      */
  754.     public function author() {
  755.         $gplus   = false;
  756.  
  757.         if ( is_singular() ) {
  758.             global $post;
  759.             $gplus = get_the_author_meta( 'googleplus', $post->post_author );
  760.         } else if ( is_home() ) {
  761.             if ( isset( $this->options['plus-author'] ) )
  762.                 $gplus = get_the_author_meta( 'googleplus', $this->options['plus-author'] );
  763.         }
  764.  
  765.         $gplus = apply_filters( 'wpseo_author_link', $gplus );
  766.  
  767.         if ( $gplus )
  768.             echo '<link rel="author" href="' . $gplus . '"/>' . "\n";
  769.  
  770.     }
  771.  
  772.     /**
  773.      * Outputs the meta keywords element.
  774.      *
  775.      * @return string
  776.      */
  777.     public function metakeywords() {
  778.         global $wp_query;
  779.  
  780.         if ( !isset( $this->options['usemetakeywords'] ) || !$this->options['usemetakeywords'] )
  781.             return;
  782.  
  783.         $metakey = '';
  784.  
  785.         if ( is_singular() ) {
  786.             global $post;
  787.             $metakey = wpseo_get_value( 'metakeywords' );
  788.             if ( isset( $this->options['metakey-' . $post->post_type] ) && ( !$metakey || empty( $metakey ) ) ) {
  789.                 $metakey = wpseo_replace_vars( $this->options['metakey-' . $post->post_type], (array) $post );
  790.             }
  791.         } else {
  792.             if ( $this->is_home_posts_page() && isset( $this->options['metakey-home'] ) ) {
  793.                 $metakey = wpseo_replace_vars( $this->options['metakey-home'], array() );
  794.             } else if ( $this->is_home_static_page() ) {
  795.                 global $post;
  796.                 $metakey = wpseo_get_value( 'metakey' );
  797.                 if ( ( $metakey == '' || !$metakey ) && isset( $this->options['metakey-' . $post->post_type] ) )
  798.                     $metakey = wpseo_replace_vars( $this->options['metakey-' . $post->post_type], (array) $post );
  799.             } else if ( is_category() || is_tag() || is_tax() ) {
  800.                 $term = $wp_query->get_queried_object();
  801.  
  802.                 $metakey = wpseo_get_term_meta( $term, $term->taxonomy, 'metakey' );
  803.                 if ( !$metakey && isset( $this->options['metakey-' . $term->taxonomy] ) )
  804.                     $metakey = wpseo_replace_vars( $this->options['metakey-' . $term->taxonomy], (array) $term );
  805.             } else if ( is_author() ) {
  806.                 $author_id = get_query_var( 'author' );
  807.                 $metakey   = get_the_author_meta( 'metakey', $author_id );
  808.                 if ( !$metakey && isset( $this->options['metakey-author'] ) )
  809.                     $metakey = wpseo_replace_vars( $this->options['metakey-author'], (array) $wp_query->get_queried_object() );
  810.             }
  811.  
  812.         }
  813.  
  814.         $metakey = apply_filters( 'wpseo_metakey', trim( $metakey ) );
  815.  
  816.         if ( !empty( $metakey ) )
  817.             echo '<meta name="keywords" content="' . esc_attr( strip_tags( stripslashes( $metakey ) ) ) . '"/>' . "\n";
  818.  
  819.     }
  820.  
  821.     /**
  822.      * Outputs the meta description element or returns the description text.
  823.      *
  824.      * @param bool $echo Whether or not to echo the description.
  825.      * @return string
  826.      */
  827.     public function metadesc( $echo = true ) {
  828.         if ( get_query_var( 'paged' ) && get_query_var( 'paged' ) > 1 )
  829.             return;
  830.  
  831.         global $post, $wp_query;
  832.  
  833.         $metadesc = '';
  834.         if ( is_singular() ) {
  835.             $metadesc = wpseo_get_value( 'metadesc' );
  836.             if ( $metadesc == '' || !$metadesc ) {
  837.                 if ( isset( $this->options['metadesc-' . $post->post_type] ) && $this->options['metadesc-' . $post->post_type] != '' )
  838.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $post );
  839.             }
  840.         } else {
  841.             if ( is_search() ) {
  842.                 $metadesc = '';
  843.             } else if ( $this->is_home_posts_page() && isset( $this->options['metadesc-home'] ) ) {
  844.                 $metadesc = wpseo_replace_vars( $this->options['metadesc-home'], array() );
  845.             } else if ( $this->is_posts_page() ) {
  846.                 $metadesc = wpseo_get_value( 'metadesc', get_option( 'page_for_posts' ) );
  847.                 if ( ( $metadesc == '' || !$metadesc ) && isset( $this->options['metadesc-' . $post->post_type] ) ) {
  848.                     $page     = get_post( get_option( 'page_for_posts' ) );
  849.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $page );
  850.                 }
  851.             } else if ( $this->is_home_static_page() ) {
  852.                 global $post;
  853.                 $metadesc = wpseo_get_value( 'metadesc' );
  854.                 if ( ( $metadesc == '' || !$metadesc ) && isset( $this->options['metadesc-' . $post->post_type] ) )
  855.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $post );
  856.             } else if ( is_category() || is_tag() || is_tax() ) {
  857.                 $term = $wp_query->get_queried_object();
  858.  
  859.                 $metadesc = wpseo_get_term_meta( $term, $term->taxonomy, 'desc' );
  860.                 if ( !$metadesc && isset( $this->options['metadesc-' . $term->taxonomy] ) )
  861.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-' . $term->taxonomy], (array) $term );
  862.             } else if ( is_author() ) {
  863.                 $author_id = get_query_var( 'author' );
  864.                 $metadesc  = get_the_author_meta( 'wpseo_metadesc', $author_id );
  865.                 if ( !$metadesc && isset( $this->options['metadesc-author'] ) )
  866.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-author'], (array) $wp_query->get_queried_object() );
  867.             } else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
  868.                 $post_type = get_query_var( 'post_type' );
  869.                 if ( isset( $this->options['metadesc-ptarchive-' . $post_type] ) && '' != $this->options['metadesc-ptarchive-' . $post_type] ) {
  870.                     $metadesc = wpseo_replace_vars( $this->options['metadesc-ptarchive-' . $post_type], (array) $wp_query->get_queried_object() );
  871.                 }
  872.             }
  873.         }
  874.  
  875.         $metadesc = apply_filters( 'wpseo_metadesc', trim( $metadesc ) );
  876.  
  877.         if ( $echo !== false ) {
  878.             if ( !empty( $metadesc ) )
  879.                 echo '<meta name="description" content="' . esc_attr( strip_tags( stripslashes( $metadesc ) ) ) . '"/>' . "\n";
  880.             else if ( current_user_can( 'manage_options' ) && is_singular() )
  881.                 echo '<!-- ' . __( 'Admin only notice: this page doesn\'t show a meta description because it doesn\'t have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template.', 'wordpress-seo' ) . ' -->' . "\n";
  882.         } else {
  883.             return $metadesc;
  884.         }
  885.  
  886.     }
  887.  
  888.     /**
  889.      * Based on the redirect meta value, this function determines whether it should redirect the current post / page.
  890.      *
  891.      * @return mixed
  892.      */
  893.     function page_redirect() {
  894.         if ( is_singular() ) {
  895.             global $post;
  896.             if ( !isset( $post ) )
  897.                 return;
  898.             $redir = wpseo_get_value( 'redirect', $post->ID );
  899.             if ( !empty( $redir ) ) {
  900.                 wp_redirect( $redir, 301 );
  901.                 exit;
  902.             }
  903.         }
  904.     }
  905.  
  906.     /**
  907.      * Outputs noindex values for the current page.
  908.      */
  909.     public function noindex_page() {
  910.         echo '<meta name="robots" content="noindex" />' . "\n";
  911.     }
  912.  
  913.     /**
  914.      * Send a Robots HTTP header preventing feeds from being indexed in the search results while allowing search engines to follow the links in the feed.
  915.      *
  916.      * @since 1.1.7
  917.      */
  918.     public function noindex_feed() {
  919.         if ( is_feed() )
  920.             header( "X-Robots-Tag: noindex,follow", true );
  921.     }
  922.  
  923.     /**
  924.      * Adds rel="nofollow" to a link, only used for login / registration links.
  925.      *
  926.      * @param string $input The link element as a string.
  927.      * @return string
  928.      */
  929.     public function nofollow_link( $input ) {
  930.         return str_replace( '<a ', '<a rel="nofollow" ', $input );
  931.     }
  932.  
  933.     /**
  934.      * When certain archives are disabled, this redirects those to the homepage.
  935.      */
  936.     function archive_redirect() {
  937.         global $wp_query;
  938.  
  939.         if (
  940.             ( isset( $this->options['disable-date'] ) && $this->options['disable-date'] && $wp_query->is_date ) ||
  941.             ( isset( $this->options['disable-author'] ) && $this->options['disable-author'] && $wp_query->is_author ) ||
  942.             ( isset( $this->options['disable-post_formats'] ) && $this->options['disable-post_formats'] && $wp_query->is_tax( 'post_format' ) )
  943.         ) {
  944.             wp_safe_redirect( get_bloginfo( 'url' ), 301 );
  945.             exit;
  946.         }
  947.     }
  948.  
  949.     /**
  950.      * If the option to redirect attachments to their parent is checked, this performs the redirect.
  951.      *
  952.      * An extra check is done for when the attachment has no parent.
  953.      */
  954.     function attachment_redirect() {
  955.         global $post;
  956.         if ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && $post->post_parent != 0 ) {
  957.             wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
  958.             exit;
  959.         }
  960.     }
  961.  
  962.     /**
  963.      * Trailing slashes for everything except is_single().
  964.      *
  965.      * Thanks to Mark Jaquith for this code.
  966.      *
  967.      * @param string $url
  968.      * @param string $type
  969.      * @return string
  970.      */
  971.     function add_trailingslash( $url, $type ) {
  972.         if ( 'single' === $type || 'single_paged' === $type ) {
  973.             return $url;
  974.         } else {
  975.             return trailingslashit( $url );
  976.         }
  977.     }
  978.  
  979.     /**
  980.      * Removes the ?replytocom variable from the link, replacing it with a #comment-<number> anchor.
  981.      *
  982.      * @param string $link The comment link as a string.
  983.      * @return string
  984.      */
  985.     public function remove_reply_to_com( $link ) {
  986.         return preg_replace( '/href=\'(.*(\?|&)replytocom=(\d+)#respond)/', 'href=\'#comment-$3', $link );
  987.     }
  988.  
  989.     /**
  990.      * Removes unneeded query variables from the URL.
  991.      */
  992.     public function clean_permalink() {
  993.         if ( is_robots() || get_query_var( 'sitemap' ) )
  994.             return;
  995.  
  996.         global $wp_query;
  997.  
  998.         // Recreate current URL
  999.         $cururl = 'http';
  1000.         if ( isset( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" ) {
  1001.             $cururl .= "s";
  1002.         }
  1003.         $cururl .= "://";
  1004.         if ( $_SERVER["SERVER_PORT"] != "80" )
  1005.             $cururl .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  1006.         else
  1007.             $cururl .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  1008.  
  1009.         $properurl = '';
  1010.  
  1011.         if ( is_singular() ) {
  1012.             global $post;
  1013.             if ( empty( $post ) )
  1014.                 $post = $wp_query->get_queried_object();
  1015.  
  1016.             $properurl = get_permalink( $post->ID );
  1017.  
  1018.             $page = get_query_var( 'page' );
  1019.             if ( $page && $page != 1 ) {
  1020.                 $post       = get_post( $post->ID );
  1021.                 $page_count = substr_count( $post->post_content, '<!--nextpage-->' );
  1022.                 if ( $page > ( $page_count + 1 ) )
  1023.                     $properurl = user_trailingslashit( trailingslashit( $properurl ) . ( $page_count + 1 ) );
  1024.                 else
  1025.                     $properurl = user_trailingslashit( trailingslashit( $properurl ) . $page );
  1026.             }
  1027.  
  1028.             // Fix reply to comment links, whoever decided this should be a GET variable?
  1029.             $result = preg_match( '/(\?replytocom=[^&]+)/', $_SERVER["REQUEST_URI"], $matches );
  1030.             if ( $result )
  1031.                 $properurl .= str_replace( '?replytocom=', '#comment-', $matches[0] );
  1032.  
  1033.             // Prevent cleaning out posts & page previews for people capable of viewing them
  1034.             if ( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && current_user_can( 'edit_post' ) )
  1035.                 $properurl = '';
  1036.         } else if ( is_front_page() ) {
  1037.             if ( $this->is_home_posts_page() ) {
  1038.                 $properurl = get_bloginfo( 'url' ) . '/';
  1039.             } elseif ( $this->is_home_static_page() ) {
  1040.                 global $post;
  1041.                 $properurl = get_permalink( $post->ID );
  1042.             }
  1043.         } else if ( is_category() || is_tag() || is_tax() ) {
  1044.             $term = $wp_query->get_queried_object();
  1045.             if ( is_feed() )
  1046.                 $properurl = get_term_feed_link( $term->term_id, $term->taxonomy );
  1047.             else
  1048.                 $properurl = get_term_link( $term, $term->taxonomy );
  1049.         } else if ( is_search() ) {
  1050.             $s         = preg_replace( '/(%20|\+)/', ' ', get_search_query() );
  1051.             $properurl = get_bloginfo( 'url' ) . '/?s=' . rawurlencode( $s );
  1052.         } else if ( is_404() ) {
  1053.             if ( function_exists( 'is_multisite' ) && is_multisite() && !is_subdomain_install() && is_main_site() ) {
  1054.                 if ( $cururl == get_bloginfo( 'url' ) . '/blog/' || $cururl == get_bloginfo( 'url' ) . '/blog' ) {
  1055.                     if ( $this->is_home_static_page() )
  1056.                         $properurl = get_permalink( get_option( 'page_for_posts' ) );
  1057.                     else
  1058.                         $properurl = get_bloginfo( 'url' ) . '/';
  1059.                 }
  1060.             }
  1061.         }
  1062.  
  1063.         if ( !empty( $properurl ) && $wp_query->query_vars['paged'] != 0 && $wp_query->post_count != 0 ) {
  1064.             if ( is_search() ) {
  1065.                 $properurl = get_bloginfo( 'url' ) . '/page/' . $wp_query->query_vars['paged'] . '/?s=' . rawurlencode( get_search_query() );
  1066.             } else {
  1067.                 $properurl = user_trailingslashit( trailingslashit( $properurl ) . 'page/' . $wp_query->query_vars['paged'] );
  1068.             }
  1069.         }
  1070.  
  1071.         // Prevent cleaning out the WP Subscription managers interface for everyone
  1072.         foreach ( array( 'wp-subscription-manager' ) as $get ) {
  1073.             if ( isset( $_GET[$get] ) ) {
  1074.                 $properurl = '';
  1075.             }
  1076.         }
  1077.  
  1078.         // Allow plugins to register their own variables not to clean
  1079.         $whitelisted_extravars = apply_filters( 'wpseo_whitelist_permalink_vars', array() );
  1080.  
  1081.         if ( isset( $this->options['cleanpermalink-googlesitesearch'] ) && $this->options['cleanpermalink-googlesitesearch'] ) {
  1082.             // Prevent cleaning out Google Site searches
  1083.             $whitelisted_extravars = array_merge( $whitelisted_extravars, array( 'q', 'cx', 'debug', 'cof', 'ie', 'sa' ) );
  1084.         }
  1085.  
  1086.         if ( isset( $this->options['cleanpermalink-googlecampaign'] ) && $this->options['cleanpermalink-googlecampaign'] ) {
  1087.             // Prevent cleaning out Google Analytics campaign variables
  1088.             $whitelisted_extravars = array_merge( $whitelisted_extravars, array( 'utm_campaign', 'utm_medium', 'utm_source', 'utm_content', 'utm_term' ) );
  1089.         }
  1090.  
  1091.         if ( isset( $this->options['cleanpermalink-extravars'] ) && strlen( $this->options['cleanpermalink-extravars'] ) > 0 ) {
  1092.             $whitelisted_extravars = array_merge( $whitelisted_extravars, explode( ',', $this->options['cleanpermalink-extravars'] ) );
  1093.         }
  1094.  
  1095.         foreach ( $whitelisted_extravars as $get ) {
  1096.             if ( isset( $_GET[trim( $get )] ) ) {
  1097.                 $properurl = '';
  1098.             }
  1099.         }
  1100.  
  1101.         if ( !empty( $properurl ) && $cururl != $properurl ) {
  1102.             wp_safe_redirect( $properurl, 301 );
  1103.             exit;
  1104.         }
  1105.     }
  1106.  
  1107.     /**
  1108.      * Replaces the possible RSS variables with their actual values.
  1109.      *
  1110.      * @param string $content The RSS content that should have the variables replaced.
  1111.      * @return string
  1112.      */
  1113.     function rss_replace_vars( $content ) {
  1114.         global $post;
  1115.  
  1116.         $authorlink   = '<a rel="author" href="' . get_author_posts_url( $post->post_author ) . '">' . get_the_author() . '</a>';
  1117.         $postlink     = '<a href="' . get_permalink() . '">' . get_the_title() . "</a>";
  1118.         $bloglink     = '<a href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'name' ) . '</a>';
  1119.         $blogdesclink = '<a href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' ) . '</a>';
  1120.  
  1121.         $content = stripslashes( $content );
  1122.         $content = str_replace( "%%AUTHORLINK%%", $authorlink, $content );
  1123.         $content = str_replace( "%%POSTLINK%%", $postlink, $content );
  1124.         $content = str_replace( "%%BLOGLINK%%", $bloglink, $content );
  1125.         $content = str_replace( "%%BLOGDESCLINK%%", $blogdesclink, $content );
  1126.         return $content;
  1127.     }
  1128.  
  1129.     /**
  1130.      * Adds the RSS footer (or header) to the full RSS feed item.
  1131.      *
  1132.      * @param string $content Feed item content.
  1133.      * @return string
  1134.      */
  1135.     function embed_rssfooter( $content ) {
  1136.         if ( is_feed() ) {
  1137.  
  1138.             if ( isset( $this->options['rssbefore'] ) && !empty( $this->options['rssbefore'] ) ) {
  1139.                 $content = "<p>" . $this->rss_replace_vars( $this->options['rssbefore'] ) . "</p>" . $content;
  1140.             }
  1141.             if ( isset( $this->options['rssafter'] ) && !empty( $this->options['rssafter'] ) ) {
  1142.                 $content .= "<p>" . $this->rss_replace_vars( $this->options['rssafter'] ) . "</p>";
  1143.             }
  1144.         }
  1145.         return $content;
  1146.     }
  1147.  
  1148.     /**
  1149.      * Adds the RSS footer (or header) to the excerpt RSS feed item.
  1150.      *
  1151.      * @param string $content Feed item excerpt.
  1152.      * @return string
  1153.      */
  1154.     function embed_rssfooter_excerpt( $content ) {
  1155.         if ( is_feed() ) {
  1156.  
  1157.             if ( isset( $this->options['rssbefore'] ) && !empty( $this->options['rssbefore'] ) ) {
  1158.                 $content = "<p>" . $this->rss_replace_vars( $this->options['rssbefore'] ) . "</p><p>" . $content . "</p>";
  1159.             }
  1160.             if ( isset( $this->options['rssafter'] ) && !empty( $this->options['rssafter'] ) ) {
  1161.                 $content = "<p>" . $content . "</p><p>" . $this->rss_replace_vars( $this->options['rssafter'] ) . "</p>";
  1162.             }
  1163.         }
  1164.         return $content;
  1165.     }
  1166.  
  1167.     /**
  1168.      * Used in the force rewrite functionality this retrieves the output, replaces the title with the proper SEO
  1169.      * title and then flushes the output.
  1170.      */
  1171.     function flush_cache() {
  1172.         global $wp_query, $wpseo_ob, $sep;
  1173.  
  1174.         if ( !$wpseo_ob )
  1175.             return;
  1176.  
  1177.         $content = ob_get_contents();
  1178.  
  1179.         $old_wp_query = $wp_query;
  1180.  
  1181.         wp_reset_query();
  1182.  
  1183.         $title = $this->title( '', $sep );
  1184.  
  1185.         // Find all titles, strip them out and add the new one in within the debug marker, so it's easily identified whether a site uses force rewrite.
  1186.         if ( preg_match_all( '/<title>(.*)?<\/title>/i', $content, $matches ) ) {
  1187.             $count = count( $matches[0] );
  1188.             if ( $count > 0 ) {
  1189.                 $i = 0;
  1190.                 while ( $count > $i ) {
  1191.                     $content = str_replace( $matches[0][$i], '', $content );
  1192.                     $i++;
  1193.                 }
  1194.             }
  1195.         }
  1196.         $content = str_replace( $this->debug_marker( false ), $this->debug_marker( false ) . "\n" . '<title>' . $title . '</title>', $content );
  1197.  
  1198.         ob_end_clean();
  1199.  
  1200.         $GLOBALS['wp_query'] = $old_wp_query;
  1201.  
  1202.         echo $content;
  1203.     }
  1204.  
  1205.     /**
  1206.      * Starts the output buffer so it can later be fixed by flush_cache()
  1207.      */
  1208.     function force_rewrite_output_buffer() {
  1209.         global $wpseo_ob;
  1210.         $wpseo_ob = true;
  1211.         ob_start();
  1212.     }
  1213.  
  1214.     /**
  1215.      * Function used in testing whether the title should be force rewritten or not.
  1216.      *
  1217.      * @param string $title
  1218.      * @return string
  1219.      */
  1220.     function title_test_helper( $title ) {
  1221.         if ( !defined( 'DONOTCACHEPAGE' ) )
  1222.             define( 'DONOTCACHEPAGE', true );
  1223.  
  1224.         if ( !defined( 'DONOTCACHCEOBJECT' ) )
  1225.             define( 'DONOTCACHCEOBJECT', true );
  1226.  
  1227.         if ( !defined( 'DONOTMINIFY' ) )
  1228.             define( 'DONOTMINIFY', true );
  1229.  
  1230.         global $wp_version;
  1231.         if ( $_SERVER['HTTP_USER_AGENT'] == "WordPress/${wp_version}; " . get_bloginfo( 'url' ) . " - Yoast" )
  1232.             return 'This is a Yoast Test Title';
  1233.         return $title;
  1234.     }
  1235.  
  1236. }
  1237.  
  1238. function initialize_wpseo_front() {
  1239.     global $wpseo_front;
  1240.     $wpseo_front = new WPSEO_Frontend;
  1241. }
  1242. add_action( 'init', 'initialize_wpseo_front' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement