PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/geniusdivision/genius-division-wordpress-theme
PHP | 388 lines | 263 code | 56 blank | 69 comment | 77 complexity | 4aaf3c5a8bf5b743512dac5dd2a81848 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. 'term404' => '',
  107. );
  108. if ( isset( $args['post_content'] ) )
  109. $args['post_content'] = wpseo_strip_shortcode( $args['post_content'] );
  110. if ( isset( $args['post_excerpt'] ) )
  111. $args['post_excerpt'] = wpseo_strip_shortcode( $args['post_excerpt'] );
  112. $r = (object) wp_parse_args( $args, $defaults );
  113. $max_num_pages = 1;
  114. if ( !is_single() ) {
  115. $pagenum = get_query_var( 'paged' );
  116. if ( $pagenum === 0 )
  117. $pagenum = 1;
  118. if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages != '' && $wp_query->max_num_pages != 0 )
  119. $max_num_pages = $wp_query->max_num_pages;
  120. } else {
  121. global $post;
  122. $pagenum = get_query_var( 'page' );
  123. $max_num_pages = ( isset( $post->post_content ) ) ? substr_count( $post->post_content, '<!--nextpage-->' ) : 1;
  124. if ( $max_num_pages >= 1 )
  125. $max_num_pages++;
  126. }
  127. // Let's do date first as it's a bit more work to get right.
  128. if ( $r->post_date != '' ) {
  129. $date = mysql2date( get_option( 'date_format' ), $r->post_date );
  130. } else {
  131. if ( get_query_var( 'day' ) && get_query_var( 'day' ) != '' ) {
  132. $date = get_the_date();
  133. } else {
  134. if ( single_month_title( ' ', false ) && single_month_title( ' ', false ) != '' ) {
  135. $date = single_month_title( ' ', false );
  136. } else if ( get_query_var( 'year' ) != '' ) {
  137. $date = get_query_var( 'year' );
  138. } else {
  139. $date = '';
  140. }
  141. }
  142. }
  143. $replacements = array(
  144. '%%date%%' => $date,
  145. '%%searchphrase%%' => esc_html( get_query_var( 's' ) ),
  146. '%%page%%' => ( $max_num_pages > 1 && $pagenum > 1 ) ? sprintf( $sep . ' ' . __( 'Page %d of %d', 'wordpress-seo' ), $pagenum, $max_num_pages ) : '',
  147. '%%pagetotal%%' => $max_num_pages,
  148. '%%pagenumber%%' => $pagenum,
  149. '%%term404%%' => sanitize_text_field ( str_replace( '-', ' ', $r->term404 ) ),
  150. );
  151. if ( isset( $r->ID ) ) {
  152. $replacements = array_merge( $replacements, array(
  153. '%%caption%%' => $r->post_excerpt,
  154. '%%category%%' => wpseo_get_terms( $r->ID, 'category' ),
  155. '%%excerpt%%' => ( !empty( $r->post_excerpt ) ) ? strip_tags( $r->post_excerpt ) : wp_html_excerpt( strip_shortcodes( $r->post_content ),155 ),
  156. '%%excerpt_only%%' => strip_tags( $r->post_excerpt ),
  157. '%%focuskw%%' => wpseo_get_value( 'focuskw', $r->ID ),
  158. '%%id%%' => $r->ID,
  159. '%%modified%%' => mysql2date( get_option( 'date_format' ), $r->post_modified ),
  160. '%%name%%' => get_the_author_meta( 'display_name', !empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ) ),
  161. '%%tag%%' => wpseo_get_terms( $r->ID, 'post_tag' ),
  162. '%%title%%' => stripslashes( $r->post_title ),
  163. '%%userid%%' => !empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ),
  164. ) );
  165. }
  166. if ( !empty( $r->taxonomy ) ) {
  167. $replacements = array_merge( $replacements, array(
  168. '%%category_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  169. '%%tag_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  170. '%%term_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
  171. '%%term_title%%' => $r->name,
  172. ) );
  173. }
  174. foreach ( $replacements as $var => $repl ) {
  175. if ( !in_array( $var, $omit ) )
  176. $string = str_replace( $var, $repl, $string );
  177. }
  178. if ( strpos( $string, '%%' ) === false ) {
  179. $string = preg_replace( '/\s+/u', ' ', $string );
  180. return trim( $string );
  181. }
  182. if ( isset( $wp_query->query_vars['post_type'] ) && preg_match_all( '/%%pt_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  183. $pt = get_post_type_object( $wp_query->query_vars['post_type'] );
  184. $pt_plural = $pt_singular = $pt->name;
  185. if ( isset( $pt->labels->singular_name ) )
  186. $pt_singular = $pt->labels->singular_name;
  187. if ( isset( $pt->labels->name ) )
  188. $pt_plural = $pt->labels->name;
  189. $string = str_replace( '%%pt_single%%', $pt_singular, $string );
  190. $string = str_replace( '%%pt_plural%%', $pt_plural, $string );
  191. }
  192. if ( preg_match_all( '/%%cf_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  193. global $post;
  194. foreach ( $matches as $match ) {
  195. $string = str_replace( $match[0], get_post_meta( $post->ID, $match[1], true ), $string );
  196. }
  197. }
  198. if ( preg_match_all( '/%%ct_desc_([^%]+)?%%/u', $string, $matches, PREG_SET_ORDER ) ) {
  199. global $post;
  200. foreach ( $matches as $match ) {
  201. $terms = get_the_terms( $post->ID, $match[1] );
  202. $string = str_replace( $match[0], get_term_field( 'description', $terms[0]->term_id, $match[1] ), $string );
  203. }
  204. }
  205. if ( preg_match_all( '/%%ct_([^%]+)%%(single%%)?/u', $string, $matches, PREG_SET_ORDER ) ) {
  206. foreach ( $matches as $match ) {
  207. $single = false;
  208. if ( isset( $match[2] ) && $match[2] == 'single%%' )
  209. $single = true;
  210. $ct_terms = wpseo_get_terms( $r->ID, $match[1], $single );
  211. $string = str_replace( $match[0], $ct_terms, $string );
  212. }
  213. }
  214. $string = preg_replace( '/\s+/u', ' ', $string );
  215. return trim( $string );
  216. }
  217. /**
  218. * Retrieve a post's terms, comma delimited.
  219. *
  220. * @param int $id ID of the post to get the terms for.
  221. * @param string $taxonomy The taxonomy to get the terms for this post from.
  222. * @param bool $return_single If true, return the first term.
  223. * @return string either a single term or a comma delimited string of terms.
  224. */
  225. function wpseo_get_terms( $id, $taxonomy, $return_single = false ) {
  226. // If we're on a specific tag, category or taxonomy page, return that and bail.
  227. if ( is_category() || is_tag() || is_tax() ) {
  228. global $wp_query;
  229. $term = $wp_query->get_queried_object();
  230. return $term->name;
  231. }
  232. if ( empty( $id ) || empty( $taxonomy ) )
  233. return '';
  234. $output = '';
  235. $terms = get_the_terms( $id, $taxonomy );
  236. if ( $terms ) {
  237. foreach ( $terms as $term ) {
  238. if ( $return_single )
  239. return $term->name;
  240. $output .= $term->name . ', ';
  241. }
  242. return rtrim( trim( $output ), ',' );
  243. }
  244. return '';
  245. }
  246. /**
  247. * Retrieve a taxonomy term's meta value.
  248. *
  249. * @param string|object $term term to get the meta value for
  250. * @param string $taxonomy name of the taxonomy to which the term is attached
  251. * @param string $meta meta value to get
  252. * @return bool|mixed value when the meta exists, false when it does not
  253. */
  254. function wpseo_get_term_meta( $term, $taxonomy, $meta ) {
  255. if ( is_string( $term ) )
  256. $term = get_term_by( 'slug', $term, $taxonomy );
  257. if ( is_object( $term ) )
  258. $term = $term->term_id;
  259. else
  260. return false;
  261. $tax_meta = get_option( 'wpseo_taxonomy_meta' );
  262. if ( isset( $tax_meta[$taxonomy][$term] ) )
  263. $tax_meta = $tax_meta[$taxonomy][$term];
  264. else
  265. return false;
  266. return ( isset( $tax_meta['wpseo_' . $meta] ) ) ? $tax_meta['wpseo_' . $meta] : false;
  267. }
  268. /**
  269. * Strip out the shortcodes with a filthy regex, because people don't properly register their shortcodes.
  270. *
  271. * @param string $text input string that might contain shortcodes
  272. * @return string $text string without shortcodes
  273. */
  274. function wpseo_strip_shortcode( $text ) {
  275. return preg_replace( '|\[[^\]]+\]|s', '', $text );
  276. }
  277. /**
  278. * Redirect /sitemap.xml to /sitemap_index.xml
  279. */
  280. function wpseo_xml_redirect_sitemap() {
  281. global $wp_query;
  282. $current_url =( isset($_SERVER["HTTPS"] ) && $_SERVER["HTTPS"]=='on' ) ? 'https://' : 'http://';
  283. $current_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  284. // must be 'sitemap.xml' and must be 404
  285. if ( home_url( '/sitemap.xml' ) == $current_url && $wp_query->is_404) {
  286. wp_redirect( home_url( '/sitemap_index.xml' ) );
  287. }
  288. }
  289. /**
  290. * Initialize sitemaps. Add sitemap rewrite rules and query var
  291. */
  292. function wpseo_xml_sitemaps_init() {
  293. $options = get_option( 'wpseo_xml' );
  294. if ( !isset( $options['enablexmlsitemap'] ) || !$options['enablexmlsitemap'] )
  295. return;
  296. // redirects sitemap.xml to sitemap_index.xml
  297. add_action( 'template_redirect', 'wpseo_xml_redirect_sitemap', 0 );
  298. $GLOBALS['wp']->add_query_var( 'sitemap' );
  299. $GLOBALS['wp']->add_query_var( 'sitemap_n' );
  300. $GLOBALS['wp']->add_query_var( 'xslt' );
  301. add_rewrite_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
  302. add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
  303. add_rewrite_rule( 'sitemap\.xslt$', 'index.php?xslt=1', 'top' );
  304. }
  305. add_action( 'init', 'wpseo_xml_sitemaps_init', 1 );
  306. /**
  307. * Notify search engines of the updated sitemap.
  308. */
  309. function wpseo_ping_search_engines( $sitemapurl = null ) {
  310. $options = get_option( 'wpseo_xml' );
  311. $base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '';
  312. if ( $sitemapurl == null )
  313. $sitemapurl = urlencode( home_url( $base . 'sitemap_index.xml' ) );
  314. // Always ping Google and Bing, optionally ping Ask and Yahoo!
  315. wp_remote_get( 'http://www.google.com/webmasters/tools/ping?sitemap=' . $sitemapurl );
  316. wp_remote_get( 'http://www.bing.com/webmaster/ping.aspx?sitemap=' . $sitemapurl );
  317. if ( isset( $options['xml_ping_yahoo'] ) && $options['xml_ping_yahoo'] )
  318. wp_remote_get( 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=3usdTDLV34HbjQpIBuzMM1UkECFl5KDN7fogidABihmHBfqaebDuZk1vpLDR64I-&url=' . $sitemapurl );
  319. if ( isset( $options['xml_ping_ask'] ) && $options['xml_ping_ask'] )
  320. wp_remote_get( 'http://submissions.ask.com/ping?sitemap=' . $sitemapurl );
  321. }
  322. add_action( 'wpseo_ping_search_engines', 'wpseo_ping_search_engines' );
  323. function wpseo_store_tracking_response() {
  324. if ( ! wp_verify_nonce( $_POST['nonce'], 'wpseo_activate_tracking' ) )
  325. die();
  326. $options = get_option( 'wpseo' );
  327. $options['tracking_popup'] = 'done';
  328. if ( $_POST['allow_tracking'] == 'yes' )
  329. $options['yoast_tracking'] = true;
  330. update_option( 'wpseo', $options );
  331. }
  332. add_action('wp_ajax_wpseo_allow_tracking', 'wpseo_store_tracking_response');