Guest User

Untitled

a guest
Feb 15th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. <?php
  2. /*
  3. To generate A to Z listing
  4. */
  5. class AlphabetPlugin {
  6. //to hold search result from database
  7. var $result;
  8. //to hold generated html code
  9. var $html;
  10. //to hold link parameter text like /?cat=
  11. var $link_text;
  12. //to hold header
  13. var $header_text;
  14. //background color
  15. var $bg_color; //future use
  16. //text color
  17. var $text_color; //future use
  18.  
  19. //Constructor
  20. function AlphabetPlugin()
  21. {
  22. $this->html = "";
  23. }
  24.  
  25. // load language files
  26. public function aplhabet_listing_set_lang_file() {
  27. # set the language file
  28. $currentLocale = get_locale();
  29. if(!empty($currentLocale)) {
  30. $moFile = dirname(__FILE__) . "/lang/" . $currentLocale . ".mo";
  31. if (@file_exists($moFile) && is_readable($moFile)) {
  32. load_textdomain(AL_I18N_DOMAIN, $moFile);
  33. }
  34.  
  35. }
  36. }
  37.  
  38. //register settings
  39. public function aplhabet_listing_register_settings() {
  40. register_setting( 'alphabet-listing-settings-group', 'alphabet-listing-settings', array( &$this, 'validate_settings') );
  41. }
  42.  
  43. //validate user input
  44. public function validate_settings( $settings ) {
  45. $settings['type'] = (preg_match('/^(post|page|category|hry)$/i', $settings['type']) ? strtolower($settings['type']) : "post");
  46. $settings['bg_colour'] = (preg_match('/^#[a-f0-9]{6}$/i', $settings['bg_colour']) ? $settings['bg_colour'] : "#f0f0f0");
  47. $settings['text_colour'] = (preg_match('/^#[a-f0-9]{6}$/i', $settings['text_colour']) ? $settings['text_colour'] : "#424242");
  48. return $settings;
  49. }
  50.  
  51. // activating the default values
  52. public function aplhabet_listing_activate() {
  53. $new_options = array(
  54. 'title' => 'A to Z listing',
  55. 'type' => 'post',
  56. 'bg_colour' => '#f0f0f0',
  57. 'text_colour' => '#424242'
  58. );
  59. add_option('alphabet-listing-settings',$new_options);
  60. }
  61.  
  62. public function alphabet_listing_create_menu() {
  63.  
  64. // create new top-level menu
  65. add_menu_page(
  66. __('Alphabet Listing', AL_I18N_DOMAIN),
  67. __('Alphabet Listing', AL_I18N_DOMAIN),
  68. 0,
  69. AL_DIRECTORY.'/alphabet_listing_settings.php',
  70. '',
  71. plugins_url('/images/icon.png', __FILE__)
  72. );
  73.  
  74.  
  75. add_submenu_page(
  76. AL_DIRECTORY.'/alphabet_listing_settings.php',
  77. __("Alphabet Listing Settings", AL_I18N_DOMAIN),
  78. __("Settings", AL_I18N_DOMAIN),
  79. 0,
  80. AL_DIRECTORY.'/alphabet_listing_settings.php'
  81. );
  82.  
  83. add_submenu_page(
  84. AL_DIRECTORY.'/alphabet_listing_settings.php',
  85. __("Alphabet Listing Help", AL_I18N_DOMAIN),
  86. __("Help", AL_I18N_DOMAIN),
  87. 9,
  88. AL_DIRECTORY.'/alphabet_listing_help.php'
  89. );
  90. }
  91.  
  92.  
  93. // deactivating
  94. public function aplhabet_listing_deactivate() {
  95. // needed for proper deletion of every option
  96. delete_option('alphabet-listing-settings');
  97.  
  98. }
  99.  
  100. //To inject the css that is need for rendering alphabets
  101. public function inject_css()
  102. {
  103. wp_register_style( 'prefix-style', AL_URL . "css/alphabet_listing.css" );
  104. wp_enqueue_style( 'prefix-style' );
  105. }
  106.  
  107. //To read post table and return all posts
  108. public function get_all_titles($type)
  109. {
  110. global $wpdb;
  111. //reset
  112. $sql = "";
  113. $this->html = "";
  114. switch ($type)
  115. {
  116. case 'post':
  117. $sql = "select id, post_title, post_name from $wpdb->posts where post_status = 'publish' AND post_type = 'post' ORDER BY post_title";
  118. break;
  119. case 'hry':
  120. $sql = "select id, post_title, post_name from $wpdb->posts where post_status = 'publish' AND post_type = 'wpsc-product' ORDER BY post_title";
  121. break;
  122. case 'page':
  123. $sql = "select id, post_title, post_name from $wpdb->posts where post_status = 'publish' AND post_type = 'page' ORDER BY post_title";
  124. break;
  125. case 'category':
  126. $sql = "SELECT term_id as id, name as post_title FROM $wpdb->terms ORDER BY name";
  127. break;
  128. }
  129.  
  130. $this->result = $wpdb->get_results($sql, ARRAY_A );
  131. }
  132. /*
  133. To generate A to Z html with links
  134. note: css need to the there to show the links properly
  135. */
  136. public function generateAtoZHtml()
  137. {
  138.  
  139. $startCapital = 65;
  140. $startSmall = 97;
  141.  
  142. $this->html .= "<div id='wp-alphabet-listing'>";
  143. $this->html .= "<section style=\"background-color:". $this->bg_color .";\">";
  144. $this->html .= "<h2>". $this->header_text ."</h2>";
  145. $this->html .= "<ol>\n";
  146.  
  147. for($i = 0;$i<26;$i++)
  148. {
  149. $hasItem = FALSE;
  150. $tempHtml = "";
  151. $this->html .= "<li><a style=\"color:". $this->text_color.";\" href='#'>" . chr($startCapital + $i) . "</a>\n";
  152. foreach($this->result as $row)
  153. {
  154. if (( $row['post_title'][0] == chr($startCapital + $i)) || ( $row['post_title'][0] == chr($startSmall + $i)))
  155. {
  156. $tempHtml .= "<li><a href='". $this->link_text ."/".$row['post_name']."'>" . substr($row['post_title'],0,22) . "</a></li>\n";
  157. $hasItem = TRUE;
  158. ///
  159. }
  160. }
  161. if ($hasItem)
  162. {
  163. $this->html .= "<div>\n" . "<ul>\n" . $tempHtml . "</ul>\n" . "</div>\n";
  164. }
  165.  
  166. $this->html .= "</li>\n";
  167. }
  168. $this->html .= "</ol>\n";
  169. $this->html .= "</section>";
  170. $this->html .= "</div>";
  171. }
  172.  
  173. //to use shortcode and return html accordingly
  174. //also sets default values, for default options priority is short code then user settings
  175. public function atoz_shortcode($atts)
  176. {
  177. $wp_al_options = get_option('alphabet-listing-settings');
  178. extract(shortcode_atts(array(
  179. 'type' => '',
  180. 'title' => '',
  181. 'bg_color' => '',
  182. 'text_color' => ''
  183. ), $atts));
  184. //set type
  185. if ($type == '')
  186. $type = $wp_al_options['type'];
  187. //set title
  188. if ($title == '')
  189. $this->header_text = $wp_al_options['title'];
  190. else
  191. $this->header_text = $title;
  192.  
  193. //set background color
  194. if ($bg_color == '')
  195. $this->bg_color = $wp_al_options['bg_colour'];
  196. else
  197. $this->bg_color = $bg_color;
  198.  
  199. //set text color
  200. if ($text_color == '')
  201. $this->text_color = $wp_al_options['text_colour'];
  202. else
  203. $this->text_color = $text_color;
  204.  
  205. // check what type user entered
  206. switch (strtolower($type))
  207. {
  208. case 'post':
  209. $this->get_all_titles('post');
  210. $this->link_text = "p";
  211. break;
  212. case 'hry':
  213. $this->get_all_titles('hry');
  214. $this->link_text = "product-category";
  215. break;
  216. case 'page':
  217. $this->get_all_titles('page');
  218. $this->link_text = "page_id";
  219. break;
  220. case 'category':
  221. $this->get_all_titles('category');
  222. $this->link_text = "cat";
  223. break;
  224. default:
  225. $this->get_all_titles('post');
  226. $this->link_text = "p";
  227. }
  228. $this->generateAtoZHtml();
  229. return $this->html;
  230.  
  231. }
  232.  
  233.  
  234. }
  235.  
  236. ?>
Advertisement
Add Comment
Please, Sign In to add comment