/content/plugins/w3-total-cache/Util_PageUrls.php

https://gitlab.com/karlen/ayo_wp · PHP · 788 lines · 492 code · 127 blank · 169 comment · 107 complexity · a5053972aeb973a3b23112d8cf0ae386 MD5 · raw file

  1. <?php
  2. namespace W3TC;
  3. class Util_PageUrls {
  4. /**
  5. * Returns full urls for frontpage including older pages
  6. *
  7. * @param int $limit_post_pages default is 10
  8. * @return array
  9. */
  10. static public function get_frontpage_urls( $limit_post_pages = 10 ) {
  11. static $frontpage_urls = array();
  12. if ( !isset( $frontpage_urls[$limit_post_pages] ) ) {
  13. $front_page = get_option( 'show_on_front' );
  14. $full_urls = array();
  15. $home_path = Util_Environment::home_url_uri();
  16. $site_path = Util_Environment::site_url_uri();
  17. $full_urls[] = get_home_url() . '/';
  18. if ( $site_path != $home_path ) {
  19. $full_urls[] = site_url() . '/';
  20. }
  21. $full_urls = array_merge( $full_urls,
  22. self::get_older_pages( $home_path, $limit_post_pages ) );
  23. $frontpage_urls[$limit_post_pages] = $full_urls;
  24. }
  25. return $frontpage_urls[$limit_post_pages];
  26. }
  27. /**
  28. * Return full urls for the page with posts including older pages
  29. *
  30. * @param int $limit_post_pages default is 10
  31. * @return array
  32. */
  33. static public function get_postpage_urls( $limit_post_pages = 10 ) {
  34. static $postpage_urls = array();
  35. if ( !isset( $postpage_urls[$limit_post_pages] ) ) {
  36. $posts_page_uri = '';
  37. $full_urls = array();
  38. $posts_page_id = get_option( 'page_for_posts' );
  39. if ( $posts_page_id ) {
  40. $posts_page_uri = get_page_uri( $posts_page_id );
  41. $page_link = get_home_url() . '/' . trim( $posts_page_uri, '/' ) . '/';
  42. $full_urls[] = $page_link;
  43. }
  44. if ( $posts_page_uri )
  45. $full_urls = array_merge( $full_urls, self::get_older_pages( $posts_page_uri, $limit_post_pages ) );
  46. $postpage_urls[$limit_post_pages] = $full_urls;
  47. }
  48. return $postpage_urls[$limit_post_pages];
  49. }
  50. /**
  51. * Return older pages listing posts
  52. *
  53. * @param unknown $posts_page_uri
  54. * @param int $limit_post_pages default is 10
  55. * @return array
  56. */
  57. static private function get_older_pages( $posts_page_uri, $limit_post_pages = 10 ) {
  58. $full_urls = array();
  59. $count_posts = wp_count_posts();
  60. $posts_number = $count_posts->publish;
  61. $posts_per_page = get_option( 'posts_per_page' );
  62. $posts_pages_number = @ceil( $posts_number / $posts_per_page );
  63. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  64. $posts_pages_number = $limit_post_pages;
  65. }
  66. for ( $pagenum = 2; $pagenum <= $posts_pages_number; $pagenum++ ) {
  67. $home_pagenum_link = self::get_pagenum_link( $posts_page_uri, $pagenum );
  68. $full_urls[] = $home_pagenum_link;
  69. }
  70. return $full_urls;
  71. }
  72. /**
  73. * Returns all urls related to a post
  74. *
  75. * @param unknown $post_id
  76. * @return array
  77. */
  78. static public function get_post_urls( $post_id ) {
  79. static $post_urls = array();
  80. if ( !isset( $post_urls[$post_id] ) ) {
  81. $full_urls = array();
  82. $post_link = get_permalink( $post_id );
  83. $post_uri = str_replace( Util_Environment::home_domain_root_url(), '', $post_link );
  84. $full_urls[] = $post_link;
  85. $uris[] = $post_uri;
  86. $post = get_post( $post_id );
  87. $matches =array();
  88. if ( $post && ( $post_pages_number = preg_match_all( '/\<\!\-\-nextpage\-\-\>/', $post->post_content, $matches ) )>0 ) {
  89. global $wp_rewrite;
  90. $post_pages_number++;
  91. for ( $pagenum = 2; $pagenum <= $post_pages_number; $pagenum++ ) {
  92. if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID )
  93. $post_pagenum_link = trailingslashit( $post_link ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $pagenum, 'single_paged' );
  94. else
  95. $post_pagenum_link = trailingslashit( $post_link ) . user_trailingslashit( $pagenum, 'single_paged' );
  96. $full_urls[] = $post_pagenum_link;
  97. }
  98. }
  99. $post_urls[$post_id] = $full_urls;
  100. }
  101. return $post_urls[$post_id];
  102. }
  103. /**
  104. * Return full urls for the posts comment pages
  105. *
  106. * @param unknown $post_id
  107. * @return array
  108. */
  109. static public function get_post_comments_urls( $post_id ) {
  110. static $post_comments_urls = array();
  111. if ( !isset( $post_comments_urls[$post_id] ) ) {
  112. $full_urls = array();
  113. $comments_number = get_comments_number( $post_id );
  114. $comments_per_page = get_option( 'comments_per_page' );
  115. $comments_pages_number = @ceil( $comments_number / $comments_per_page );
  116. for ( $pagenum = 1; $pagenum <= $comments_pages_number; $pagenum++ ) {
  117. $comments_pagenum_link = self::get_comments_pagenum_link( $post_id, $pagenum );
  118. $full_urls[] = $comments_pagenum_link;
  119. }
  120. $post_comments_urls[$post_id] = $full_urls;
  121. }
  122. return $post_comments_urls[$post_id];
  123. }
  124. /**
  125. * Return full urls for the authors pages
  126. *
  127. * @param unknown $post_author
  128. * @param int $limit_post_pages default is 10
  129. * @return array
  130. */
  131. static public function get_post_author_urls( $post_author, $limit_post_pages = 10 ) {
  132. static $feed_author_urls = array();
  133. $key = md5( $post_author . ',' . $limit_post_pages );
  134. if ( !isset( $post_author_urls[$key] ) ) {
  135. $full_urls = array();
  136. $posts_number = count_user_posts( $post_author );
  137. $posts_per_page = get_option( 'posts_per_page' );
  138. $posts_pages_number = @ceil( $posts_number / $posts_per_page );
  139. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  140. $posts_pages_number = $limit_post_pages;
  141. }
  142. $author_link = get_author_posts_url( $post_author );
  143. $author_uri = str_replace( Util_Environment::home_domain_root_url(), '', $author_link );
  144. for ( $pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++ ) {
  145. $author_pagenum_link = self::get_pagenum_link( $author_uri, $pagenum );
  146. $full_urls[] = $author_pagenum_link;
  147. }
  148. $post_author_urls[$key] = $full_urls;
  149. }
  150. return $post_author_urls[$key];
  151. }
  152. /**
  153. * Returns full urls to post terms pages
  154. *
  155. * @param unknown $terms
  156. * @param int $limit_post_pages default is 10
  157. * @return array
  158. */
  159. static public function get_post_terms_urls( $terms, $limit_post_pages = 10 ) {
  160. static $post_terms_urls = array();
  161. $key = md5( self::_term_hash( $terms ) . ',' . $limit_post_pages );
  162. if ( !isset( $post_terms_urls[$key] ) ) {
  163. $full_urls = array();
  164. $posts_per_page = get_option( 'posts_per_page' );
  165. foreach ( $terms as $term ) {
  166. $term_link = get_term_link( $term, $term->taxonomy );
  167. $term_uri = str_replace( Util_Environment::home_domain_root_url(), '', $term_link );
  168. $posts_pages_number = @ceil( $term->count / $posts_per_page );
  169. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  170. $posts_pages_number = $limit_post_pages;
  171. }
  172. for ( $pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++ ) {
  173. $term_pagenum_link = self::get_pagenum_link( $term_uri, $pagenum );
  174. $full_urls[] = $term_pagenum_link;
  175. }
  176. }
  177. $post_terms_urls[$key] = $full_urls;
  178. }
  179. return $post_terms_urls[$key];
  180. }
  181. /**
  182. * Return full urls for daily archive pages based on provided post
  183. *
  184. * @param unknown $post
  185. * @param int $limit_post_pages default is 10
  186. * @return array
  187. */
  188. static public function get_daily_archive_urls( $post, $limit_post_pages = 10 ) {
  189. static $daily_archive_urls = array();
  190. $post_type = $post->post_type;
  191. $archive_slug = self::_get_archive_slug( $post );
  192. $key = md5( $post->ID . ',' . $limit_post_pages );
  193. if ( !isset( $daily_archive_urls[$key] ) ) {
  194. $full_urls = array();
  195. $post_date = strtotime( $post->post_date );
  196. $post_year = gmdate( 'Y', $post_date );
  197. $post_month = gmdate( 'm', $post_date );
  198. $post_day = gmdate( 'd', $post_date );
  199. $posts_per_page = get_option( 'posts_per_page' );
  200. $posts_number = self::get_archive_posts_count( $post_year, $post_month, $post_day, $post_type );
  201. $posts_pages_number = @ceil( $posts_number / $posts_per_page );
  202. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  203. $posts_pages_number = $limit_post_pages;
  204. }
  205. $day_link = get_day_link( $post_year, $post_month, $post_day );
  206. $day_uri = $archive_slug . str_replace( Util_Environment::home_domain_root_url(), '', $day_link );
  207. for ( $pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++ ) {
  208. $day_pagenum_link = self::get_pagenum_link( $day_uri, $pagenum );
  209. $full_urls[] = $day_pagenum_link;
  210. }
  211. $daily_archive_urls[$key] = $full_urls;
  212. }
  213. return $daily_archive_urls[$key];
  214. }
  215. /**
  216. * Return full urls for montly archive pages based on provided post
  217. *
  218. * @param unknown $post
  219. * @param int $limit_post_pages default is 10
  220. * @return array
  221. */
  222. static public function get_monthly_archive_urls( $post, $limit_post_pages = 10 ) {
  223. static $monthly_archive_urls = array();
  224. $post_type = $post->post_type;
  225. $archive_slug = self::_get_archive_slug( $post );
  226. $key = md5( $post->ID . ',' . $limit_post_pages );
  227. if ( !isset( $monthly_archive_urls[$key] ) ) {
  228. $full_urls = array();
  229. $post_date = strtotime( $post->post_date );
  230. $post_year = gmdate( 'Y', $post_date );
  231. $post_month = gmdate( 'm', $post_date );
  232. $posts_per_page = get_option( 'posts_per_page' );
  233. $posts_number = self::get_archive_posts_count( $post_year, $post_month, '', $post_type );
  234. $posts_pages_number = @ceil( $posts_number / $posts_per_page );
  235. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  236. $posts_pages_number = $limit_post_pages;
  237. }
  238. $month_link = get_month_link( $post_year, $post_month );
  239. $month_uri = $archive_slug . str_replace( Util_Environment::home_domain_root_url(), '', $month_link );
  240. for ( $pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++ ) {
  241. $month_pagenum_link = self::get_pagenum_link( $month_uri, $pagenum );
  242. $full_urls[] = $month_pagenum_link;
  243. }
  244. $monthly_archive_urls[$key] = $full_urls;
  245. }
  246. return $monthly_archive_urls[$key];
  247. }
  248. /**
  249. * Return full urls for yearly archive pages based on provided post
  250. *
  251. * @param unknown $post
  252. * @param int $limit_post_pages default is 10
  253. * @return array
  254. */
  255. static public function get_yearly_archive_urls( $post, $limit_post_pages = 10 ) {
  256. static $yearly_archive_urls = array();
  257. $post_type = $post->post_type;
  258. $archive_slug = self::_get_archive_slug( $post );
  259. $key = md5( $post->ID . ',' . $limit_post_pages );
  260. if ( !isset( $yearly_archive_urls[$key] ) ) {
  261. $full_urls = array();
  262. $post_date = strtotime( $post->post_date );
  263. $post_year = gmdate( 'Y', $post_date );
  264. $posts_per_page = get_option( 'posts_per_page' );
  265. $posts_number =self::get_archive_posts_count( $post_year, '', '', $post_type );
  266. $posts_pages_number = @ceil( $posts_number / $posts_per_page );
  267. if ( $limit_post_pages > 0 && $posts_pages_number > $limit_post_pages ) {
  268. $posts_pages_number = $limit_post_pages;
  269. }
  270. $year_link = get_year_link( $post_year );
  271. $year_uri = $archive_slug . str_replace( Util_Environment::home_domain_root_url(), '', $year_link );
  272. for ( $pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++ ) {
  273. $year_pagenum_link = self::get_pagenum_link( $year_uri, $pagenum );
  274. $full_urls[] = $year_pagenum_link;
  275. }
  276. $yearly_archive_urls[$key] = $full_urls;
  277. }
  278. return $yearly_archive_urls[$key];
  279. }
  280. /**
  281. * Return full urls for the provided feed types
  282. *
  283. * @param unknown $feeds
  284. * @param string|null $post_type
  285. * @return array
  286. */
  287. static public function get_feed_urls( $feeds, $post_type = null ) {
  288. static $feed_urls = array();
  289. $key = md5( implode( ',', $feeds ) . $post_type );
  290. if ( !isset( $feed_urls[$key] ) ) {
  291. $full_urls = array();
  292. foreach ( $feeds as $feed ) {
  293. $feed_link = self::get_feed_link( $feed, $post_type );
  294. $full_urls[] = $feed_link;
  295. }
  296. $feed_urls[$key] = $full_urls;
  297. }
  298. return $feed_urls[$key];
  299. }
  300. /**
  301. * Return full urls for the provided post id and feed types
  302. *
  303. * @param unknown $post_id
  304. * @param unknown $feeds
  305. * @return array
  306. */
  307. static public function get_feed_comments_urls( $post_id, $feeds ) {
  308. static $feed_comments_urls = array();
  309. $key = md5( implode( ',', $feeds ) . $post_id );
  310. if ( !isset( $feed_comments_urls[$key] ) ) {
  311. $full_urls = array();
  312. foreach ( $feeds as $feed ) {
  313. $post_comments_feed_link = self::get_post_comments_feed_link( $post_id, $feed );
  314. $full_urls[] = $post_comments_feed_link;
  315. }
  316. $feed_comments_urls[$key] = $full_urls;
  317. }
  318. return $feed_comments_urls[$key];
  319. }
  320. /**
  321. * Returns full urls for the provided post author and feed types
  322. *
  323. * @param unknown $post_author
  324. * @param unknown $feeds
  325. * @return array
  326. */
  327. static public function get_feed_author_urls( $post_author, $feeds ) {
  328. static $post_author_urls = array();
  329. $key = md5( implode( ',', $feeds ) . $post_author );
  330. if ( !isset( $feed_author_urls[$key] ) ) {
  331. $full_urls = array();
  332. foreach ( $feeds as $feed ) {
  333. $author_feed_link = self::get_author_feed_link( $post_author, $feed );
  334. $full_urls[] = $author_feed_link;
  335. }
  336. $feed_author_urls[$key] = $full_urls;
  337. }
  338. return $feed_author_urls[$key];
  339. }
  340. /**
  341. * Returns full urls for the provided terms and feed types
  342. *
  343. * @param unknown $terms
  344. * @param unknown $feeds
  345. * @return array
  346. */
  347. static public function get_feed_terms_urls( $terms, $feeds ) {
  348. static $feed_terms_urls = array();
  349. $key = md5( implode( ',', $feeds ) . self::_term_hash( $terms ) );
  350. if ( !isset( $feed_terms_urls[$key] ) ) {
  351. $full_urls = array();
  352. foreach ( $terms as $term ) {
  353. foreach ( $feeds as $feed ) {
  354. $term_feed_link = self::get_term_feed_link( $term->term_id, $term->taxonomy, $feed );
  355. $full_urls[] = $term_feed_link;
  356. }
  357. }
  358. $feed_terms_urls[$key] = $full_urls;
  359. }
  360. return $feed_terms_urls[$key];
  361. }
  362. /**
  363. * Returns full urls for the provided url path based pages, ie /some/page.
  364. *
  365. * @param unknown $pages
  366. * @return array
  367. */
  368. static public function get_pages_urls( $pages ) {
  369. static $pages_urls = array();
  370. $key = md5( implode( ',', $pages ) );
  371. if ( !isset( $pages_urls[$key] ) ) {
  372. $full_urls = array();
  373. foreach ( $pages as $page_slug ) {
  374. if ( $page_slug ) {
  375. $page_link = get_home_url() . '/' . trim( $page_slug, '/' ) . '/';
  376. $full_urls[] = $page_link;
  377. }
  378. }
  379. $pages_urls[$key] = $full_urls;
  380. }
  381. return $pages_urls[$key];
  382. }
  383. /**
  384. * Workaround for get_pagenum_link function
  385. *
  386. * @param string $url
  387. * @param int $pagenum
  388. * @return string
  389. */
  390. static public function get_pagenum_link( $url, $pagenum = 1 ) {
  391. $request_uri = $_SERVER['REQUEST_URI'];
  392. $_SERVER['REQUEST_URI'] = $url;
  393. if ( is_admin() ) {
  394. $link = self::get_pagenum_link_admin( $pagenum );
  395. } else {
  396. $link = get_pagenum_link( $pagenum );
  397. }
  398. $_SERVER['REQUEST_URI'] = $request_uri;
  399. return $link;
  400. }
  401. /**
  402. * Workaround for get_pagenum_link static public function when in admin
  403. *
  404. * @param unknown $pagenum
  405. * @return string
  406. */
  407. static public function get_pagenum_link_admin( $pagenum ) {
  408. global $wp_rewrite;
  409. $pagenum = (int) $pagenum;
  410. $request = remove_query_arg( 'paged' );
  411. $home_root = parse_url( home_url() );
  412. $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
  413. $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  414. $request = preg_replace( '|^'. $home_root . '|', '', $request );
  415. $request = preg_replace( '|^/+|', '', $request );
  416. $qs_regex = '|\?.*?$|';
  417. preg_match( $qs_regex, $request, $qs_match );
  418. if ( !empty( $qs_match[0] ) ) {
  419. $query_string = $qs_match[0];
  420. $request = preg_replace( $qs_regex, '', $request );
  421. } else {
  422. $query_string = '';
  423. }
  424. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request );
  425. $request = preg_replace( '|^index\.php|', '', $request );
  426. $request = ltrim( $request, '/' );
  427. $base = trailingslashit( get_bloginfo( 'url' ) );
  428. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  429. $base .= 'index.php/';
  430. if ( $pagenum > 1 ) {
  431. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  432. }
  433. $result = $base . $request . $query_string;
  434. $result = apply_filters( 'get_pagenum_link', $result );
  435. return $result;
  436. }
  437. /**
  438. * Workaround for get_comments_pagenum_link function
  439. *
  440. * @param integer $post_id
  441. * @param integer $pagenum
  442. * @param integer $max_page
  443. * @return string
  444. */
  445. static public function get_comments_pagenum_link( $post_id, $pagenum = 1, $max_page = 0 ) {
  446. if ( isset( $GLOBALS['post'] ) && is_object( $GLOBALS['post'] ) ) {
  447. $old_post = &$GLOBALS['post'];
  448. } else {
  449. $GLOBALS['post'] = new stdClass();
  450. $old_post = null;
  451. }
  452. $GLOBALS['post']->ID = $post_id;
  453. $link = get_comments_pagenum_link( $pagenum, $max_page );
  454. if ( $old_post ) {
  455. $GLOBALS['post'] = &$old_post;
  456. }
  457. return $link;
  458. }
  459. /**
  460. * Returns number of posts in the archive
  461. *
  462. * @param int $year
  463. * @param int $month
  464. * @param int $day
  465. * @return int
  466. */
  467. static public function get_archive_posts_count( $year = 0, $month = 0, $day = 0, $post_type/* = 'post'*/ ) {
  468. global $wpdb;
  469. $filters = array(
  470. 'post_type = "' . $post_type .'"',
  471. 'post_status = "publish"'
  472. );
  473. if ( $year ) {
  474. $filters[] = sprintf( 'YEAR(post_date) = %d', $year );
  475. }
  476. if ( $month ) {
  477. $filters[] = sprintf( 'MONTH(post_date) = %d', $month );
  478. }
  479. if ( $day ) {
  480. $filters[] = sprintf( 'DAY(post_date) = %d', $day );
  481. }
  482. $where = implode( ' AND ', $filters );
  483. $sql = sprintf( 'SELECT COUNT(*) FROM %s WHERE %s', $wpdb->posts, $where );
  484. $count = (int) $wpdb->get_var( $sql );
  485. return $count;
  486. }
  487. /**
  488. * Workaround for get_feed_link function, remove filtering.
  489. *
  490. * @param string $feed
  491. * @param null|string $post_type
  492. * @return mixed
  493. */
  494. static public function get_feed_link( $feed = '', $post_type=null ) {
  495. /**
  496. *
  497. *
  498. * @var $wp_rewrite WP_Rewrite
  499. */
  500. global $wp_rewrite;
  501. if ( $post_type )
  502. return get_post_type_archive_feed_link( $post_type, $feed );
  503. $permalink = $wp_rewrite->get_feed_permastruct();
  504. if ( '' != $permalink ) {
  505. if ( false !== strpos( $feed, 'comments_' ) ) {
  506. $feed = str_replace( 'comments_', '', $feed );
  507. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  508. }
  509. if ( get_default_feed() == $feed )
  510. $feed = '';
  511. $permalink = str_replace( '%feed%', $feed, $permalink );
  512. $permalink = preg_replace( '#/+#', '/', "/$permalink" );
  513. $output = home_url( user_trailingslashit( $permalink, 'feed' ) );
  514. } else {
  515. if ( empty( $feed ) )
  516. $feed = get_default_feed();
  517. if ( false !== strpos( $feed, 'comments_' ) )
  518. $feed = str_replace( 'comments_', 'comments-', $feed );
  519. $output = home_url( "?feed={$feed}" );
  520. }
  521. return $output;
  522. }
  523. /**
  524. * Workaround for get_post_comments_feed_link function, remove filtering.
  525. *
  526. * @param int $post_id
  527. * @param string $feed
  528. * @return string
  529. */
  530. static public function get_post_comments_feed_link( $post_id = 0, $feed = '' ) {
  531. $post_id = absint( $post_id );
  532. if ( ! $post_id )
  533. $post_id = get_the_ID();
  534. if ( empty( $feed ) )
  535. $feed = get_default_feed();
  536. if ( '' != get_option( 'permalink_structure' ) ) {
  537. if ( 'page' == get_option( 'show_on_front' ) && $post_id == get_option( 'page_on_front' ) )
  538. $url = _get_page_link( $post_id );
  539. else
  540. $url = get_permalink( $post_id );
  541. $url = trailingslashit( $url ) . 'feed';
  542. if ( $feed != get_default_feed() )
  543. $url .= "/$feed";
  544. $url = user_trailingslashit( $url, 'single_feed' );
  545. } else {
  546. $type = get_post_field( 'post_type', $post_id );
  547. if ( 'page' == $type )
  548. $url = home_url( "?feed=$feed&amp;page_id=$post_id" );
  549. else
  550. $url = home_url( "?feed=$feed&amp;p=$post_id" );
  551. }
  552. return $url;
  553. }
  554. /**
  555. * Workaround for get_author_feed_link function, remove filtering.
  556. *
  557. * @param unknown $author_id
  558. * @param string $feed
  559. * @return string
  560. */
  561. static public function get_author_feed_link( $author_id, $feed = '' ) {
  562. $author_id = (int) $author_id;
  563. $permalink_structure = get_option( 'permalink_structure' );
  564. if ( empty( $feed ) )
  565. $feed = get_default_feed();
  566. if ( '' == $permalink_structure ) {
  567. $link = home_url( "?feed=$feed&amp;author=" . $author_id );
  568. } else {
  569. $link = get_author_posts_url( $author_id );
  570. if ( $feed == get_default_feed() )
  571. $feed_link = 'feed';
  572. else
  573. $feed_link = "feed/$feed";
  574. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  575. }
  576. return $link;
  577. }
  578. /**
  579. * Workaround for get_term_feed_link function, remove filtering.
  580. *
  581. * @param unknown $term_id
  582. * @param string $taxonomy
  583. * @param string $feed
  584. * @return bool|string
  585. */
  586. static public function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  587. $term_id = ( int ) $term_id;
  588. $term = get_term( $term_id, $taxonomy );
  589. if ( empty( $term ) || is_wp_error( $term ) )
  590. return false;
  591. if ( empty( $feed ) )
  592. $feed = get_default_feed();
  593. $permalink_structure = get_option( 'permalink_structure' );
  594. if ( '' == $permalink_structure ) {
  595. if ( 'category' == $taxonomy ) {
  596. $link = home_url( "?feed=$feed&amp;cat=$term_id" );
  597. }
  598. elseif ( 'post_tag' == $taxonomy ) {
  599. $link = home_url( "?feed=$feed&amp;tag=$term->slug" );
  600. } else {
  601. $t = get_taxonomy( $taxonomy );
  602. $link = home_url( "?feed=$feed&amp;$t->query_var=$term->slug" );
  603. }
  604. } else {
  605. $link = get_term_link( $term_id, $term->taxonomy );
  606. if ( $feed == get_default_feed() )
  607. $feed_link = 'feed';
  608. else
  609. $feed_link = "feed/$feed";
  610. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  611. }
  612. return $link;
  613. }
  614. static public function complement_with_mirror_urls( $queued_urls ) {
  615. $config = Dispatcher::config();
  616. if ( !$config->get_boolean( 'pgcache.mirrors.enabled' ) ||
  617. Util_Environment::is_wpmu_subdomain() )
  618. return $queued_urls;
  619. $home_urls = $config->get_array( 'pgcache.mirrors.home_urls' );
  620. $url_prefix = trailingslashit( get_home_url() );
  621. $urls = array_keys( $queued_urls );
  622. foreach ( $urls as $url ) {
  623. if ( substr( $url, 0, strlen( $url_prefix ) ) != $url_prefix )
  624. continue;
  625. foreach ( $home_urls as $home ) {
  626. if ( !empty( $home ) ) {
  627. $mirror_url = $home . substr( $url, strlen( $url_prefix ) );
  628. $queued_urls[$mirror_url] = '*';
  629. }
  630. }
  631. }
  632. return $queued_urls;
  633. }
  634. static private function _term_hash( $terms ) {
  635. $term_hash = array();
  636. foreach ( $terms as $term )
  637. $term_hash[] = $term->term_id;
  638. $term_hashed = md5( implode( ',', $term_hash ) );
  639. return $term_hashed;
  640. }
  641. /**
  642. *
  643. *
  644. * @param unknown $post
  645. * @return string
  646. */
  647. static private function _get_archive_slug( $post ) {
  648. $archive_slug = '';
  649. global $wp_post_types;
  650. $args = $wp_post_types[$post->post_type];
  651. if ( $args->has_archive ) {
  652. global $wp_rewrite;
  653. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  654. if ( $args->rewrite['with_front'] )
  655. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  656. else
  657. $archive_slug = $wp_rewrite->root . $archive_slug;
  658. }
  659. return $archive_slug;
  660. }
  661. }