Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. // edit these defines (articles is just an example of a Page slug)
  2. define('RESULTS_PAGE_SLUG', 'articles');
  3. define('LETTER_PREFIX', '');
  4.  
  5. function get_letter_ids() {
  6. $letter = get_query_var('letter');
  7. if($letter) {
  8. global $wpdb;
  9. // get post ID's that start with the letter from the query var
  10. return $wpdb->get_col("select ID from $wpdb->posts where $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish') AND post_title LIKE '".esc_sql(like_escape($letter))."%' ");
  11. }
  12. return;
  13. }
  14.  
  15. function get_alphabet_links() {
  16. $alphabet_array = range('a','z');
  17. $current_letter = get_query_var('letter');
  18. $results_page = get_page_by_title( (string) RESULTS_PAGE_SLUG );
  19. $available_letters = get_available_post_letters();
  20.  
  21. $html = '';
  22. if($results_page && !empty($available_letters)) {
  23. $page_permalink = get_permalink( $results_page );
  24. $html .= '<ul>';
  25. foreach($alphabet_array as $letter){
  26. if(in_array($letter, $available_letters)) {
  27. if($current_letter == $letter) {
  28. $html .= '<li class="current_letter">' . strtoupper ( $letter ) . '</li>';
  29. } else {
  30. if(get_option( 'permalink_structure' )) {
  31. $permalink = esc_url( $page_permalink .sanitize_title( (string) LETTER_PREFIX.$letter));
  32. } else {
  33. $permalink = add_query_arg( 'letter', $letter, esc_url($page_permalink));
  34. }
  35.  
  36. $html .= '<li><a href="' . $permalink . '">' . strtoupper ( $letter ) . '</a></li>';
  37. }
  38. } else {
  39. $html .= '<li class="not_available_letter">' . strtoupper ( $letter ) . '</li>';
  40. }
  41. }
  42. $html .= '</ul>';
  43. }
  44. return $html;
  45. }
  46.  
  47.  
  48. function get_available_post_letters(){
  49.  
  50. if ( false === ( $post_letters = get_transient( 'all_post_letters' ) ) ) {
  51.  
  52. $args = array(
  53. 'posts_per_page' => -1,
  54. 'ignore_sticky_posts' => 1,
  55. 'post_status' => 'publish',
  56. 'no_found_rows' => true,
  57. );
  58.  
  59. $posts = get_posts($args);
  60. $post_letters = array();
  61. if($posts) {
  62. foreach ($posts as $post) {
  63. $first_letter = strtolower(substr($post->post_title, 0, 1));
  64. if(preg_match('/[a-z]/', $first_letter)) {
  65. $post_letters[] = $first_letter;
  66. }
  67. }
  68. }
  69. $post_letters = array_unique($post_letters);
  70. set_transient('all_post_letters', $post_letters);
  71.  
  72. }
  73.  
  74. return $post_letters;
  75. }
  76.  
  77. // delete transient when updating a post
  78. add_action( 'save_post', 'update_all_ids' );
  79. add_action( 'delete_post', 'update_all_ids' );
  80.  
  81. function update_all_ids( $post_id ) {
  82. //verify post is not a revision
  83. if ( !wp_is_post_revision( $post_id ) ) {
  84. delete_transient( 'all_post_letters' );
  85. }
  86. }
  87.  
  88. add_action('init', 'add_letter_rewrite_rules');
  89.  
  90. function add_letter_rewrite_rules() {
  91. $prefix = trim((string) LETTER_PREFIX);
  92. $page_slug = trim((string) RESULTS_PAGE_SLUG);
  93.  
  94. if(!is_admin() && get_page_by_title( $page_slug )) {
  95.  
  96. // sanitize the letter prefix before adding the rewrite rule
  97. $prefix_end = '';
  98. if('-' == substr($prefix, -1)) {
  99. $prefix_end = '-';
  100. }
  101. $prefix = sanitize_title($prefix) . $prefix_end;
  102.  
  103. $options = array(
  104. 'rules' => array(
  105. '(.?.+?)/' . $prefix . '([a-z])/page/?([0-9]{1,})/?$' => 'index.php?pagename='.$page_slug.'&letter=$matches[2]&paged=$matches[3]',
  106. '(.?.+?)/' . $prefix . '([a-z])(/[0-9]+)?/?$' => 'index.php?pagename='.$page_slug.'&letter=$matches[2]&page=$matches[3]',
  107. ),
  108. 'query_vars' => array('letter')
  109. );
  110.  
  111. $add_rewrite_rules = new ARR_add_rewrite_rules($options);
  112. }
  113. }
  114.  
  115. if(!class_exists('ARR_add_rewrite_rules')) {
  116. class ARR_add_rewrite_rules {
  117.  
  118. var $query_vars = array();
  119. var $rules = array();
  120.  
  121. function __construct($options = NULL){
  122. if(!is_null($options)){
  123. $this->init($options);
  124. }
  125. }
  126.  
  127. function init($options){
  128. foreach($options as $key => $value){
  129. $this->$key = $value;
  130. }
  131.  
  132. if(get_option( 'permalink_structure' ) && !empty($this->rules)) {
  133. add_action('wp_head', array(&$this, 'flush_rules'));
  134. add_action('generate_rewrite_rules', array(&$this, 'add_rules'));
  135. }
  136.  
  137. if(!empty($this->query_vars)){
  138. add_filter('query_vars', array(&$this, 'add_query_vars'));
  139. }
  140.  
  141. }
  142.  
  143. function add_query_vars($query_vars){
  144. foreach($this->query_vars as $var){
  145. $query_vars[] = $var;
  146. }
  147. return $query_vars;
  148. }
  149.  
  150. function add_rules(){
  151. global $wp_rewrite;
  152. $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
  153. }
  154.  
  155. function rules_exist(){
  156. global $wp_rewrite;
  157.  
  158. foreach($this->rules as $key => $rule){
  159. if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){
  160. return FALSE;
  161. }
  162. }
  163. return TRUE;
  164. }
  165.  
  166. function flush_rules(){
  167. global $wp_rewrite;
  168. // only flush rules if new rules are added
  169. if(!$this->rules_exist()){
  170. //echo 'Rules are flused (refresh the page again)';
  171. $wp_rewrite->flush_rules();
  172. }
  173. }
  174.  
  175. } // class
  176. } // class exists
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement