PageRenderTime 84ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/inc/wpseo-functions.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 367 lines | 250 code | 53 blank | 64 comment | 73 complexity | 4f899213228bb54ff030d588b6ca897a MD5 | raw file
  1. <?php
  2. /**
  3. * @package Internals
  4. */
  5. if ( !defined( 'WPSEO_VERSION' ) ) {
  6. header( 'HTTP/1.0 403 Forbidden' );
  7. die;
  8. }
  9. /**
  10. * Get the value from the post custom values
  11. *
  12. * @param string $val name of the value to get
  13. * @param int $postid post ID of the post to get the value for
  14. * @return bool|mixed
  15. */
  16. function wpseo_get_value( $val, $postid = 0 ) {
  17. $postid = absint( $postid );
  18. if ( $postid === 0 ) {
  19. global $post;
  20. if ( isset( $post ) && isset( $post->post_status ) && $post->post_status != 'auto-draft')
  21. $postid = $post->ID;
  22. else
  23. return false;
  24. }
  25. $custom = get_post_custom( $postid );
  26. if ( !empty( $custom['_yoast_wpseo_' . $val][0] ) )
  27. return maybe_unserialize( $custom['_yoast_wpseo_' . $val][0] );
  28. else
  29. return false;
  30. }
  31. /**
  32. * @param string $meta the meta to change
  33. * @param mixed $val the value to set the meta to
  34. * @param int $postid the ID of the post to change the meta for.
  35. */
  36. function wpseo_set_value( $meta, $val, $postid ) {
  37. update_post_meta( $postid, '_yoast_wpseo_' . $meta, $val );
  38. }
  39. /**
  40. * Retrieve an array of all the options the plugin uses. It can't use only one due to limitations of the options API.
  41. *
  42. * @return array of options.
  43. */
  44. function get_wpseo_options_arr() {
  45. $optarr = array( 'wpseo', 'wpseo_permalinks', 'wpseo_titles', 'wpseo_rss', 'wpseo_internallinks', 'wpseo_xml', 'wpseo_social' );
  46. return apply_filters( 'wpseo_options', $optarr );
  47. }
  48. /**
  49. * Retrieve all the options for the SEO plugin in one go.
  50. *
  51. * @return array of options
  52. */
  53. function get_wpseo_options() {
  54. static $options;
  55. if ( !isset( $options ) ) {
  56. $options = array();
  57. foreach ( get_wpseo_options_arr() as $opt ) {
  58. $options = array_merge( $options, (array) get_option( $opt ) );
  59. }
  60. }
  61. return $options;
  62. }
  63. /**
  64. * @param string $string the string to replace the variables in.
  65. * @param array $args the object some of the replacement values might come from, could be a post, taxonomy or term.
  66. * @param array $omit variables that should not be replaced by this function.
  67. * @return string
  68. */
  69. function wpseo_replace_vars( $string, $args, $omit = array() ) {
  70. $args = (array) $args;
  71. $string = strip_tags( $string );
  72. // Let's see if we can bail super early.
  73. if ( strpos( $string, '%%' ) === false )
  74. return trim( preg_replace( '/\s+/u', ' ', $string ) );
  75. global $sep;
  76. if ( !isset( $sep ) || empty( $sep ) )
  77. $sep = '-';
  78. $simple_replacements = array(
  79. '%%sep%%' => $sep,
  80. '%%sitename%%' => get_bloginfo( 'name' ),
  81. '%%sitedesc%%' => get_bloginfo( 'description' ),
  82. '%%currenttime%%' => date( get_option( 'time_format' ) ),
  83. '%%currentdate%%' => date( get_option( 'date_format' ) ),
  84. '%%currentday%%' => date( 'j' ),
  85. '%%currentmonth%%' => date( 'F' ),
  86. '%%currentyear%%' => date( 'Y' ),
  87. );
  88. foreach ( $simple_replacements as $var => $repl ) {
  89. $string = str_replace( $var, $repl, $string );
  90. }
  91. // Let's see if we can bail early.
  92. if ( strpos( $string, '%%' ) === false )
  93. return trim( preg_replace( '/\s+/u', ' ', $string ) );
  94. global $wp_query;
  95. $defaults = array(
  96. 'ID' => '',
  97. 'name' => '',
  98. 'post_author' => '',
  99. 'post_content' => '',
  100. 'post_date' => '',
  101. 'post_excerpt' => '',
  102. 'post_modified' => '',
  103. 'post_title' => '',
  104. 'taxonomy' => '',
  105. 'term_id' => '',
  106. );
  107. if ( isset( $args['post_content'] ) )
  108. $args['post_content'] = wpseo_strip_shortcode( $args['post_content'] );
  109. if ( isset( $args['post_excerpt'] ) )
  110. $args['post_excerpt'] = wpseo_strip_shortcode( $args['post_excerpt'] );
  111. $r = (object) wp_parse_args( $args, $defaults );
  112. $max_num_pages = 1;
  113. if ( !is_single() ) {
  114. $pagenum = get_query_var( 'paged' );
  115. if ( $pagenum === 0 )
  116. $pagenum = 1;
  117. if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages != '' && $wp_query->max_num_pages != 0 )
  118. $max_num_pages = $wp_query->max_num_pages;
  119. } else {
  120. global $post;
  121. $pagenum = get_query_var( 'page' );
  122. $max_num_pages = ( isset( $post->post_content ) ) ? substr_count( $post->post_content, '<!--nextpage-->' ) : 1;
  123. if ( $max_num_pages >= 1 )
  124. $max_num_pages++;
  125. }
  126. // Let's do date first as it's a bit more work to get right.
  127. if ( $r->post_date != '' ) {
  128. $date = mysql2date( get_option( 'date_format' ), $r->post_date );
  129. } else {
  130. if ( get_query_var( 'day' ) && get_query_var( 'day' ) != '' ) {
  131. $date = get_the_date();
  132. } else {
  133. if ( single_month_title( ' ', false ) && single_month_title( ' ', false ) != '' ) {
  134. $date = single_month_title( ' ', false );
  135. } else if ( get_query_var( 'year' ) != '' ) {
  136. $date = get_query_var( 'year' );
  137. } else {
  138. $date = '';
  139. }
  140. }
  141. }
  142. $replacements = array(
  143. '%%date%%' => $date,
  144. '%%searchphrase%%' => esc_html( get_query_var( 's' ) ),
  145. '%%page%%' => ( $max_num_pages > 1 && $pagenum > 1 ) ? sprintf( $sep . ' ' . __( 'Page %d of %d', 'wordpress-seo' ), $pagenum, $max_num_pages ) : '',
  146. '%%pagetotal%%' => $max_num_pages,
  147. '%%pagenumber%%' => $pagenum,
  148. );
  149. if ( isset( $r->ID ) ) {
  150. $replacements = array_merge( $replacements, array(
  151. '%%caption%%' => $r->post_excerpt,
  152. '%%category%%' => wpseo_get_terms( $r->ID, 'category' ),
  153. '%%excerpt%%' => ( !empty( $r->post_excerpt ) ) ? strip_tags( $r->post_excerpt ) : ( ( extension_loaded( 'mbstring' ) === true ) ? mb_substr( strip_shortcodes( strip_tags( $r->post_content ) ), 0, 155, 'UTF-8' ) : substr( strip_shortcodes( strip_tags( utf8_decode( $r->post_content ) ) ), 0, 155 ) ),
  154. '%%excerpt_only%%' => strip_tags( $r->post_excerpt ),
  155. '%%focuskw%%' => wpseo_get_value( 'focuskw', $r->ID ),
  156. '%%id%%' => $r->ID,
  157. '%%modified%%' => mysql2date( get_option( 'date_format' ), $r->post_modified ),
  158. '%%name%%' => get_the_author_meta( 'display_name', !empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ) ),
  159. '%%tag%%' => wpseo_get_terms( $r->ID, 'post_tag' ),
  160. '%%title%%' => stripslashes( $r->post_title ),
  161. '%%userid%%' => !empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ),
  162. ) );
  163. }
  164. if ( !empty( $r->taxonomy ) ) {
  165. $replacements = array_merge( $replacements, array(
  166. '%%category_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  167. '%%tag_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  168. '%%term_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  169. '%%term_title%%' => $r->name,
  170. ) );
  171. }
  172. foreach ( $replacements as $var => $repl ) {
  173. if ( !in_array( $var, $omit ) )
  174. $string = str_replace( $var, $repl, $string );
  175. }
  176. if ( strpos( $string, '%%' ) === false ) {
  177. $string = preg_replace( '/\s+/u', ' ', $string );
  178. return trim( $string );
  179. }
  180. if ( isset( $wp_query->query_vars['post_type'] ) && preg_match_all( '/%%pt_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  181. $pt = get_post_type_object( $wp_query->query_vars['post_type'] );
  182. $pt_plural = $pt_singular = $pt->name;
  183. if ( isset( $pt->labels->singular_name ) )
  184. $pt_singular = $pt->labels->singular_name;
  185. if ( isset( $pt->labels->name ) )
  186. $pt_plural = $pt->labels->name;
  187. $string = str_replace( '%%pt_single%%', $pt_singular, $string );
  188. $string = str_replace( '%%pt_plural%%', $pt_plural, $string );
  189. }
  190. if ( preg_match_all( '/%%cf_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  191. global $post;
  192. foreach ( $matches as $match ) {
  193. $string = str_replace( $match[0], get_post_meta( $post->ID, $match[1], true ), $string );
  194. }
  195. }
  196. if ( preg_match_all( '/%%ct_desc_([^%]+)?%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  197. global $post;
  198. foreach ( $matches as $match ) {
  199. $terms = get_the_terms( $post->ID, $match[1] );
  200. $string = str_replace( $match[0], get_term_field( 'description', $terms[0]->term_id, $match[1] ), $string );
  201. }
  202. }
  203. if ( preg_match_all( '/%%ct_([^%]+)%%(single%%)?/u', $string, $matches, PREG_SET_ORDER ) ) {
  204. foreach ( $matches as $match ) {
  205. $single = false;
  206. if ( isset( $match[2] ) && $match[2] == 'single%%' )
  207. $single = true;
  208. $ct_terms = wpseo_get_terms( $r->ID, $match[1], $single );
  209. $string = str_replace( $match[0], $ct_terms, $string );
  210. }
  211. }
  212. $string = preg_replace( '/\s+/u', ' ', $string );
  213. return trim( $string );
  214. }
  215. /**
  216. * Retrieve a post's terms, comma delimited.
  217. *
  218. * @param int $id ID of the post to get the terms for.
  219. * @param string $taxonomy The taxonomy to get the terms for this post from.
  220. * @param bool $return_single If true, return the first term.
  221. * @return string either a single term or a comma delimited string of terms.
  222. */
  223. function wpseo_get_terms( $id, $taxonomy, $return_single = false ) {
  224. // If we're on a specific tag, category or taxonomy page, return that and bail.
  225. if ( is_category() || is_tag() || is_tax() ) {
  226. global $wp_query;
  227. $term = $wp_query->get_queried_object();
  228. return $term->name;
  229. }
  230. if ( empty( $id ) || empty( $taxonomy ) )
  231. return '';
  232. $output = '';
  233. $terms = get_the_terms( $id, $taxonomy );
  234. if ( $terms ) {
  235. foreach ( $terms as $term ) {
  236. if ( $return_single )
  237. return $term->name;
  238. $output .= $term->name . ', ';
  239. }
  240. return rtrim( trim( $output ), ',' );
  241. }
  242. return '';
  243. }
  244. /**
  245. * Retrieve a taxonomy term's meta value.
  246. *
  247. * @param string|object $term term to get the meta value for
  248. * @param string $taxonomy name of the taxonomy to which the term is attached
  249. * @param string $meta meta value to get
  250. * @return bool|mixed value when the meta exists, false when it does not
  251. */
  252. function wpseo_get_term_meta( $term, $taxonomy, $meta ) {
  253. if ( is_string( $term ) )
  254. $term = get_term_by( 'slug', $term, $taxonomy );
  255. if ( is_object( $term ) )
  256. $term = $term->term_id;
  257. else
  258. return false;
  259. $tax_meta = get_option( 'wpseo_taxonomy_meta' );
  260. if ( isset( $tax_meta[$taxonomy][$term] ) )
  261. $tax_meta = $tax_meta[$taxonomy][$term];
  262. else
  263. return false;
  264. return ( isset( $tax_meta['wpseo_' . $meta] ) ) ? $tax_meta['wpseo_' . $meta] : false;
  265. }
  266. /**
  267. * Strip out the shortcodes with a filthy regex, because people don't properly register their shortcodes.
  268. *
  269. * @param string $text input string that might contain shortcodes
  270. * @return string $text string without shortcodes
  271. */
  272. function wpseo_strip_shortcode( $text ) {
  273. return preg_replace( '|\[[^\]]+\]|s', '', $text );
  274. }
  275. /**
  276. * Initialize sitemaps. Add sitemap rewrite rules and query var
  277. */
  278. function wpseo_xml_sitemaps_init() {
  279. $options = get_option( 'wpseo_xml' );
  280. if ( !isset( $options['enablexmlsitemap'] ) || !$options['enablexmlsitemap'] )
  281. return;
  282. $GLOBALS['wp']->add_query_var( 'sitemap' );
  283. $GLOBALS['wp']->add_query_var( 'sitemap_n' );
  284. add_rewrite_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
  285. add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
  286. }
  287. add_action( 'init', 'wpseo_xml_sitemaps_init', 1 );
  288. /**
  289. * Notify search engines of the updated sitemap.
  290. */
  291. function wpseo_ping_search_engines( $sitemapurl = null ) {
  292. $options = get_option( 'wpseo_xml' );
  293. $base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '';
  294. if ( $sitemapurl == null )
  295. $sitemapurl = urlencode( home_url( $base . 'sitemap_index.xml' ) );
  296. // Always ping Google and Bing, optionally ping Ask and Yahoo!
  297. wp_remote_get( 'http://www.google.com/webmasters/tools/ping?sitemap=' . $sitemapurl );
  298. wp_remote_get( 'http://www.bing.com/webmaster/ping.aspx?sitemap=' . $sitemapurl );
  299. if ( isset( $options['xml_ping_yahoo'] ) && $options['xml_ping_yahoo'] )
  300. wp_remote_get( 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=3usdTDLV34HbjQpIBuzMM1UkECFl5KDN7fogidABihmHBfqaebDuZk1vpLDR64I-&url=' . $sitemapurl );
  301. if ( isset( $options['xml_ping_ask'] ) && $options['xml_ping_ask'] )
  302. wp_remote_get( 'http://submissions.ask.com/ping?sitemap=' . $sitemapurl );
  303. }
  304. add_action( 'wpseo_ping_search_engines', 'wpseo_ping_search_engines' );
  305. function wpseo_store_tracking_response() {
  306. if ( ! wp_verify_nonce( $_POST['nonce'], 'wpseo_activate_tracking' ) )
  307. die();
  308. $options = get_option( 'wpseo' );
  309. $options['tracking_popup'] = 'done';
  310. if ( $_POST['allow_tracking'] == 'yes' )
  311. $options['yoast_tracking'] = true;
  312. update_option( 'wpseo', $options );
  313. }
  314. add_action('wp_ajax_wpseo_allow_tracking', 'wpseo_store_tracking_response');