Advertisement
sgaffney

functions.php

Nov 6th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.36 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Functions of bbPress's Twenty Ten theme
  5.  *
  6.  * @package bbPress
  7.  * @subpackage BBP_Twenty_Ten
  8.  * @since Twenty Ten 1.1
  9.  */
  10.  
  11. // Exit if accessed directly
  12. if ( !defined( 'ABSPATH' ) ) exit;
  13.  
  14. /** Theme Setup ***************************************************************/
  15.  
  16. if ( !class_exists( 'tumbleboard' ) ) :
  17. /**
  18.  * Loads bbPress Twenty Ten Theme functionality
  19.  *
  20.  * Usually functions.php contains a few functions wrapped in function_exisits()
  21.  * checks. Since bbp-twenty-ten is intended to be used both as a child theme and
  22.  * for Theme Compatibility, we've moved everything into one convenient class
  23.  * that can be copied or extended.
  24.  *
  25.  * See @link BBP_Theme_Compat() for more.
  26.  *
  27.  * @since bbPress (r3277)
  28.  *
  29.  * @package bbPress
  30.  * @subpackage BBP_Twenty_Ten
  31.  */
  32. class tumbleboard extends BBP_Theme_Compat {
  33.  
  34.     /** Functions *************************************************************/
  35.  
  36.     /**
  37.      * The main bbPress (Twenty Ten) Loader
  38.      *
  39.      * @since bbPress (r3277)
  40.      *
  41.      * @uses BBP_Twenty_Ten::setup_globals()
  42.      * @uses BBP_Twenty_Ten::setup_actions()
  43.      */
  44.     public function __construct() {
  45.         $this->setup_globals();
  46.         $this->setup_actions();
  47.     }
  48.  
  49.     /**
  50.      * Component global variables
  51.      *
  52.      * @since bbPress (r2626)
  53.      * @access private
  54.      *
  55.      * @uses plugin_dir_path() To generate bbPress plugin path
  56.      * @uses plugin_dir_url() To generate bbPress plugin url
  57.      * @uses apply_filters() Calls various filters
  58.      */
  59.     private function setup_globals() {
  60.         global $bbp;
  61.  
  62.         // Theme name to help identify if it's been extended
  63.         $this->name = 'tumbleboard';
  64.  
  65.         // Version of theme in YYYMMDD format
  66.         $this->version = '20110921';
  67.  
  68.         // Setup the theme path
  69.         $this->dir = WP_PLUGIN_DIR . '/tumbleboard/wptumble-twentyten';
  70.  
  71.         // Setup the theme URL
  72.         $this->url = WP_PLUGIN_URL . '/tumbleboard/wptumble-twentyten';
  73.     }
  74.  
  75.     /**
  76.      * Setup the theme hooks
  77.      *
  78.      * @since bbPress (r3277)
  79.      * @access private
  80.      *
  81.      * @uses add_filter() To add various filters
  82.      * @uses add_action() To add various actions
  83.      */
  84.     private function setup_actions() {
  85.  
  86.         // Add theme support for bbPress
  87.         // add_action( 'after_setup_theme',        array( $this, 'add_theme_support'     ) );
  88.  
  89.         // Enqueue theme CSS
  90.         // add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_styles'        ) );
  91.  
  92.         // Enqueue theme JS
  93.         add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_scripts'       ) );
  94.  
  95.         // Enqueue theme script localization
  96.         add_filter( 'bbp_enqueue_scripts',      array( $this, 'localize_topic_script' ) );
  97.  
  98.         // Output some extra JS in the <head>
  99.         add_action( 'bbp_head',                 array( $this, 'head_scripts'          ) );
  100.  
  101.         // Handles the ajax favorite/unfavorite
  102.         add_action( 'wp_ajax_dim-favorite',     array( $this, 'ajax_favorite'         ) );
  103.  
  104.         // Handles the ajax subscribe/unsubscribe
  105.         add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription'     ) );
  106.     }
  107.  
  108.     /**
  109.      * Sets up theme support for bbPress
  110.      *
  111.      * Because this theme comes bundled with bbPress template files, we add it
  112.      * to the list of things this theme supports. Note that the function
  113.      * "add_theme_support()" does not /enable/ theme support, but is instead an
  114.      * API for telling WordPress what it can already do on its own.
  115.      *
  116.      * If you're looking to add bbPress support into your own custom theme, you'll
  117.      * want to make sure it includes all of the template files for bbPress, and then
  118.      * use: add_theme_support( 'bbpress' ); in your functions.php.
  119.      *
  120.      * @since bbPress (r2652)
  121.      */
  122.     public function add_theme_support() {
  123.         add_theme_support( 'bbpress' );
  124.     }
  125.  
  126.     /**
  127.      * Load the theme CSS
  128.      *
  129.      * @since bbPress (r2652)
  130.      *
  131.      * @uses wp_enqueue_style() To enqueue the styles
  132.      */
  133.     public function enqueue_styles() {
  134.  
  135.         // Right to left
  136.         if ( is_rtl() ) {
  137.  
  138.             // TwentyTen
  139.             wp_enqueue_style( 'twentyten',     get_template_directory_uri() . '/style.css', '',          $this->version, 'screen' );
  140.             wp_enqueue_style( 'twentyten-rtl', get_template_directory_uri() . '/rtl.css',   'twentyten', $this->version, 'screen' );
  141.  
  142.             // bbPress specific
  143.             wp_enqueue_style( 'bbp-twentyten-bbpress', get_stylesheet_directory_uri() . '/css/bbpress-rtl.css', 'twentyten-rtl', $this->version, 'screen' );
  144.  
  145.         // Left to right
  146.         } else {
  147.  
  148.             // TwentyTen
  149.             wp_enqueue_style( 'twentyten', get_template_directory_uri() . '/style.css', '', $this->version, 'screen' );
  150.  
  151.             // bbPress specific
  152.             wp_enqueue_style( 'bbp-twentyten-bbpress', get_stylesheet_directory_uri() . '/css/bbpress.css', 'twentyten', $this->version, 'screen' );
  153.         }
  154.     }
  155.  
  156.     /**
  157.      * Enqueue the required Javascript files
  158.      *
  159.      * @since bbPress (r2652)
  160.      *
  161.      * @uses bbp_is_single_topic() To check if it's the topic page
  162.      * @uses get_stylesheet_directory_uri() To get the stylesheet directory uri
  163.      * @uses bbp_is_single_user_edit() To check if it's the profile edit page
  164.      * @uses wp_enqueue_script() To enqueue the scripts
  165.      */
  166.     public function enqueue_scripts() {
  167.  
  168.         if ( bbp_is_single_topic() )
  169.             wp_enqueue_script( 'bbp_topic', WP_PLUGIN_URL . '/tumbleboard/wptumble-twentyten/js/topic.js', array( 'wp-lists' ), $this->version );
  170.  
  171.         if ( bbp_is_single_user_edit() )
  172.             wp_enqueue_script( 'user-profile' );
  173.     }
  174.  
  175.     /**
  176.      * Put some scripts in the header, like AJAX url for wp-lists
  177.      *
  178.      * @since bbPress (r2652)
  179.      *
  180.      * @uses bbp_is_single_topic() To check if it's the topic page
  181.      * @uses admin_url() To get the admin url
  182.      * @uses bbp_is_single_user_edit() To check if it's the profile edit page
  183.      */
  184.     public function head_scripts() {
  185.         if ( bbp_is_single_topic() ) : ?>
  186.  
  187.         <script type='text/javascript'>
  188.             /* <![CDATA[ */
  189.             var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
  190.             /* ]]> */
  191.         </script>
  192.  
  193.         <?php elseif ( bbp_is_single_user_edit() ) : ?>
  194.  
  195.         <script type="text/javascript" charset="utf-8">
  196.             if ( window.location.hash == '#password' ) {
  197.                 document.getElementById('pass1').focus();
  198.             }
  199.         </script>
  200.  
  201.         <?php
  202.         endif;
  203.     }
  204.  
  205.     /**
  206.      * Load localizations for topic script
  207.      *
  208.      * These localizations require information that may not be loaded even by init.
  209.      *
  210.      * @since bbPress (r2652)
  211.      *
  212.      * @uses bbp_is_single_topic() To check if it's the topic page
  213.      * @uses is_user_logged_in() To check if user is logged in
  214.      * @uses bbp_get_current_user_id() To get the current user id
  215.      * @uses bbp_get_topic_id() To get the topic id
  216.      * @uses bbp_get_favorites_permalink() To get the favorites permalink
  217.      * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
  218.      * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  219.      * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic
  220.      * @uses bbp_get_topic_permalink() To get the topic permalink
  221.      * @uses wp_localize_script() To localize the script
  222.      */
  223.     public function localize_topic_script() {
  224.  
  225.         // Bail if not viewing a single topic
  226.         if ( !bbp_is_single_topic() )
  227.             return;
  228.  
  229.         // Bail if user is not logged in
  230.         if ( !is_user_logged_in() )
  231.             return;
  232.  
  233.         $user_id = bbp_get_current_user_id();
  234.  
  235.         $localizations = array(
  236.             'currentUserId' => $user_id,
  237.             'topicId'       => bbp_get_topic_id(),
  238.         );
  239.  
  240.         // Favorites
  241.         if ( bbp_is_favorites_active() ) {
  242.             $localizations['favoritesActive'] = 1;
  243.             $localizations['favoritesLink']   = bbp_get_favorites_permalink( $user_id );
  244.             $localizations['isFav']           = (int) bbp_is_user_favorite( $user_id );
  245.             $localizations['favLinkYes']      = __( 'favorites',                                         'bbpress' );
  246.             $localizations['favLinkNo']       = __( '?',                                                 'bbpress' );
  247.             $localizations['favYes']          = __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' );
  248.             $localizations['favNo']           = __( '%favAdd% (%favLinkNo%)',                            'bbpress' );
  249.             $localizations['favDel']          = __( '&times;',                                           'bbpress' );
  250.             $localizations['favAdd']          = __( 'Add this topic to your favorites',                  'bbpress' );
  251.         } else {
  252.             $localizations['favoritesActive'] = 0;
  253.         }
  254.  
  255.         // Subscriptions
  256.         if ( bbp_is_subscriptions_active() ) {
  257.             $localizations['subsActive']   = 1;
  258.             $localizations['isSubscribed'] = (int) bbp_is_user_subscribed( $user_id );
  259.             $localizations['subsSub']      = __( 'Subscribe',   'bbpress' );
  260.             $localizations['subsUns']      = __( 'Unsubscribe', 'bbpress' );
  261.             $localizations['subsLink']     = bbp_get_topic_permalink();
  262.         } else {
  263.             $localizations['subsActive'] = 0;
  264.         }
  265.  
  266.         wp_localize_script( 'bbp_topic', 'bbpTopicJS', $localizations );
  267.     }
  268.  
  269.     /**
  270.      * Add or remove a topic from a user's favorites
  271.      *
  272.      * @since bbPress (r2652)
  273.      *
  274.      * @uses bbp_get_current_user_id() To get the current user id
  275.      * @uses current_user_can() To check if the current user can edit the user
  276.      * @uses bbp_get_topic() To get the topic
  277.      * @uses check_ajax_referer() To verify the nonce & check the referer
  278.      * @uses bbp_is_user_favorite() To check if the topic is user's favorite
  279.      * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
  280.      * @uses bbp_add_user_favorite() To add the topic from user's favorites
  281.      */
  282.     public function ajax_favorite() {
  283.         $user_id = bbp_get_current_user_id();
  284.         $id      = intval( $_POST['id'] );
  285.  
  286.         if ( !current_user_can( 'edit_user', $user_id ) )
  287.             die( '-1' );
  288.  
  289.         if ( !$topic = bbp_get_topic( $id ) )
  290.             die( '0' );
  291.  
  292.         check_ajax_referer( 'toggle-favorite_' . $topic->ID );
  293.  
  294.         if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
  295.             if ( bbp_remove_user_favorite( $user_id, $topic->ID ) ) {
  296.                 die( '1' );
  297.             }
  298.         } else {
  299.             if ( bbp_add_user_favorite( $user_id, $topic->ID ) ) {
  300.                 die( '1' );
  301.             }
  302.         }
  303.  
  304.         die( '0' );
  305.     }
  306.  
  307.     /**
  308.      * Subscribe/Unsubscribe a user from a topic
  309.      *
  310.      * @since bbPress (r2668)
  311.      *
  312.      * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  313.      * @uses bbp_get_current_user_id() To get the current user id
  314.      * @uses current_user_can() To check if the current user can edit the user
  315.      * @uses bbp_get_topic() To get the topic
  316.      * @uses check_ajax_referer() To verify the nonce & check the referer
  317.      * @uses bbp_is_user_subscribed() To check if the topic is in user's
  318.      *                                 subscriptions
  319.      * @uses bbp_remove_user_subscriptions() To remove the topic from user's
  320.      *                                        subscriptions
  321.      * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
  322.      */
  323.     public function ajax_subscription() {
  324.         if ( !bbp_is_subscriptions_active() )
  325.             return;
  326.  
  327.         $user_id = bbp_get_current_user_id();
  328.         $id      = intval( $_POST['id'] );
  329.  
  330.         if ( !current_user_can( 'edit_user', $user_id ) )
  331.             die( '-1' );
  332.  
  333.         if ( !$topic = bbp_get_topic( $id ) )
  334.             die( '0' );
  335.  
  336.         check_ajax_referer( 'toggle-subscription_' . $topic->ID );
  337.  
  338.         if ( bbp_is_user_subscribed( $user_id, $topic->ID ) ) {
  339.             if ( bbp_remove_user_subscription( $user_id, $topic->ID ) ) {
  340.                 die( '1' );
  341.             }
  342.         } else {
  343.             if ( bbp_add_user_subscription( $user_id, $topic->ID ) ) {
  344.                 die( '1' );
  345.             }
  346.         }
  347.  
  348.         die( '0' );
  349.     }
  350. }
  351.  
  352. /**
  353.  * Instantiate a new BBP_Twenty_Ten class inside the $bbp global. It is
  354.  * responsible for hooking itself into WordPress where apprpriate.
  355.  */
  356. if ( 'bbPress' == get_class( $bbp ) ) {
  357.     $bbp->theme_compat->theme = new tumbleboard();
  358. }
  359.  
  360. endif;
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.                 /**
  368.                 * Return the author id used within the loop
  369.                 *
  370.                 * could not find bbpress function to return ID
  371.                 *
  372.                 * my whole intent is to get ID so I can use it elsewhere
  373.                 */
  374.                 function tumble_reply_author_id( $reply_id = 0 ) {
  375.                     echo tumble_get_reply_author_id( $reply_id );
  376.                 }
  377.                     /**
  378.                      * Return the author id of the reply
  379.                      *
  380.                      */
  381.  
  382.                      // JJ seems to use the reply_id equals zero everywhere, so I might as well also lol
  383.                     function tumble_get_reply_author_id( $reply_id = 0 ) {
  384.                         $reply_id = bbp_get_reply_id( $reply_id );
  385.  
  386.                         // have to handle anonymous here or within template?
  387.                         if ( !bbp_is_reply_anonymous( $reply_id ) )
  388.                             $author = get_the_author_meta( 'ID', bbp_get_reply_author_id( $reply_id ) );
  389.  
  390.                         // Cant think of a good else to use as if anonymous I dont want to print anything
  391.                         else
  392.                             $author = get_post_meta( $reply_id, '0', true );
  393.  
  394.                         // dont laugh, if I am using this wrong, then I will learn
  395.                         return apply_filters( 'tumble_get_reply_author_id', $author, $reply_id );
  396.                     }
  397.  
  398.  
  399.                 /**
  400.                 * Used to print out the custom post-type counts
  401.                 *
  402.                 * uses tumble_get_reply_author_id() to get ID
  403.                 *
  404.                 * User inputs the post-type desired
  405.                 */
  406.                 function tumble_user_posts_by_type( $post_type ) {
  407.                     echo tumble_get_user_posts_by_type( $post_type );
  408.                 }
  409.                     /**
  410.                     * Return the count of supplied post-type
  411.                     *
  412.                     * uses tumble_get_reply_author_id() to get ID
  413.                     *
  414.                     */
  415.                     function tumble_get_user_posts_by_type($post_type) {
  416.  
  417.                         // why do i have to call the global here, is it not already called globaly?
  418.                         global $wpdb;
  419.  
  420.                         $userid = bbp_get_reply_author_id( $reply_id );
  421.                         $where = get_posts_by_author_sql($post_type, TRUE, $userid);
  422.  
  423.                         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
  424.  
  425.                         return apply_filters('get_usernumposts', $count, $userid);
  426.                     }
  427. ?>
  428.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement