PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/themes/sandbox/functions.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 478 lines | 366 code | 74 blank | 38 comment | 58 complexity | 6b8c4fcb7f2d24caf4719dc30687e01e MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. // And thus begins the Sandbox guts! Registers our default
  3. // options, specifically loads the 2c-1.css file as default skin
  4. function sandbox_get_option($name) {
  5. $defaults = array(
  6. 'skin' => '2c-l',
  7. );
  8. $options = array_merge($defaults, (array) get_option('sandbox_options'));
  9. if ( isset($options[$name]) )
  10. return $options[$name];
  11. return false;
  12. }
  13. // Andy really goes nuts with arrays, which has been a good thing. Very good.
  14. function sandbox_set_options($new_options) {
  15. $options = (array) get_option('sandbox_options');
  16. $options = array_merge($options, (array) $new_options);
  17. return update_option('sandbox_options', $options);
  18. }
  19. // Template tag: echoes a stylesheet link if one is selected
  20. function sandbox_stylesheets() {
  21. $skin = sandbox_get_option('skin');
  22. if ( strtolower(get_current_theme()) !== 'sandbox 0.6.1' ) {
  23. ?>
  24. <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_uri() ?>" />
  25. <?php
  26. } else if ( $skin !== 'none' ) {
  27. ?>
  28. <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_template_directory_uri() . "/skins/$skin.css" ?>" title="Sandbox" />
  29. <?php
  30. }
  31. }
  32. // Template tag: echoes a link to skip navigation if the
  33. // global_navigation option is set to "Y" in the skin file
  34. function sandbox_skipnav() {
  35. if ( !sandbox_get_option('globalnav') )
  36. return;
  37. echo '<p class="access"><a href="#content" title="'.__('Skip navigation to the content', 'sandbox').'">'.__('Skip navigation', 'sandbox').'</a></p>';
  38. }
  39. // Template tag: echoes a page list for navigation if the
  40. // global_navigation option is set to "Y" in the skin file
  41. function sandbox_globalnav() {
  42. if ( !sandbox_get_option('globalnav') )
  43. return;
  44. echo "<div id='globalnav'><ul id='menu'>";
  45. $menu = wp_list_pages('title_li=&sort_column=menu_order&echo=0');
  46. echo str_replace(array("\r", "\n", "\t"), '', $menu); // Strip intratag whitespace
  47. echo "</ul></div>";
  48. }
  49. // Template tag: echoes semantic classes in the <body>
  50. function sandbox_body_class( $print = true ) {
  51. global $wp_query, $current_user;
  52. $c = array('wordpress');
  53. sandbox_date_classes(time(), $c);
  54. is_home() ? $c[] = 'home' : null;
  55. is_archive() ? $c[] = 'archive' : null;
  56. is_date() ? $c[] = 'date' : null;
  57. is_search() ? $c[] = 'search' : null;
  58. is_paged() ? $c[] = 'paged' : null;
  59. is_attachment() ? $c[] = 'attachment' : null;
  60. is_404() ? $c[] = 'four04' : null; // CSS does not allow a digit as first character
  61. if ( is_single() ) {
  62. the_post();
  63. $c[] = 'single';
  64. if ( isset($wp_query->post->post_date) )
  65. sandbox_date_classes(mysql2date('U', $wp_query->post->post_date), $c, 's-');
  66. foreach ( (array) get_the_category() as $cat )
  67. $c[] = 's-category-' . $cat->category_nicename;
  68. $c[] = 's-author-' . get_the_author_login();
  69. rewind_posts();
  70. }
  71. else if ( is_author() ) {
  72. $author = $wp_query->get_queried_object();
  73. $c[] = 'author';
  74. $c[] = 'author-' . $author->user_nicename;
  75. }
  76. else if ( is_category() ) {
  77. $cat = $wp_query->get_queried_object();
  78. $c[] = 'category';
  79. $c[] = 'category-' . $cat->category_nicename;
  80. }
  81. else if ( is_page() ) {
  82. the_post();
  83. $c[] = 'page';
  84. $c[] = 'page-author-' . get_the_author_login();
  85. rewind_posts();
  86. }
  87. if ( $current_user->ID )
  88. $c[] = 'loggedin';
  89. $c = join(' ', apply_filters('body_class', $c));
  90. return $print ? print($c) : $c;
  91. }
  92. // Template tag: echoes semantic classes in each post <div>
  93. function sandbox_post_class( $print = true ) {
  94. global $post, $sandbox_post_alt;
  95. $c = array("p$sandbox_post_alt", $post->post_status);
  96. $c[] = 'author-' . get_the_author_login();
  97. sandbox_date_classes(mysql2date('U', $post->post_date), $c);
  98. if ( ++$sandbox_post_alt % 2 )
  99. $c[] = 'alt';
  100. $c = join(' ', get_post_class( $c, $post->ID ));
  101. return $print ? print($c) : $c;
  102. }
  103. $sandbox_post_alt = 1;
  104. // Template tag: echoes semantic classes for a comment <li>
  105. function sandbox_comment_class( $print = true ) {
  106. global $comment, $post, $sandbox_comment_alt;
  107. $c = array($comment->comment_type, "c$sandbox_comment_alt");
  108. if ( $comment->user_id > 0 ) {
  109. $user = get_userdata($comment->user_id);
  110. $c[] = "byuser commentauthor-$user->user_login";
  111. if ( $comment->user_id === $post->post_author )
  112. $c[] = 'bypostauthor';
  113. }
  114. sandbox_date_classes(mysql2date('U', $comment->comment_date), $c, 'c-');
  115. if ( 0 == ++$sandbox_comment_alt % 2 )
  116. $c[] = 'alt';
  117. if ( is_trackback() ) {
  118. $c[] = 'trackback';
  119. }
  120. $c = join(' ', apply_filters('comment_class', $c));
  121. return $print ? print($c) : $c;
  122. }
  123. // Adds four time- and date-based classes to an array
  124. // with all times relative to GMT (sometimes called UTC)
  125. function sandbox_date_classes($t, &$c, $p = '') {
  126. $t = $t + (get_settings('gmt_offset') * 3600);
  127. $c[] = $p . 'y' . gmdate('Y', $t); // Year
  128. $c[] = $p . 'm' . gmdate('m', $t); // Month
  129. $c[] = $p . 'd' . gmdate('d', $t); // Day
  130. $c[] = $p . 'h' . gmdate('h', $t); // Hour
  131. }
  132. // Returns a list of the post's categories, minus the queried one
  133. function sandbox_cats_meow($glue) {
  134. $current_cat = single_cat_title('', false);
  135. $separator = "\n";
  136. $cats = explode($separator, get_the_category_list($separator));
  137. foreach ( $cats as $i => $str ) {
  138. if ( strstr($str, ">$current_cat<") ) {
  139. unset($cats[$i]);
  140. break;
  141. }
  142. }
  143. if ( empty($cats) )
  144. return false;
  145. return trim(join($glue, $cats));
  146. }
  147. // Sandbox widgets: Replaces the default search widget with one
  148. // that matches what is in the Sandbox sidebar by default
  149. function widget_sandbox_search($args) {
  150. extract($args);
  151. if ( empty($title) )
  152. $title = __('Search', 'sandbox');
  153. ?>
  154. <?php echo $before_widget ?>
  155. <?php echo $before_title ?><label for="s"><?php echo $title ?></label><?php echo $after_title ?>
  156. <form id="searchform" method="get" action="<?php bloginfo('url') ?>">
  157. <div>
  158. <input id="s" name="s" type="text" value="<?php the_search_query(); ?>" size="10" />
  159. <input id="searchsubmit" name="searchsubmit" type="submit" value="<?php _e('Find &raquo;', 'sandbox') ?>" />
  160. </div>
  161. </form>
  162. <?php echo $after_widget ?>
  163. <?php
  164. }
  165. // Sandbox widgets: Replaces the default meta widget with one
  166. // that matches what is in the Sandbox sidebar by default
  167. function widget_sandbox_meta($args) {
  168. extract($args);
  169. if ( empty($title) )
  170. $title = __('Meta', 'sandbox');
  171. ?>
  172. <?php echo $before_widget; ?>
  173. <?php echo $before_title . $title . $after_title; ?>
  174. <ul>
  175. <?php wp_register() ?>
  176. <li><?php wp_loginout() ?></li>
  177. <?php wp_meta() ?>
  178. </ul>
  179. <?php echo $after_widget; ?>
  180. <?php
  181. }
  182. // Sandbox widgets: Adds the Sandbox's home link as a widget, which
  183. // appears when NOT on the home page OR on a page of the home page
  184. function widget_sandbox_homelink($args) {
  185. extract($args);
  186. $options = get_option('widget_sandbox_homelink');
  187. $title = empty($options['title']) ? __('&laquo; Home') : $options['title'];
  188. ?>
  189. <?php if ( !is_home() || is_paged() ) { ?>
  190. <?php echo $before_widget; ?>
  191. <?php echo $before_title ?><a href="<?php bloginfo('url') ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php echo $title ?></a><?php echo $after_title ?>
  192. <?php echo $after_widget; ?>
  193. <?php } ?>
  194. <?php
  195. }
  196. // Sandbox widgets: Adds the option to set the text for the home link widget
  197. function widget_sandbox_homelink_control() {
  198. $options = $newoptions = get_option('widget_sandbox_homelink');
  199. if ( $_POST["homelink-submit"] ) {
  200. $newoptions['title'] = strip_tags(stripslashes($_POST["homelink-title"]));
  201. }
  202. if ( $options != $newoptions ) {
  203. $options = $newoptions;
  204. update_option('widget_sandbox_homelink', $options);
  205. }
  206. $title = esc_attr( $options['title'] );
  207. ?>
  208. <p style="text-align:left;"><?php _e('Adds a link to the home page on every page <em>except</em> the home.', 'sandbox'); ?></p>
  209. <p>
  210. <label for="homelink-title">
  211. <?php _e('Link Text:'); ?>
  212. <input class="widefat" id="homelink-title" name="homelink-title" type="text" value="<?php echo $title; ?>" />
  213. </label>
  214. </p>
  215. <input type="hidden" id="homelink-submit" name="homelink-submit" value="1" />
  216. <?php
  217. }
  218. // Sandbox widgets: Adds a widget with the Sandbox RSS links
  219. // as they appear in the default Sandbox sidebar, which are good
  220. function widget_sandbox_rsslinks($args) {
  221. extract($args);
  222. $options = get_option('widget_sandbox_rsslinks');
  223. $title = empty($options['title']) ? __('RSS Links') : $options['title'];
  224. ?>
  225. <?php echo $before_widget; ?>
  226. <?php echo $before_title . $title . $after_title; ?>
  227. <ul>
  228. <li><a href="<?php bloginfo('rss2_url') ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> RSS 2.0 Feed" rel="alternate" type="application/rss+xml"><?php _e('All posts', 'sandbox') ?></a></li>
  229. <li><a href="<?php bloginfo('comments_rss2_url') ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> Comments RSS 2.0 Feed" rel="alternate" type="application/rss+xml"><?php _e('All comments', 'sandbox') ?></a></li>
  230. </ul>
  231. <?php echo $after_widget; ?>
  232. <?php
  233. }
  234. // Sandbox widgets: Adds the option to set the text for the RSS link widget
  235. function widget_sandbox_rsslinks_control() {
  236. $options = $newoptions = get_option('widget_sandbox_rsslinks');
  237. if ( $_POST["rsslinks-submit"] ) {
  238. $newoptions['title'] = strip_tags(stripslashes($_POST["rsslinks-title"]));
  239. }
  240. if ( $options != $newoptions ) {
  241. $options = $newoptions;
  242. update_option('widget_sandbox_rsslinks', $options);
  243. }
  244. $title = esc_attr( $options['title'] );
  245. ?>
  246. <p>
  247. <label for="rsslinks-title">
  248. <?php _e('Title:'); ?>
  249. <input class="widefat" id="rsslinks-title" name="rsslinks-title" type="text" value="<?php echo $title; ?>" />
  250. </label>
  251. </p>
  252. <input type="hidden" id="rsslinks-submit" name="rsslinks-submit" value="1" />
  253. <?php
  254. }
  255. // Template tag & Sandbox widget: Creates a string to produce
  256. // links in either WP 2.1 or then WP 2.0 style, relative to install
  257. function widget_sandbox_links() {
  258. wp_list_bookmarks(array('title_before'=>'<h3>', 'title_after'=>'</h3>', 'show_images'=>true));
  259. }
  260. // Sandbox skins menu: creates the array to collect
  261. // information from the skins currently installed
  262. function sandbox_skin_info($skin) {
  263. $info = array(
  264. 'skin_name' => $skin,
  265. 'skin_uri' => '',
  266. 'description' => '',
  267. 'version' => '1.0',
  268. 'author' => __('Anonymous', 'sandbox'),
  269. 'author_uri' => '',
  270. 'global_navigation' => 'Y',
  271. );
  272. if ( !file_exists(TEMPLATEPATH."/skins/$skin.css") )
  273. return array();
  274. $css = (array) file(TEMPLATEPATH."/skins/$skin.css");
  275. foreach ( $css as $line ) {
  276. if ( strstr($line, '*/') )
  277. return $info;
  278. if ( !strstr($line, ':') )
  279. continue;
  280. list ( $k, $v ) = explode(':', $line, 2);
  281. $k = str_replace(' ', '_', strtolower(trim($k)));
  282. if ( array_key_exists($k, $info) )
  283. $info[$k] = stripslashes(wp_filter_kses(trim($v)));
  284. }
  285. }
  286. // Sandbox skins menu: Registers the workings of the skins menu
  287. function sandbox_admin_skins() {
  288. $skins = array();
  289. if ( isset ( $_GET['message'] ) ) {
  290. switch ( $_GET['message'] ) {
  291. case 'updated' :
  292. echo "\n<div id='message' class='updated fade'><p>".__('Sandbox skin saved successfully.', 'sandbox')."</p></div>\n";
  293. break;
  294. }
  295. }
  296. $current_skin = sandbox_get_option('skin');
  297. $_skins = glob(TEMPLATEPATH.'/skins/*.css');
  298. foreach ( $_skins as $k => $v ) {
  299. $info = array();
  300. preg_match('/\/([^\/]+).css$/i', $v, $matches);
  301. if ( !empty($matches[1]) ) {
  302. $skins[$matches[1]] = sandbox_skin_info($matches[1]);
  303. }
  304. }
  305. ?>
  306. <script type="text/javascript">
  307. <!-- function showme(o) { document.getElementById('show').src = o.src; } //-->
  308. </script>
  309. <div class="wrap">
  310. <h2><?php _e('Current Skin', 'sandbox') ?></h2>
  311. <div id="currenttheme">
  312. <?php if ( file_exists(get_template_directory() . "/skins/$current_skin.png") ) : ?>
  313. <img src="<?php echo get_template_directory_uri() . "/skins/$current_skin.png"; ?>" alt="<?php _e('Current skin preview', 'sandbox'); ?>" />
  314. <?php endif; ?>
  315. <?php
  316. if ( is_array($skins[$current_skin]) )
  317. extract($skins[$current_skin]);
  318. if ( !empty($skin_uri) )
  319. $skin_name = "<a href=\"$skin_uri\" title=\"$skin_name by $author\">$skin_name</a>";
  320. if ( !empty($author_uri) )
  321. $author = "<a href=\"$author_uri\" title=\"$author\">$author</a>";
  322. ?>
  323. <h3><?php printf(__('%1$s %2$s by %3$s'), $skin_name, $version, $author) ; ?></h3>
  324. <p><?php echo $description; ?></p>
  325. </div>
  326. <div class="clearer" style="clear:both;"></div>
  327. <h2><?php _e('Available Skins', 'sandbox') ?></h2>
  328. <?php
  329. foreach ( $skins as $skin => $info ) :
  330. if ( $skin == $current_skin || !is_array($info) )
  331. continue;
  332. extract($info);
  333. $activate_link = "themes.php?page=skins&amp;action=activate&amp;skin=$skin";
  334. // wp_nonce_url first introduced in WP 2.0.3
  335. if ( function_exists('wp_nonce_url') )
  336. $activate_link = wp_nonce_url($activate_link, 'switch-skin_' . $skin);
  337. ?>
  338. <div class="available-theme">
  339. <h3><a href="<?php echo $activate_link; ?>" title="Activate the <?php echo "$skin_name"; ?> skin"><?php echo "$skin_name $version"; ?></a></h3>
  340. <a href="<?php echo $activate_link; ?>" class="screenshot" title="Activate the <?php echo "$skin_name"; ?> skin">
  341. <?php if ( file_exists(get_template_directory() . "/skins/$skin.png" ) ) : ?>
  342. <img src="<?php echo get_template_directory_uri() . "/skins/$skin.png"; ?>" alt="<?php echo "$skin_name"; ?>" />
  343. <?php endif; ?>
  344. </a>
  345. <p><?php echo $description; ?></p>
  346. </div>
  347. <?php endforeach; ?>
  348. <!--<h2><?php _e('Sandbox Info', 'sandbox'); ?></h2>
  349. <p><?php printf(__('Check the <a href="%1$s" title="Read the Sandbox readme.html">documentation</a> for help installing new skins and information on the rich semantic markup that makes the Sandbox unique.', 'sandbox'), get_template_directory_uri() . '/readme.html'); ?></p>-->
  350. </div>
  351. <?php
  352. }
  353. // Sandbox skins menu: initializes the settings for the skins menu
  354. function sandbox_init() {
  355. load_theme_textdomain('sandbox');
  356. if ( $GLOBALS['pagenow'] == 'themes.php'
  357. && isset($_GET['page']) && $_GET['page'] == 'skins'
  358. && isset($_GET['action']) && $_GET['action'] == 'activate'
  359. && current_user_can('edit_theme_options') ) {
  360. check_admin_referer('switch-skin_' . $_GET['skin']);
  361. $info = sandbox_skin_info($_GET['skin']);
  362. sandbox_set_options(array(
  363. 'skin' => wp_filter_kses($_GET['skin']),
  364. 'globalnav' => bool_from_yn($info['global_navigation'])
  365. ));
  366. wp_redirect('themes.php?page=skins&message=updated');
  367. }
  368. }
  369. // Sandbox skins menu: tells WordPress (nicely) to load the skins menu
  370. function sandbox_admin_menu() {
  371. add_theme_page(__('Theme Options', 'sandbox'), __('Theme Options', 'sandbox'), 'edit_theme_options', 'skins', 'sandbox_admin_skins');
  372. }
  373. // Sandbox widgets: initializes Widgets for the Sandbox
  374. function sandbox_widgets_init() {
  375. // Overrides the Widgets default and uses <h3>'s for sidebar headings
  376. $p = array(
  377. 'before_title' => "<h3 class='widgettitle'>",
  378. 'after_title' => "</h3>\n",
  379. );
  380. // How many? Two?! That's it?
  381. register_sidebars(2, $p);
  382. // Registers the widgets specific to the Sandbox, as set earlier
  383. unregister_widget('WP_Widget_Search');
  384. wp_register_sidebar_widget('search', __('Search', 'sandbox'), 'widget_sandbox_search');
  385. unregister_widget('WP_Widget_Meta');
  386. wp_register_sidebar_widget('meta', __('Meta', 'sandbox'), 'widget_sandbox_meta');
  387. unregister_widget('WP_Widget_Links');
  388. wp_register_sidebar_widget('links', __('Links', 'sandbox'), 'widget_sandbox_links');
  389. wp_register_sidebar_widget('home-link', __('Home Link', 'widgets'), 'widget_sandbox_homelink');
  390. wp_register_widget_control('home-link', __('Home Link', 'widgets'), 'widget_sandbox_homelink_control');
  391. wp_register_sidebar_widget('rss-links', __('RSS Links', 'widgets'), 'widget_sandbox_rsslinks');
  392. wp_register_widget_control('rss-links', __('RSS Links', 'widgets'), 'widget_sandbox_rsslinks_control');
  393. }
  394. // Runs our code at the end to check that everything needed has loaded
  395. add_action('init', 'sandbox_init', 1);
  396. add_action('widgets_init', 'sandbox_widgets_init');
  397. add_action('admin_menu', 'sandbox_admin_menu');
  398. // Adds filters for greater compliance
  399. add_filter('archive_meta', 'wptexturize');
  400. add_filter('archive_meta', 'convert_smilies');
  401. add_filter('archive_meta', 'convert_chars');
  402. add_filter('archive_meta', 'wpautop');
  403. add_theme_support( 'automatic-feed-links' );
  404. add_custom_background();